Build a dApp on Binance Smart Chain With Secure Data Feeds
In April 2019, popular cryptocurrency exchange Binance launched Binance Chain, a network built and optimized for fast exchanging, and the home of the BNB token. On Binance Chain, you can:
- Send and receive BNB
- Issue new tokens
- Send, receive, burn/mint, and freeze/unfreeze tokens
- Propose to create trading pairs between two different tokens
- Send orders to buy or sell assets through trading pairs created on the chain
Binance Chain is great for users looking to speed up their trading. However, it isn’t EVM-compatible and doesn’t support smart contracts, which is by design. To allow for smart contract creation, the Binance team also created Binance Smart Chain (BSC), a network that uses a Proof of Staked Authority (PoSA) consensus algorithm and that is EVM-compatible, enabling smart contract developers to build programmable dApps that natively integrate with the Binance Chain.
This dual chain architecture allows fast transactions on the exchange side, while still enabling smart contracts. The Binance team actually coined the term “CeDeFi”, or “Centralized Decentralized Finance”, to describe this hybrid approach, which makes the trade-off of using less decentralized architecture for additional transaction throughput, while still ensuring developers can deploy applications in a permissionless manner and use the same composable tools from Ethereum, such as Solidity and Chainlink Price Feeds.
The Binance Smart Chain Chainlink Price Feeds are available on Binance mainnet and can be used to build applications on BSC that require decentralized, tamper-proof data inputs. In this technical tutorial, we’ll walk you through how to work with Binance Smart Chain, the BNB token, and Chainlink oracles so you can quickly get started building externally connected smart contracts on BSC, even if you have no experience with Ethereum.
Building With BSC
Tools we’ll use
Since BSC is EVM-compatible, we can use the same tools from our Solidity environment, such as Truffle, Hardhat, MetaMask and more. In this tutorial, we’ll use Brownie, a Python smart contract development framework, to work with BSC, since Brownie has a powerful forking feature that we can use natively, as the Binance testnet currently doesn’t support the price feeds.
If you’re interested in running a Truffle or Hardhat program with Binance Smart Chain instead of Brownie, look into ganache-cli’s forking feature or skip ahead to where we talk about forking—we will mention how to run this with ganache. Forking with ganache-cli is what Brownie does on the backend when running our tests.
Requirements
- python3
- nodejs
- ganache-cli
- brownie
To check your Python version, in your terminal type:
python --version
For nodejs, type:
node -v
You can download python and nodejs here. Node comes pre-installed with npm. Then, make sure your have ganache-cli installed with:
npm install -g ganache-cli
Or
yarn global add ganache-cli
Then finally, install brownie with
pip install eth-brownie
Or
pip3 install eth-brownie
And now we are all set up!
Getting Started
Now that we have everything set up, go ahead and unbox the chainlink-mix for brownie. This is a boilerplate template for working with Chainlink smart contracts. If you want to learn more about it, you can learn how to deploy smart contracts of any blockchain with python in our blog here.
For working with mainnet or testnet Binance Smart Chain, you normally need the BNB token, similar to working with ETH on the Ethereum blockchain. When you deploy smart contracts, you’ll need to deploy them with BNB.
We are going to do everything 100% locally, so we don’t need any testnet ETH, LINK, or BNB to get started.
First, let’s bake the Brownie mix:
brownie bake chainlink-mix cd chainlink
And we are in our project. If we run ls we can see what’s in our directory.
- build : This is where the project keeps track of your deployed smart contracts and compiled contracts
- contracts : The source code of your contracts, typically written in Solidity or Vyper
- interfaces : A layout of interfaces you’ll need to work with deployed contracts. Every interaction with a contract needs an ABI and an address. Interfaces are great ways to get a contract’s ABI
- scripts : Scripts we create to automate processes of working with our contracts
- tests : Tests
- brownie-config.yaml : This is where we have all the information for Brownie to understand how to work with our smart contract. What blockchain do we want to deploy to? Are there any special parameters we want to set? All these are set in the config file.
requirements.txt , README.md , LICENSE , and .gitignore can be ignored for now. You’ll find out what they are for as you practice.
We will be deploying PriceFeed.sol to our local environment that will be forked from the Binance main chain.
Adding a Forking Network
In order to work with the Binance Chain, we need a Remote Procedure Call (RPC) URL or host. This is a URL that we will make API calls to in order to connect with the Binance Smart Chain. If interested, you can also run your own Binance Smart Chain node and connect to that.
We can find the RPC URLs for Binance in their documentation. We will also need the ChainID. For now, let’s use these:
host=https://bsc-dataseed.binance.org/ chainid=56
And last, we will need the Price Feed address of whichever Chainlink Price Feed we want to work with. The brownie chainlink-mix comes pre-installed with the Binance ETH/USD Price Feed, but if you want a list of all the Chainlink Price Feeds on Binance, you can always check the Binance or Chainlink documentation for more. If you look in the brownie-config.yaml file, you’ll see a section under networks called binance-fork. This has all the variables we need to work with a fork of Binance.
Now, we want to tell Brownie to use Binance to connect to, but we want to fork it. Forking a chain means taking a copy of that chain and running it locally, this way we don’t have to pay any gas and can iterate quickly with testing. It also means that once the forked chain is down, everything is deleted! We can add a fork of Binance to the Brownie network with the following command:
brownie networks add development binance-fork cmd=ganache-cli host=http://127.0.0.1 fork=https://bsc-dataseed1.binance.org accounts=10 mnemonic=brownie port=8545
This will run a local ganache-cli chain on our localhost at port 8545. It will use https://bsc-dataseed1.binance.org as its source to fork when we deploy. If done correctly, you’ll see something like:
Brownie v1.13.0 - Python development framework for Ethereum SUCCESS: A new network 'binance-fork' has been added └─binance-fork ├─id: binance-fork ├─cmd: ganache-cli ├─cmd_settings: {'fork': 'https://bsc-dataseed1.binance.org', 'accounts': 10, 'mnemonic': 'brownie', 'port': 8545} └─host: http://127.0.0.1
You can check all the networks by running brownie networks list.
Deploying Your Contract
Now that everything is set up, we can deploy and read from our contract on our local ganache forked chain. In the scripts folder there is a script called deploy_price_consumer_v3.py. We can run this with:
brownie run scripts/price_feed_scripts/deploy_price_consumer_v3.py --network binance-fork
And you’ll see an output like:
Brownie v1.13.0 - Python development framework for Ethereum ChainlinkMixProject is the active project. Launching 'ganache-cli --accounts 10 --fork https://bsc-dataseed1.binance.org --mnemonic brownie --port 8545 --hardfork istanbul'... Running 'scripts/price_feed_scripts/deploy_price_consumer_v3.py::main'... Transaction sent: 0x63022ee6c741ffb31ec6f8f29d3d2412c0a81a557a316a9a9752603825b8e96d Gas price: 0.0 gwei Gas limit: 6721975 Nonce: 0 PriceFeed.constructor confirmed - Block: 4398765 Gas used: 132364 (1.97%) PriceFeed deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87 The current price of ETH is 135462000000 Terminating local RPC client...
So, we just:
- Forked the Binance Chain and ran it locally
- Deployed a smart contract to it
- And read the price of ETH from it
The current price of ETH is 135462000000
Congratulations! You’re on your way to claiming some Binance bounties!
Going Further
Now that you know how to deploy smart contracts with Binance Smart Chain, you can go deeper into BSC, or even work with Matic, xDai, or other side chains and layer 2s. If you prefer Hardhat and Truffle, see if you can take the ganache-cli command over there and run some local tests. There are many hackathons coming up, so be sure to enter for a chance to work with other talented people in the space, win some prizes, and make your mark as a smart contract developer.
If you want to keep expanding the capabilities of your smart contracts, visit the Chainlink developer documentation and join the technical discussion in Discord. If you build something great with Binance Smart Chain, Brownie, Truffle, Hardhat, or any other Chainlink integration, be sure to tag us with @chainlink so we can check out all the cool work you do!
The Chainlink Fall 2021 Hackathon kicks off October 22, 2021. Whether you’re a developer, creator, artist, blockchain expert, or completely new to the space, this hackathon is the perfect place to kickstart your smart contract development journey and learn from industry-leading mentors. Secure your spot today to compete for over $300k in prizes.