In this document I will explain how to read Binance Smart Chain wallet transactions in Python.

Context:

Cryptocurrency is relatively a new asset type, many people struggle to understand its ecosystem. This document is not about explaining cryptocurrency in detail, but some context is required to understand the main topic.

In a nutshell we create one or more wallets to deposit our cryptocurrency asset. These wallets may be hosted by public cryptocurrency exchanges like Binance, Coinbase, etc or stored in private devices like mobile phone, web browser extension, such as TrustWallet, Metamask etc.

Most of the cryptocurrency exchanges help their users to generate account transaction statements. But problems may arise when people save their assets in private wallets, i.e. outside cryptocurrency exchanges. Managing and calculating their account transactions become trickier for these private wallets, but in some cases we do need that calculation; for example they are required to be counted in the tax self assessment report (in many countries).

Binance Smart Chain (BSC):

Binance Smart Chain (BSC) also known as BNB Chain is one of the biggest blockchain networks that hosts smart contracts-based applications. BSC provides a platform to buy, sell and swap decentralized tokens, NFTs etc.

BSCScan:

BSCScan is a comprehensive tool to view wallets, assets in those wallets, and their transactions.

We can view a list of transactions, download csv files of a wallet address. But it becomes quite laborious to calculate manually, if we have dozens and in some cases hundreds of transactions.

API to explore BSCScan:

You can find the documentation for BSCScan API here

BSCScan provides a set of APIs for the developers to read wallet details. We can use HTTP client in any programming language/script or simply curl command to call these APIs. In this text, I will explain how to write a Python script to read wallet transactions from BSCScan.

First of all we need to get our API Key from the BSCscan website by following these steps,

  1. Create an account on BSCscan and login.
  2. Go to the account Profile page and find the “API-KEYs” option from the left side menu.

Do not share this API Key to anyone.

Image Source

Python Scripting:

I use BscScan package from bscscan-python to read BSC-based wallets transactions. It provides an extensive list of functions to interact with BSCScan APIs, you can find an index here.

We have to input our API key (copied from BSCScan website) to BscScan package. BscScan calls are asynchronous, and response may take some time, depending on the resultset and page size we define.

import asyncio
import json

from bscscan import BscScan

YOUR_API_KEY = "THIS_IS_API_KEY"

async def main():
  async with BscScan(YOUR_API_KEY) as bsc:
    print(await bsc.get_bnb_balance(address="0x2SJFJSDLFJLKSJFKLJSD"))

    print(await bsc.get_tx_receipt_status(txhash="0x4sfdflsdjkfHSDJGSDGSJDGSGJ"))

    transactions = await bsc.get_normal_txs_by_address(address="0x2SJFJSDLFJLKSJFKLJSD",
        startblock=0,
        endblock=99999999,sort="asc")

    for i, tx in enumerate(transactions):
      print("transaction # :" + str(i))
      print("blockNumber: " + tx["blockNumber"])
      print("timeStamp: " + tx["timeStamp"])
      print("hash: " + tx["hash"])
      print("from: " + tx["to"])
      print("value: " + tx["value"])
      print("contractAddress: " + tx["contractAddress"])    

if __name__ == "__main__":
  asyncio.run(main())

Note: I have written a fake wallet address and hashcode of the transaction in the above script.

In this example I call three functions, and print their output on console,

  • get_bnb_balance() fetches the balance of the wallet by address.
  • get_tx_receipt_status() gets receipt status of the transaction by its hashcode.
  • get_normal_txs_by_address() brings all the transaction details by wallet address. This is the most interesting (for me or possibly for many people) function call.

We need to process the response to calculate profit/loss or whatever calculation we want to do.

Here I read all the transactions by a wallet address and then loop through them to print selected fields,

transactions = await bsc.get_normal_txs_by_address(address="0x2SJFJSDLFJLKSJFKLJSD",
startblock=0,
endblock=99999999,sort="asc")

for i, tx in enumerate(transactions):
print("transaction # :" + str(i))
print("blockNumber: " + tx["blockNumber"])
print("timeStamp: " + tx["timeStamp"])
print("hash: " + tx["hash"])
print("from: " + tx["to"])
print("value: " + tx["value"])

run the script,

python bsc_scan_transactions.py

Output should be like,

$ python bsc_scan_transactions.py
0
{'status': '1'}
transaction # :0
blockNumber: 3456679
timeStamp: 1633894562
hash: 0xce453a1303d9a11cab1004d2
from: 0x2eff6bbd59dfhghghhdd1d23c9c58
value: 184970520000000000
transaction # :1
blockNumber: 5565656
timeStamp: 1633895222
hash: 0x3dfcc7a14693f50c78c33522
from: 0x2gfhfhd8fdhfdhgf58
value: 555119150000000000
transaction # :2
blockNumber: 5656565
timeStamp: 1633896713
hash: 0x1d7b221da28103c1ac52ecc35b4e7db
from: 0x10ed43cfdhghgdhhdddd
value: 730089670000000000
..

Final Wording:

The script like this may help us to calculate the cryptocurrency transactions happening in BSC (or any) wallet. Python libraries like bscscan-python makes it easy to call BSCScan APIs.

We recommend to read more knowledgeable articles here.

References:

In this document I will explain how to read Binance Smart Chain wallet transactions in Python.

Context:

Cryptocurrency is relatively a new asset type, many people struggle to understand its ecosystem. This document is not about explaining cryptocurrency in detail, but some context is required to understand the main topic.

In a nutshell we create one or more wallets to deposit our cryptocurrency asset. These wallets may be hosted by public cryptocurrency exchanges like Binance, Coinbase, etc or stored in private devices like mobile phone, web browser extension, such as TrustWallet, Metamask etc.

Most of the cryptocurrency exchanges help their users to generate account transaction statements. But problems may arise when people save their assets in private wallets, i.e. outside cryptocurrency exchanges. Managing and calculating their account transactions become trickier for these private wallets, but in some cases we do need that calculation; for example they are required to be counted in the tax self assessment report (in many countries).

Binance Smart Chain (BSC):

Binance Smart Chain (BSC) also known as BNB Chain is one of the biggest blockchain networks that hosts smart contracts-based applications. BSC provides a platform to buy, sell and swap decentralized tokens, NFTs etc.

BSCScan:

BSCScan is a comprehensive tool to view wallets, assets in those wallets, and their transactions.

We can view a list of transactions, download csv files of a wallet address. But it becomes quite laborious to calculate manually, if we have dozens and in some cases hundreds of transactions.

API to explore BSCScan:

You can find the documentation for BSCScan API here

BSCScan provides a set of APIs for the developers to read wallet details. We can use HTTP client in any programming language/script or simply curl command to call these APIs. In this text, I will explain how to write a Python script to read wallet transactions from BSCScan.

First of all we need to get our API Key from the BSCscan website by following these steps,

  1. Create an account on BSCscan and login.
  2. Go to the account Profile page and find the “API-KEYs” option from the left side menu.

Do not share this API Key to anyone.

Image Source

Python Scripting:

I use BscScan package from bscscan-python to read BSC-based wallets transactions. It provides an extensive list of functions to interact with BSCScan APIs, you can find an index here.

We have to input our API key (copied from BSCScan website) to BscScan package. BscScan calls are asynchronous, and response may take some time, depending on the resultset and page size we define.

import asyncio
import json

from bscscan import BscScan

YOUR_API_KEY = "THIS_IS_API_KEY"

async def main():
  async with BscScan(YOUR_API_KEY) as bsc:
    print(await bsc.get_bnb_balance(address="0x2SJFJSDLFJLKSJFKLJSD"))

    print(await bsc.get_tx_receipt_status(txhash="0x4sfdflsdjkfHSDJGSDGSJDGSGJ"))

    transactions = await bsc.get_normal_txs_by_address(address="0x2SJFJSDLFJLKSJFKLJSD",
        startblock=0,
        endblock=99999999,sort="asc")

    for i, tx in enumerate(transactions):
      print("transaction # :" + str(i))
      print("blockNumber: " + tx["blockNumber"])
      print("timeStamp: " + tx["timeStamp"])
      print("hash: " + tx["hash"])
      print("from: " + tx["to"])
      print("value: " + tx["value"])
      print("contractAddress: " + tx["contractAddress"])    

if __name__ == "__main__":
  asyncio.run(main())

Note: I have written a fake wallet address and hashcode of the transaction in the above script.

In this example I call three functions, and print their output on console,

  • get_bnb_balance() fetches the balance of the wallet by address.
  • get_tx_receipt_status() gets receipt status of the transaction by its hashcode.
  • get_normal_txs_by_address() brings all the transaction details by wallet address. This is the most interesting (for me or possibly for many people) function call.

We need to process the response to calculate profit/loss or whatever calculation we want to do.

Here I read all the transactions by a wallet address and then loop through them to print selected fields,

transactions = await bsc.get_normal_txs_by_address(address="0x2SJFJSDLFJLKSJFKLJSD",
startblock=0,
endblock=99999999,sort="asc")

for i, tx in enumerate(transactions):
print("transaction # :" + str(i))
print("blockNumber: " + tx["blockNumber"])
print("timeStamp: " + tx["timeStamp"])
print("hash: " + tx["hash"])
print("from: " + tx["to"])
print("value: " + tx["value"])

run the script,

python bsc_scan_transactions.py

Output should be like,

$ python bsc_scan_transactions.py
0
{'status': '1'}
transaction # :0
blockNumber: 3456679
timeStamp: 1633894562
hash: 0xce453a1303d9a11cab1004d2
from: 0x2eff6bbd59dfhghghhdd1d23c9c58
value: 184970520000000000
transaction # :1
blockNumber: 5565656
timeStamp: 1633895222
hash: 0x3dfcc7a14693f50c78c33522
from: 0x2gfhfhd8fdhfdhgf58
value: 555119150000000000
transaction # :2
blockNumber: 5656565
timeStamp: 1633896713
hash: 0x1d7b221da28103c1ac52ecc35b4e7db
from: 0x10ed43cfdhghgdhhdddd
value: 730089670000000000
..

Final Wording:

The script like this may help us to calculate the cryptocurrency transactions happening in BSC (or any) wallet. Python libraries like bscscan-python makes it easy to call BSCScan APIs.

We recommend to read more knowledgeable articles here.

References:

© 2024 Solution Toolkit . All rights reserved.