5

With web3, it is possible to call a method of a smart contract at a specific block :

myContract.methods.methodName().call(transactionObject, blockNumber, callback)

Is there a way to call it after a specific tx in a specific block ?

Why do I want to do that ?

I would like to fetch the state of a specific variable. In the case multiple transactions interact with the same smart contract, I can only get the state of that specific variable at the last executed transaction of the block. However, I would like to be able to fetch the variation of this variable inside the block and in-between 2 transaction that are interacting with the same smart contract and changing the state.

I'm open to any idea, including without using web3

1

First let me say that I don't know if you can do it with Web3 (easily), but my guess would be no.

Personally I would use The Graph. You can use it to listen to specific events, functions calls or blocks, see handlers here. Inside the mappings you can further filter for any transaction data to identify the transactions you are looking for.

The whole process is a little bit more involved as it requires a setup with the smart contract address, ABI and subgraph generation. If you want to do this only one time, there might be a simpler way to do it. Otherwise I think the effort is worth it. On Ethereum mainnet it's particularly easy using the hosted service.

You can find more details about The Graph in the docs or in my tutorial.

1
  • 1
    Thanks ! After a bit of coding, I was able to fetch the state I wanted after each transactions using the graph :)
    – Xavier59
    Oct 25 '20 at 20:55
0

It should be possible with Web3 to listen for the creation of new blocks, then iterate into the recipients of transactions and search for the contract you're looking for. If found decode the bytecode to find the signature of the function you're looking for. It requires finding function signature beforehand but if it is for one contract, it is doable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.