0

I'm desperately trying to find an answer as to how I can accurately test my smart contract balances, as not only can I not get the test to return the correct balance number, but I also am not seeing any test ether leaving my Hardhat test account either.

Relevant code is below, what am I doing wrong??? I've looked everywhere for a concrete answer but I can't seem to find one. The idea is just a simple pool contract that is only accepting deposits (in ETH, important) at this point, and then should accurately state that it has the user's deposited amount of funds.

Am I referencing the amount wrong? Is it a conversion issue between the two? Anyone know what gives on this? I feel like this action is super simple so I must be on the wrong page about something, but both balances currently return zero.

Contract:

contract DepositPool {

  mapping(address => uint256) public userBalance;

  function makeDeposit(
        uint256 _amount,
        address _from
      ) public payable {
          require(_amount > 0, "Must input amount to deposit");

          _amount = msg.value;
          _from = msg.sender;

        userBalance[msg.sender] += msg.value;

        console.log("User balance: ", userBalance[msg.sender]);
        console.log("Contract balance: ", address(this).balance);
    }
}

Test:

describe('DepositPool', function () {
    it('Should accept deposits and have correct balances', async function () {

        const Pool = await ethers.getContractFactory('DepositPool');
        const pool = await Pool.deploy();
        await pool.deployed();

        const [user] = await ethers.getSigners();

        const makeADeposit = await pool.connect(user).makeDeposit(20, user.address);

        await makeABet.wait();
        console.log(makeABet);
        console.log(await pool.getPoolBalance().toString());

        assert.equal(20, 20, 'Pool balance should equal 20');
    });

**using Hardhat's console.log import by the way if anyone is wondering why those log statements are in there

THANK YOU!

0

When you send ETH, the value is the amount in Wei, from the msg.sender, so no need to pass amount and from.

You can try this

contract DepositPool {

  mapping(address => uint256) public userBalance;

  function makeDeposit() public payable {
        require(msg.value > 0, "Must input amount to deposit");

        userBalance[msg.sender] += msg.value;

        console.log("User balance: ", userBalance[msg.sender]);
        console.log("Contract balance: ", address(this).balance);
    }
}

When you test, just send ETH use from and value

describe('DepositPool', function () {
    it('Should accept deposits and have correct balances', async function () {

        const Pool = await ethers.getContractFactory('DepositPool');
        const pool = await Pool.deploy();
        await pool.deployed();

        const [user] = await ethers.getSigners();

        const makeADeposit = await pool.connect(user).makeDeposit({from: user.address, value: 20});

        await makeABet.wait();
        console.log(makeABet);
        console.log(await pool.getPoolBalance().toString());

        assert.equal(20, 20, 'Pool balance should equal 20');
    });
1
  • Thank you for your answer! The above does make sense to me and that is how I initially had it setup, however my test always errors out whenever I remove the amount variable in the contract and tells me it's an invalid BigNumber value for the test argument. Even on Remix I can't get the correct balances to return, I feel like this is so simple but I just have zero idea as to why the balances won't update at all
    – jkiles
    Sep 14 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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