简体   繁体   English

有没有办法获得合同的特定事件?

[英]Is there a way to get a specific event for a contract?

I'm going to run a contract in Ethereum, Binance, Polygon, and Clayton.我将在 Ethereum、Binance、Polygon 和 Clayton 中运行合约。

Is there a best practice for polling events that occur in a contract?是否有针对合约中发生的轮询事件的最佳实践?

I want to get the event by requesting, not by subscribing to it.我想通过请求而不是订阅来获取事件。

If we use EtherScan api, are all sequences decisively guaranteed?如果我们用EtherScan api,是不是所有的序列都果断保证了?

The goal is to send requests periodically and store all the specific events that occurred in the contract in db.目标是定期发送请求并将合约中发生的所有特定事件存储在数据库中。

I'd appreciate your help.我很感激你的帮助。

When you query the raw blockchain data, each event log contains logIndex , which is the position of the event within the block.当您查询原始区块链数据时,每个事件日志都包含logIndex ,即块内事件的 position。

Example with web3js:使用 web3js 的示例:

const pastEventLogs = await web3.eth.getPastLogs({
    fromBlock: 16168375,
    toBlock: 16168375,
    address: "0xdAC17F958D2ee523a2206206994597C13D831ec7", // USDT on Ethereum
    topics: [
        web3.utils.keccak256("Transfer(address,address,uint256)")
    ]
});

returns回报

...
    address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
    blockHash: '0x026c2f62c8ad51b1e28c5fe6885c6025b2c4145aef08b65feb10b6e20dd2c0bc',
    blockNumber: 16168375,
    data: '0x0000000000000000000000000000000000000000000000000000000001618bd0',
    logIndex: 732,
...

Authors of 3rd party APIs (eg Etherscan) can chose to order their results according to the logIndex but they can also chose different ordering.第 3 方 API(例如 Etherscan)的作者可以选择根据logIndex对结果进行排序,但他们也可以选择不同的排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM