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. Setup the token contracts
First, you must import the @tokamak-network/core-utils package.
You can use the L1 standard bridge contract to bridge the ETH from L1 to L2. You will receive the same amount in the L2 after successfully depositing it.
6.1. Deposit tokens
Predefine your deposit amount.
const depositAmount = 1e6;
Execute your deposit transaction by using the bridgeETH function.
let l1ETHBalance = await l1Signer.getBalance()
let l2ETHBalance = await ethContractOnL2.balanceOf(l2Signer.address)
console.log(`ETH Token on L1:${l1ETHBalance} \\n
ETH Token on L2: ${l2ETHBalance}`);
7. Withdraw tokens
To withdraw ETH from L2 to L1, you can use the L2 standard bridge contract.
Predefine your withdrawal amount.
const withdrawAmount = 1e6;
7.1. Initialize your withdrawal transaction
To withdraw your tokens on L2, you can use the withdrawETH function.
let l1ETHBalance = await l1Signer.getBalance()
let l2ETHBalance = await ethContractOnL2.balanceOf(l2Signer.address)
console.log(`ETH Token on L1:${l1ETHBalance} \\n
ETH Token on L2: ${l2ETHBalance}`);