Send Function is not working in test file using ethers.js

85 views Asked by At

I write a wallet smart contract in which one address can send ethers to other addresses. I'm using the HardHat framework and this package. when i use await addr1.sendTransaction( {to: addr2, value: transaction} ) it work. but when I try to use the send function it doesn't work.

methods i try and faild.

await wallet.send({from: addr1, to: addr2.address, value: transaction});
-------------
await wallet.send(addr2.address, transaction, {from: addr1.address});
-------------
await wallet.connect(addr1).send(addr2.address, transaction);
-------------
await wallet.connect(addr1).send({from: addr1, to: addr2.address, value: transaction});
-------------
await wallet.connect(addr1).send

Package.json

{
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.2.3",
    "ethers": "^5.0.0",
    "hardhat": "^2.18.2"
  },
  "dependencies": {
    "@nomiclabs/hardhat-waffle": "^2.0.6",
    "@openzeppelin/contracts": "^5.0.0",
    "chai": "^4.3.10",
    "ethereum-waffle": "^4.0.10"
  }
}

Wallet.sol

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";


//Wallet Smat Contract 
contract Wallet {

    string public walletName;
    address private owner;

    //Events 
    event Transfer(address sender, address recipent, uint amount);
    event Receive(address _from, uint _amount);

    mapping (address => uint) _balance;
    mapping (address => mapping(address => uint)) _tokenBalance;

    constructor() {
        owner = msg.sender; // Function to receive Ether. msg.data must be empty
        walletName = "Huzaifa Wallet";
    }

    //Function to receive Ether
    receive() external payable {
        _balance[msg.sender] = _balance[msg.sender]+(msg.value);

        emit Receive(msg.sender, msg.value);
    }

    //Function to send Ether
    function send(address payable recipient, uint amount) public {
        require(_balance[msg.sender] >= amount, "Not have enough amount");
        _balance[msg.sender] = _balance[msg.sender]-(amount);
        _balance[recipient] = _balance[recipient]+(amount);

        emit Transfer(msg.sender, recipient, amount);
    }
}

//some more code

Test File

const { ethers } = require('hardhat');

describe("Wallet Contract", () => {

    let wallet, owner, addr1, addr2, addr3, token, beforeBalance, afterBalance, balance;
    let transaction = ethers.utils.parseEther("10");

    beforeEach(async () => {
        // Deploy Wallet Contract
        let Wallet = await ethers.getContractFactory("Wallet");
        [owner, addr1, addr2, addr3, _] = await ethers.getSigners();
        wallet = await Wallet.deploy();
        await wallet.deployed();

        balance = await ethers.provider.getBalance(wallet.address);
        console.log("Wallet Balance", ethers.utils.formatEther(balance));

        balance = await owner.getBalance();
        console.log("Owner Balance", ethers.utils.formatEther(balance));

        balance = await addr1.getBalance();
        console.log("Addr1 Balance", ethers.utils.formatEther(balance));

        balance = await addr2.getBalance();
        console.log("Addr2 Balance", ethers.utils.formatEther(balance));

        balance = await addr3.getBalance();
        console.log("Addr3 Balance", ethers.utils.formatEther(balance));
        
        console.log("Transaction Amount", ethers.utils.formatEther(transaction));

        await wallet.connect(addr1).send(addr2.address, transaction);
    });

Error

Wallet Balance 0.0
Owner Balance 9999.997711135
Addr1 Balance 10000.0
Addr2 Balance 10000.0
Addr3 Balance 10000.0
Transaction Amount 10.0
    1) "before each" hook for "should deploy Successfuly"


  0 passing (18s)
  1 failing

  1) Wallet Contract
       "before each" hook for "should deploy Successfuly":
     Error: VM Exception while processing transaction: reverted with reason string 'Not have enough amount'
    at Wallet.send (contracts/Wallet.sol:34)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at runNextTicks (node:internal/process/task_queues:64:3)
    at listOnTimeout (node:internal/timers:538:9)
    at processTimers (node:internal/timers:512:7)
    at HardhatNode._mineBlockWithPendingTxs (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:1866:23)
    at HardhatNode.mineBlock (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:524:16)
    at EthModule._sendTransactionAndReturnHash (node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1482:18)```
1

There are 1 answers

0
Akeel Ahmed Qureshi On

The reason of the error is specified in the error stack trace:

Error: reverted with reason string 'Not have enough amount'

from balance = await ethers.provider.getBalance(wallet.address) this line we can see that the balance of wallet is 0. which mean wallet haven't received any Ethers from any address.

That is the reason when you try to transfer ethers using send method:

await wallet.connect(addr1).send(addr2.address, transaction);

This method check weather the addr1 have enough balance or not. And the

_balance[msg.sender] >= amount

resolve to 0 > 10. Thus you are getting this error.

First you should transfer some ethers from addr1 to you Wallet contract. Then only you can transfer the ethers using send method.