Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web3.eth.abi.encodeParameters breaks for BN #3221

Closed
Velenir opened this issue Nov 19, 2019 · 1 comment · Fixed by #3238
Closed

web3.eth.abi.encodeParameters breaks for BN #3221

Velenir opened this issue Nov 19, 2019 · 1 comment · Fixed by #3238
Labels
1.x 1.0 related issues Enhancement Includes improvements or optimizations

Comments

@Velenir
Copy link
Contributor

Velenir commented Nov 19, 2019

Expected behavior

Same as with non-BN numbers

> coder.encodeParameters(['uint256'], [42])
'0x000000000000000000000000000000000000000000000000000000000000002a'

Actual behavior

> coder.encodeParameters(['uint256'], [new BN(42)])
Thrown:
Error: invalid number value (arg="", coderType="uint256", value="2a")
    at Object.throwError (./web3_test/node_modules/ethers/utils/errors.js:68:17)
    at CoderNumber.encode (./web3_test/node_modules/ethers/utils/abi-coder.js:353:20)
    at ./web3_test/node_modules/ethers/utils/abi-coder.js:605:59
    at Array.forEach (<anonymous>)
    at pack (./web3_test/node_modules/ethers/utils/abi-coder.js:604:12)
    at CoderTuple.encode (./web3_test/node_modules/ethers/utils/abi-coder.js:764:16)
    at AbiCoder.encode (./web3_test/node_modules/ethers/utils/abi-coder.js:897:77)
    at ABICoder.encodeParameters (./web3_test/node_modules/web3-eth-abi/src/index.js:96:27) {
  reason: 'invalid number value',
  code: 'INVALID_ARGUMENT',
  arg: '',
  coderType: 'uint256',
  value: BN { negative: 0, words: [ 42 ], length: 1, red: null }
}
> 

This breaks any abi encoding for contract methods, so I can't pass a BN to any method accepting uint. Like Contract.methods.method(BN).encodeABI | call | send

Steps to reproduce the behavior

> const BN = require('bn.js')
undefined
> const coder = require('web3-eth-abi')
undefined
> coder.encodeParameters(['uint256'], [42])
'0x000000000000000000000000000000000000000000000000000000000000002a'
> coder.encodeParameters(['uint256'], [new BN(42)])
Thrown:
...

Logs

Versions

node --- v12.13.0
web3 --- 1.2.4

The problem is actually in ethers.js, ethers-io/ethers.js#653
I only create this issue to track progress on that and for visibility
If you could just update your dependencies when it is fixed, would be great.

@Velenir
Copy link
Contributor Author

Velenir commented Nov 20, 2019

So it seems ethers.js intentionally doesn't support BN

As web3 supports them (right?) there's a need for a change in web3-eth-abi
Something like

return ethersAbiCoder.encode(this.mapTypes(types), params.map(param => BN.isBN(param) ? param.toString() : param))

on L96
Maybe?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Enhancement Includes improvements or optimizations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants