8

I have a situation I have isolated that seems very bizarre to me and I cannot figure out. I am running truffle v1.0.2 (not latest) and testrpc v2.0.5.

When I send ETH from an account to a contract with a fallback function that calls a library function, the sending account is reduced by twice as much as the send value. When I remove the library call, it sends the expected amount. It should all be clear from the below code. What's going on?

You can clone and test it for yourself at: https://github.com/raineorshine/double-send.

Contract.sol:

import "MyLib.sol";

contract Contract {
  function() {
    // The test is showing that the balance of account[1] is decremented 
    // by 200 instead of the expected 100.
    // If I remove this line, the balance of account[1] is decremented 
    // by 100.
    MyLib.doNothing();
  }
}

MyLib.sol:

library MyLib {
  function doNothing() {
  }
}

test/spec.js:

describe('test', () => {
  contract('Contract', () => {
    it('send to deployed address', () => {
      const contract = Contract.deployed()
      const initialBalance = web3.eth.getBalance(web3.eth.accounts[1])
      console.log(web3.fromWei(initialBalance).toString())
      web3.eth.sendTransaction({ from: web3.eth.accounts[1], to: contract.address, value: web3.toWei(100) })

      const finalBalance = web3.eth.getBalance(web3.eth.accounts[1])
      console.log(web3.fromWei(finalBalance).toString())

      const diff = web3.fromWei(initialBalance.minus(finalBalance))
      console.log('initialBalance - finalBalance == ' + diff)

      assert(diff > 99 && diff < 101, 'Balance after is not ~100 less than balance before.')
    })
  })
})

Here is the test output:

  test
    Contract: Contract
21267647932558650071.31300778513203191
21267647932558649871.313007785132010657
initialBalance - finalBalance == 200.000000000000021253
      1) send to deployed address
    > No events were emitted


  0 passing (2s)
  1 failing

  1) test Contract: Contract send to deployed address:
     AssertionError: Balance after is not ~100 less than balance before.
1

It has been fixed in latest 4.1.0 release

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.