Etherspot
These are our V1 docs and will be deprecated soon. Please visit https://etherspot.fyi/introduction to use our new version of the SDK.
  • Welcome to Etherspot
    • Chains, Bridges & DEXes
    • Social Logins
    • Web3 Logins
  • Transaction Kit
    • Introduction
    • Code Sandboxes
    • Quick Start
    • React Hooks
      • useEtherspotAssets()
      • useEtherspotNfts()
      • useEtherspotHistory()
        • getAccountTransactions()
        • getAccountTransaction()
      • useEtherspotTransactions()
        • estimate()
        • send()
      • useEtherspotAddresses()
      • useEtherspotBalances()
    • React Components
      • <EtherspotTransactionKit />
      • <EtherspotBatches />
      • <EtherspotBatch />
      • <EtherspotTransaction />
      • <EtherspotContractTransaction />
      • <EtherspotApprovalTransaction />
      • <EtherspotTokenTransferTransaction />
  • BUIDLER React Component
    • Introduction
    • Installation
    • Integrate React Component
    • Shared Sessions
    • Wallet Connectors
    • Blocks
      • Send
      • Batching Transaction
      • Multicall Transaction
      • Swaps
      • Bridges
      • Custom Contract Interactions
      • Styling
    • Build Your Own Block
      • Cross-chain KLIMA DAO Staking
  • Etherspot SDK Guides
    • Requirements
    • Install Etherspot SDK
    • Bootstrap Etherspot SDK
      • Instantiate Etherspot SDK
    • Events
    • Etherspot Block Explorer
    • Etherspot Playground
    • Social Login using Etherspot SDK
    • Sponsored Transactions
  • Use Cases & Guides
    • Crosschain Streaming
    • Token Swaps
    • Transactions
      • Historical
      • Sending
    • Multi-chain Bridges
      • ERC20 Bridge
      • DAI - xDai Bridge
      • xDai - DAI Bridge
      • Native Token Bridge
    • Custom Contract Interaction
    • Multi-chain Assets
    • Multi-chain Gas Prices
    • Peer-to-Peer Payments
  • Reference
    • Etherspot SDK API Docs
    • Etherspot SDK on Github
    • Etherspot on NPM
    • Etherspot Playground
    • Etherspot Block Explorer
    • Etherspot Architecture
    • EIP-1271
    • Etherspot/Pillar Audit
  • Brand Assets
    • Etherspot Brand Assets
  • Security
    • Security
  • Get in touch
    • โš’๏ธDiscord
    • Twitter
    • Telegram
Powered by GitBook
On this page
  • Before we continue...
  • Sending DAI to TokenBridge
  • Finished!

Was this helpful?

  1. Use Cases & Guides
  2. Multi-chain Bridges

DAI - xDai Bridge

Transfer of DAI tokens to xDai native tokens using token bridge

PreviousERC20 BridgeNextxDai - DAI Bridge

Last updated 3 years ago

Was this helpful?

Before we continue, please ensure that you have had a look at our Supported Ethereum Chains, 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 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
yarn 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]);
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
  .batchExecuteAccountTransaction({
    to: daiContractAddress,
    data: transactionRequest
  })
  .catch(console.error);

/**
* 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
  .estimateGatewayBatch()
  .catch(console.error);

/**
* 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
  .submitGatewayBatch()
  .catch(console.error);

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

  • Check out the Supported Ethereum Chains

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

Finished!

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: .

Here are some helpful links:

Prototype your ideas now with the

๐Ÿงน
๐ŸŽ‰
๐Ÿš
๐Ÿ›‘
โ†—๏ธ
โ†˜๏ธ
โš ๏ธ
โœ‰๏ธ
Install Etherspot SDK
Bootstrap Etherspot SDK
Etherspot Playground
DAI - xDai Bridge