How to create a smart contract on Binance Smart Chain? (Solidity, Truffle)

Investment Rookie
25 followers
2 Video Views·Oct 28, 2022
There is a new blockchain that is getting a lot of traction. In less than two months a lot of projects
were launched on this blockchain especially in DeFi. This offers new yield farming and staking
opportunities for investors and a new playground for blockchain developers who want to launch their
project. This blockchain has low transaction fees, fast transaction time, it's based on the technology
of Ethereum and it's backed by one of the most successful crypto exchanges. I'm talking of the
Binance Smart Chain. So no matter if you're an investor or a developer you have to know about
this blockchain now when it's not crowded yet and you can still have this amazing untapped potential.
So in this video I'm going to introduce the Binance Smart Chain. I'm going to explain how it works and
how to deploy a smart contract on it. Like I said it's based on the technology of Ethereum so you
will be able to reuse a lot of the technologies you already know as a blockchain developer like
Solidity, Truffle, Remix etc. There are still some small differences with Ethereum and I will explain
these in this video. And at the end of the video I will tell you whether Binance Smart Chain will
replace Ethereum as the main blockchain for DeFi and decentralized applications. If you don't know
me I'm Julian and on my channel InTheBlocks I teach DeFi and blockchain development and before we
continue quick mention to my free training down below if you want to learn how you can become a
professional blockchain developer and make $100,000 a year check out my free training down below.
So Binance Chain and Binance Smart Chain are two different blockchains and it's a bit confusing so
I'm going to explain the difference. So let's start from the beginning. Binance is one of the
most popular centralized exchange for crypto. In less than two years after its creation, Binance
already made $1 billion of profit. That's huge! Even the biggest tech companies like Facebook,
Amazon etc. didn't grow so fast. The reason for the success of Binance is that they only trade
crypto against crypto, they don't deal with fiat money at all and this allows them to totally
skip the KYC procedure which is a huge pain in other centralized exchanges like Coinbase.
So even though Binance is doing well they know that they face way more competition from DeFi.
So to compete with DeFi and decentralized exchanges in 2019 they launched their own
blockchain called the Binance Chain. So with Binance Chain and its integrated decentralized
exchange called Binance DEX you can combine the advantage of blockchain like decentralization
with the advantages of centralized exchanges like speed. It uses a proof-of-stake consensus mechanism
where miners which are called validators earn fees based on the blocks they add to the blockchain.
So currently Binance chooses all the validators but in the future it's supposed to become truly
decentralized and decided by the community. Binance Chain was a good beginning but it was a little
bit limited because it doesn't allow to have a smart contract like Ethereum. So that's why
in August this year Binance launched another blockchain called Binance Smart Chain. Binance
Smart Chain is interoperable with the Binance Chain meaning that you can transfer assets between both
which allow you to combine the very high scalability of Binance Chain with the advanced
capability of Binance Smart Chain. Binance hopes this will allow to develop a full ecosystem of
decentralized applications that can potentially compete with what we have on Ethereum. For developers and
investors the most interesting is Binance Smart Chain and I'm going to explain this in the next
section. Binance Smart Chain is a fork of Geth, the Go implementation of Ethereum. It shares most of
the same features but with a few differences. Compared to Ethereum, Binance Smart Chain has
shorter block time around five seconds and lower transaction fees. For example for a simple money
transfer it costs only one cent of a dollar. So like for Ethereum you have to pay transaction
fees but the currency used for these fees is the BNB token and not Ether. So BNB is a token that
lives on the Binance Chain, the other blockchain of Binance. BNB is a token that follows the BAP20
standard, a token standard that extends the ERC20 token standard of Ethereum. The Binance Smart Chain
uses the Ethereum virtual machine for its smart contracts so it means you can use all the tools
you already know for Ethereum like Solidity, Truffle, Remix, Web3 and Metamask. The consensus algorithm
of Binance Smart Chain is different from Ethereum. It's called a proof-of-stake authority consensus.
There are 21 validators that add new blocks to the blockchain so they are the equivalents of
miners on Ethereum. These validators are paid transaction fees in BNB token based on the blocks
they mine. To be a validator you need to stake some BNB token. If you try to manipulate the
blockchain your BNB stake will be slashed which means you will lose your money. So this is to give
a validator an incentive to stay honest. The validator that has the most stake is chosen to
validate the next block. These 21 validators are already pre-selected by Binance and in any case
it can be a bit technical to run a validator node so it's not really accessible to the average end
user. However it's possible for any investors to participate in the validation mechanism by
delegating your BNB tokens to a validator. So in this case the same rule as the validator applies
to your BNB token. If the validator is selected for the next block and produces a valid block
you will earn some reward proportionally to the stake you sent. If the validator produces a wrong
block you will lose your stake and if the validator is not selected for the next block nothing happens.
Unfortunately there is no UI to stake your BNB coins at the moment so you have to use the
command line tool of Binance Chain called BNBCLI. You will find the instruction in the documentation
of Binance in order to use this tool. So just to be clear although it's called the Binance Chain
client you can use it both for the Binance Chain and the Binance Smart Chain. So that's it for how
the Binance Chain works. Next I'm going to show you how you can deploy a smart contract on the
Binance Smart Chain. Because Binance Smart Chain is based on Ethereum you can reuse all the tools
you already use for Ethereum like 3GT, Truffle etc. So I'm not going to explain the basic tech
of Ethereum here but I'm going to focus on what is specific to Binance Smart Chain especially
tools like Foset, Blockchain Explorer etc. If you want to learn the basics of Ethereum programming
I have a few videos you can follow on my channel and I put these links in the description. So I
have already created a smart contract project and we are going to deploy it. So this is a Truffle
project a framework for smart contract. My smart contract is very simple you can save and modify
an integer. This is nothing super exciting here but I just want to show you the deployment part.
I also have a migration file to explain Truffle how to deploy my smart contract. What's interesting
is inside the Truffle config file. So first I have installed HDI provider which allows you to
configure how to connect to the blockchain. So with this you are going to build a provider object
where you pass your private keys and your URL to your blockchain node. So I'm going to explain
all of this. So first you need to generate an Ethereum address. So I like to use this website
vanityeth.tk. Scroll down and you can generate an address like this and here you click to reveal
the private key. So after I'm going to put this private key in this array here and for this URL
here. So that's the URL to a testnet of the Binance Smart Chain. So the Binance Smart Chain like for
Ethereum you have the mainnet and you also have testnet where you can try out your application.
So in order to deploy on the testnet or mainnet of Binance Smart Chain there is no equivalent to
infra but instead you can grab directly some URL to some node on this blockchain. So
in your documentation you will see a page on a JSON RPC endpoint and so we're not going to deploy
to mainnet but the procedure is exactly the same. So you go to testnet for BSC so Binance Smart Chain
and you have several URL here. So we're going to take the first one and so yeah so I've already
copied this here for the URL key and after once you have this you need to create a new entry for
network. So I call this Binance testnet. So I pass the provider network ID is 97. So this is another
one for mainnet and here I put the gas limit so I just put 1 million that should be enough.
And so after I'm going back to my address generator I'm going to copy the address
and the next I'm going to the faucet for the Binance Smart Chain. So this is exactly like
for faucet for the public testnet of Ethereum. You input your address and it's going to give you
some token for free. So on Binance Smart Chain we don't use Ether but we use the BNB token in
order to pay for transaction. So here is what you're going to get. So we choose one BNB.
So you wait a bit and after it tells you that this is funded. So after you can check that you have
received the fake BNB token by going to the blockchain explorer of the Binance Smart Chain
and you put your address and boom we can see our transaction here and we can see our balance of
one BNB token. So after I'm going to my terminal and at the root of my project I'm going to run
truffle migrate reset and for the network I call this binance testnet.
All right okay so I have a typo so I'm going to fix this in truffle config. Okay I forgot a comma
here. That's no big deal. Let's run our command again. So it's going to take some time because
it has to interact with the node of the Binance Smart Chain testnet. So it's slightly slower than
when you just deploy this on a local development blockchain like ganache. And our deployment is
finished and successful. So here we can see the address of our smart contract. So I'm going to
copy this. So back to our testnet explorer. So first I'm going to reload this page for the
sending address so we can see that we have some new transactions. These are the different
transactions for the contract deployment. So behind the hood truffle uses another smart contract for
deployment which is called migration.source. That's why here you see several transactions.
And here we can search for the address of our smart contract and here we can see that this is
a contract. So this is the compiled bytecode. So obviously this is not very readable but they have
a decompiler. So if you use a decompiler actually you can see the original source code of the smart
contract. But the point is that our smart contract is deployed on the testnet of the Binance Smart
Chain. And so for deploying to the Binance Smart Chain mainnet this is exactly the same except that
you change the URL and for that you can go back to this page I showed you before,
jsonapps endpoint in the documentation of the Binance Smart Chain. So I explained what is the
Binance Smart Chain and how to deploy a smart contract on it. The next step would be to deploy
a front-end as well. So this is no different than creating a front-end for Ethereum. So you have to
use JavaScript, Web3 and Metamask. For the integration with Metamask this is exactly the
same as for Ethereum. However your users will have to manually configure their Metamask with
a new network that points to a node of Binance Smart Chain. Binance also has a wallet distributed
as a Chrome extension. It already has configured the network of a Binance Smart Chain so for users
it's kind of easier. But for developers the integration is a bit different from Metamask
so you will have to check out their documentation. But the problem is that not many users have
installed this Binance wallet so I'm not sure that it's actually worth the hassle to integrate it.
So the big question is will Binance Smart Chain replace Ethereum? So in the short term we are
going to see a lot of DeFi forks launching on Binance Smart Chain. This will give a lot of
yield farming opportunities for investors and for developers it's also a big opportunity to
launch your project. As long as we don't have a solution for lower transaction fees on Ethereum,
Binance Smart Chain has a chance to take some market share from Ethereum. But when we'll finally
have a lower transaction fee on Ethereum, probably starting with some layer 2 solution, I expect that
there will be a big comeback to Ethereum. So my recommendation is this. Currently make sure to
monitor the situation on Binance Smart Chain but at the same time don't be too worried about Ethereum
because long term Ethereum will stay the main blockchain. I have no doubt about this.
So as you can see to build a decentralized application on the Binance Smart Chain you
need to know how to develop decentralized applications on Ethereum. I have many videos on
this like this playlist where we learn Solidity or this other video where we built five decentralized
applications on Ethereum with a smart contract test and frontend. I'll see you there!
