[英]bsc get transactions by wallet address Web3.py
How can I track tokens transactions of a list of wallets in the bsc network?如何在 bsc 网络中跟踪钱包列表的代币交易?
I think about using websocket and filter function.我考虑使用 websocket 和过滤功能。 I think it's possible to use the
topics
as a part of the filter parameters and reflect only the Transfer
event to/from watched address, so that my app doesn't have to handle unnecessary data.我认为可以将
topics
用作过滤器参数的一部分,并仅反映到/从监视地址的Transfer
事件,这样我的应用程序就不必处理不必要的数据。
But I'm doing something wrong and don't know how to correctly give list of wallets (or at least only one wallet) as a parameter to my filter function.但是我做错了,不知道如何正确地将钱包列表(或至少只有一个钱包)作为我的过滤器功能的参数。 How to do that?
怎么做?
And I have problems with getting data from Transfer
event as I don't know how to decode a HexBytes
type.而且我在从
Transfer
事件中获取数据时遇到了问题,因为我不知道如何解码HexBytes
类型。 I saw web3.js functions for it but nothing with web3.py.我看到了 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))
At last I found the solution.最后我找到了解决方案。 At first I wrote the same code using node.js, because web3.js makes it much simplier for me to understand how it actually works.
起初我使用 node.js 编写了相同的代码,因为 web3.js 让我更容易理解它的实际工作原理。 It has better methods naming, better docs, etc
它有更好的命名方法、更好的文档等
So back to web.py:所以回到 web.py:
For getting Transfer
event signature I used this code transferEventSignature = web3.toHex(Web3.sha3(text='Transfer(address,address,uint256)'))
为了获得
Transfer
事件签名,我使用了这个代码transferEventSignature = web3.toHex(Web3.sha3(text='Transfer(address,address,uint256)'))
For encoding/decoding you can use eth_abi
library对于编码/解码,您可以使用
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 worked for me, but would sometimes get out of sync with the network. GetBlock.io 为我工作,但有时会与网络不同步。 I have had better success with this service: https://moralis.io/
我在这项服务上取得了更好的成功: https : //moralis.io/
I hope somebody will find this usefull.我希望有人会发现这很有用。
The bscscan
api is not reliable. bscscan
api 不可靠。 The problem is that the API is behind a cloudfare DDoS protection and a captcha is asked sometimes.问题是 API 位于 cloudfare DDoS 保护之后,有时会询问验证码。 With Python code, there is no way to bypass this captcha check, unfortunately.
不幸的是,使用 Python 代码无法绕过这种验证码检查。
Here is my code to track BEP20 token transactions:这是我跟踪 BEP20 代币交易的代码:
def log_new(event_filter):
for event in event_filter.get_new_entries():
handle_event(event)
def handle_event(event):
receipt = web3.eth.waitForTransactionReceipt(event['transactionHash'])
logs = contract.events.Transfer().processReceipt(receipt)
args = logs[0]['args']
print ("EVENT", event)
hashstr = binascii.b2a_hex(event['transactionHash'])
name = contract.functions.name().call()
decimals = contract.functions.decimals().call()
symbol = contract.functions.symbol().call()
item = {
"from": args["from"],
"to": args.to,
"value": args.value,
"blockNumber":event['blockNumber'],
"transhash": "0x" + hashstr.decode("ascii"),
"timeStamp" : get_block_timestamp(event['blockNumber']),
"tokenSymbol" : symbol, #"KAMPAY"
"decimals" : decimals,
"name" : name
}
print(item)
block_filter = web3.eth.filter({'fromBlock':'latest','address':MYTOKENADDRESS})
while 1:
log_new(block_filter)
time.sleep(1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.