0

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 { expect } = require('chai');
const { ethers } = require('hardhat');

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

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

    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();

        // wallet Balance Before Transaction 
        beforeBalance = await ethers.provider.getBalance(wallet.address);
        console.log("Wallet Before Balance", ethers.utils.formatEther(beforeBalance));

        // Addr1 balance before transaction 
        beforeBalance = await addr1.getBalance();
        console.log("Addr1 Before Balance", ethers.utils.formatEther(beforeBalance));
        
        // Transaction Amount 100 Ethers
        console.log("Transaction Amount", ethers.utils.formatEther(transaction));

        // Sending Ethers from Addr1 to Wallet Address
        await addr1.sendTransaction({ to: wallet.address, value: transaction });
        afterBalance = await addr1.getBalance();
        console.log("Addr1 After Balance", ethers.utils.formatEther(afterBalance));

        // Wallet Balance after transaction
        afterBalance = await ethers.provider.getBalance(wallet.address);
        console.log(`Wallet After Balance ${ethers.utils.formatEther(afterBalance)} 
        Now the Seceond Transaction using Send function`);

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

        // Sending Ethers from Wallet to Addr2 
        await wallet.send(addr2.address, transaction);
    });

Error

Wallet Before Balance 0.0
Addr1 Before Balance 10000.0
Transaction Amount 100.0
Addr1 After Balance 9899.999919908843833176
Wallet After Balance 100.0 
        Now the Seceond Trnasaction
Addr2 Balance 10000.0
    1) "before each" hook for "should deploy Successfuly"


  0 passing (2s)
  1 failing

  1) Wallet Contract
       "before each" hook for "should deploy Successfuly":
     Error: VM Exception while processing transaction: reverted with reason string 'Not enough funds'
    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 Answer 1

0

In your test, I do not see you sending Ether to the contract.

You are calling the send method in your contract but the balance is 0.

Try to send ether to the contract first with addr1. This will update the balance.

After this is done, you can call the send method.

Since you are working from multiple account in your test you will have to connect the address you want to use to the contract.

You are most likely calling the send method as the owner account. To use a different account, you must connect it: https://docs.ethers.org/v5/api/contract/contract/#Contract-connect

Try the following;

// Sending Ethers from Wallet to Addr2 
        await wallet.connect(addr1).send(addr2.address, transaction);

Now the contract will see msg.sender as addr1 which should have a balance

4
  • Sorry for that mistake. I updated the code please review it. Oct 21, 2023 at 11:08
  • Updated my answer Oct 21, 2023 at 11:29
  • It also shows an error. addr1 does not have enough funds. Oct 21, 2023 at 17:17
  • In your example, addr1 already sent the funds and the balance was deducted?! Oct 21, 2023 at 17:24

Your Answer

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

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