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 Fuji 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 the Dispatch test L1.
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({
// BlockchainID of Dispatch L1
destinationBlockchainID: 0x9f3be606497285d0ffbb5ac9ba24aa60346a9b1812479ed66cb329f394a4b1c7,
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.
Deploy the Sender Contract
Deploy the sender contract on the Fuji C-Chain:
forge create --rpc-url fuji-c --private-key $PK contracts/interchain-messaging/invoking-functions/SimpleCalculatorSenderOnCChain.sol:SimpleCalculatorSenderOnCChain --broadcast
[⠊] Compiling...
[⠒] Compiling 1 files with Solc 0.8.18
[⠢] Solc 0.8.18 finished in 84.20ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC // [$FUNDED_ADDRESS]
Deployed to: 0x789a5FDac2b37FCD290fb2924382297A6AE65860 // [$SENDER_ADDRESS]
Transaction hash: 0xd6cb47dd4ff38e4447711ebbec8b646b93f492f48e80b51719f860984cc25413
Save the Sender Contract Address
Overwrite the SENDER_ADDRESS
environment variable with the new address:
export SENDER_ADDRESS={your-sender-address}
Is this guide helpful?