Native Token Bridge
Cross chain transfer of native tokens between Ropsten and Sokol.
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 transfer native tokens from Ropsten Testnet to Sokol Testnet.
๐ Before we continue...
We're going to be using four Etherspot SDK instances here:
A
ropsten
Etherspot SDK for our user walletA
ropsten
Etherspot SDK for the Payment Hub on this network
โ๏ธ We will use this instance to send our DAI from and ETH to pay the gas fees.
A
sokol
Etherspot SDK for our user walletA
sokol
Etherspot SDK for the Payment Hub on this network
โ๏ธ 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 ropsten
and sokol
versions of the SDK. Remember to use the same private key or authentication for both SDK instances to get the same Ethereum address on both Ropsten Testnet and Sokol Testnet.
Getting started
We're going to use two of the four instances of the Etherspot SDK as "Payment Hubs".
Payment Hubs are nothing more than instances of the Etherspot SDK, but exist purely to hold funds for cross-chain transfer.
For this guide, we're going to transfer ETH from the Ropsten
network to the Sokol
network. To achieve this, we're going to send ETH from our user wallet to the Ropsten
Payment Hub (which itself is an instance of the Etherspot SDK), and the Sokol
Payment Hub (which, again, is another instance of the Etherspot SDK) will send the transferred amount back to the user's wallet on the Sokol
network.
We're going to assume that we're working with the following definitions:
Ensure Payment Hubs are funded
For this process to work, we need to make sure that the Payment Hubs have enough liquidity in them to facilitate the bridge transfer. We're going to be working with the p2pDepositAddress
from the Payment Hub Etherspot SDKs. We can get this address as follows:
The p2pDepositAddress
exists purely to provide liquidity for other operations. Your address
is unaffected.
We now need to add Test ETH from the Ropsten Faucet. Please follow the instructions on the Ropsten Faucet website to receive Test ETH to the p2pDepositAddress
.
Once you have received the Test ETH to the p2pDepositAddress
in the ropstenEtherspotPaymentHub
instance, we need to perform the same set of operations for the Sokol
Payment Hub Etherspot SDK.
We now need to add Test ETH from the Sokol Faucet. Please follow the instructions on the Sokol Faucet website to receive Test ETH to the p2pDepositAddress
.
Once both p2pDepositAddress
from Sokol
and Ropsten
are funded with Test ETH from their respective faucets, we're ready to move on.
Add liquidity to the Payment Hubs
Now that our Payment Hubs are funded with Test ETH to their respective p2pDepositAddress
, the next step is to update the Payment Hubs with the amount of liquidity we wish to provide from our respective p2pDepositAddress
.
Please ensure that the p2pDepositAddress
has received enough native tokens (in this case, ETH) before adding liquidity using the code above.
Once we've completed the liquidity addition operation, we're ready to move on.
Activate the Payment Hub Bridge
In order to allow transfer from one chain to another, we need to activate the Payment Hub Bridge using the destination Payment Hub SDK instance. Here's how to achieve this:
Once the above step is completed, the destination Sokol
Payment Hub bridged with the Ropsten
network name and you're ready to move on to the next step.
Exchanging with the Payment Hubs
From our Ropsten
user wallet, we first need to transfer the amount of ETH that we want to exchange to ETH on Sokol
to our Ropsten
user wallet's p2pDepositAddress
. We will do this in the normal way that we usually send Transactions.
Before we continue, let's clear the Ropsten
Etherspot SDK Transaction Batch queue. We're keeping the house clean ๐งน
We're going to perform a series of steps to:
Add the transaction to the batch
Estimate the gas required to perform this transaction
Send the batch to Etherspot to be processed
Make sure to transfer native tokens less than the available liquidity on the Payment Hubs.
Once the above batch transaction has been confirmed, we need to perform two steps:
Call the
updatePaymentHubDeposit
method on our usersRopsten
Etherspot SDK with the reference to theRopsten
Payment Hub, and the amount we wish to make available from ourp2pDepositAddress
to theRopsten
Payment HubCall the
transferPaymentHubDeposit
method on our usersRopsten
Etherspot SDK, which will instruct the Payment Hub to move the funds from one Payment Hub to the destination Payment Hub.
Once the above has been completed, the internal ledger operations of the Payment Hub mechanisms will move the available liquidity from the source Payment Hub on Ropsten to the destination Payment Hub on Sokol
. We're now ready to withdraw the funds on the destination Payment Hub on Sokol
.
Withdraw from the destination Payment Hub
To be able to withdraw the above exchangeAmount
from the destination Sokol Payment Hub, we need to perform a few final steps.
We're now working primarily with the Sokol
Payment Hub Etherspot SDK and the Sokol
User Etherspot SDK.
First, we need to instruct the Sokol User Etherspot SDK instance that we are going to make a withdrawal by calling the updatePaymentHubDeposit
method on the Sokol
Etherspot User's SDK instance.
Internally a new hash has been created, which we will later find and sign to allow the withdrawal to take place. This process allows Etherspot to make the necessary liquidity checks before allowing the withdrawal to take place.
The next step is to find the transaction hash which needs to be signed from the Sokol
Payment Hub. We'll first retrieve a list of uncommitted Payment Hub transaction items.
We then need to find the transaction hash to be signed. For the purposes of this guide, we're going to assume that there is just one uncommitted Payment Channel returned from the getP2PPaymentChannels
method call.
Once we have this information, we're going to perform a basic validation check to check two things:
That the
paymentHubChannel.state
is"Opened"
That the
sokolEtherspotUser.state.accountAddress
and thepaymentHubChannel.recipient
are the same
Providing that the two above validation points are true, we can sign the Payment Channel hash and commit the Payment Channel.
Committing the Payment Channel with with the signed hash from the previous code example moves the exchangeAmount
to the final destination address.
Once you have finished making one or more transactions against the hubs, we are going to commit the Payment Channels that was created by the sender Payment Hub. This is the last step to be completed with the Payment Hub transactions as there could be many transfers between the Etherspot User SDK and the Payment Hub. The next step will total-up the amount transferred so that just a single, minimal gas fee is paid.
To commit the Payment Channel, we need to get the hash generated for the Payment Channel that was previously created. This can be obtained by calling getP2PPaymentChannels
on the Etherspot SDK.
Firstly, retrieve a list of uncommitted Payment Hub transaction items.
For the purposes of this guide, we're going to assume that there is just one uncommitted Payment Channel returned from the getP2PPaymentChannels
method call.
Once we have this information, we're going to perform a basic validation check to check two things:
That the
paymentHubChannel.state
is"Signed"
That the
ropstenEtherspotUser.state.accountAddress
and thepaymentHubChannel.recipient
are the same
Providing that the two above validation points are true, we can commit the Payment Channel to receive the total amount of funds transferred from your Etherspot SDK p2pDepositAddress
to sender's Payment Hub p2pDepositAddress
.
๐ Finished!
Last updated