3

How is it possible to test if my contract actually transfers money to a specific account?

function _transfer(address receiver_, uint256 payout) internal{
    receiver_.transfer(payout);
}

Testing in JavaScript Syntax. Expecting something like:

assert.equal(accounts[1].balance, 100 Ether);

1 Answer 1

9

To assert the balance of an account, try this:

let expectedBalance = web3.toBigNumber(web3.toWei(3, 'ether'));
let actualBalance = await web3.eth.getBalance(accounts[1]);

assert.deepEqual(actualBalance, expectedBalance, "Balance incorrect!");
3
  • Thanks. Especially using web3.eth is very helpful: web3.js - Ethereum JavaScript API
    – Senju
    Jun 27, 2018 at 6:12
  • 1
    As an update to this answer, in current versions of web3 the BigNumber functionality used in this answer is now in web3.utils as web3.utils.toBN. Documentation Here Jul 6, 2019 at 5:23
  • If you're using ethers and hardhat, there's a convenient function there too: ethers.utils.formatEther(big_number_balance);
    – Olshansk
    Oct 17, 2021 at 20:25

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.