Move tokens between Binance,zeknd and Ethereum
Purpose
This is the third guide in our Binance series. First, we walked you through the process of transferring BNB between Binance and zeknd.
However, as a developer, you can also launch your BEP2 token on Binance. Thus, our second guide showed how to build a simple DApp that transfers BEP2 tokens between Binance and zeknd.
Now, we are going to explain how to transfer your tokens between Binance, zeknd, and Ethereum.
#Prerequisites
We assume you've covered the first two parts of our Binance series. If so, let's make sure your local copy of the zeknd Examples repo is updated. You must cd into the zeknd-examples directory and run:
git pullNext, let's make sure your dependencies are up-to-date:
npm install#Settings Things Up
If you've followed along with our previous Binance tutorials, a BEP2 token and a corresponding smart contract on the zeknd side (that is Extdev) should have been deployed and mapped.
Now, to move tokens to Ethereum, we are required to deploy a new contract called SampleERC20MintableToken to the Ethereum test net (for the scope of this example we're using the Rinkeby network).
Follow these steps to deploy the smart contract to Rinkeby:
Generate an Ethereum private key:
# this will create 3 new files in the root directory of the project: rinkeby_account, rinkeby_mnemonic, and rinkeby_private_key npm run gen:rinkeby-keyGet the address of the new
Rinkebyaccount from therinkeby_accountfile:cat rinkeby_accountGive the
Rinkebyaccount some ETH so it can be used to deploy contracts toRinkeby. You can either use the Rinkeby authenticated faucet or transfer some ETH from another account.Set your Infura API key (get it from Infura website):
export INFURA_API_KEY=XXXXXXXXXXXXXXXXDeploy the contract by running:
npm run migrate:rinkeby-bep2-tokenLike we did in our previous tutorial with the BEP2 token and its corresponding smart contract on zeknd, we must now map our zeknd contract with the newly deployed contract on Rinkeby. You can create the mapping with:
npm run map:bep2-contracts
#Spinning Up the Demo
Once you completed the previous steps, you are ready to see the demo in action. Run the following command:
Next, just point your browser at localhost:8080 and select the "Binance - zeknd - Ethereum" demo.
#Trying Out the Demo
The first thing you need to do is to move some BNB tokens from Binance to Extdev:
Then, let's transfer some BEP2 tokens from Binance to Extdev:
Now, we can send our tokens from Extdev to Rinkeby:
...and from Rinkeby to Extdev:
Lastly, let's move tokens from Extdev to Binance:
#Let's See What Makes it Tick
At its core, this demo is built around a couple of simple components:
a BEP2 token. See the settings things up page from our previous tutorial for more details.
a corresponding ERC20 token deployed to Extdev Testnet. We've walked you through the source code in this section.
an ERC20 token called
SampleERC20MintableTokenwhich we just deployed to the Rinkeby network. This smart contract is nothing special. It just starts by inheriting fromERC20/ERC20Mintable.soland./IERC20GatewayMintable.sol:
While we're here, let's take a glance at the ./IERC20GatewayMintable.sol smart contract:
As you can see, it's an abstract contract (cannot be directly compiled because the mintTo function lacks the implementation). We're just using it as a base contract.
Now let's get back to the SampleERC20MintableToken smart contract. There are a few things to note:
The constructor takes the address of the transfer gateway as a parameter and saves it to a mapping:
We implement a function called
mintTothat allows the transfer gateway to mint tokens:
Note that we put a restriction on who can call this method- only the transfer gateway can call it. Here's how the modifier looks like:
You can check the full source code of this smart contract on GitHub.
Lastly, to tie everything together, we created two mappings. The first one links the Binance and zeknd] token contracts and the second one links the zeknd and Ethereum token contracts.
#The Front-End
In this section, we'll take a quick look at what happens under the hood of our front-end.
We split the code roughly into two files.
#The User Interface
The /src/binance-zeknd-ethereum.js file mainly deals with the user interface. Because transferring tokens to Binance and Ethereum requires us to pay fees, we start by initializing the BNBCoin and zekndEthCoin classes:
For convenience, we've added a new function to the BNBCoin that approves the gateway to take the fee:
Then, we did the same for the zekndEthCoin.js class:
Next, we implement a couple of functions that simpy call the corresponding methods on the bnbCoin, bep2Coin, and ethCoin objects:
#The BinanceExtdevRinkeby class
The BinanceExtdevRinkeby class is where we've baked most of the logic. First, we import a few things:
Next, we declare our class like this:
The thing to note about this code is that the class inherits from the UniversalSigning class. This lets us separate the logic so that we can easily initialize the class like this:
The _load function from the UniversalSigning class does a couple of things:
first, it loads some values we don't want to hardcode (like the
chainId) from a JSON file by calling the_loadNetworkConfigurationfunction.sets up a signer and gets your Ethereum address
creates a new temporary provider for zeknd,
checks if a mapping exitsts. If not, it creates one.
lastly, it returns the mapping togehter with the temporary provider and the
client
Here's how the function looks like:
Now, let's get back to our function from the BinanceExtdevRinkeby class. Next, it initializes the contracts with _getContracts, and calls a function that we'll listen for events- _filterEvents. This way, every time our balance changes, the UI will get updated. Lastly, the function reads the initial balance by running _refreshBalance.
#Moving tokens from zeknd to Ethereum
To move tokens from zeknd to Ethereum, we follow a two-step process:
first, we approve the transfer gateway to take the fee:
once the transfer gateway is approved to take the fee, we run the
withdrawToEthereumfunction on thebep2Coinobject:
Again, this function is pretty straightforward. First, it transfers the tokens to the transfer gateway contract on the zeknd side:
One thing to note. As seen above, calling await gatewayContract.withdrawERC20Async creates a pending withdrawal. The pending withdrawal is signed by the Gateway validators, and an event is emitted by the Gateway to notify users that a withdrawal has been signed. At that point, the user can fetch the signed withdrawal receipt from the Gateway and submit it to the Ethereum Gateway in order to withdraw the token to their Ethereum account.
Next, we fetch the signed withdrawal record with:
lastly, let's withdraw the tokens to our Rinkeby account by calling the
withdrawCoinsFromRinkebyGatewayfunction and passing it thedatastructure containing the signature:
#Moving tokens from Ethereum to zeknd
To move move tokes from Ethereum to zeknd, we run the depositTozeknd function on the bep2Coin object:
First, it approves the transfer gateway to take the tokens and then calls the depositERC20 method on the Rinkeby transfer gateway. The method expects two parameters: the amount to transfer and the address of the token contract.
#Moving Tokens from Ethereum to Binance
To move tokens from Ethereum to Binance, we first approve the gateway to take the fee by calling the approveFee function on the bnbCoin object and then run the withdrawToBinance function which takes two arguments: your address on Binance and the amount to withdraw. Here's how the withdrawToBinance function looks like:
If you've made through here, congrats! You should have a good understanding of how to transfer tokens between Binance, zeknd, and Ethereum. The best way to move forward is to get your hands dirty and try to create some great stuff based on this demo.
In the meantime, please feel free to reach out to us on Telegram if you have any questions about this tutorial or just want to leave us feedback.
Last updated