0%

eth | bsc 根据区块查询所有交易

这里是利用 web3.py 对接 bsc 网络,并根据区块来查询该区块的全部交易信息。


参考资料



bsc


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import time
import traceback

from web3 import Web3
from web3.middleware import geth_poa_middleware

# 检测是否有人购买某种币种
def check_buy_coin(data):
if data is None:
return False
try:
transactions = data.transactions
for transaction in transactions:
try:
tx = Web3.toHex(transaction.hash).lower()
info = transaction.input
method = info[:10]
if method in ["0x38ed1739", "0x8803dbee", "0x7ff36ab5", "0xfb3bdb41"]:
print(f"one buy this coin tx is {tx}")
return True
except Exception as e:
print(f"latest check_buy_coin in is error")
print(traceback.format_exc())
continue
return False
except Exception as e:
print(f"latest check_buy_coin is error")
print(traceback.format_exc())



if __name__ == '__main__':
bsc_url1 = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc_url1))
web3.middleware_onion.inject(geth_poa_middleware, layer=0)
infos = web3.eth.get_block(11735342, True)
check_buy_coin(infos)

注意,获取 bsc 的区块交易,必须加入

web3.middleware_onion.inject(geth_poa_middleware, layer=0)

否则会报错,具体原因,参考「参考资料」。

我们在上面

1
2
3
if method in ["0x38ed1739", "0x8803dbee", "0x7ff36ab5", "0xfb3bdb41"]:
print(f"one buy this coin tx is {tx}")
return True

来解析交易的 input 逻辑。

请我喝杯咖啡吧~