如何过滤以太坊地址

发布于 2025-01-12 18:57:06 字数 591 浏览 2 评论 0原文

我尝试编写一个脚本来获取我的以太坊地址的交易。 这个脚本在监控合约地址时有效,但如果我想监控我自己的地址或非合约地址,它不会收到任何交易

def handle_event(event):
    print(event)
    print(Web3.toJSON(event))    

def log_loop(event_filter, poll_interval):
    while True:
        for event in event_filter.get_new_entries():
            handle_event(event)
        time.sleep(poll_interval)

def main():
    event_filter = web3.eth.filter({"address": trackAddress})
    #get_block = web3.eth.get_block('latest')
    #block_filter = web3.eth.filter('latest')
    log_loop(event_filter, 2)

if __name__ == '__main__':
    main()

I try to write a script to get transactions of my ethereum address as they happen.
This script here works when monitoring a contractAddress, but if i want to monitor my own address or non contract address, it dont get any transactions

def handle_event(event):
    print(event)
    print(Web3.toJSON(event))    

def log_loop(event_filter, poll_interval):
    while True:
        for event in event_filter.get_new_entries():
            handle_event(event)
        time.sleep(poll_interval)

def main():
    event_filter = web3.eth.filter({"address": trackAddress})
    #get_block = web3.eth.get_block('latest')
    #block_filter = web3.eth.filter('latest')
    log_loop(event_filter, 2)

if __name__ == '__main__':
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故乡的云 2025-01-19 18:57:06

据我所知,使用filter命令只能监控合约上发生的事件。此选项不适用于您的情况。

我将编写我更熟悉的 JavaScript 代码,但您可以将其改编为 python。我可能会这样做:

async function checkBlock(address) {
    const block = await web3.eth.getBlock('latest');
    console.log(`Checking new block ${block.number}`);

    for (let txHash of block.transactions) {
        const tx = await web3.eth.getTransaction(txHash);
        if (address === tx.to.toLowerCase()) {
            console.log(`New transaction found. Block - ${block.number}`);
            console.log(`Transaction: ${tx}`);
        }
    }
}

您可以将此函数插入​​到 setInterval 中,并大约每隔 ~15 秒调用它。为什么是15秒?平均而言,每 15 秒就会出现一个新区块。

另一种选择是使用 ethers.js 库。您可以直接订阅区块并请求交易信息一个新的块。但我不知道python是否存在这样的库。

您还可以执行以下操作:

const topicSets = [
    utils.id("Transfer(address,address,uint256)"),
    null,
    [
        null,
        hexZeroPad(address, 32)
    ]
]
provider.on(topicSets, (log, event) => {
    // Emitted any token is sent TO your address
})

在这种情况下,每次您的地址收到 ERC-20 代币时都会触发处理程序。

As far as I know, using the filter command, you can only monitor events that occur on the contract. This option will not work in your case.

I'll write the JavaScript code as I'm more familiar with it, but you can adapt it to python. I would probably do something like this:

async function checkBlock(address) {
    const block = await web3.eth.getBlock('latest');
    console.log(`Checking new block ${block.number}`);

    for (let txHash of block.transactions) {
        const tx = await web3.eth.getTransaction(txHash);
        if (address === tx.to.toLowerCase()) {
            console.log(`New transaction found. Block - ${block.number}`);
            console.log(`Transaction: ${tx}`);
        }
    }
}

You can insert this function into setInterval and call it approximately every ~15 seconds. Why 15 seconds? On average, a new block appears just every 15 seconds.

Another option is using the ethers.js library. You can subscribe directly to blocks and request transaction information for a new block. But I don't know if such a library exists for python.

You can also do something like this with:

const topicSets = [
    utils.id("Transfer(address,address,uint256)"),
    null,
    [
        null,
        hexZeroPad(address, 32)
    ]
]
provider.on(topicSets, (log, event) => {
    // Emitted any token is sent TO your address
})

In this case, the handler will be triggered every time you receive ERC-20 tokens to your address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文