1

To simplify my question, let's say I have a Test.sol with a function that simply output the input:

function testFunc(uint256 value) public pure returns (uint256) {
  return value;
}

Now I want to test it in truffle

it('uint input test', async () => {
  var test = await Test.deployed()
  res = await test.testFunc.call(0x10000000000000000000000000000001)
  console.log(res)
  console.log(res.toNumber())
  console.log(res.toString(16))
  assert.equal(res, 0x10000000000000000000000000000001, 'wrong')
})

I'm expecting the result should be same as the input, but it fails the assert. Outputs:

BigNumber { s: 1, e: 37, c: [ 2126764793, 25586540000000 ] }
2.1267647932558654e+37
1000000000000001d172c5f60ac00000

So 0x10000000000000000000000000000001 is changed to 0x1000000000000001d172c5f60ac00000, I assume it's the JavaScript number overflow problem?

It causes problem when I try to test the bit operations, such as checking ERC721 token ID. How can I bypass it?

  • 1
    The problem is javascript numbers do not have enough precision. Either compare them as BigNumers (the objects returned by web3js), or transform them into strings and compare the as such. – Ismael Jul 24 '18 at 3:36
0

While this may not be an exact answer, it works when using chai promise:

require('chai')
  .use(require('chai-as-promised'))
  .use(require('chai-bignumber')(BigNumber))
  .should()

...

const res = await test.testFunc.call(0x10000000000000000000000000000001)
res.toString().should.be.equal("0x0000000000000000000000000000000000000000")

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.