bsc通过电子钱包地址Web3.py获取交易

2024-04-28 06:35:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如何跟踪bsc网络中钱包列表的令牌交易

我考虑使用websocket和filter函数。我认为可以使用topics作为过滤器参数的一部分,并且只在监视的地址之间反映Transfer事件,这样我的应用程序就不必处理不必要的数据

但我做错了,不知道如何正确地将钱包列表(或至少只有一个钱包)作为过滤函数的参数。怎么做

我在从Transfer事件获取数据时遇到问题,因为我不知道如何解码HexBytes类型。我看到了web3.js的功能,但是web3.py没有

address_list = ['0x67fdE6D04a82689a59E5188f9B572CBeF53D4763', '...', '...']

web3 = Web3(Web3.WebsocketProvider('wss://bsc.getblock.io/mainnet/?api_key=your_api_key'))
web3_filter = web3.eth.filter({'topics': address_list}) 
while True:
    for event in web3_filter.get_new_entries():
        print(web3.toJSON(web3.eth.wait_for_transaction_receipt(event).logs))

Tags: key函数api列表参数address事件filter
3条回答

最后我找到了解决办法。起初,我使用node.js编写了相同的代码,因为web3.js使我更容易理解它的实际工作原理。它有更好的命名方法、更好的文档等

那么回到web.py:

为了获取Transfer事件签名,我使用了以下代码transferEventSignature = web3.toHex(Web3.sha3(text='Transfer(address,address,uint256)'))

对于编码/解码,您可以使用eth_abi

from web3 import Web3
from eth_abi import encode_abi, decode_abi
from hexbytes import HexBytes

encoded_wallet = (web3.toHex(encode_abi(['address'], [wallet])) # encoding

web3 = Web3(Web3.WebsocketProvider('wss://speedy-nodes-nyc.moralis.io/api-key/bsc/mainnet/ws'))
event_filter = web3.eth.filter({'topics': [transferEventSignature, None, encoded_wallet]}) # setting up a filter with correct parametrs
        while True:
            for event in event_filter.get_new_entries():
                decoded_address = decode_abi(['address'], HexBytes(event.topics[2])) # decoding wallet 
                value = decode_abi(['uint256'], HexBytes(event.data)) # decoding event.data

                tokenContractAddress = event.address

                contractInstance = web3.eth.contract(address=tokenContractAddress, abi=jsonAbi) # jsonAbi is standart erc-20 token abi 
                # I used simplified JSON abi that is only able to read decimals, name and symbol

                name = contractInstance.functions.name().call() 
                decimals = contractInstance.functions.decimals().call()
                symbol = contractInstance.functions.symbol().call()
                # getting any token information

                # doing some useful stuff

GetBlock.io为我工作,但有时会与网络不同步。 我在这项服务上取得了更好的成功:https://moralis.io/

我希望有人会觉得这有用

bscscanapi不可靠。问题是API在cloudfare DDoS保护背后,有时会询问验证码。不幸的是,对于Python代码,无法绕过此验证码检查

Bscscan提供免费基本使用的API(5个请求/秒)

因此,对于事务列表(有不同类型的事务,包括正常内部bep-20等),您可以使用this

示例用法为this

相关问题 更多 >