Deposit and Withdraw BEP-2
Last updated
Last updated
BEP-2 represents a common set of rules for token management on the Binance Chain and it specifies:
What information makes a token on Binance Chain
What actions can be performed on a token on Binance Chain
See the for more details.
This is the second 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. This guide shows how to build a simple DApp that transfers BEP2 tokens between Binance TestNet and zeknd TestNet. The third guide in the series will explain how to transfer a token between Binance, zeknd, and Ethereum.
Let's get the ball rolling!
Node.js 10 or higher (recommended v10.15.3)
To check out if Node.js is installed on your computer, fire up a terminal and type the following command:
If Node.js is already installed, you'll see something like the following:
If Node.js is not installed, or you're running an older version, you can download the installer from
a Binance account. Follow the instructions on the page to create an account.
the Binance chain . You can grab it from this . Note that, for the scope of this example, we will be using the testnet
version.
Issuing a new token on Binance TestNet will require you to pay a fee of 500 TestNet BNB tokens. Thus, before moving forward with this example, you must get at least 500 TestNet BNB tokens by following the steps from below:
Repeat the step from above for wallet B and then for C.
Once finished, click the Submit button
. This will credit each wallet with 200 TestNet BNB tokens.
Fill in the input box with the address of the wallet A and make sure you send all the tokens by clicking the Max
button:
Now, confirm the transaction details and the tokens will be transferred to wallet A:
Repeat the process for wallet B
.
At this point, wallet A
should hold 600 BNB tokens.
Before issuing a new token on Binance TestNet, you must import wallet A
locally. To do so, cd
into the directory where you downloaded the Binance TestNet CLI and run the following command:
This will import your wallet by recovering it from your seed phrase. Note that the recovery seed phrase is just the mnemonic phrase we asked you to save somewhere safe a bit earlier. You will be asked to type your password and then to enter your recovery seed phrase:
You can check if your key was successfully imported with the following:
The output should look similar to this:
Run the following command to issue a mintable
token on Binance TestNet:
Note: In the above command, replace
<YOUR-TOKEN-NAME>
and<YOUR-TOKEN-SYMBOL>
with your token name and symbol. Make sure they're unique.
First, you will be asked to type the password you set earlier:
Next you'll see something like this printed out to the console:
Note that Binance is adding a 3 letter suffix. As seen above (Issued BTE-A16
), our token symbol is BTE-A16
. Yours will be different.
Minting tokens is as easy as running this command:
You will be required to type your password and then you should see something like:
-examples
RepositoryAt this point, we're ready to clone
the zeknd-examples
GitHub repository. Fire up a terminal, cd
into your directory of choice, and run the following command:
This will download the repository into a new directory called zeknd-examples
. Let's cd
into it and install the dependencies:
To move BEP-2 tokens between Binance TestNet and Extdev TestNet we must deploy a corresponding ERC20 smart contract to zeknd. For demonstration purposes, we've created a shell of an smart contract - SampleBEP2Token
. Its source code is listed below:
As you can see, it basically inherits from ./BEP2TokenTemplate.sol,
. This smart contract provides a buch of useful functions and you can use it in your own apps by simply inheriting from it. Let's go ahead take a look at ./BEP2TokenTemplate.sol
:
The most important thing to note is the mintToGateway
function. This allows the Transfer Gateway to mint tokens that are transferred from Binance.
Note: You can find these contracts in the
./truffle/contracts
directory.
Now that we know how our ERC20 smart contracts look like, it is time to generate a zeknd private key:
The above command will create a new private key and save it to the ./truffle/
zeknd_private_key
file.
At this point, you're ready to deploy the smart contract to zeknd TestNet. This is as simple as:
Once you've deployed the SampleBEP2Token
contract you'll need to send a request to the Transfer Gateway to create a mapping between the BEP2 and ERC20 tokens. This way, when the Transfer Gateway is notified that a token from Binance has been deposited to the hot wallet address, it will mint a matching token in your zeknd ERC20 contract (unless the token already exists on the zeknd TestNet). The Transfer Gateway will refuse to create a contract mapping unless you provide proof that you deployed both contracts. To prove that you deployed both contracts, you are required to:
convert your Binance token symbol to an address that zeknd-js
is able to work with:
build a message by concating the addresses of your Binance and zeknd tokens:
sign the message using your Binance private key:
Lastly, call the addContractMappingAsync
on the Binance Gateway with a bunch of parameters:
Note that, on success,addContractMappingAsync
doesn't return anything. However, if the mapping already exists, it'll throw- Failed to commit Tx: TG012: contract mapping already exists
.
We've gone ahead and baked all the logic in the ./scripts/map-binance-contracts.js
script. Once you finish this tutorial, feel free to take a look and get a glimpse of how it works.
But before running the said script, we need to do some prep work:
Save the symbol of our BEP2 token into a file called ./binance/binance-token-symbol.txt
:
Next, we need to extract the Binance private key from the keystore file. Copy your keystore_a
file into the ./binance
folder and rename it to binance_keystore
.
To map the contracts, type:
Now that you've put everything in place, let's go ahead create the mapping:
Great! Pat yourself on the back. You're now ready to spin up the demo project:
Wait a bit until the demo gets compiled and then point your web browser at http://localhost:8080. Click on the Deposit and Withdraw BEP2
tokens and you'll see a page that looks similar to the screenshot below:
Of course, your initial BEP2 and BNB balances will be 0. Go to Binance, and transfer some BEP2 and BNP to zeknd TestNet. To do so, set the To Address
to tbnb1gc7azhlup5a34t8us84x6d0fluw57deuf47q9w
(that is the hot wallet address) and don't forget to put your zeknd address in the memo field.
So far, we learned how to set things up and spin up the demo. Next, we'll look into how it works under the hood.
In this section, we'll explain the basics of building a bare bones web user interface that lets users move tokens between Binance and zeknd. You'll find everything into to the following files:
src/bep2-deposit-withdraw.js
src/bep2/BEP2Coin.js
src/bnb/BNBCoin.js
In the next chapters, we'll take a closer look at the most important lines of code from these files.
This file deals with user interaction.
Things to note:
Withdrawing BEP2 tokens to Binance requires a fee to be paid. Hence, the depositWithdrawBEP2Example
function instantiates and then initializes both the BNBCoin
and BEP2Coin
classes:
Next, the withdrawBEP2
first calls this.bnbCoin.approveFee
to approve the fee and only after the fee gets approved, it goes ahead and calls this.bep2Coin.withdrawBEP2
:
If you followed along with our BNB tutorial, this should look very familiar to you. The most important change we've made is that we've added the approveFee
function:
Also, we've replaced the hard coded Transfer Gateway address with a nice function that resolves the address by name:
The BEP2Coin
class is similar in many respects to BNBCoin
class, but there are a few differences. Let's take a closer look at how it works.
First, we initialize a couple of things:
This sets the current network to extdev
, instantiates a new Client
, gets the zeknd address, initializes a web3 instance and our contracts. Then, it calls _filterEvents
so the balances get automatically updated. Lastly, it reads the initial balance and updates the status to waiting
.
To withdraw BEP2 tokens we must first approve the transfer gateway to take them, then we wait a bit and check if the allowance
gets updated. If so, we go ahead and call withdrawTokenAsync
(that is the function that actually withdraw the tokens):
And that's about everything you need to know when it comes to moving tokens between Binance and zeknd!
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.
Go to the Binance page and create three Binance TestNet wallets. Let's call them A, B, and C. Download the keystore files and give them some meaningful names such as keystore_a
, keystore_b
, and keystore_c
. Alsdo, don't forget to save the mnemonics somewhere safe.
Head over to the and unlock wallet A. This will take you the Binance DEX main page. Select your account from the top menu bar and copy your address. Next, paste it to a new document.
At this point, you are ready to get yourself some BNB TestNet tokens. Go to the and paste your addresses:
Now, what's left to do is to transfer the BNB TestNet tokens from wallet B and C to wallet A. Let's wallet B by uploading the keystore file and typing the password. This will redirect you to the Binance TestNet home page. From the top menu bar select Balances
and then click Send
:
For more details on how to issue tokens, see the page.
Now, you can check your balance on the Binance website by opening .