1

solidity types data structure is like this:

enum RequestType {
        SpecificReq,
        ArbitraryReq
    }

    enum ChannelType {
        InterestBearingOpen,
        InterestBearingMutual,
    }

    struct ChannelPayload {
        string channelAddress;
        ChannelType channelType;
    }

    struct SpecificRequestPayload {
        bytes4 functionSig;
        ChannelPayload channelData;
    }

    struct ArbitraryRequestPayload {
        bytes4 functionSig;
    }

this is how I'm encoding and decoding

    function encodeSpecificReqPayload (SpecificRequestPayload memory _payload) public view returns (bytes memory) {
        return abi.encode(_payload, msg.sender, RequestType.SpecificReq);
    }

    function encodeArbitraryReqPayload (ArbitraryRequestPayload memory _payload) public view returns (bytes memory) {
        return abi.encode(_payload, msg.sender, RequestType.ArbitraryReq);
    }

    function decodePayload (bytes memory _payload) public pure returns (uint8 requestType) {
        (,, requestType) = abi.decode(_payload, (bytes, address, uint8));

        if (requestType == 0) {
            // Specific Req Type
            (SpecificRequestPayload memory reqPayload, address sender,) =
                abi.decode(_payload, (SpecificRequestPayload, address, uint8));

            // ROUTE to SPECIFIC REQUEST ROUTING Function
        } else {
            // Arbitrary Req Type
            (ArbitraryRequestPayload memory reqPayload, address sender,) =
                abi.decode(_payload, (ArbitraryRequestPayload, address, uint8));

            // Directly call ARBITRARY REQUEST FUNCTION
        }
    }

It's throwing panic: out of memory exception at abi.decode line when payload is generate from one of the encode functions and sent into decodePayload function how to resolve this?

PS: it's a cross-chain messaging transfer, and due to this only a single payload can be sent in a tx

1 Answer 1

2

Update: Got the solution

The struct payload parameter must be encoded first and then encoded along with other values.

function encodeSpecificReqPayload (SpecificRequestPayload memory _payload) public view returns (bytes memory) {
    bytes memory reqPayloadBytes = abi.encode(_payload);
    return abi.encode(reqPayloadBytes, msg.sender, RequestType.SpecificReq);
}

function encodeArbitraryReqPayload (ArbitraryRequestPayload memory _payload) public view returns (bytes memory) {
    bytes memory reqPayloadBytes = abi.encode(_payload);
    return abi.encode(reqPayloadBytes, msg.sender, RequestType.ArbitraryReq);
}

function decodePayload (bytes memory _payload) public pure {
    (bytes memory payloadS, address sender ,uint8 requestType) = abi.decode(_payload, (bytes, address, uint8));

    if (requestType == 0) {
        // Specific Req Type
        (SpecificRequestPayload memory reqPayload) =
            abi.decode(payloadS, (SpecificRequestPayload));

        // ROUTE to SPECIFIC REQUEST ROUTING Function
    } else {
        // Arbitrary Req Type
        (ArbitraryRequestPayload memory reqPayload) =
            abi.decode(payloadS, (ArbitraryRequestPayload));

        // Directly call ARBITRARY REQUEST FUNCTION
    }
}

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.