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 Blockchain ID (HEX) 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 contracts/interchain-messaging/invoking-functions/SimpleCalculatorSenderOnCChain.sol:SimpleCalculatorSenderOnCChain
[⠊] Compiling...
[⠒] Compiling 1 files with Solc 0.8.18
[⠢] Solc 0.8.18 finished in 84.20ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0x789a5FDac2b37FCD290fb2924382297A6AE65860
Transaction hash: 0xd6cb47dd4ff38e4447711ebbec8b646b93f492f48e80b51719f860984cc25413

Save the Sender Contract Address

Overwrite the SENDER_ADDRESS environment variable with the new address:

export SENDER_ADDRESS=0x789a5FDac2b37FCD290fb2924382297A6AE65860

On this page