Function Calls

Function Call #1 – Calling The Receivers

    /// @notice function of emitting event Propose for catching by receiverkeepers
    /// @param protocolId protocol id to interact
    /// @param dstChainId dest chain id
    /// @param protocolAddress dest protocol address is bytes32 cause dstChainId may be non-EVM
    /// @param functionSelector dest protocol function id, for EVM is abi func selector, for non-EVM may be any id format
    /// @param params function params for dest protocol function
    function propose(
        bytes32 protocolId,
        uint256 dstChainId,
        bytes calldata protocolAddress,
        bytes functionSelector,
        bytes calldata params
    ) external isAllowedProposer(protocolId) {
        emit Propose(protocolId, nonce++, dstChainId, protocolAddress, functionSelector, params);
    }

Caller

contract MyContract {
    struct YourMsgStruct {
        string str;
        uint num;
    }

    IProposer endPoint;
    address ethContractAddress;

    function sendProposeOperation() external {
        bytes32 protocolId = bytes32("your-protocol-id");
        uint destChainId = 1; // eth
        bytes memory destContractAddress = abi.encode(ethContractAddress);
        bytes memory selector = abi.encode(bytes4(keccak256(bytes("receive(bytes)"))));
        YourMsgStruct memory msg = YourMsgStruct("hello world", 9000);
        bytes memory encodedPayload = abi.encode(msg);
        endPoint.propose(
                protocolId,
                destChainId,
                destContractAddress,
                selector,
                encodedPayload);
    }
}

Explainer

The `propose` function is designed to emit a structured event containing details about a proposed interaction with a protocol on possibly another blockchain.

This event can then be caught by Receivers, which may perform further actions such as validating the proposal, executing cross-chain calls, or any other required process as per the system's design.

Function Call #2 – Receive Prompt From End Point

/// @notice Receive function example, always should take 1 arg (bytes)
/// @param data encoded: bytes32 protocolId, uint256 srcChainId, uint256 srcBlockNumber, bytes32 srcOpTxId, bytes payload
function receive(bytes calldata data) external onlyRole(SPOTTER) {
    (bytes32 protocolId, uint256 srcChainId, uint256 srcBlockNumber, bytes32 srcOpTxId, bytes memory payload) = abi.decode(data, (bytes32, uint256, uint256, bytes32, bytes));
    YourMsgStruct memory data = abi.decode(payload, (YourMsgStruct));
}

Explainer

This `receiver`function, is designed to be triggered by an End Point contract on a destination blockchain network as part of an operation that has gained consensus approval. This role restriction ensures that only authorized entities can trigger this function, enhancing security.

The function receives a single parameter, `data`, which is a byte array (`bytes calldata`). This array encapsulates multiple pieces of information:

  • protocolId: The identifier of protocol, where this operation was created and approved.

  • srcChainId: The ID of the source blockchain from which the operation originated.

  • srcBlockNumber: The block number on the source blockchain at which the operation was initiated or recorded.

  • srcOpTxId: A unique identifier (transaction ID) for the operation on the source blockchain.

  • payload: Additional data associated with the operation, which can be specific to the operation's context.

The function begins by decoding the `data` parameter to extract the `srcChainId`, `srcBlockNumber`, `srcOpTxId`, and the `payload`. This is done using the `abi.decode` function.

After extracting the basic information, the function further decodes the `payload` to obtain more specific data relevant to the operation.

For Web2 triggers, the srcChainId, srcBlockNumber, and srcOpTxId parameters can contain information about source identifier. This can be beneficial for processing information from various sources differently.

Last updated