Skip to main content

Using the Binance Smart Chain with Python


For most people, crypto is just a way to invest in a highly speculative asset. Bitcoin is seen as digital gold, as something you can buy, hold and sell at a higher price. But crypto currencies and blockchain technologies are used in many more applications, and creating innovative products on top of these networks starts with connecting to the network. Whether you're trying to create a digital wallet, interact with smart contracts, or build a completely new application, you need to use code and connect to the blockchain of your choice.

In this post, we'll use Python and connect to the Binance Smart Chain. While there is a lot of crypto documentation out there for JavaScript, the amount is smaller for Python, so hopefully this post will give you a good place to start.

Connecting to the chain

First, you'll need to download the web3 library. You can use pip to install it on your system:

pip install web3

Once installed, open up a Python interpreter and let's initialize the module:

from web3.middleware import geth_poa_middleware
from web3 import Web3

addr = "0xf130E4...d198"

w3 = Web3(Web3.HTTPProvider('https://bsc-dataseed1.binance.org:443'))
w3.middleware_onion.inject(geth_poa_middleware, layer=0)

Here, replace the address on the third line with your own BSC address. If you have an existing wallet that supports the Binance Smart Chain, you should have an address that starts with 0x. We'll use this address to query balances.

Get block information

In a traditional application, unless you decide to use one of the public web APIs that already have the entire ledger and can provide you more direct information, the way you query the blockchain is to look at the entirety of the chain. For example, you can find all transactions related to a specific address by scanning the 'to' and 'from' of all the blocks, which can be a lengthy process unless you cache the information beforehand.

You can look at a specific block with the following code:

block = w3.eth.get_block('latest')
w3.toHex(block['hash'])

Here, instead of 'latest', you can use a block number. You can get the current number of blocks in the chain with this line:

w3.eth.block_number

Fetching your coin balance

Every blockchain has a base token, and in the case of BSC the token is BNB. When you call the get_balance function, you have to specify the unit of measurement in ether, but it's actually your BNB balance that will be returned:

float(w3.fromWei(w3.eth.get_balance(addr), 'ether'))

Calling a smart contract

Calling a smart contract requires both the contract address and an ABI, which is a structure describing the functions of the contract. There are many standard functions that can be used, but in our case all we'll be doing is getting the balance of a specific token, so our ABI will only include the GetBalance function.

In order to get the proper coin address, you can look it up on the BscScan website. In our example, we'll be using the ADA BEP20 coin contract address:

ABI = """[
  {
    "constant":true,
    "inputs":[{"name":"_owner","type":"address"}],
    "name":"balanceOf",
    "outputs":[{"name":"balance","type":"uint256"}],
    "type":"function"
  },
  {
    "constant":true,
    "inputs":[],
    "name":"decimals",
    "outputs":[{"name":"","type":"uint8"}],
    "type":"function"
  }
]"""

contract = w3.eth.contract(Web3.toChecksumAddress('0x3ee2200efb3400fabb9aacf31297cbdd1d435d47'), abi=ABI)
float(w3.fromWei(contract.functions.balanceOf(addr).call(), 'ether'))

If all went well, you should get your balance in ADA for the wallet address defined above.