Thanos development story — 3
Thanos SDK
The TOP project team planned to launch the Thanos mainnet in December and unveiled the new test network Thanos-Sepolia to the world on July 1st. The Thanos network is an L2 that focuses on scaling the Ethereum mainnet based on the optimistic rollup and uses the ERC-20 as the native token.
To interact with the Thanos network, we should use the SDK written by Typescript, which we published on NPM.
First of all, let’s talk about the Thanos’s SDK features:
- Deposit and withdraw ERC-20 tokens
- Deposit and withdraw ETH
- Deposit and withdraw the native token(TON)
- Prove and finalize the withdrawal transactions
- Estimate the L1/L2 gas fees
CrossChainMessenger
The CrossChainMessenger class helps us move the assets between the Ethereum mainnet and the Thanos network.
For example: deposit ETH from the Ethereum mainnet to the Thanos, or initiate a withdrawal transaction back from Thanos to Ethereum, keeping track of the transaction status to be finalized on the Ethereum and finalizing the transaction after the challenge time elapsed.
Deposit and Withdraw
In the CrossChainMessenger class, we support depositing and withdrawing the native token:
approveNativeToken
: approve the native token on the L1 with the L1Bridge contract before depositing from Ethereum to ThanosbridgeNativeToken
: deposit the native token from Ethereum to Thanos via the L1Bridge contractwithdrawNativeToken
: withdraw the native token back from Thanos to Ethereum via the L2Bridge contract
And, the CrossChainMessenger supports depositing and withdrawing pre-defined ERC-20 tokens:
approveERC20
: approve the ERC-20 token with the L1Bridge before depositing from Ethereum to ThanosbridgeERC20
: deposit the ERC-20 token from Ethereum to Thanos via L1Bridge contract via theL1Bridge contractwithdrawERC20
: withdraw the ERC-20 token back from Thanos to Ethereum via the L2Bridge contract
Also, the CrossChainMessenger supports depositing and withdrawing ETH:
bridgeETH
: deposit ETH from Ethereum to Thanos via the L1Bridge contractwithdrawETH
: withdraw ETH back from Thanos to Ethereum via the L2Bridge contract
Prove and Finalize the withdrawal transactions
To support proving and finalizing the withdrawal transactions with the OptimismPortal contract, we have the following:
proveMessage
: Proves a cross-chain message that was sent from L2 to L1. Only applicable for L2 to L1finalizeMessage
: Finalizes a cross-chain message that was sent from L2 to L1. Only applicable for L2 to L1
L2Provider and related utilities
Thanos SDK has some utilities for handling the Thanos gas model. We have the following:
getL1GasPrice
: Gets the current L1 gas price as seen on L2estimateL1Gas
: Estimates the amount of L1 gas required for a given L2 transactionestimateL1GasCost
: Estimates the amount of L1 gas cost for a given L2 transaction in WeiestimateL2GasCost
: Estimates the L2 gas cost for a given L2 transaction in WeiestimateTotalGasCost
: Estimates the total gas cost for a given L2 transaction in WeiasL2Provider
: Returns a provider wrapped as a Thanos L2 provider. Adds a few extra helper functions to simplify estimating the gas usage for a transaction on Optimism. Returns a COPY of the original provider.
Portal
To interact with the OptimismPortal contract, we can initiate an OptimismPortal instance
We support the following functions:
waitingDepositTransactionRelayed
: wait for the deposit transaction from Ethereum to Thanos to be relayedgetMessageStatus
: get the message status(from L1 to L2 by receipt or from L2 to L1 by receipt)waitingDepositTransactionRelayedUsingL1Tx
: wait for the deposit transaction from Ethereum to Thanos to be relayed by the L1 transaction receiptcalculateReplayedDepositTxID
: calculate the relayed deposit transaction IDgetL2BlockNumberInOO
: get the L2 block number in the Optimism Portal contractcalculateWithdrawalMessage
: calculate the withdrawal messagewaitForWithdrawalTxReadyForRelay
: wait for the withdrawal transaction to be ready for relayinggetChallengePeriodSeconds
: get the challenge period in secondsgetProvenWithdrawal
: get the proven withdrawalgetFinalizedWithdrawalStatus
: get the finalized withdrawal statusdepositTransaction
: deposit the L2 native token transaction via the Optimism Portal contractinitiateWithdrawal
: initiate the withdrawal transaction from the Optimism Portal contractproveWithdrawalTransaction
: prove the withdrawal transactionwaitForFinalization
: wait to finalize the withdrawal transactionfinalizeWithdrawalTransaction
: finalize the withdrawal transaction
Using Thanos SDK
This is an example of depositing ETH from Ethereum to Thanos:
- We initiate a CrossChainMessenger instance based on these configurations
- Calling the
brigeETH
with the amount as the input param - Calling
wait
to wait for this transaction to be successful
Or to withdraw the ETH from Thanos to Ethereum:
- We initiate a CrossChainMessenger instance based on these configurations
- Calling
withdrawETH
function with theamount
as the input on L2 - Waiting for this transaction to be ready to prove on L1
- Proving the transaction on L1
- Waiting for this transaction to be ready to finalize on L1
- Finalizing this transaction on L1