0

I'm going to run a contract in Ethereum, Binance, Polygon, and 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?

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.

1 Answers1

0

When you query the raw blockchain data, each event log contains logIndex, which is the position of the event within the block.

Example with 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 (e.g. Etherscan) can chose to order their results according to the logIndex but they can also chose different ordering.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • Wow! You saved my life. One more thing I'm curious about is that all the events received in web3.js above are about committed transactions? –  Dec 12 '22 at 14:07
  • 1
    @Primrose Transactions are executed by a miner/validator when producing a new block. And event logs are emitted when the transaction is executed ... Which means that transactions that are not yet included in a block, do not have event logs (yet). – Petr Hejda Dec 12 '22 at 14:14