logoAcademy

Create Simple Calculator Receiver

Create a contract that receives multiple parameters and execute a function

On our Avalanche L1, we need to create the receiver part of our cross-chain calculator. It will receive two numbers and store the result.

src/02a-encoding-multiple-paramters/SimpleCalculatorReceiverOnAvalanche L1.sol
pragma solidity ^0.8.18;
 
import "@teleporter/ITeleporterMessenger.sol";
import "@teleporter/ITeleporterReceiver.sol";
 
contract SimpleCalculatorReceiverOnAvalanche L1 is ITeleporterReceiver {
    ITeleporterMessenger public immutable teleporterMessenger =
        ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);
 
    uint256 public result_num;
 
    function receiveTeleporterMessage(bytes32, address, bytes calldata message) external {
        // Only the Interchain Messaging receiver can deliver a message.
        require(
            msg.sender == address(teleporterMessenger), "CalculatorReceiverOnAvalanche L1: unauthorized TeleporterMessenger"
        );
 
        (uint256 a, uint256 b) = abi.decode(message, (uint256, uint256)); 
        _calculatorAdd(a, b);
    }
 
    function _calculatorAdd(uint256 _num1, uint256 _num2) internal {
        result_num = _num1 + _num2;
    }
}
 

Deploy the Receiver Contract

Deploy the receiver contract on your Avalanche L1:

forge create --rpc-url myblockchain --private-key $PK src/2a-invoking-functions/SimpleCalculatorReceiverOnAvalanche L1.sol:SimpleCalculatorReceiverOnAvalanche L1

Then save the sender contract address (displayed in Deployed To:) in an environment variable:

export RECEIVER_ADDRESS=0x123...
Updated:

On this page

Edit on Github