Why this does not work?
pragma solidity ^0.8.0;
contract DecodeTest {
function test() external pure returns (string memory s, uint n1, int n2, bool b) {
string memory str = "testing";
uint number1 = 123;
int number2 = -321;
bool boolean = true;
bytes memory data = abi.encode(str, number1, number2, boolean);
(s, data) = abi.decode(data, (string, bytes));
(n1, data) = abi.decode(data, (uint, bytes));
(n2, data) = abi.decode(data, (int, bytes));
(b, data) = abi.decode(data, (bool, bytes));
}
}
I was expecting that abi.decode
would return the remaining bytes on each call.
It is generating an overflow on the second decode call.
Is there another way to decode in steps?
My real use case is to decode arbitrary data returned from external calls, in which the called function can return different number of arguments and different ordering of types
abi.encode
/abi.decode
use the Contract ABI specification. The way it is used in the contract isn't supported because you are mixing fixed size variables (uint) with dynamic variables (string, bytes) and that the encoding will not align as it is expected.