To use Thanos SDK, first, you must install the package from NPM.
npm install @tokamak-network/thanos-sdk@latest
2. Set Environments
// .env example
PRIVATE_KEY=change_me
L1_RPC_URL=change_me
L2_RPC_URL=change_me
L1_TOKEN=change_me
L2_TOKEN=change_me
3. Create providers and wallets
const privateKey = process.env.PRIVATE_KEY;
const l1RpcProvider = new ethers.providers.JsonRpcProvider(process.env.L1_RPC_URL);
const l2RpcProvider = new ethers.providers.JsonRpcProvider(process.env.L2_RPC_URL)
const l1Wallet = new ethers.Wallet(privateKey, l1RpcProvider);
const l2Wallet = new ethers.Wallet(privateKey, l2RpcProvider);
4. Use Thanos SDK to interact between two networks
CrossChainMessenger in Thanos SDK is designed to facilitate cross-chain communication between Ethereum and the L2 built by Thanos Stack. It allows sending messages and executes transactions between L1 and L2, simplifying moving assets between them.
We already defined the known network on the CrossChainMessenger class. But if your network is custom, you can initialize the CrossChainMessenger instance with the custom opts.contracts
5. Set the L1 and L2 ERC-20 addresses
The l1token must be deployed on the L1 network. And the l2Token must be deployed on the L2 network.
const erc20ABI = [{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"faucet","outputs":[],"stateMutability":"nonpayable","type":"function"}]
const l1ERC20 = new ethers.Contract(l1Erc20Addr, erc20ABI, l1Wallet);
const l2ERC20 = new ethers.Contract(l2Erc20Addr, erc20ABI, l2Wallet);
7. Deposit tokens
You can use the L1 standard bridge contract to bridge the ERC-20 tokens from L1 to L2. You will receive the same amount in the L2 after successfully depositing it.
7.1. Approve the StandardBridge use of your tokens
const walletAddr = l1Wallet.address;
const l1Balance = await l1ERC20.balanceOf(walletAddr);
const l2Balance = await l2ERC20.balanceOf(walletAddr);
console.log(`Token on L1:${l1Balance} Token on L2:${l2Balance}`);
8. Withdraw tokens
To withdraw the tokens from L2 to L1, you can use the L2 standard bridge contract.
Predefine your withdrawal amount.
const withdrawAmount = 1e6;
8.1. Approve if your token is USDC
Because the USDC isn’t supported natively on the L2 network, so we follow the Circle standard to bring USDC to our L2 network.
Because of that, the USDC bridged token on the L2 network isn’t implemented by IOptimismMintableERC20 interfaces. So, you must approve the USDC bridged to our L2 standard bridge contract before executing the withdrawal transaction.
8.5. Wait for your withdrawal transaction until it is ready to finalize
The final step is to finalize your withdrawal transaction on L1. This can only happen after the fault-proof period has elapsed. On Thanos Sepolia, the period is 24 mins.