DAI - xDai Bridge

Transfer of DAI tokens to xDai native tokens using token bridge

Before we continue, please ensure that you have had a look at our Supported Ethereum Chains, followed the steps in Install Etherspot SDK and how to Bootstrap Etherspot SDK. We're assuming that you have completed these steps before going forward.

In this example, we're going to show you how to send DAI to xDai using their TokenBridge and Etherspot.

๐Ÿ›‘ Before we continue...

We're going to be using two Etherspot SDK instances here:

  • A mainnet version

โ†—๏ธ We will use this instance to send our DAI from and ETH to pay the gas fees.

  • A xDai version

โ†˜๏ธ We will use this instance to receive our xDai on the xDai chain.

โš ๏ธ Make sure you've checked out Supported Ethereum Chains before you continue as we also show you the code to instantiate mainnet and xDai versions of the SDK. Remember to use the same private key for both SDK instances to get the same Ethereum address on both mainnet and xDai.

This example use case is quite simple and straight forward, as the TokenBridge is a managed service in itself, however this example is often a requirement for many bridge services and serves as a building block.

We're using mainnet to send assets for this example. For other networks, please ensure that you are using the correct contract addresses for that network.

Please make sure your that your mainnet Etherspot address is funded with DAI tokens and enough ETH to pay the gas fees required.

โœ‰๏ธ Sending DAI to TokenBridge

First, let's install a prerequisite NPM package we're going to need. The erc-20-abi package will provide us with the ERC20 token interface to interact with.

npm i erc-20-abi

Next, let's retrieve the the abi from the erc-20-abi package.

* Note: Also make sure that your `mainnet` and `xDai`
* instances of the Etherspot SDK are available here.
* For the purposes of this demonstration, we're going
* to assume the following:
* The mainnet Etherspot SDK:
* - const mainnetEtherspotSdk
* The xDai Etherspot SDK:
* - const xdaiEtherspotsdk

import { abi } from 'erc-20-abi';

Next, let's define our essential variables. We need the DAI token contract address and the Token Bridge contract address. Remember, on different networks, the contract address is different! The contract addresses before are for mainnet.

// WARNING: The following contract addresses are only for mainnet.
const daiContractAddress = "0x6b175474e89094c44da98b954eedeac495271d0f";
const tokenBridgeContractAddress = "0x4aa42145Aa6Ebf72e164C9bBC74fbD3788045016";

Next up, let's prepare our request to interact with the contract, and calculate the value we want to send.

* WARNING! The minimum amount that can be transferred 
* to the Token Bridge is 10 DAI. Please make sure you
* have enough DAI in your Etherspot address.
const daiTransferAmount = ethers.utils.parseEther("10");

// Construct a new token interface that we can talk to...
const tokenAbiInterface = new ethers.utils.Interface(abi); 

// Then create a "transfer" transaction request to the
// Token Bridge...
const transactionRequest = tokenAbiInterface.encodeFunctionData("transfer", [tokenBridgeContractAddress, daiTransferAmount]);

Before we continue, let's clear the Etherspot SDK Transaction Batch queue. We're keeping the house clean ๐Ÿงน

await mainnetEtherspotSdk.clearGatewayBatch();

Finally, we're going to perform a series of steps to:

  1. Add the transaction to the batch

  2. Estimate the gas required to perform this transaction

  3. Send the batch to Etherspot to be processed

* Step 1: Add the transaction (which instructs the DAI
* contract to perform a transfer to the Token Bridge
* contract address) to a clean "batch" of transactions.
* Note: You can batch many transactions together and
* submit them as one request for a more gas-efficient
* operation. Here, we're just adding 1 transaction to
* this batch.
const batchResponse = await mainnetEtherspotSdk
    to: daiContractAddress,
    data: transactionRequest

* Step 2: Estimate the gas required to perform this
* operation. This is useful for presenting to users
* and allowing them to make a final decision.
const estimateResponse = await mainnetEtherspotSdk

* Step 3: Finally, send this batch to Etherspot for
* processing. We'll manage the transaction, queuing,
* retries and endevour to do whatever it takes to
* get this transaction on the chosen blockchain.
const submissionResponse = await mainnetEtherspotSdk

๐ŸŽ‰ Finished!

Once this process has completed, the TokenBridge service will send your xDai to the same address, but on the xDai chain.

Your xDai will arrive in the xDai version of your Etherspot address, which is accessible via the xDai version of your Etherspot SDK which was created earlier: xdaiEtherspotSdk. Did you miss that bit? Check out the "Before we continue" section: DAI - xDai Bridge.

๐Ÿš Here are some helpful links:

Last updated