首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web3py -正在等待TX?

Web3py -正在等待TX?
EN

Ethereum用户
提问于 2022-01-15 16:53:25
回答 2查看 474关注 0票数 1

是否有任何方法可以获得指定地址的待定事务?我可以获取当前挂起块的所有挂起事务,但我不能过滤它们。我能做到:

txHashPending = w3.eth.filter('pending').get_all_entries()

但当我这么做的时候

txHashPending = w3.eth.filter({'fromBlock':'pending','toBlock':'pending','address':'My BSC address'}).get_all_entries()

它返回已确认/验证的事务。

我怎么才能解决这个问题?谢谢。

EN

回答 2

Ethereum用户

发布于 2022-01-15 18:35:04

来自文档

在创建新的日志筛选器时,filter_params应该是一个具有以下键的字典。地址:字符串或字符串列表,每个字符串有20个字节(可选)契约地址,或者日志应该来自的地址列表。

你试过address而不是from吗?

票数 1
EN

Ethereum用户

发布于 2023-05-14 20:46:13

您试图在那里使用的筛选方法实际上是对事件日志进行筛选;这就是为什么要返回已经验证过的信息。

您可以了解更多关于事件筛选器在这里的信息。

要获取有关挂起事务的信息,您需要检查每个事务散列,标识to字段并将其与所需的地址匹配。

更有效的方法是使用WebSocket和订阅方法;这将不断地向您发送一个挂起的事务流,您可以检查。

下面是一个这样做的示例;请注意,您可能需要安装WebSocket模块:

注意,这个脚本使用web3.py V6,您需要一个WebSocket和HTTP端点。WSS为连续流的挂起事务,HTTP获取详细信息。

此脚本执行以下操作:

  • 使用Websocket和HTTP端点连接到BNB节点
  • 调用eth_subscribe方法并使用WebSocket接收挂起的事务的连续流。
  • 在HTTP端点上使用get_transaction检查事务散列。
  • to字段与添加的地址匹配。
  • 如果有匹配,则打印数据。
代码语言:javascript
复制
# Import required libraries
import asyncio
import os
import json
import websockets
from web3 import Web3  
from web3.exceptions import TransactionNotFound

# Replace with your own Ethereum node WebSocket URL
eth_node_ws_url = 'YOUR_WSS_ENDPOINT'
web3 = Web3(Web3.HTTPProvider('YOUR_HTTPS_ENDPOINT')) 

# The address you are interested in
target_address = '0x13f4EA83D0bd40E75C8222255bc855a974568Dd4' # Pancake router V3

async def subscribe_to_pending_transactions(ws_url):
    # Continuously try to connect and subscribe
    while True:
        try:
            # Establish a WebSocket connection to the Ethereum node
            async with websockets.connect(ws_url) as websocket:
                # Send a subscription request for new pending transactions
                await websocket.send(json.dumps({
                    "id": 1,
                    "method": "eth_subscribe",
                    "params": [
                        "newPendingTransactions"
                    ],
                    "jsonrpc": "2.0"
                }))

                # Wait for the subscription response and print it
                subscription_response = await websocket.recv()
                print(f'Subscription response: {subscription_response}')

                # Continuously process incoming transaction hashes
                while True:
                    # Receive a transaction hash and parse it as JSON
                    tx_hash = await websocket.recv()
                    tx_hash_data = json.loads(tx_hash)

                    # Print the transaction hash data
                    # Extract the transaction hash
                    tx_hash = tx_hash_data.get('params', {}).get('result')
                    print(f'New pending transaction hash: {tx_hash}')

                    # Try to get tx details
                    try:
                        tx = web3.eth.get_transaction(tx_hash)
                        # Check if the transaction is to the target address
                        if tx['to'] == target_address:
                            print(f'Transaction to {target_address}')
                            print(tx)
                    except TransactionNotFound:
                        print(f'Transaction {tx_hash} not found')

        # If there's an exception (e.g., connection error), attempt to reconnect
        except Exception as e:
            print(f'Error: {e}')
            print('Reconnecting...')
            await asyncio.sleep(5)

# Execute the subscription function
asyncio.run(subscribe_to_pending_transactions(eth_node_ws_url))

只需将您想要签入的地址放在target_address = '0x13f4EA83D0bd40E75C8222255bc855a974568Dd4'中--在本例中,我只是使用PancakeSwap V3路由器;基本上,这个脚本检查到路由器上的所有挂起的事务并打印详细信息。从这里,您可以使用数据进一步分析。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/119003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档