Skip to content

Instantly share code, notes, and snippets.

@traderbagel
Created September 4, 2018 11:04
Show Gist options
  • Save traderbagel/0c35c109e509baa7373a5b35efdf3147 to your computer and use it in GitHub Desktop.
Save traderbagel/0c35c109e509baa7373a5b35efdf3147 to your computer and use it in GitHub Desktop.
crawl_bnb_transaction.py
import datetime
import concurrent.futures
from collections import namedtuple
BNB_transaction = namedtuple('BNB_transactions', ['hash', 'created_at', 'block', 'from_address', 'to_address', 'amount'])
def get_receipt(tx):
if isinstance(tx, AttributeDict):
tx = tx.hash
return web3.eth.getTransactionReceipt(tx)
def get_address_from_topic(topic):
hex40 = web3.toHex(topic)[-40:]
address = web3.toChecksumAddress(hex40)
return address
def get_balance_from_data(data):
wei_balance = web3.toInt(hexstr=data)
ether_balance = web3.fromWei(wei_balance, 'ether')
return ether_balance
def crawler(height):
# Get transactions from block
block = web3.eth.getBlock(height, full_transactions=True)
block_timestamp = datetime.datetime.fromtimestamp(block.timestamp)
# Get receipt of each transaction
receipts = []
with concurrent.futures.ThreadPoolExecutor() as executor:
for receipt in executor.map(get_receipt, block.transactions):
receipts.append(receipt)
# Error Handling
if len(receipts) != len(block.transactions):
raise Exception("Receipt count not equal")
if any(receipt is None for receipt in receipts):
raise Exception("Some receipt is None")
# Parse BNB transaction
bnb_transactions = []
for receipt in receipts:
if not receipt.logs: # logs may be None
continue
for log in receipt.logs:
if not (log.address and
web3.toChecksumAddress(log.address) == BNB_ADDRESS and
web3.toHex(log.topics[0]) == TRANSFER_LOG_HASH):
continue
bnb_transactions.append(BNB_transaction(
hash=web3.toHex(receipt.transactionHash),
created_at=block_timestamp,
block=height,
from_address=get_address_from_topic(log.topics[1]),
to_address=get_address_from_topic(log.topics[2]),
amount=get_balance_from_data(log.data),
))
return bnb_transactions
crawler(6259346)
''' Output
[BNB_transactions(
hash='0xba48fc0e658bfd927370ae0dda6fd37793e504e90289cd8216e986ebf714699b',
created_at=datetime.datetime(2018, 9, 3, 0, 20, 10),
block=6259346,
from_address='0x98702707fe04c38CE752aFad9C392f4A46289274',
to_address='0x3f5CE5FBFe3E9af3971dD833D26bA9b5C936f0bE',
amount=Decimal('8.5')
)]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment