How to use Binance Smart Chain in your Dapp

Deploying and onboarding users to the Binance Smart Chain (BSC)

Defi has been a major contributor to the Binance Smart Chain taking off recently. Along with increasing gas costs on Ethereum mainnet which are actually at one of the lowest levels since a long time at the time of this writing, but will likely pump again at the next ETH price pump.

So how does one deploy to it and what actually is the BSC?

What is the Binance Smart Chain?

Binance Chain has been launched in 2018 by Binance. It's a tendermint-based chain with its main functionality to transfer assets. This allowed for a more decentralized form of value transfer within the Binance ecosystem.

In September 2020 they added the Binance Smart Chain (BSC) to it. This is its own separate chain, but is part of a dual chain design which synchronizes state with the original Binance Chain. The BSC itself is simply a geth fork, so it relies on the same technology as ETH 1.0. But the gas costs are much lower due to a higher output achieved by a very limited amount of validators.

Is BSC decentralized?

First we have to understand that decentralization exists on a spectrum. While some projects strive for perfectionism to be as decentralized as technically viable (did someone say Injective Protocol?), that doesn't mean other approaches are all fully centralized. So what are the factors that make BSC more centralized or more decentralized?

Decentralized: BSC runs on a Proof of Stake (PoS) combined with Proof of Authority (PoA) which they call Proof of Staked Authority (PoSA), see also the Whitepaper here.. In its dual chain design, the original Binance Chain is running on the PoS, while Binance Smart Chain itself is a pure PoA consensus. However, the validators in the PoA consensus are governed through Binance Chain staking and are thus not fully centralized. This is certainly better than a regular centralized exchange or a pure PoA system.

Centralized: As for the BSC, I probably don't have to tell you that POA is the opposite of decentralized. You are essentially trusting the few selected authorities. In this case the authorities are managed through governance from the Binance Chain. So how centralized is the PoS in Binance Chain? The total amount of validators are limited to 21 with Binance itself likely having control over the largest validators. I'll let you decide how decentralized this is in your opinion.

All in all you can see BSC tends to be more on the centralized side of the spectrum, but it's still significantly more decentralized than simply using Binance.

How to deploy to Binance Smart Chain

Deploying to the Binance Smart Chain is very simple. If you're using Truffle, simply add the Binance Smart Chain network configuration like this:

bsc: {
      provider: () => new HDWalletProvider(mnemonic, 'https://bsc-dataseed.binance.org'),
      network_id: 56,
    },
},
bsc-testnet: {
      provider: () => new HDWalletProvider(mnemonic, 'https://data-seed-prebsc-1-s1.binance.org:8545'),
      network_id: 97,
}

You'll also need funds in the chain of course. For the testnet you can use the faucet here. For how to transfer funds from the public chain, read on.

How to onboard users to your Binance Smart Chain Dapp

1. Choice of wallet

These days you have much more than just MetaMask as possible wallets to support available. Take a look at Yearn Finance for example. They are supporting 11 different wallets alone at the time of this writing.

Wallet Options

2. MetaMask example live in action

We'll focus on MetaMask as the biggest wallet with the most features. With MetaMask you can nowadays actually request to connect directly to a custom network.

If you want to see this live, check out Pancake Swap as example. Unless you already have Binance Smart Chain added, it will request to add this network. Or simply click 'Add network' in the Binance bridge.

3. How to add BSC automatically for users

You can see on the right how to add Binance Smart Chain automatically to the wallet for the user. This will show the popup you see above. If the user confirms, the network is added and automatically switched to.

This is all thanks to EIP-3085 with the new RPC method wallet_addEthereumChain . See also the MetaMask docs here.

const params = [{
    "chainId": "0x38", // 56 in decimal
    "chainName": "Smart Chain",
    "rpcUrls": [
        "https://bsc-dataseed.binance.org"
    ],
    "nativeCurrency": {
        "name": "Binance Coin",
        "symbol": "BNB",
        "decimals": 18
    },
    "blockExplorerUrls": [
        "https://bscscan.com"
    ]
}]

try {
    await ethereum.request({
        method: 'wallet_addEthereumChain',
        params,
    })
} catch (error) {
    // something failed, e.g., user denied request
}
Binance Bridge

Using the Binance Bridge

You can use the bridge here to transfer funds in and out of Binance Smart Chain from the Ethereum network directly. It will lock the tokens on the bridge contract in the Ethereum mainnet. The bridge works very similar to the PoA network bridge. It may even be a fork of it, but I couldn't find the source code of the bridge anywhere. Please let me know if you know the answer to this.

At the start the bridge was working only unidirectionally, but it's now working in both directions. Meaning you can transfer assets

  1. from Ethereum to BSC an
  2. from BSC to Ethereum

Defi on Binance Smart Chain

One of the advantages of BSC may be its vast ecosystem, in particular Defi projects. Just take a look at the following image provided by BNB Swap:

BSC Defi

For an overview of more than just Defi, check out the Github of the ecosystem here. What are the most popular projects?

  • PancakeSwap: The Uniswap v2 fork for BSC. Similar to SushiSwap, the fork has added a few extra features, namely staking, a lottery and NFT support, see the guide here and if you are not aware of AMMs, you might want to check out my guides of Uniswap v2, v3 and SushiSwap.
  • Venus: The MakerDao and Compound fork of BSC is a money market where you can borrow and lend BEP-20 (ERC-20 on BSC) tokens.
  • Autofarm: This is essentially the Yearn Finance for BSC. You can participate in a vault and it automatically tries to find the most optimal way for you to earn a yield on your deposits.
  • BurgerSwap: BurgerSwap is also a popular AMM on BSC. You can do BEP-20 token swaps and provide liquidity. It implemented ERC-2917 and it for once is not a fork of Uniswap. They wanted to improve upon the incentive and governance model of Uniswap.
  • Spartan Protocol: This is a combination of a liquidity pools based on AMMs, synthetics assets (tokenized derivatives) and a lending protocol.
  • Cream: Cream is another popular lending protocol which exists on Ethereum and that is also deployed on BSC.

Markus Waas

Solidity Developer

More great blog posts from Markus Waas

© 2024 Solidity Dev Studio. All rights reserved.

This website is powered by Scrivito, the next generation React CMS.