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
YARN
1
npm i erc-20-abi
Copied!
1
yarn erc-20-abi
Copied!
Next, let's retrieve the the abi from the erc-20-abi package.
1
/**
2
* Note: Also make sure that your `mainnet` and `xDai`
3
* instances of the Etherspot SDK are available here.
4
*
5
* For the purposes of this demonstration, we're going
6
* to assume the following:
7
*
8
* The mainnet Etherspot SDK:
9
* - const mainnetEtherspotSdk
10
*
11
* The xDai Etherspot SDK:
12
* - const xdaiEtherspotsdk
13
*/
14
​
15
import { abi } from 'erc-20-abi';
Copied!
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.
1
// WARNING: The following contract addresses are only for mainnet.
2
const daiContractAddress = "0x6b175474e89094c44da98b954eedeac495271d0f";
3
const tokenBridgeContractAddress = "0x4aa42145Aa6Ebf72e164C9bBC74fbD3788045016";
Copied!
Next up, let's prepare our request to interact with the contract, and calculate the value we want to send.
1
/**
2
* WARNING! The minimum amount that can be transferred
3
* to the Token Bridge is 10 DAI. Please make sure you
4
* have enough DAI in your Etherspot address.
5
*/
6
const daiTransferAmount = ethers.utils.parseEther("10");
7
​
8
// Construct a new token interface that we can talk to...
9
const tokenAbiInterface = new ethers.utils.Interface(abi);
10
​
11
// Then create a "transfer" transaction request to the
12
// Token Bridge...
13
const transactionRequest = tokenAbiInterface.encodeFunctionData("transfer", [tokenBridgeContractAddress, daiTransferAmount]);
Copied!
Before we continue, let's clear the Etherspot SDK Transaction Batch queue. We're keeping the house clean
🧹
1
await mainnetEtherspotSdk.clearGatewayBatch();
Copied!
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
1
/**
2
* Step 1: Add the transaction (which instructs the DAI
3
* contract to perform a transfer to the Token Bridge
4
* contract address) to a clean "batch" of transactions.
5
*
6
* Note: You can batch many transactions together and
7
* submit them as one request for a more gas-efficient
8
* operation. Here, we're just adding 1 transaction to
9
* this batch.
10
*/
11
const batchResponse = await mainnetEtherspotSdk
12
.batchExecuteAccountTransaction({
13
to: daiContractAddress,
14
data: transactionRequest
15
})
16
.catch(console.error);
17
​
18
/**
19
* Step 2: Estimate the gas required to perform this
20
* operation. This is useful for presenting to users
21
* and allowing them to make a final decision.
22
*/
23
const estimateResponse = await mainnetEtherspotSdk
24
.estimateGatewayBatch()
25
.catch(console.error);
26
​
27
/**
28
* Step 3: Finally, send this batch to Etherspot for
29
* processing. We'll manage the transaction, queuing,
30
* retries and endevour to do whatever it takes to
31
* get this transaction on the chosen blockchain.
32
*/
33
const submissionResponse = await mainnetEtherspotSdk
34
.submitGatewayBatch()
35
.catch(console.error);
Copied!

​
πŸŽ‰
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 modified 3mo ago