How to bridge assets between Ethereum and DBK Chain use Optimism SDK

How to bridge assets between Ethereum and DBK Chain use Optimism SDK

This tutorial explains how you can use the Optimism SDK to bridge ETH from L1 (Ethereum) to L2 (DBK Chain). The Optimism SDK is an easy way to add bridging functionality to your JavaScript-based application. It also provides some safety rails to prevent common mistakes that could cause ETH or ERC-20 tokens to be made inaccessible.

Dependencies

Create a Demo Project

You're going to use the Optimism SDK for this tutorial. Since the Optimism SDK is a Node.js library, you'll need to create a Node.js project to use it.

Make a Project Folder

mkdir op-sample-project
cd op-sample-project

Initialize the Project

pnpm init -y

Install the Optimism SDK

pnpm add @eth-optimism/sdk

Install ethers.js

pnpm add ethers@^5

Add a Private Key to Your Environment

You need a private key in order to sign transactions. Set your private key as an environment variable with the export command. Make sure this private key corresponds to an address that has ETH.

export TUTORIAL_PRIVATE_KEY=0x...

Import Dependencies

Create a new file called index.js and import the necessary dependencies.

const optimism = require("@eth-optimism/sdk")
const ethers = require("ethers")

Initialize the SDK

Initialize the SDK with the rpc of the ETH and DBK Chain.

privateKey = process.env.TUTORIAL_PRIVATE_KEY
const l1Provider = new ethers.providers.StaticJsonRpcProvider("your_eth_rpc")
const l2Provider = new ethers.providers.StaticJsonRpcProvider("https://rpc.mainnet.dbkchain.io/")
const l1Wallet = new ethers.Wallet(privateKey, l1Provider)
const l2Wallet = new ethers.Wallet(privateKey, l2Provider)

Deposit ETH

Now that you have some ETH on L1 you can deposit that ETH into the L1StandardBridge contract. You'll then receive the same number of ETH on L2 in return.

Check your wallet balance on L1

See how much ETH you have on L1 so you can confirm that the deposit worked later on.

console.log((await l1Wallet.getBalance()).toString())

Create a CrossChainMessenger instance

The Optimism SDK exports a CrossChainMessenger class that makes it easy to interact with the L1StandardBridge contract.

Create an instance of the CrossChainMessenger class:

const AddressManager = '0x306402f889035e2Cbd7e396080bf365ADB38B7DC'
const L1CrossDomainMessenger = '0xDEfab7699Ed60a863dce4B1095576F6d9EC5d254'
const L1StandardBridge = '0x28f1b9F457CB51E0af56dff1d11CD6CEdFfD1977'
const OptimismPortal = '0x63CA00232F471bE2A3Bf3C4e95Bc1d2B3EA5DB92'
const L2OutputOracle = '0x0341bb689CB8a4c16c61307F4BdA254E1bFD525e'

const messenger = new optimism.CrossChainMessenger({
  l1ChainId: 1,
  l2ChainId: 20240603,
  l1SignerOrProvider: l1Wallet,
  l2SignerOrProvider: l2Wallet,
  contracts: {
    l1: {
      AddressManager: AddressManager,
      L1CrossDomainMessenger: L1CrossDomainMessenger,
      L1StandardBridge: L1StandardBridge,
      OptimismPortal: OptimismPortal,
      L2OutputOracle: L2OutputOracle,

      // Need to be set to zero for this version of the SDK.
      StateCommitmentChain: ethers.constants.AddressZero,
      CanonicalTransactionChain: ethers.constants.AddressZero,
      BondManager: ethers.constants.AddressZero,
    },
    l2: optimism.DEFAULT_L2_CONTRACT_ADDRESSES,
  }
})

Deposit your ETH

Now you can deposit your ETH into the Standard Bridge contract. You'll deposit a small amount of ETH just to demonstrate the process.

tx = await messenger.depositETH(ethers.utils.parseEther('0.0042'))
await tx.wait()

Wait for the deposit to be relayed

You can use the waitForMessageStatus function to wait for the deposit to be relayed to L2.

Check your wallet balance on L1

You should now have less ETH on L1.

console.log((await l1Wallet.getBalance()).toString())

Check your wallet balance on L2

You should now have more ETH on L2.

console.log((await l2Wallet.getBalance()).toString())

Withdraw ETH

You just bridged some ETH from L1 to L2. Nice! Now you're going to repeat the process in reverse to bridge some ETH from L2 to L1.

Start your withdrawal on L2

The first step to withdrawing ETH from L2 to L1 is to start the withdrawal on L2.

const withdrawal = await messenger.withdrawETH(ethers.utils.parseEther('0.004'))
await withdrawal.wait()

Check your wallet balance on L2

You should now have less ETH on L2, but your wallet balance on L1 will not have changed yet.

console.log((await l2Wallet.getBalance()).toString())

Wait until the withdrawal is ready to prove

The second step to withdrawing ETH from L2 to L1 is to prove to the bridge on L1 that the withdrawal happened on L2. You first need to wait until the withdrawal is ready to prove.

await messenger.waitForMessageStatus(withdrawal.hash, optimism.MessageStatus.READY_TO_PROVE)

This step can take one hour. Feel free to take a quick break while you wait.

Prove the withdrawal on L1

Once the withdrawal is ready to be proven, you'll send an L1 transaction to prove that the withdrawal happened on L2.

await messenger.proveMessage(withdrawal.hash)

Wait until the withdrawal is ready for relay

The final step to withdrawing ETH from L2 to L1 is to relay the withdrawal on L1. This can only happen after the fault proof period has elapsed. On OP Mainnet, this takes 7 days.

await messenger.waitForMessageStatus(withdrawal.hash, optimism.MessageStatus.READY_FOR_RELAY)

Relay the withdrawal on L1

Once the withdrawal is ready to be relayed you can finally complete the withdrawal process.

await messenger.finalizeMessage(withdrawal.hash)

Wait until the withdrawal is relayed

Now you simply wait until the message is relayed.

await messenger.waitForMessageStatus(withdrawal.hash, optimism.MessageStatus.RELAYED)

Check your wallet balance on L1

You should now have more ETH on L1.

console.log((await l1Wallet.getBalance()).toString())

Last updated