logoAcademy

Receiver Contract

Create a contract to receive messages with Teleporter.

Now it's time to deploy our receiver contract on our Subnet. It will implement the callback for the TeleporterMessenger contract when the message is received, decoding our message and storing the last received string.

Read the Receiver Contract

The following contract is located inside src/0-send-receive directory. Read through the contract below and and understand what is happening:

src/0-send-receive/receiverOnSubnet.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 ReceiverOnSubnet is ITeleporterReceiver {
    ITeleporterMessenger public immutable messenger = ITeleporterMessenger(0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf);
 
    string public lastMessage;
 
    function receiveTeleporterMessage(bytes32, address, bytes calldata message) external {
        // Only the Interchain Messaging receiver can deliver a message.
        require(msg.sender == address(messenger), "ReceiverOnSubnet: unauthorized TeleporterMessenger");
 
        // Store the message.
        lastMessage = abi.decode(message, (string));
    }
}```
 
The key things to understand:
 
- **The key things to understand:**
- **Importing Interchain Messaging contracts (L8, L9):** We are importing the ITeleporterMessenger and ITeleporterReceiver interface we looked at in the previous activity.
- **Inheriting from ITeleporterReceiver (L11):** We are inheriting the interface that will require us to implement the receiveTeleporterMessage() function.
- **Defining the lastMessage variable (L14):** Setting the lastMessage variable as public, will make that variable readable from the outside without the need of a getValue function.
- **Implementing receiveTeleporterMessage (L16):** We implement the function that will be called when the message is received. Please note that we are not checking who calls that function for now. So anyone can pretend to deliver messages for now.
- **Decode message (L21):** We decode the message using abi.decode(message, (string)), which takes the bytes array as the first input and a tupel of the types of the encoded data in the message. Since our message only contains a single value, the tupel only has one value.
 
## Deploy Receiver Contract
 
To deploy a contract using Foundry use the following command:
 
```bash
forge create --rpc-url myblockchain --private-key $PK src/0-send-receive/receiverOnSubnet.sol:ReceiverOnSubnet

Then save the receiver contract address in an environment variable:

export RECEIVER_ADDRESS="0x123..."
Updated:

On this page

Edit on Github