How to Install and Use Binance Smart Chain (BSC) πŸš€

Saturday, Jan 11, 2025 | 7 minute read

GitHub Trend
How to Install and Use Binance Smart Chain (BSC) πŸš€

Experience lightning-fast performance and seamless compatibility with popular development tools! πŸ”₯ A fantastic ecosystem awaits for creating dApps effortlessly, making your blockchain endeavors more enjoyable and efficient! 🌐✨

In this fast-paced blockchain era, finding a development platform that combines high performance with a rich ecosystem has become the dream of many developers!✨

Unveiling Binance Smart Chain: The New Darling of Blockchain Developers 🌟

Binance Smart Chain (BSC) is a leading blockchain platform focused on enhancing programmability and interoperability, particularly excelling in its relationship with the BNB Beacon Chain! 🀝 BSC’s design philosophy is highly compatible with Ethereum’s smart contracts and development tools, allowing developers to easily create decentralized applications (dApps) and making blockchain development feel like a walk in the park!

How to Install BSC πŸ”§

Before diving into BSC, you’ll need to install the necessary tools in your development environment! Here’s a simple step-by-step guide to install and configure BSC:

1. Install geth πŸ› οΈ

First off, we need to install geth, which is the main client for BSC. You can choose an appropriate installation method based on your operating system.

MacOS Installation via Homebrew 🍏

If you’re a MacOS user, installing via Homebrew is super convenient:

  1. Ensure Homebrew is installed:

    brew -v
    
    • Use this command to check if you have Homebrew installed.
  2. Run the following commands to install geth:

    brew tap ethereum/ethereum
    brew install ethereum
    
    • brew tap adds the necessary repository, while brew install installs Ethereum-related tools including geth.
Ubuntu Installation via PPA 🐧

Ubuntu users need not worry; we have straightforward installation steps:

  1. Enable the Launchpad repository:

    sudo add-apt-repository -y ppa:ethereum/ethereum
    
    • This step adds the Ethereum PPA repository for easier installation.
  2. Install the stable version:

    sudo apt-get update
    sudo apt-get install ethereum
    
    • apt-get update updates the package listings, after which you can use apt-get install to install Ethereum and its tools.
Windows Installation πŸ’»

Windows users can follow this installation process:

  1. Download the precompiled binaries from the download page. Once installed, geth will automatically be added to your system PATH.
    • Make sure to select the correct version for your system!
Docker Container 🐳

If you’re a Docker user, you can run it like this:

  1. Pull and run the Docker image:
    docker pull ethereum/client-go
    docker run -it -p 30303:30303 ethereum/client-go
    
    • docker pull is used to fetch the official geth image, while docker run starts the container.
Build from Source πŸ”„

For those who prefer building from source, just:

  1. Clone the repository and build:
    git clone https://github.com/ethereum/go-ethereum.git
    cd go-ethereum
    make geth
    
    • git clone downloads the repository, cd navigates to the project directory, and make geth compiles geth.

2. Build All Useful Tools πŸ› οΈ

To build all tools, run:

make all
  • This command compiles all necessary tools according to what’s defined in the Makefile.

3. Set Environment Variables 🌐

After installation, if you encounter errors, you can set the following environment variables to help solve issues:

export CGO_CFLAGS="-O -D__BLST_PORTABLE__" 
export CGO_CFLAGS_ALLOW="-O -D__BLST_PORTABLE__"
  • Setting environment variables is often done to optimize the compilation process and ensure specific features or settings work properly.

Executables πŸ’Ό

In the BSC project, several command-line tools can be found in the cmd directory. Below is a brief description of the tools:

Command Description
geth The primary BNB Smart Chain client binary, capable of running as a full node, archive node, or light node with RPC interface support
clef A standalone signing tool for geth
devp2p A tool for network layer interaction with nodes
abigen A generator for contract ABI and Go package source code
bootnode A lightweight node for network discovery
evm A development tool for running bytecode snippets in a configurable environment
rlpdump A development tool that converts RLP binary dumps into user-friendly hierarchical representations

Running geth πŸš€

Next, let’s take a look at how to start a BSC full node and ensure it runs smoothly!

Steps to Start the Full Node πŸ’»

  1. Download the Pre-Built Binary:

    # Linux
    wget $(curl -s https://api.github.com/repos/bnb-chain/bsc/releases/latest | grep browser_ | grep geth_linux | cut -d\\" -f4)
    mv geth_linux geth
    chmod -v u+x geth
    
    # MacOS
    wget $(curl -s https://api.github.com/repos/bnb-chain/bsc/releases/latest | grep browser_ | grep geth_mac | cut -d\\" -f4)
    mv geth_macos geth
    chmod -v u+x geth
    
    • wget is used to download files from the specified URL.
    • mv renames the downloaded file to geth for easier access.
    • chmod modifies the file permissions to make it executable.
  2. Download Configuration Files:

    # mainnet
    wget $(curl -s https://api.github.com/repos/bnb-chain/bsc/releases/latest | grep browser_ | grep mainnet | cut -d\\" -f4)
    unzip mainnet.zip
    
    # testnet
    wget $(curl -s https://api.github.com/repos/bnb-chain/bsc/releases/latest | grep browser_ | grep testnet | cut -d\\" -f4)
    unzip testnet.zip
    
    • Here, we download the configuration files for both the mainnet and testnet and unzip them into the current directory.
  3. Download Snapshot: You can download the latest chaindata snapshot here.

    • Snapshots help you quickly sync the node’s state.
  4. Start the Full Node:

    ./geth --config ./config.toml --datadir ./node --cache 8000 --rpc.allow-unprotected-txs --history.transactions 0
    
    • This command launches geth, loading the configuration, specifying the node data directory, and configuring cache and other options.
  5. Monitor Node Status: You can track node logs by viewing ./node/bsc.log.

    • The logs contain all important information about the node to help with troubleshooting.
  6. Interact with the Full Node: You can use the built-in interactive JavaScript console of geth.

    • In the console, you can execute various commands to interact directly with the node.

Configuration πŸ“

You can use a configuration file to start the node instead of using command-line flags:

$ geth --config /path/to/your_config.toml
  • This method makes the entire startup process clearer and easier to manage.

To export your existing configuration:

$ geth --your-favourite-flags dumpconfig
  • The dumpconfig command displays the current configuration settings for easy backup and sharing.

Starting Bootnode 🧩

Bootnode is an essential part of network discovery; let’s check out the setup process:

  1. Generate a key:

    bootnode -genkey boot.key
    
    • The generated key is used to identify and connect to the network.
  2. Start the bootnode:

    bootnode -nodekey boot.key -addr :30311 -network bsc
    
    • This command starts the bootnode, pointing to the generated key and setting network parameters.

The logs will show that the bootnode is running:

enode://3063d1c9e1b824cfbb7c7b6abafa34faec6bb4e7e06941d218d760acdd7963b274278c5c3e63914bd6d1b58504c59ec5522c56f883baceb8538674b92da48a96@127.0.0.1:0?discport=30311
  • Ensuring the bootnode runs smoothly is key to joining the network!

Code Examples πŸ’»

When developing on BSC, the following code examples can help you get started quickly:

JavaScript Contract Deployment Example πŸ“œ

// Import Web3 library for interacting with the blockchain
const Web3 = require('web3');

// Create an instance of Web3, connecting to the BSC network
const web3 = new Web3('https://bsc-dataseed.binance.org/');

// Contract ABI and bytecode
const contractABI = [...]; // Your contract ABI
const contractBytecode = '0x...'; // Your contract bytecode

// Async function to deploy the contract
const deploy = async () => {
    // Get available accounts
    const accounts = await web3.eth.getAccounts();
    
    // Deploy the contract
    const result = await new web3.eth.Contract(contractABI)
        .deploy({ data: contractBytecode })
        .send({ from: accounts[0], gas: '1000000' });

    // Output the contract address
    console.log('Contract deployed to', result.options.address);
};

// Call the deploy function
deploy();
  • The above code connects to the BSC mainnet and deploys a simple smart contract. web3 is a JavaScript library for interacting with Ethereum and its compatible chains.

Solidity Smart Contract Example πŸ“–

// Specify the version of Solidity
pragma solidity ^0.8.0;

// Define a simple storage contract
contract SimpleStorage {
    uint256 storedData; // Storage variable

    // Function to set data
    function set(uint256 x) public {
        storedData = x; // Storing the input value
    }

    // Function to retrieve stored data
    function get() public view returns (uint256) {
        return storedData; // Returning the stored data
    }
}
  • The Solidity code above defines a simple storage contract that can store and retrieve an unsigned integer.

With these steps and examples, whether you’re a beginner or an experienced developer, you’ll find it easy to install and use BSC to develop your own projects! πŸ’°πŸ’°πŸ’°

Β© 2024 - 2025 GitHub Trend

πŸ“ˆ Fun Projects πŸ”