Package: blocknative-sdk
Blocknative Python SDKInstallVirtualEnv QuickStart$virtualenvbn
$../bn/bin/activate(bn)$pip3install--upgradepip(bn)$pip3installblocknative-sdk(bn)$curl'https://raw.githubusercontent.com/blocknative/python-sdk/main/examples/subscribe.py'>subscribe.py(bn)$python3subscribe.pymyapikey.keyManual Installpython3setup.pyinstallAPI KeyTo get started using the Blocknative Python SDK you must first obtain an API Key. You can do so by heading over toBlocknative.com!UsageBasic usagefromblocknative.streamimportStreamimportjson# Initialize the streamstream=Stream('<API_KEY>')# Define your transaction handler which has the context of a specific subscription.asyncdeftxn_handler(txn,unsubscribe):# Output the transaction data to the consoleprint(json.dumps(txn,indent=4))# Define the address you want to watchuniswap_v2_address='0x7a250d5630b4cf539739df2c5dacb4c659f2488d'# Register the subscriptionstream.subscribe_address(uniswap_v2_address,txn_handler)# Start the websocket connection and start receiving events!stream.connect()Unsubscribingfromblocknative.streamimportStreamimportjson# Initialize the streamstream=Stream('<API_KEY>')# Define your transaction handlerasyncdeftxn_handler(txn,unsubscribe):iftxn['status']=="confirmed":# Output the transaction data to the consoleprint(json.dumps(txn,indent=4))# Unsubscribe from this subscriptionunsubscribe()# Define the address you want to watchuniswap_v2_address='0x7a250d5630b4cf539739df2c5dacb4c659f2488d'# Register the subscriptionstream.subscribe_address(uniswap_v2_address,txn_handler)# Start the websocket connection and start receiving events!stream.connect()Using Filtersfromblocknative.streamimportStreamimportjsonstream=Stream('<API_KEY>')asyncdeftxn_handler(txn,unsubscribe):# This will only get called with transactions that have status of 'confirmed'# This is due to the global filter aboveprint(json.dumps(txn,indent=4))uniswap_v2_address='0x7a250d5630b4cf539739df2c5dacb4c659f2488d'filters=[{'status':'confirmed'}]# Global filter will apply to all of these subscriptionsstream.subscribe_address(curve_fi_address,txn_handler,filter=filters)# Start the websocket connection and start receiving events!stream.connect()Using Global FiltersSimilar as above but this time we use global filters which will apply to all subscriptions.fromblocknative.streamimportStreamimportjsonglobal_filters=[{'status':'confirmed'}]stream=Stream('<API_KEY>',global_filters=global_filters)asyncdeftxn_handler(txn,unsubscribe):# This will only get called with transactions that have status of 'confirmed'# This is due to the global filter aboveprint(json.dumps(txn,indent=4))uniswap_v2_address='0x7a250d5630b4cf539739df2c5dacb4c659f2488d'curve_fi_address='0xdf5e0e81dff6faf3a7e52ba697820c5e32d806a8'sushi_swap_address='0xd9e1ce17f2641f24ae83637ab66a2cca9c378b9f'# Global filter will apply to all of these subscriptionsstream.subscribe_address(curve_fi_address,txn_handler)stream.subscribe_address(uniswap_v2_address,txn_handler)stream.subscribe_address(sushi_swap_address,txn_handler)# Start the websocket connection and start receiving events!stream.connect()Connecting to Binance Smart Chainfromblocknative.streamimportStreamimportjsonBSC_NETWORK_ID=56# Initialize the stream - specify network_id `56` to connect to bsc mainstream=Stream('<API_KEY>',network_id=BSC_NETWORK_ID)# Define your transaction handler which has the context of a specific subscription.asyncdeftxn_handler(txn,unsubscribe):# Output the transaction data to the consoleprint(json.dumps(txn,indent=4))# Define the address you want to watchpancakeswap_v2_address='0x10ed43c718714eb63d5aa57b78b54704e256024e'# Register the subscriptionstream.subscribe_address(pancakeswap_v2_address,txn_handler)# Start the websocket connection and start receiving events!stream.connect()