xDai - DAI Bridge
Transfer xDai on xDai to DAI on Mainnet using Etherspot SDK
Last updated
Transfer xDai on xDai to DAI on Mainnet using Etherspot SDK
Last updated
Before we continue, please ensure that you have had a look at our , followed the steps in and how to . We're assuming that you have completed these steps before going forward.
In this example, we're going to show you how to send xDai to Dai using their TokenBridge and Etherspot.
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 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 more complex than the previous guide in this series: , but completes the cycle of DAIxDai.
Please make sure your that your xDai
Etherspot address is funded with enough xDAI to pay the gas fees required.
The first step in our journey to change xDai back to DAI is to send our xDai, using the xDai version of the Etherspot SDK, to the xDai TokenBridge.
Let's define our required variables:
Next, let's specify how much xDai we would like to send.
Next, we're going to perform a series of steps to:
Add the transaction to the batch
Estimate the gas required to submit and execute this batch
Send the batch to Etherspot to be processed
Check to see when the transaction has been sent
Once gatewayBatchStatus.state
is equal to "Sent"
, indicating that the transactions in the batch are due to be confirmed, we'll lift the hash of the transaction into its own variable.
Next, we need to construct an instance of the xDai bridge contract from the ethers library. We'll communicate with the contract to fetch the required data to continue.
Next, we're going to perform three steps:
Query the contract for a corresponding message hash for xDai amount and the xDai Etherspot address
Use the message hash from the previous point to get a message payload from the contract
Check if the message payload is valid, and then get the required signature to continue.
Once the transaction above gets confirmed, pass the required parameters along with the hash of the transaction made earlier to the contract methods to get the signature from token bridge.
In this final section, we're going to initiate a move from the DAI contract address to our own Etherspot address on mainnet - it's final destination.
First, let's construct a contract interface to communicate with the DAI contract:
Now, we have completed all the required prerequisites to withdraw the DAI from TokenBridge. We're going to perform a series of steps to:
Add the transaction to the batch
Estimate the gas required to submit and execute this batch
Send the batch to Etherspot to be processed
Let's ensure that we have no batches of transactions waiting in the queue to be sent. We like to keep the house clean.
Let's ensure that we have no batches of transactions waiting in the queue to be sent. We like to keep the house clean.
gatewayBatchStatus.state | meaning |
"Sent" | The transactions that existed in the batch are due to be confirmed. |
"Sending" | The transactions that existed in the batch are currently pending broadcast on the blockchain. |
"Queued" | The transactions that existed in the batch are queued by Etherspot to be processed. |