Bridging L1 and L2 (Cross Domain Messenger)
Titan offers Cross Domain Messenger functionality through the Titan SDK, enabling seamless asset transfers between L1 and L2. Transferring assets from L1 to L2 is referred to as a deposit, while moving assets from L2 back to L1 is known as withdraw.
This article will provide a Javascript code example demonstrating how to deposit or withdraw ETH using the Titan SDK. Titan SDK makes it easy for service developers to implement bridging functions to move assets between L1 and L2 without calling contracts directly.
You can find executable example code here.
Setup
Titan SDK can be imported to use the supported methods/types in the library. The
ethers
library is imported to use the functionality required to interact with the Ethereum blockchain.
Using the
ethers
library, you can create a provider and wallet instance to generate signatures and send transactions on the L1 and L2 networks.l1RpcProvider
:ethers.providers.JsonRpcProvider
instance for L1l2RpcProvider
:ethers.providers.JsonRpcProvider
instance for L2l1Wallet
: Create a wallet for the L1 network usingethers.Wallet
l2Wallet
: Create a wallet for the L2 network usingethers.Wallet
You create a
BatchCrossChainMessenger
instance of the Titan SDK. The parameters for creation are as follows. The created instance is used to transfer ETH.l1ChainId: L1 network chain ID
l2ChainId: L2 network chain ID
l1SignerOrProvider: L1 network wallet
l2SignerOrProvider: L2 network wallet
The
l1ChainId
andl2ChainId
should be the Chain ID of the network supported by the Titan SDK. For example, to make deposits and withdrawals on the Titan Mainnet,l1ChainId
should be 1, the chain ID of the Ethereum Mainnet, andl2ChainId
should be 55004, the chain ID of the Titan Mainnet. (See 'Connection information' section.)
Deposit
It is very simple to request deposit transaction to move ETH from L1 to L2. You can bridge the desired amount of ETH by calling
depositETH()
on thecrossChainMessenger
instance you created during the setup phase. (L1 account must have at least the amount of ETH you are depositing + the appropriate gas fee to send the transaction).After sending the deposit transaction, execute
wait()
. You have to wait for the transaction to be included in the block and for the transaction to be received.
The deposit process internally calls functions of the contracts deployed on L1 and L2. The unit of transfer that a contract passes to another contract is called a
message
.waitForMessageStatus()
is a method that waits until themessage
, which is the first parameter, is changed to the message state defined in the second parameter. When the deposit is completed and the ETH is moved from L1 to L2, the status of message changes toRELAYED
.You should check if the deposit has been completed successfully by checking if
response
has changed toRELAYED
.
Withdraw
Titan SDK makes it easy to move ETH from L2 to L1. You can call
withdrawETH()
to bridge as much ETH as you want. (L2 account must have at least the amount of ETH you are withdrawing + the appropriate gas fee to send the transaction).
When you request a withdraw transaction from L2, the generated L2 transactions are rolled-up by the sequencer and proposer, which roll-up the transaction and State Root from L2 to L1. You wait for the message's status to change to
READY_FOR_RELAY
by callingwaifForMessageStatus()
.When the State Root is successfully rolled up to L1 by the proposer, the message's status will change to
READY_FOR_RELAY
. It will take 7 days for the status to change toREADY_FOR_RELAY
because Optimistic Rollup on the mainnet defines 7 days as the validation period for fraud proof.
You can call
finalizeBatchMessage()
to request a relay transaction from L2 to L1. The method requires an array of messages as a parameter. ThefinalizeBatchMessage()
will put the messages stored in the array into one transaction and relay them all simultaneously.Once the withdraw is successfully completed and the desired amount of ETH has been moved from L2 to L1, the message's status will change to
RELAYED
.
Last updated