logoAcademy

Create Simple Calculator Sender

Create a contract that sends multiple parameters of a function

Now, our goal is to create a dApp that works as a cross-Avalanche L1 Calculator, receiving multiple parameters and using those for a calculation. For now our calculator will only have the Sum function.

Create Sender Contract

On the Local C-Chain, we need to create the sender part of our cross-chain calculator. This will send two numbers (uint) to the receiver contract on your Avalanche L1.

src/02a-encoding-multiple-paramters/SimpleCalculatorSenderOnCChain.sol
pragma solidity ^0.8.18;
 
import "@teleporter/ITeleporterMessenger.sol";
 
contract SimpleCalculatorSenderOnCChain {
    ITeleporterMessenger public immutable teleporterMessenger =
        ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);
 
    function sendAddMessage(address destinationAddress, uint256 num1, uint256 num2) external {
        teleporterMessenger.sendCrossChainMessage(
            TeleporterMessageInput({
                // Replace with chain id of your Avalanche L1 (see instructions in Readme)
                destinationBlockchainID: 0xd7ed7b978d4d6c478123bf9b326d47e69f959206d34e42ea4de2d1d2acbc93ea, 
                destinationAddress: destinationAddress,
                feeInfo: TeleporterFeeInfo({feeTokenAddress: address(0), amount: 0}),
                requiredGasLimit: 100000,
                allowedRelayerAddresses: new address[](0),
                message: encodeAddData(num1, num2)
            })
        );
    }
 
    //Encode helpers
    function encodeAddData(uint256 a, uint256 b) public pure returns (bytes memory) {
        bytes memory paramsData = abi.encode(a, b); 
        return paramsData;
    }
}

To increase the readability of the code, we have created a helper function encodeAddData that encodes the two numbers into a single byte array.

Replace destinationBlockchainID

Don't forget to replace the destinationBlockchainID with the one from your Avalanche L1!

Deploy the Sender Contract

Deploy the sender contract on the Local C-Chain:

forge create --rpc-url local-c --private-key $PK src/2a-invoking-functions/SimpleCalculatorSenderOnCChain.sol:SimpleCalculatorSenderOnCChain

Save the Sender Contract Address

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

export SENDER_ADDRESS=0x123...
Updated:

On this page

Edit on Github