Tuesday, 3 November 2020

Issue encoding array of addresses for Web3

I'm trying to call the Uniswap Router contract like:

// dummy data
Function: swapExactETHForTokens(uint256 amountOutMin, address[] path, address to, uint256 deadline)

MethodID: 0x7ff36ab5
[0]:  000000000000000000000000000000000000000000000000000000dd0f593444
[1]:  0000000000000000000000000000000000000000000000000000000000000080
[2]:  00000000000000000000000047c0a182235478ca13d248d049eaa28d4ff7520f
[3]:  000000000000000000000000000000000000000000000000000000005f9a1e44
[4]:  0000000000000000000000000000000000000000000000000000000000000002
[5]:  000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
[6]:  0000000000000000000000001a969239e12f07281f8876d11afcee081d872adf

In my TypeScript code I have:

const abi = web3.eth.abi.encodeFunctionCall(
    {
      type: 'function',
      name: 'swapExactTokensForTokens',
      inputs: [
        {
          name: 'amountIn',
          type: 'uint256',
        },
        {
          name: 'amountOutMin',
          type: 'uint256',
        },
        {
          name: 'path',
          type: 'address[]',
        },
        {
          name: 'to',
          type: 'address',
        },
        {
          name: 'deadline',
          type: 'uint256',
        },
      ],
    },
    [
      fromWei,
      toWei,
      // the problem
      web3.eth.abi.encodeParameter('address[]', [
        '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
        '0x20fe562d797a42dcb3399062ae9546cd06f63280',
      ]),
      getAddress(),
      deadline,
    ]
  )

From the error, it looks like encodeFunctionCall wasn't expecting an encoded array (at least, the way I did it). So how would I pass an array of addresses?

(node:21008) UnhandledPromiseRejectionWarning:
Error: expected array value
(argument="path", value="0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc200000000000000000000000020fe562d797a42dcb3399062ae9546cd06f63280", code=INVALID_ARGUMENT, version=abi/5.0.0-beta.153)

Full source: https://github.com/ZaneH/tradr/blob/56d670aeec09ed0e8c3e359c48ae8b36fdea7c30/packages/backend/src/modules/Trades.ts#L37



from Issue encoding array of addresses for Web3

No comments:

Post a Comment