Deploy Smart Contract on binance chain

Steps to deploy and verify smart contract on Binance smart chain

Smart Contract deployment on BSC using truffle

Haris Mumtaz

--

Using truffle framework here to deploy our first smart contract on BSC.

At start you need to have smart contract on your truffle project. In our case I have created a ‘HappyToken’. you can also clone from HappyToken Git repo for quick start.

Now we are going to create our account on meta mask that will be used to deploy our contracts. If you already have one you can skip this step.

Install Meta Mask on your chrome extension, once you have installed, navigate to meta mask on your chrome and create an account.

NOTE: when creating an account write down your seed phrase somewhere in safe place. we will use this later to deploy our contract.

After that you will be logged in and should have connected with Ethereum MainNet network by default on your account but as we will deploy our contract to Binance Smart Chain, for that we need to add those networks manually on our account since Meta Mask don’t provide BSC network by default.

Now to add BSC mainNet and test Networks, goto your accounts settings. navigate to the network tab and click on Add Network at, First we are going to add BSC mainNet. you need enter the following details:

NetworkName: Smart Chain

New RPC URL: https://bsc-dataseed.binance.org/

ChainId: 56

Currency Symbol: BNB

BlockChain Explorer: https://bscscan.com

and save it. Now you will see Smart Chain in your network list.

Same like we can add BSC test network with the following details:

NetworkName: Smart Chain — Testnet

New RPC URL: https://data-seed-prebsc-1-s1.binance.org:8545/

ChainId: 97

Currency Symbol: BNB

BlockChain Explorer: https://testnet.bscscan.com

Now we have setup our meta mask with the BSC networks to be use. I’m going to use the BSC test network to deploy our contract, so, will select Smart Chain — Testnet from the network list on meta mask.

You need to have some BNB on your account to deploy smart contract. for test network you can transfer mock BNB from this address into your account:

https://testnet.binance.org/faucet-smart

Now come back to your truffle project and install these dependencies on it:

npm install @truffle/hdwallet-provider

npm install truffle-plugin-verify

you need to have/create truffle-config.js file in your project directory. Truffle uses this file to have plugins and network configurations.

I have created one like this

Now in the above code snippet we have imported our env.json file which contains mnemonic (Its a seed phrase of your meta mask account which we stored in a safe place at the time of creating an account) and a BSC API Key.

You can get your BSC API Key by registering your self on https://bscscan.com.

once you have registered, Log into your account and navigate to https://bscscan.com/myapikey and click on Add key

name your key (I have named Happy in our case) and save it. Now you will have your BSC Key. replace your BSC key and seed phrase in env.json file.

Network Configuration

In truffle-config.js file we have added our network configuration on which we want to deploy our contracts. I have added two networks here. 1st is the bsc which is main network of BSC and the other is bscTestnet which is the test network to deploy our contract in test environment.

in configurationnetwork_id is the same as chain ID we had in our meta mask account for smart chain networks. It tells us that which blockchain we have connected.

I have used solidity compiler version 0.6.12 you can change this according to your requirement.

Compile

It’s time to compile our HappyToken Contract. Run the command to compile our created smart contract.

truffle compile — network bscTestnet

this will create a build folder and a compiled version of our smart contract. compiled file contains the contract ABI, which later we will be using to verify our deployed contract.

Deploy

Now the excited part comes. we will deploy our compiled contract now on bscTestnet . run the migration on truffle to deploy our contract.

Truffle uses migrations to deploy smart contracts.

Run this command

truffle migrate — network bscTestnet

This will run the migration and deploy our smart contract into the network which we have provided in command line argument.

If all goes well, it will show the summary of deployed code just like below

Remember address, transaction_hash and other details provided would differ, Above is just to provide an idea of structure.

Copy the contract address from the summary and go to https://testnet.bscscan.com/ and search there by pasting it your copied address. you will see your contract deployed on explorer.

non verify contract

Blockchain requires to verify our contract for source code verification. It provides transparency for users interacting with smart contracts.

We are going to verify our contract using our generated contract on truffle project.

run this command to verify your contract:

truffle run verify HappyToken@{your contract address} — network bscTestnet

Above HappyToken is the contract name(replace your contract name) which we have deployed earlier and the second param is the contract address of our deployed smart contract.

After running the command we will get the success summary like below

Successfully verified contract

Now copy the link from the summary result and hit your browser. you will be redirected to your verified deployed contract.

verified contract

whoof, You have successfully deployed your first smart contract on Binance Smart Chain. Now you can read your contract and use it on your applications using web3.

--

--

Haris Mumtaz

Technology enthusiast and an explorer of an ideas. 💡