1

I want to use an already abi-encoded parameter and encode it again with another value. Encoding works, but I cannot figure out the decoding. I technically could decode it and encode it again with that extra parameter but I think there should be a smarter way.

In my original code I already get an encoded parameter through the function call and want to encode it with extra information and then pass it to another function which then decodes everything together.

The following is an example contract that tries to mimic what I want to do:

Contract test {
    struct MyStruct {
        uint a;
        uint b;
    }

    function dummycall() external {
        // Fill with some data
        MyStruct[] struct;
        struct.push((1, 2));
        struct.push((3, 4));

        data = abi.encode(struct);

        // This works
        uint256 number = 1;
        bytes memory encoded = abi.encode(data, number);

        // How do I decode this now? This does not work:
        uint256 _decodedNumber;
        Struct[] _decodedStruct;
        (_decodedStruct, _decodedNumber) = abi.decode(encoded, (Struct[], uint256))
        
    } 
}

1 Answer 1

0

Adding an extra parameter is quite complicated, unless all the parameters are fixed size.

The function abi.encode uses the solidity abi specification https://docs.soliditylang.org/en/develop/abi-spec.html.

It has one part for the fixed size parameters (uintXXX, bytesYY, struct, etc) and offsets for the dynamic sized parameters, and another part for the dynamic size parameters (strings, variable size arrays, etc).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.