Created
February 24, 2021 13:09
-
-
Save DanielZlotin/8cc7fa1c1073feb3d37e68cbcf03dc29 to your computer and use it in GitHub Desktop.
[web3] reproduction of runtime error when passing bn.js values inside structs or arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { BN } = require("bn.js"); | |
const Web3 = require("web3"); | |
const web3 = new Web3(); | |
const value = new BN("12345"); | |
console.log( | |
web3.eth.abi.encodeParameters( | |
[{ internalType: "uint256", name: "input1", type: "uint256" }], | |
[value] | |
) | |
); | |
console.log( | |
web3.eth.abi.encodeParameters( | |
[ | |
{ internalType: "uint256", name: "input1", type: "uint256" }, | |
{ internalType: "uint256", name: "input1", type: "uint256" }, | |
], | |
[value, value] | |
) | |
); | |
// fails with Error: invalid BigNumber value (argument="value", value="3039", code=INVALID_ARGUMENT, version=bignumber/5.0.14): | |
// works when sending value as string | |
console.log( | |
web3.eth.abi.encodeParameters( | |
[{ internalType: "uint256[1]", name: "input1", type: "uint256[1]" }], | |
[[value]] | |
) | |
); | |
// fails with Error: invalid BigNumber value (argument="value", value="3039", code=INVALID_ARGUMENT, version=bignumber/5.0.14): | |
// works when sending value as string | |
console.log( | |
web3.eth.abi.encodeParameters( | |
[ | |
{ | |
components: [ | |
{ internalType: "uint256", name: "uint256_0", type: "uint256" }, | |
{ internalType: "uint256", name: "uint256_1", type: "uint256" }, | |
], | |
internalType: "struct Foo.Bar", | |
name: "input1", | |
type: "tuple", | |
}, | |
], | |
[[value, value]] | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment