Receiving a Message
Learn to receive messages with Avalanche Interchain Messaging.
To receive a message we need to enable our cross-L1 dApps to being called by the Interchain Messaging contract.
The Interchain Messaging does not know our contract and what functions it has. Therefore, our dApp on the destination Subnet has to implement the ITeleporterReceiver interface. It is very straight forward and only requires a single method for receiving the message that then can be called by the Interchain Messaging contract:
The function receiveTeleporterMessage has three parameters:
originChainID
: The chainID where the message originates from, meaning where the user or contract called thesendCrossChainMessage
function of the Interchain Messaging contractoriginSenderAddress
: The address of the user or contract that called thesendCrossChainMessage
function of the Interchain Messaging contract on the origin Subnetmessage
: The message encoded in bytes
An example for a contract being able to receive Interchain Messaging messages and storing these in a mapping could look like this:
This contract stores the last Message
and it's sender of each chain it has received. When it is instantiated, the address of the Interchain Messaging contract is supplied to the constructor. The contract implements the ITelepoterReceiver
interface and therefore we also implement the receiveTeleporterMessage
function.