logoAcademy

Sender Contract

Adapt the sender contract to also receive messages

The sender contract has now two tasks:

  • Send a message to the receiver: Same as in the last example
  • Receive a message back from the receiver: Now our sender contract needs to be able to receive a message back from the receiver.

Therefore, we need to change the sender contract to be able to receive a message back. We will need to implement the same interface ITeleporterReceiver as the receiver contract and implement the receiveTeleporterMessage function.

src/1-send-roundtrip/senderOnCChain.sol
// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
 
// SPDX-License-Identifier: Ecosystem
 
pragma solidity ^0.8.18;
 
import "@teleporter/ITeleporterMessenger.sol";
import "@teleporter/ITeleporterReceiver.sol"; 
 
contract SenderOnCChain is ITeleporterReceiver { 
    ITeleporterMessenger public immutable messenger = ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);
 
    string public rountripMessage;
 
    /**
     * @dev Sends a message to another chain.
     */
    function sendMessage(address destinationAddress) external {
        messenger.sendCrossChainMessage(
            TeleporterMessageInput({
                // Replace with blockchainID of your Avalanche L1 (see instructions in Readme)
                destinationBlockchainID: 0xb72b346fcc8c1ebb30087e2d2841eac9302dde8fc5969dcc84fad6db5ebd261d,
                destinationAddress: destinationAddress,
                feeInfo: TeleporterFeeInfo({feeTokenAddress: address(0), amount: 0}),
                requiredGasLimit: 100000,
                allowedRelayerAddresses: new address[](0),
                message: abi.encode("Hello")
            })
        );
    }
 
    function receiveTeleporterMessage(bytes32, address, bytes calldata message) external { 
        // Only the Interchain Messaging receiver can deliver a message.
        require(msg.sender == address(messenger), "SenderOnCChain: unauthorized TeleporterMessenger");
 
        // Store the message.
        rountripMessage = abi.decode(message, (string));
    }
}

On this page

No Headings