Skip to main content

Bridge Contract

The Bridge contract is a core component of the Compose Network that enables cross-chain token transfers between different rollups. It implements a burn-and-mint mechanism where tokens are burned on the source chain and minted on the destination chain.

Overview

The Bridge contract facilitates secure cross-chain token transfers by:

  • Burning tokens on the source chain
  • Sending cross-chain messages via the Mailbox
  • Minting tokens on the destination chain upon confirmation
  • Providing acknowledgment mechanisms for transfer completion

Contract Interface

contract Bridge is IBridge {
IMailbox public mailbox;

constructor(address _mailbox);
function send(...) external;
function receiveTokens(...) external returns (address token, uint256 amount);
function checkAck(...) external view returns (bytes memory);
}

Functions

send(otherChainId, token, sender, receiver, amount, sessionId, destBridge)

Description: Initiates a cross-chain token transfer by burning tokens on the source chain and sending a message to the destination chain.

ParameterTypeDescription
otherChainIduint256The chain ID of the destination chain
tokenaddressThe address of the token contract to be transferred
senderaddressThe address of the token sender on the source chain
receiveraddressThe address of the token recipient on the destination chain
amountuint256The amount of tokens to be bridged
sessionIduint256Unique identifier for this transfer session
destBridgeaddressThe address of the Bridge contract on the destination chain

Requirements:

  • sender must be the same as msg.sender (only the token owner can initiate the transfer)
  • The sender must have sufficient token balance
  • The token contract must support the burn function

Process:

  1. Validates that the caller is the token sender
  2. Burns the specified amount of tokens from the sender
  3. Encodes transfer data (sender, receiver, token, amount)
  4. Sends a cross-chain message via the Mailbox to the destination chain
  5. Emits a DataWritten event

Events:

  • DataWritten(bytes data)

receiveTokens(otherChainId, sender, receiver, sessionId, srcBridge)

Description: Processes incoming token transfers on the destination chain by reading the cross-chain message and minting tokens to the receiver.

ParameterTypeDescription
otherChainIduint256The chain ID of the source chain where tokens were burned
senderaddressThe original sender's address from the source chain
receiveraddressThe recipient's address on this chain
sessionIduint256The session ID that matches the original transfer
srcBridgeaddressThe address of the Bridge contract on the source chain

Returns:

  • token (address): The address of the token that was transferred
  • amount (uint256): The amount of tokens that were transferred

Requirements:

  • msg.sender must be the same as receiver (only the intended recipient can claim tokens)
  • A valid cross-chain message must exist in the Mailbox
  • The message data must match the provided parameters

Process:

  1. Validates that the caller is the intended receiver
  2. Reads the cross-chain message from the Mailbox
  3. Decodes the message to extract transfer details
  4. Validates that the decoded data matches the provided parameters
  5. Mints the tokens to the receiver
  6. Sends an acknowledgment message back to the source chain
  7. Emits a TokensReceived event

Events:

  • TokensReceived(address token, uint256 amount)

checkAck(chainDest, destBridge, sessionId)

Description: Checks if an acknowledgment message has been received from the destination chain, confirming that the token transfer was completed.

ParameterTypeDescription
chainDestuint256The chain ID of the destination chain
destBridgeaddressThe address of the Bridge contract on the destination chain
sessionIduint256The session ID of the transfer to check

Returns:

  • bytes memory: The acknowledgment message data, or empty bytes if no acknowledgment exists

Use Cases:

  • Verify that a cross-chain transfer was successfully completed
  • Implement UI feedback for transfer status
  • Handle failed transfers or retry mechanisms

Events

DataWritten

event DataWritten(bytes data)

Emitted when a cross-chain message is sent via the Mailbox.

TokensReceived

event TokensReceived(address token, uint256 amount)

Emitted when tokens are successfully minted to the receiver on the destination chain.