The L2 TBTC Gateway contract is a sophisticated cross-chain token bridge implementation built on the Sui blockchain. It provides a secure mechanism for token transfers and redemption across different blockchain networks using Wormhole's messaging infrastructure.
Stores the core state of the gateway, including:
- Processed VAA (Verified Action Approval) hashes
- Trusted emitters and receivers
- Minting limits and current minted amount
- Initialization and pause status
Manages critical capabilities:
- TBTC Minter Capability
- Wormhole Emitter Capability
- TBTC Treasury Capability
A capability that grants administrative privileges to manage the gateway.
Stores wrapped tokens for cross-chain transfers aquired from the bridge.
Initializes the gateway with all required capabilities.
Parameters:
_: &AdminCap: Admin capabilitystate: &mut GatewayState: Gateway state objectwormhole_state: &WormholeState: Wormhole stateminter_cap: TBTC::MinterCap: TBTC minter capabilitytreasury_cap: TreasuryCap<TBTC::TBTC>: TBTC treasury capability
Behavior:
- Verifies the gateway is not already initialized
- Creates an emitter capability
- Shares the capabilities object
- Initializes the token treasury
- Emits a
GatewayInitializedevent
Adds a trusted emitter from another blockchain.
Parameters:
_: &AdminCap: Admin capabilitystate: &mut GatewayState: Gateway stateemitter_id: u16: Chain ID of the emitteremitter: vector<u8>: External address of the emitter
Behavior:
- Adds the emitter to the trusted emitters table
- Emits an
EmitterRegisteredevent
Adds a trusted receiver on another blockchain.
Parameters:
_: &AdminCap: Admin capabilitystate: &mut GatewayState: Gateway statereceiver_id: u16: Chain ID of the receiverreceiver: vector<u8>: External address of the receiver
Behavior:
- Adds the receiver to the trusted receivers table
- Emits a
ReceiverRegisteredevent
Administrative methods to pause or unpause the gateway.
Parameters:
_: &AdminCap: Admin capabilitystate: &mut GatewayState: Gateway state
Behavior:
- Sets the gateway's pause status
- Emits
PausedorUnpausedevents
Updates the maximum amount of tokens that can be minted.
Parameters:
_: &AdminCap: Admin capabilitystate: &mut GatewayState: Gateway statenew_limit: u64: New minting limit
Behavior:
- Updates the minting limit
- Emits a
MintingLimitUpdatedevent
Transfers administrative capabilities to a new admin.
Parameters:
admin_cap: AdminCap: Current admin capabilitynew_admin: address: Address of the new admin
Behavior:
- Transfers admin capability to the new admin
- Emits an
AdminChangedevent
Redeems tokens from a Wormhole VAA (Verified Action Approval).
Parameters:
- Multiple state and capability objects
vaa_bytes: vector<u8>: Encoded VAAclock: &Clock: Current time- Other blockchain state objects
Behavior:
- Verifies the VAA is valid and from a trusted source
- Checks minting limits
- Mints or transfers wrapped tokens to the recipient
- Emits a
TokensRedeemedevent
Sends TBTC tokens to another blockchain via the token bridge.
Parameters:
- Multiple state and capability objects
recipient_chain: u16: Destination chain IDrecipient_address: vector<u8>: Recipient's addresscoins: Coin<TBTC::TBTC>: Tokens to sendnonce: u32: Unique transaction identifiermessage_fee: Coin<sui::sui::SUI>: Fee for message transmission
Behavior:
- Burns local tokens
- Prepares a cross-chain transfer
- Publishes a message via Wormhole
- Emits a
TokensSentevent
Sends wrapped tokens to another blockchain.
Parameters:
Similar to send_tokens, but works with wrapped token types.
Behavior:
- Prepares a cross-chain transfer of wrapped tokens
- Publishes a message via Wormhole
- Emits a
TokensSentevent
Several helper methods provide utility functions:
emitter_exists: Checks if an emitter is trustedget_emitter: Retrieves a trusted emitter's addressreceiver_exists: Checks if a receiver is trustedget_receiver: Retrieves a trusted receiver's addressis_initialized: Checks gateway initialization statusis_paused: Checks gateway pause statusget_minting_limit: Retrieves current minting limitget_minted_amount: Retrieves current minted token amount
The contract emits various events to track important actions:
EmitterRegisteredReceiverRegisteredPausedUnpausedMintingLimitUpdatedTokensRedeemedTokensSentAdminChanged
- Requires admin capabilities for sensitive operations
- Prevents replay attacks through VAA hash tracking
- Implements minting limits
- Supports pausing the entire gateway
- Relies on trusted emitters and receivers
Open the terminal and navigate to the project directory. Run the following command to publish the contract:
sui client publish ./ --gas-budget 1000000In order to get the contract going the deployer has to call the init method.
/// Admin function to initialize the gateway with all required capabilities
/// Requires AdminCap
/// state - Gateway state
/// wormhole_state - Wormhole state
/// minter_cap - TBTC minter capability
/// treasury_cap - TBTC treasury capability
/// ctx - Transaction context
/// Emits GatewayInitialized event
public entry fun initialize_gateway<CoinType>(
_: &AdminCap,
state: &mut GatewayState,
wormhole_state: &WormholeState,
minter_cap: TBTC::MinterCap,
treasury_cap: TreasuryCap<TBTC::TBTC>,
ctx: &mut TxContext,
) { ....}After the gateway is initialized, the deployer has to add the trusted emitters, receivers, change minting limit etc.
This contract is licensed under GPL-3.0-only.