Thanos development story — 3

Thanos SDK

Nam Pham
Tokamak Network
4 min readAug 22, 2024

--

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.

CrossChainMessenger class

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 Thanos
  • bridgeNativeToken : deposit the native token from Ethereum to Thanos via the L1Bridge contract
  • withdrawNativeToken : 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 Thanos
  • bridgeERC20 : deposit the ERC-20 token from Ethereum to Thanos via L1Bridge contract via theL1Bridge contract
  • withdrawERC20 : 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 contract
  • withdrawETH : 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 L1
  • finalizeMessage : 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 L2
  • estimateL1Gas : Estimates the amount of L1 gas required for a given L2 transaction
  • estimateL1GasCost : Estimates the amount of L1 gas cost for a given L2 transaction in Wei
  • estimateL2GasCost : Estimates the L2 gas cost for a given L2 transaction in Wei
  • estimateTotalGasCost : Estimates the total gas cost for a given L2 transaction in Wei
  • asL2Provider : 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 relayed
  • getMessageStatus : 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 receipt
  • calculateReplayedDepositTxID : calculate the relayed deposit transaction ID
  • getL2BlockNumberInOO : get the L2 block number in the Optimism Portal contract
  • calculateWithdrawalMessage : calculate the withdrawal message
  • waitForWithdrawalTxReadyForRelay : wait for the withdrawal transaction to be ready for relaying
  • getChallengePeriodSeconds : get the challenge period in seconds
  • getProvenWithdrawal : get the proven withdrawal
  • getFinalizedWithdrawalStatus : get the finalized withdrawal status
  • depositTransaction : deposit the L2 native token transaction via the Optimism Portal contract
  • initiateWithdrawal : initiate the withdrawal transaction from the Optimism Portal contract
  • proveWithdrawalTransaction : prove the withdrawal transaction
  • waitForFinalization : wait to finalize the withdrawal transaction
  • finalizeWithdrawalTransaction : finalize the withdrawal transaction

Using Thanos SDK

This is an example of depositing ETH from Ethereum to Thanos:

  1. We initiate a CrossChainMessenger instance based on these configurations
  2. Calling the brigeETH with the amount as the input param
  3. Calling wait to wait for this transaction to be successful
Deposit transaction example

Or to withdraw the ETH from Thanos to Ethereum:

  1. We initiate a CrossChainMessenger instance based on these configurations
  2. Calling withdrawETH function with the amount as the input on L2
  3. Waiting for this transaction to be ready to prove on L1
  4. Proving the transaction on L1
  5. Waiting for this transaction to be ready to finalize on L1
  6. Finalizing this transaction on L1

--

--