0

After compiling the below dead-simple contract

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

contract SendWei {
    function sendWei(address payable recipient, uint256 amount) public {
        require(amount > 0, "Amount must be greater than 0");
        recipient.transfer(amount);
    }
}

I start the hardhat local network

npx hardhat node

run the test script of hardhat on hardhat local network by running

npx hardhat test --typecheck test/sendwei-test.ts

Below is the test script

import { expect } from "chai";
import { ethers } from "hardhat";
import { Contract } from "ethers";

describe("SendWei", () => {
  let myContract: Contract;

  beforeEach(async () => {
      // Get the contract factory
      const SendWei = await ethers.getContractFactory("SendWei");

      // Deploy the contract
      myContract = await SendWei.deploy();
  });

    it("should send 10 wei to an address", async () => {
        // Define the recipient address
        const recipient = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8";
        const amount = 10;

        // Get the initial balance of the recipient
        const signer = await ethers.getSigner(recipient);
        const initialBalance = await (await signer.getBalance(recipient)).toNumber();

        // Send 10 wei to the recipient
        await myContract.sendWei(recipient, amount);

        // Get the final balance of the recipient
        const finalBalance = await (await signer.getBalance(recipient)).toNumber();

        // Calculate the difference between the initial and final balances
        const difference = finalBalance - initialBalance;

        // Expect the difference to be equal to the amount sent
        expect(difference).to.equal(amount);
    });
});

But I am getting this error below

SendWei
    1) should send 10 wei to an address


  0 passing (3s)
  1 failing

  1) SendWei
       should send 10 wei to an address:
     InvalidInputError: Received invalid block tag 642829559307850963015472508762062935916233390536. Latest block number is 1
      at EthModule._resolveNewBlockTag (C:\Users\username\Documents\Dev\smart_contract\node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1430:13)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at runNextTicks (node:internal/process/task_queues:65:3)
      at listOnTimeout (node:internal/timers:528:9)
      at processTimers (node:internal/timers:502:7)
      at async EthModule._getBalanceAction (C:\Users\username\Documents\Dev\smart_contract\node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:474:34)
      at async HardhatNetworkProvider.request (C:\Users\username\Documents\Dev\smart_contract\node_modules\hardhat\src\internal\hardhat-network\provider\provider.ts:118:18)
      at async EthersProviderWrapper.send (C:\Users\username\Documents\Dev\smart_contract\node_modules\@nomiclabs\hardhat-ethers\src\internal\ethers-provider-wrapper.ts:13:20)

hardhat.config.ts

import"@nomicfoundation/hardhat-chai-matchers";
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const config: HardhatUserConfig = {
  solidity: "0.8.9",
};

export default config;

package.json

{
  "name": "smart_contract",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@nomicfoundation/hardhat-chai-matchers": "^1.0.5",
    "@nomiclabs/hardhat-ethers": "^2.2.2",
    "@types/chai": "^4.3.4",
    "@types/mocha": "^10.0.1",
    "@types/node": "^18.11.18",
    "chai": "^4.3.7",
    "ethers": "^5.7.2",
    "hardhat": "^2.12.6",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.4"
  },
  "dependencies": {
    "@nomicfoundation/hardhat-toolbox": "^2.0.1"
  }
}

I tried resetting account and switching network on Metamask back and forth but it doesn't work. Anyone help pleaseee?

1
  • Hey if this did help can you please make this an answer? It would help me a lot.
    – vampireAb
    Jan 24 at 4:55

1 Answer 1

1

There are two things at issue here,

  1. Function sendWei is not payable. Since you are transferring funds the function should be payable.

  2. While sending some funds, it is not passed as a parameter to the function it is sent with the message. The message (msg) is a global variable in solidity.

So, the contract should look like this.

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

contract SendWei {
    function sendWei(address payable recipient) public payable {
        require(msg.value > 0, "Amount must be greater than 0");
        recipient.transfer(msg.value);
    }
}

Again, your test should have the following change:

// Send 10 wei to the recipient
   await myContract.sendWei(recipient, {value: amount});
1
  • Thanks, some misunderstanding causes the bug. Your answer help clear some of my doubts.
    – Sprint
    Jan 24 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.