0

I'm trying to write a test in Truffle to test the following Solidity code:

function withdrawFunds() {
    uint funds = withdrawals[msg.sender];
    withdrawals[msg.sender] = 0;
    msg.sender.transfer(funds);
}

I'm running into trouble working out how to check whether the sender has received the correct amount of Ether stored in their withdrawals map, especially taking into account gas cost.

My current attempt to test withdrawing 1 ether.

let seller = accounts[1];

let initialBalance = web3.eth.getBalance(seller);

let withdrawal = await token.withdrawFunds({ from: seller });

var finalBalance = web3.eth.getBalance(seller);

let gasCost = getTransactionGasCost(withdrawal["tx"]);

finalBalance.minus(initialBalance).minus(gasCost);

assert.equal(
  finalBalance,
  etherToWei(1),
  "Funds were not credited to withdrawer."
);

getTransactionGasCost looks like this:

function getTransactionGasCost(tx) {
  let transaction = web3.eth.getTransactionReceipt(tx);
  let amount = transaction.gasUsed;
  let price = web3.eth.getTransaction(tx).gasPrice;

  return new BigNumber(price * amount);
}

However, at the moment running truffle test gives this error:

 Error: Invalid number of arguments to Solidity function
  at Object.InvalidNumberOfSolidityArgs (/usr/lib/node_modules/truffle/build/cli.bundled.js:43290:16)
  at SolidityFunction.validateArgs (/usr/lib/node_modules/truffle/build/cli.bundled.js:329518:22)
  at SolidityFunction.toPayload (/usr/lib/node_modules/truffle/build/cli.bundled.js:329534:10)
  at SolidityFunction.sendTransaction (/usr/lib/node_modules/truffle/build/cli.bundled.js:329607:24)
  at SolidityFunction.execute (/usr/lib/node_modules/truffle/build/cli.bundled.js:329700:37)
  at /usr/lib/node_modules/truffle/build/cli.bundled.js:318086:16
  at Promise (<anonymous>)
  at /usr/lib/node_modules/truffle/build/cli.bundled.js:318052:18
  at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

It seems that the call to token.withdrawFunds is causing the error, but I'm not sure why as the function should take no arguments. Am I approaching this test as a whole in the correct way? Any help is very appreciated!

1

Try deleting the build folder and re-compiling all your contracts with Truffle. I was running into a similar issue when I recently updated to 4.0. Also, make sure you updated to the latest Truffle and truffle-contract versions. They release Truffle 4.0.1 a few days ago.

I updated both packages, deleted the build folder with the artifacts and compiled all contracts again and this error disappeared. (I was getting this "Invalid number of arguments to Solidity function" even though the number of arguments passed was right)

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.