web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

encodeParameters is broken on tuples

Open cd1m0 opened this issue 2 years ago • 2 comments

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

  1. Install web3-eth-abi version 4.0.3
  2. Run above code in node

Environment

Node 20

cd1m0 avatar Jul 25 '23 02:07 cd1m0

This is a bug and should be investigated and fixed. Thank you for helping to find it.

avkos avatar Jul 26 '23 01:07 avkos

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

blitz-1306 avatar Jul 26 '23 06:07 blitz-1306