0

I have a method in my solidity contract that looks something like this;

  function transfer() public payable {
    (bool sent, bytes memory data) = owner().call{value: msg.value}("");
  }

The contract inherets from the OpenZeppelin Ownable contract (hence the owner() call in the snippet above).

I am trying to write a javascript unit test that asserts that the balance of the owner account has increased by the message value after the call to transfer. This is what I have so far;

  it("transfers the value of the message to the owner's account", async () => {
    const contract = await TestContract.deployed();
    const account = accounts[2];
    const owner = accounts[0];
    const originalBalance = web3.utils.toBN(await getBalance(owner));
    const total = web3.utils.toBN(345);
    const expectedBalance = originalBalance.add(total);
    await contract.transfer({ from: account, value: total});
    const actualBalance = web3.utils.toBN(await getBalance(owner));
    assert.isTrue(expectedBalance.eq(actualBalance));
  }) 

This doesn't work but I can see that the owner account balance has changed after the call to transfer. I'm testing through truffle, with ganache running.

What is a good way to test/assert that funds where transferred from msg.value to an account?

1 Answer 1

0

Whats happening


To test/assert that funds were transferred from msg.value to an account, you can modify your existing test case to check the balance of the owner account before and after the transfer, and ensure that the difference between the two balances is equal to msg.value.

What to do


Here's an example of how you could do that using Mocha and Chai:

const { expect } = require("chai");

it("transfers the value of the message to the owner's account", async () => {
  const contract = await TestContract.deployed();
  const account = accounts[2];
  const owner = accounts[0];
  const originalBalance = web3.utils.toBN(await getBalance(owner));
  const total = web3.utils.toBN(345);

  // Make the transfer
  await contract.transfer({ from: account, value: total });

  // Check that the balance of the owner account has increased by msg.value
  const newBalance = web3.utils.toBN(await getBalance(owner));
  const expectedBalance = originalBalance.add(total);
  expect(newBalance.sub(originalBalance)).to.be.bignumber.equal(total);
});

Explanation


In this code, we first get the original balance of the owner account, and then make the transfer by calling the transfer method on the contract. After the transfer, we get the new balance of the owner account and calculate the expected balance by adding the msg.value to the original balance.

Finally, we use Chai's bignumber library to assert that the difference between the new balance and the original balance is equal to msg.value. This ensures that the funds were transferred correctly.

0

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.