Learn to receive messages with Avalanche Interchain Messaging.
To receive a message we need to enable out cross-Subnet dApps to being called by the Interchain Messaging contract.
The Interchain Messaging does not know out 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 the sendCrossChainMessage function of the Interchain Messagin contract
originSenderAddress: The address of the user or contract that called the sendCrossChainMessage function of the Interchain Messaging contract on the origin Subnet
message: 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.