Using Web3.js Library on BNB Smart Chain
By the end of this lesson, you will be able to:
- Using Web3.js library to fetch blockchain data;
- Using Nodereal's Meganode API;
- Deploying static pages onto localhost using http-server.
Designed for anyone wanting to learn development on the BNB Smart Chain, this tutorial provides a step-by-step guide on how to use Web3.js library along with Nodereal API to fetch transaction details from the BNB Smart Chain blockchain for the given transaction hash. The technology stack used in this tutorial includes Web3.js, Nodereal MegaNode, and http-server.
We encourage our readers to perform the steps listed for a better understanding. Before jumping into the demo, make sure you have the following softwares installed.
- node v16.13.0
- npm v8.1.0
Setting up the development environment
- Install http-server:
npm install -g http-server
- Clone the BNBChain-Tutorial repository:
git clone https://github.com/bnb-chain/bnb-chain-tutorial.git
- Change the current directory:
- Install all the dependencies (node modules):
- Before running the application, make sure that you have included the HTTP link for the Nodereal Meganode API in the
index.html as shown in the figure below.
- For this project, we have used the BSC Testnet public API key, as shown in the figure below. For a complete list of Nodereal Meganode Public API keys, refer here.
- Run the application by using the command
http-server from the project directory.
- Open your web browser and navigate to any of the ports indicated by the
How to Use
- Since we have used the HTTP reference of Nodereal's Meganode API for BSC testnet, open BSCscan for Testnet, and copy the transaction hash of any transaction of your choice.
- Paste this transaction hash into the input field in our block explorer.
- Click on the Fetch Details button to fetch details of this transaction.
- You can also display other details from the received transaction receipt by changing the code block shown in the figure below as we have displayed the minimum.
- The received transaction receipt has several fields as shown in the figure below.
In this tutorial, we developed a dapp that has both backend and front-end integration that uses Web3.js library that can be used for fetching details of transactions on the BSC testnet by given a transaction hash. The tech stack includes Web3.js, Nodereal MegaNode, and http-server. Check out our GitHub for more tutorials on how to develop on BSC. If you have any questions or are stuck, reach out to us on our Discord Channel.