Overview
TheTokenMessenger contract serves as the primary interface for users to burn tokens on the source chain and mint them on the destination chain. It integrates with both the MessageTransmitter for cross-chain messaging and the TokenMinter for token minting and burning operations.
State Variables
Local Message Transmitter responsible for sending and receiving messages to/from remote domains. This is an immutable reference set during contract deployment.
Version of message body format. This immutable value defines the structure of burn messages.
Minter responsible for minting and burning tokens on the local domain.
Mapping of domain to valid TokenMessenger addresses on remote domains.
Functions
depositForBurn
Deposits and burns tokens from sender to be minted on the destination domain.Amount of tokens to burn. Must be greater than 0.
Destination domain where tokens will be minted
Address of mint recipient on destination domain as bytes32. Must be nonzero.
Address of contract to burn deposited tokens on local domain
uint64 - Unique nonce reserved by the message
Behavior:
- Transfers tokens from sender to the TokenMinter contract
- Burns the tokens via the TokenMinter
- Formats a burn message with token and recipient details
- Sends the message via MessageTransmitter
- Emits a
DepositForBurnevent - Any address can call
receiveMessage()on the destination domain
- Amount is 0
- Mint recipient is bytes32(0)
- Given burnToken is not supported
- Given destinationDomain has no TokenMessenger registered
- transferFrom() fails (e.g., insufficient balance or allowance)
- burn() fails
- MessageTransmitter returns false or reverts
depositForBurnWithCaller
Deposits and burns tokens from sender to be minted on the destination domain. The mint on the destination domain must be called by the specifieddestinationCaller.
Amount of tokens to burn
Destination domain
Address of mint recipient on destination domain
Address of contract to burn deposited tokens on local domain
Caller on the destination domain, as bytes32. Must be nonzero.
uint64 - Unique nonce reserved by message
handleReceiveMessage
Handles an incoming message received by the local MessageTransmitter. For a burn message, mints the associated token to the requested recipient on the local domain.The domain where the message originated from
The sender of the message (remote TokenMessenger)
The message body bytes containing burn message details
bool - true if successful
Validation:
- Validates the local sender is the local MessageTransmitter
- Validates the remote sender is a registered remote TokenMessenger for
remoteDomain - Validates burn message format
- Validates message body version
- Extracts mint recipient, burn token, and amount from message body
- Calls the TokenMinter to mint tokens to the recipient
- Emits a
MintAndWithdrawevent
replaceDepositForBurn
Replaces a BurnMessage to change the mint recipient and/or destination caller. Allows the sender of a previous BurnMessage to send a new message to replace the original.Original message bytes to replace
Original attestation bytes. Must be a valid attestation of
originalMessage.The new destination caller, which may be the same as the original destination caller, a new destination caller, or an empty destination caller (bytes32(0), indicating that any destination caller is valid)
The new mint recipient, which may be the same as the original mint recipient or different. Must be nonzero.
- Validates message format and attestation
- Verifies msg.sender matches the sender of the original message
- Reuses the original message’s amount and burn token
- Does not require a new deposit
- Emits a new
DepositForBurnevent
The new message will reuse the original message’s nonce. For a given nonce, all replacement message(s) and the original message are valid to broadcast on the destination domain, until the first message at the nonce confirms, at which point all others are invalidated.
addRemoteTokenMessenger
Adds a TokenMessenger for a remote domain. Only callable by the contract owner.Domain of remote TokenMessenger
Address of remote TokenMessenger as bytes32. Must be nonzero.
- tokenMessenger is bytes32(0)
- A TokenMessenger is already set for the domain
removeRemoteTokenMessenger
Removes the TokenMessenger for a remote domain. Only callable by the contract owner.Domain of remote TokenMessenger to remove
- No TokenMessenger is set for the given remote domain
addLocalMinter
Adds a minter for the local domain. Only callable by the contract owner.The address of the minter on the local domain. Must be nonzero.
- newLocalMinter is address(0)
- A local minter is already set
removeLocalMinter
Removes the minter for the local domain. Only callable by the contract owner.- No local minter is set
Events
DepositForBurn
Emitted when a DepositForBurn message is sent.Unique nonce reserved by message
Address of token burnt on source domain
Deposit amount
Address where deposit is transferred from
Address receiving minted tokens on destination domain as bytes32
Destination domain
Address of TokenMessenger on destination domain as bytes32
Authorized caller as bytes32 of receiveMessage() on destination domain. If equal to bytes32(0), any address can call receiveMessage().
MintAndWithdraw
Emitted when tokens are minted on the destination domain.Recipient address of minted tokens
Amount of minted tokens
Contract address of minted token
RemoteTokenMessengerAdded
Emitted when a remote TokenMessenger is added.RemoteTokenMessengerRemoved
Emitted when a remote TokenMessenger is removed.LocalMinterAdded
Emitted when the local minter is added.LocalMinterRemoved
Emitted when the local minter is removed.Integration
The TokenMessenger integrates with two key contracts:MessageTransmitter Integration
- Uses
sendMessage()orsendMessageWithCaller()to send burn messages cross-chain - Uses
replaceMessage()to replace existing burn messages - Implements
IMessageHandlerinterface to receive messages viahandleReceiveMessage()
TokenMinter Integration
- Calls
burn()to burn tokens on the source chain - Calls
mint()to mint tokens on the destination chain - Manages the relationship with the local TokenMinter instance