web3.js
web3.js copied to clipboard
encodeParameters is broken on tuples
Expected behavior
Running this code should succeed:
const ethABI = require("web3-eth-abi");
ethABI.encodeParameters(["tuple(uint256,uint256)"], [[1,2]]);
Actual behavior
Running this code results in the below exception:
Uncaught AbiError: Parameter encoding error
at Object.encodeParameters (/home/dimo/work/consensys/sol2maruir/node_modules/web3-eth-abi/lib/commonjs/api/parameters_api.js:67:15) {
innerError: TypeError: Cannot read properties of undefined (reading 'length')
at formatParam (/home/dimo/work/consensys/sol2maruir/node_modules/web3-eth-abi/lib/commonjs/utils.js:149:30)
at /home/dimo/work/consensys/sol2maruir/node_modules/web3-eth-abi/lib/commonjs/utils.js:197:48
at Array.forEach (<anonymous>)
at modifyParams (/home/dimo/work/consensys/sol2maruir/node_modules/web3-eth-abi/lib/commonjs/utils.js:188:18)
at Object.encodeParameters (/home/dimo/work/consensys/sol2maruir/node_modules/web3-eth-abi/lib/commonjs/api/parameters_api.js:60:45)
at REPL20:1:8
at Script.runInThisContext (node:vm:122:12)
at REPLServer.defaultEval (node:repl:570:29)
at bound (node:domain:433:15)
at REPLServer.runBound [as eval] (node:domain:444:12),
code: 205
Steps to reproduce the behavior
- Install web3-eth-abi version 4.0.3
- Run above code in node
Environment
Node 20
This is a bug and should be investigated and fixed. Thank you for helping to find it.
Additionally, for those who else would hit this issue: there is a workaround if you remove tuple and pass types in parens. An example:
const ethABI = require("web3-eth-abi");
console.log(ethABI.encodeParameters(["(uint256,uint256)"], [[1,2]]));
Results
0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002