🧑Execution Paths & Inner Payloads.

Execution Paths And InnerPayloads

While working with axelar for cross chain communication. We highlighted a pattern tha we believe should be in the developer docs and a lot of cross chan applications would use. which is Execution Paths and Inner Payloads. To describe this we'll be bringing a segment of mrKrabz contract to explain.

When you have different function updating different things you'll need to put this into play.

  1. All Functions Should Have An Execution Path ID

 // execution path handlers
    uint256 public topUpWalletPath = 1;
    uint256 public buyTicketPath = 2;
    uint256 public newRoundPath = 3;
    uint256 public setRandomWinnersPath = 4;
    uint256 public claimWinningsPath = 5;
    uint256 public withdrawFromWalletPath = 6;

  1. Always Encode The Execution Path ID As The First Param In Your Payload

This will give you the ability to decide what your inner payload would be in your _execute function.

Your innerPayload is basically all your other encoded parameters into one payload.

In this instance in our inner payload we encoded the params we need such as our FILECOIN_CHAIN_ID, _nativeAsset, _user .

We then passed the innerPayload into the main payload along with its execution path.

//inner payload
bytes memory innerPayload = abi.encode(FILECOIN_CHAIN_ID, _nativeAsset, _amount, _user);

// main payload
bytes memory payload = abi.encode(topUpWalletPath, innerPayload);

_sendPayloadFromFilecoin(payload, _estimateGasAmountOne);

  1. Separate All Execution Paths In _execute function

This allows you to be more flexible with your paramterers and have diffrenet outcomes for different function calls.

 
function _execute(string calldata, string calldata, bytes calldata payload) internal override {
        (uint256 executionPath, bytes memory innerPayload) = abi.decode(payload, (uint256, bytes));

        // top up wallet execution path
        if (executionPath == topUpWalletPath) {
            (uint256 chainid, bool nativeAsset, uint256 amount, address user) = abi.decode(
                innerPayload,
                (uint256, bool, uint256, address)
            );

            _handleTopupWalletExecutionPath(chainid, nativeAsset, amount, user);
        }

        // buy ticket path
        if (executionPath == buyTicketPath) {
            // unpack innerpayload
            (
                uint256 ticketId,
                address user,
                uint256[3] memory selectedNumbers,
                uint256 originChainId,
                bool nativeAsset,
                uint256 requiredAsset,
                uint256 currentRoundId,
                Ticket memory ticket
            ) = abi.decode(
                    innerPayload,
                    (uint256, address, uint256[3], uint256, bool, uint256, uint256, Ticket)
                );

            _handleBuyTicketexecutionPath(
                ticketId,
                user,
                selectedNumbers,
                originChainId,
                nativeAsset,
                requiredAsset,
                currentRoundId,
                ticket
            );
        }
        
        //other execution paths...
}

Last updated