Solidity – Passing ABI-Encoded Parameters to abi.encodeWithSignature

abisolidity

Given a function setXY(uint256 x, uint256 y), how can I encode data parameters x and y into a single bytes data array that can be fed into abi.encodeWithSignature('setXY(uint256,uint256)', data)?

Ethers has an ABI encoder that allows for encoding array values according to array types. This allows encoding parameters via abiCoder.encode(['uint256', 'uint256'], ['1', '2']) into a bytes array.

But, feeding this as data into abi.encodeWithSignature or abi.encodeWithSelector results in data being read incorrectly, likely due to abi.encode leaving leading zeros for each argument.

Is there any way of accomplishing this correctly?

Best Answer

You cannot use bytes data because it will be encoded as an array, and you want to encode two uint256 instead.

uint256 x = 1324;
uint256 y = 4444;
abi.encodeWithSignature('setXY(uint256,uint256)', x, y);
Related Topic