I'm trying to test a withdrawal function of my contract through truffle over testrpc. The function is following the "withdrawal from contracts" patter described here. Essentially a notifyPayment() function is invoked to fire an event and put some funds in a pendingWithdrawls[address] mapping; the recipient can then invoke a withdraw function to attempt to retrieve the funds. This seems to work, but the amount the recipient gets is slightly less than what expected, even taking transaction fees into account. Here's the testing code in truffle:

it("should allow withdrawal and remove pending funds when withdraw", function() {    
  var account_one_starting_balance;
  var account_one_ending_balance;
  var account_one_starting_pending;
  return SLA.deployed().then(function(inst) {
    instance = inst;  
    account_one_starting_balance = web3.fromWei(web3.eth.getBalance(accounts[1]));
    console.log("starting balance", account_one_starting_balance.toString(10));
    return instance.pendingWithdrawals.call(accounts[1]);
    account_one_starting_pending = web3.fromWei(pending);
    console.log("pending withdrawal", account_one_starting_pending.toString(10));
    return instance.increaseFunds({from: accounts[0], value: 2*pending});
    return instance.withdraw({from: accounts[1]});
    var tx_cost = web3.fromWei(web3.toBigNumber(withdraw_txid.receipt.gasUsed).times(web3.eth.gasPrice));
    console.log("transaction cost", tx_cost.toString(10));
    account_one_ending_balance = web3.fromWei(web3.eth.getBalance(accounts[1]));
    console.log("ending balance", account_one_ending_balance.toString(10));      
    var account_one_credit = account_one_ending_balance.minus(account_one_starting_balance);
    // the next assertion fails
    assert.equal(account_one_credit, account_one_starting_pending.minus(tx_cost), "account one was not credited");
    return instance.pendingWithdrawals.call(accounts[1]);
    assert.equal(newPending.toNumber(), 0);

notice that I went a bit overboard with big number conversion as I thought it could be a rounding problem. Here is the result of the test:

starting balance 100.030220199999944206
pending withdrawal 0.01
transaction cost 0.00037196
ending balance 100.038360399999944206
{ AssertionError: account one was not credited: expected { Object (s, e, ...) } to equal { Object (s, e, ...) }
    at /home/-/src/sla-solidity/test/sla.js:128:14
    at process._tickCallback (internal/process/next_tick.js:109:7)
  message: 'account one was not credited: expected { Object (s, e, ...) } to equal { Object (s, e, ...) }',
  showDiff: false,
  actual: { [String: '0.0081402'] s: 1, e: -3, c: [ 814020000000 ] },
  expected: { [String: '0.00962804'] s: 1, e: -3, c: [ 962804000000 ] 
} }

commenting out that assertion makes the test pass. What causes the difference in the credited amount? I'm sure I must be missing something trivial...

  • Did you ever find a solution? I have a very similar problem. – Jesse Busman Feb 25 at 10:31
  • 1
    My problem was because the transaction fee is also subtracted from the sender address. – Jesse Busman Feb 25 at 12:16
  • 1
    @JesseBusman sorry I have stopped working on this long ago, can't really help here but it was probably the same thing – manuhalo Feb 26 at 11:22

Your Answer

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

Browse other questions tagged or ask your own question.