Use Stargate to transfer an asset across blockchains.
Overview
Stargate V2 allows for same-asset bridging only, which means that USDC on Ethereum can only be swapped with USDC on some other chain.
When performing a swap you have two main options for balancing speed and gas costs:
Taking a taxi: Immediately performs a swap and sends an omnichain message to the destination chain.
Riding the bus: Allows the user to take advantage of cheaper gas costs thanks to transaction batching. When you use this approach your swap will immediately be settled on the local chain with instant guaranteed finality. However, you may need to wait before you receive the target asset on the destination chain. The message will be sent to destination chain when a "bus" reaches a set number of passengers (between 2-10). An impatient user can also choose to driveBus, by buying up the remaining bus tickets.
Instant guaranteed finality ensures that your swap will be executed, even when taking the bus.
OFT standard
As a reminder Stargate V2 interfaces are built upon the IOFT interface for OFTs on LayerZero V2. IOFT interface is available here. Documentation for building on IOFT is here.
This means that when you execute a swap on Stargate you are actually calling OFT.send(). Lets take a look at IStargate interface that extends OFT standard:
// SPDX-License-Identifier: BUSL-1.1pragmasolidity ^0.8.0;import { IOFT, SendParam, MessagingFee, MessagingReceipt, OFTReceipt } from"@layerzerolabs/lz-evm-oapp-v2/contracts/oft/interfaces/IOFT.sol";enumStargateType { Pool, OFT}structTicket {uint56 ticketId;bytes passenger;}/// @title Interface for Stargate./// @notice Defines an API for sending tokens to destination chains.interfaceIStargateisIOFT {/// @dev This function is same as `send` in OFT interface but returns the ticket data if in the bus ride mode,/// which allows the caller to ride and drive the bus in the same transaction.functionsendToken(SendParamcalldata_sendParam,MessagingFeecalldata_fee,address_refundAddress ) externalpayablereturns (MessagingReceiptmemory msgReceipt,OFTReceiptmemory oftReceipt,Ticketmemory ticket);/// @notice Returns the Stargate implementation type.functionstargateType() externalpurereturns (StargateType);}
As you can see above the Stargate interface isn't much different from an OFT. The most important piece of the interface is:
SendParam calldata _sendParam
Let's explain it:
/** * @dev Struct representing token parameters for the OFT send() operation. */structSendParam {uint32 dstEid; // Destination endpoint ID.bytes32 to; // Recipient address.uint256 amountLD; // Amount to send in local decimals.uint256 minAmountLD; // Minimum amount to send in local decimals.bytes extraOptions; // Additional options supplied by the caller to be used in the LayerZero message.bytes composeMsg; // The composed message for the send() operation.bytes oftCmd; // The OFT command to be executed, unused in default OFT implementations.}
The biggest Stargate specific difference is the use of last three properties of the struct above.
SendParam.extraOptions
extraOptions
If you use taxi mode then these options are LayerZero's execution options. You can use OptionsBuilder to prepare them. The exception to the above is that you don't need to put addExecutorLzReceiveOption() in them, because it is handled automatically by Stargate.
The function prepareRideBus contains logic to interact with Stargate and prepare arguments for token swap. You can send the swap transaction using code below:
The bus ride isn't instant. The swap is locally settled instantly, but you need to wait to receive tokens on the destination chain because bus transactions are batched together.
Bus Ticket
When you board a bus, you will receive a Ticket for your journey to the destination chain. This ticket can be acquired by capturing parameters from the sendToken() function.
If you want to check whether your bus has already left, compare your Ticket.ticketId with busQueues[dstEid].nextTicketId. If the nextTicketId is greater than your ticketId it means that your tokens were sent to the destination chain.