BNB Smart Chain

BNB Smart Chain client

Interact with your BNB Smart Chain node using BNB Smart Chain client.

  1. Install BNB Smart Chain client.

  2. Use geth attach command with the node endpoint.

    geth attach YOUR_CHAINSTACK_ENDPOINT
    

where YOUR_CHAINSTACK_ENDPOINT — your node HTTPS or WSS endpoint protected either with the key or password. See node access details.

  1. Invoke any methods from Web3 JavaScript API.

Example below demonstrates how to get the balance of an address in wei value and convert it to ether value:

> web3.fromWei(web3.eth.getBalance("0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae"))
642538.078574759898951277

GraphQL

You can use GraphQL on a dedicated node on the Growth, Business, and Enterprise subscription plans.

UI

You can query data using the graphical interface.

  1. On Chainstack, navigate to your dedicated BNB Smart Chain node. See node access details.
  2. Hover over GraphQL IDE URL and click Open.
  3. In the graphical interface that opens, run a GraphQL query.

Example to get the latest block number:

{ block { number } }

Node.js

You can build a web app to query data using node.js and axios:

const axios = require('axios');
const main = async () => {
  try {
    const result = await axios.post(
      'YOUR_CHAINSTACK_ENDPOINT',
      {
        query: `
          { block { number } }
        `
      }
    );
    console.log(result.data);
  } catch(error) {
    console.error(error);
  }
}
main();

where

  • YOUR_CHAINSTACK_ENDPOINT — your node GraphQL endpoint protected either with the key or password. See node access details.
  • query — your GraphQL query. In this case, to get the latest block number.

See also Using GraphQL with EVM-compatible nodes.

MetaMask

On node access details, click Add to MetaMask.

Truffle

Configure Truffle Suite to deploy contracts to your BNB Smart Chain nodes.

  1. Install Truffle Suite, HD Wallet-enabled Web3 provider, and create a project.

  2. Create a new environment in truffle-config.js, add your mnemonic phrase generated by a wallet and the BNB Smart Chain endpoint instead of YOUR_CHAINSTACK_ENDPOINT:

    const HDWalletProvider = require("@truffle/hdwallet-provider");
    const mnemonic = 'pattern enroll upgrade ...';
    ...
    module.exports = {
     networks: {
        chainstack: {
            provider: () => new HDWalletProvider(mnemonic, "YOUR_CHAINSTACK_ENDPOINT"),
            network_id: "*"
        },
       }
      }
    };
    

Hardhat

Configure Hardhat to deploy contracts and interact through your BNB Smart Chain nodes.

  1. Install Hardhat and create a project.

  2. Create a new environment in hardhat.config.js:

    require("@nomiclabs/hardhat-waffle");
    ...
    module.exports = {
      solidity: "0.7.3",
      networks: {
        chainstack: {
            url: "YOUR_CHAINSTACK_ENDPOINT",
            accounts: ["YOUR_PRIVATE_KEY"]
        },
      }
    };
    

    where

    • YOUR_CHAINSTACK_ENDPOINT — your node HTTPS or WSS endpoint protected either with the key or password. See node access details.
    • YOUR_PRIVATE_KEY — the private key of the account that you use to deploy the contract
  3. Run npx hardhat run scripts/deploy.js --network chainstack and Hardhat will deploy using Chainstack.

See also Forking EVM-compatible mainnet with Hardhat.

Remix IDE

To make Remix IDE interact with the network through a Chainstack node:

  1. Get MetaMask and set it to interact through a Chainstack node. See Interacting through MetaMask.
  2. In Remix IDE, navigate to the Deploy tab. Select Injected Provider - MetaMask in Environment.

This will engage MetaMask and make Remix IDE interact with the network through a Chainstack node.

web3.js

Build DApps using web3.js and BNB Smart Chain nodes deployed with Chainstack.

  1. Install web3.js.
  2. Connect over HTTP or WebSocket.

HTTP

Use the HttpProvider object to connect to your node HTTPS endpoint and get the latest block number:

const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.HttpProvider('YOUR_CHAINSTACK_ENDPOINT'));

web3.eth.getBlockNumber().then(console.log);

where YOUR_CHAINSTACK_ENDPOINT is your node HTTPS endpoint protected either with the key or password

WebSocket

Use the WebsocketProvider object to connect to your node WSS endpoint and get the latest block number:

const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.WebsocketProvider('YOUR_CHAINSTACK_ENDPOINT'));

web3.eth.getBlockNumber().then(console.log);

where YOUR_CHAINSTACK_ENDPOINT is your node WSS endpoint protected either with the key or password

web3.py

Build DApps using web3.py and BNB Smart Chain nodes deployed with Chainstack.

  1. Install web3.py.
  2. Connect over HTTP or WebSocket. See also EVM node connection: HTTP vs WebSocket.

HTTP

Use the HTTPProvider to connect to your node endpoint and get the latest block number:

from web3 import Web3

web3 = Web3(Web3.HTTPProvider('YOUR_CHAINSTACK_ENDPOINT'))
print(web3.eth.blockNumber)
from web3 import Web3

web3 = Web3(Web3.HTTPProvider('https://%s:%s@%s'% ("USERNAME", "PASSWORD", "HOSTNAME")))
print(web3.eth.blockNumber)

where

  • YOUR_CHAINSTACK_ENDPOINT — your node HTTPS endpoint protected either with the key or password
  • HOSTNAME — your node HTTPS endpoint hostname
  • USERNAME — your node access username (for password-protected endpoints)
  • PASSWORD — your node access password (for password-protected endpoints)

See also node access details.

WebSocket

Use the WebsocketProvider object to connect to your node WSS endpoint and get the latest block number:

from web3 import Web3

web3 = Web3(Web3.WebsocketProvider('YOUR_CHAINSTACK_ENDPOINT'))
print(web3.eth.blockNumber)
from web3 import Web3

web3 = Web3(Web3.WebsocketProvider('wss://%s:%s@%s'% ("USERNAME", "PASSWORD", "HOSTNAME")))
print(web3.eth.blockNumber)

where

  • YOUR_CHAINSTACK_ENDPOINT — your node WSS endpoint protected either with the key or password
  • HOSTNAME — your node WSS endpoint hostname
  • USERNAME — your node access username (for password-protected endpoints)
  • PASSWORD — your node access password (for password-protected endpoints)

See also node access details.

See also WebSocket connection to an EVM node.

web3.php

Build DApps using web3.php and BNB Smart Chain nodes deployed with Chainstack.

  1. Install web3.php.

  2. Connect over HTTP:

    <?php
    
    require_once "vendor/autoload.php";
    
    use Web3\Web3;
    use Web3\Providers\HttpProvider;
    use Web3\RequestManagers\HttpRequestManager;
    
    $web3 = new Web3(new HttpProvider(new HttpRequestManager("YOUR_CHAINSTACK_ENDPOINT", 5)));
    ?>
    

    where YOUR_CHAINSTACK_ENDPOINT is your node HTTPS endpoint protected either with the key or password

  3. Use JSON-RPC methods to interact with the node.

    Example to get the latest block number:

    <?php
    
    require_once "vendor/autoload.php";
    
    use Web3\Web3;
    use Web3\Providers\HttpProvider;
    use Web3\RequestManagers\HttpRequestManager;
    
    $web3 = new Web3(new HttpProvider(new HttpRequestManager("YOUR_CHAINSTACK_ENDPOINT, 5)));
    
    $eth = $web3->eth;
    
    $eth->blockNumber(function ($err, $data) {
         print "$data \n";
    });
    ?>
    

web3j

Build DApps using web3j and BNB Smart Chain nodes deployed with Chainstack.

Use the HttpService object to connect to your node endpoint.

Example to get the latest block number:

package getLatestBlock;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.exceptions.ClientConnectionException;
import org.web3j.protocol.http.HttpService;

import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

public final class App {

  private static final String USERNAME = "USERNAME";
  private static final String PASSWORD = "PASSWORD";
  private static final String ENDPOINT = "ENDPOINT";

  public static void main(String[] args) {
    try {

      OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
      clientBuilder.authenticator(new Authenticator() {
          @Override public Request authenticate(Route route, Response response) throws IOException {
              String credential = Credentials.basic(USERNAME, PASSWORD);
              return response.request().newBuilder().header("Authorization", credential).build();
          }
      });

      HttpService service = new HttpService(RPC_ENDPOINT, clientBuilder.build(), false);
      Web3j web3 = Web3j.build(service);


      EthBlock.Block latestBlock = web3.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false).send().getBlock();


      System.out.println("Latest Block: #" + latestBlock.getNumber());

    } catch (IOException | ClientConnectionException ex) {

      Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

}

where

  • ENDPOINT — your node HTTPS endpoint protected either with the key or password
  • USERNAME — your node access username (for password-protected endpoints)
  • PASSWORD — your node access password (for password-protected endpoints)

See also the full code on GitHub.

ethers.js

Build DApps using ethers.js and BNB Smart Chain nodes deployed with Chainstack.

  1. Install ethers.js.
  2. Connect over HTTP or WebSocket. See also EVM node connection: HTTP vs WebSocket.

HTTP

Use the JsonRpcProvider object to connect to your node endpoint and get the latest block number:

const { ethers } = require("ethers");

var urlInfo = {
    url: 'YOUR_CHAINSTACK_ENDPOINT'
};
var provider = new ethers.providers.JsonRpcProvider(urlInfo, NETWORK_ID);

provider.getBlockNumber().then(console.log);
const { ethers } = require("ethers");

var urlInfo = {
    url: 'YOUR_CHAINSTACK_ENDPOINT',
    user: 'USERNAME',
    password: 'PASSWORD'
};
var provider = new ethers.providers.JsonRpcProvider(urlInfo, NETWORK_ID);

provider.getBlockNumber().then(console.log);

where

  • YOUR_CHAINSTACK_ENDPOINT — your node HTTPS endpoint protected either with the key or password
  • USERNAME — your node access username (for password-protected endpoints)
  • PASSWORD — your node access password (for password-protected endpoints)
  • NETWORK_ID — BNB Smart Chain network ID:
    • Mainnet: 56
    • Testnet: 97

See node access details.

WebSocket

Use the WebSocketProvider object to connect to your node WSS endpoint and get the latest block number:

const { ethers } = require("ethers");

const provider = new ethers.providers.WebSocketProvider('YOUR_CHAINSTACK_ENDPOINT', NETWORK_ID);

provider.getBlockNumber().then(console.log);

where

  • YOUR_CHAINSTACK_ENDPOINT — your node WSS endpoint endpoint protected either with the key or password
  • NETWORK_ID — BNB Smart Chain network ID:
    • Mainnet: 56
    • Testnet: 97

See node access details.

Brownie

  1. Install Brownie.

  2. Use the brownie networks add command with the node endpoint:

    brownie networks add "BNB Smart Chain" ID name="NETWORK_NAME" host=YOUR_CHAINSTACK_ENDPOINT chainid=NETWORK_ID
    

    where

    • ID — any name that you will use as the network tag to run a deployment. For example, bsc-mainnet.
    • NETWORK_NAME — any name that you want to identify the network by in the list of networks. For example, Mainnet (Chainstack).
    • YOUR_CHAINSTACK_ENDPOINT — your node HTTPS or WSS endpoint
    • NETWORK_ID — BNB Smart Chain network ID:
      • Mainnet: 56
      • Testnet: 97

Example to run the deployment script:

brownie run deploy.py --network chainstack-mainnet

Foundry

  1. Install Foundry.
  2. Use --rpc-url to run the operation through your Chainstack node.

Forge

Use forge to develop, test, and deploy your smart contracts.

To deploy a contract:

forge create CONTRACT_NAME --contracts CONTRACT_PATH --private-key PRIVATE_KEY --rpc-url YOUR_CHAINSTACK_ENDPOINT

where

  • CONTRACT_NAME — name of the contract in the Solidity source code
  • CONTRACT_PATH — path to your smart contract
  • PRIVATE_KEY — the private key to your funded account that you will use to deploy the contract
  • YOUR_CHAINSTACK_ENDPOINT — your node HTTPS endpoint protected either with the key or password

Cast

Use cast to interact with the network and the deployed contracts.

To get the latest block number:

cast block-number --rpc-url YOUR_CHAINSTACK_ENDPOINT

where YOUR_CHAINSTACK_ENDPOINT is your node HTTPS endpoint protected either with the key or password