I want to know how does the openzeppelin defender or any other similar product listen to function calls of smart contracts. Listening to events is a fairly simple task with the help of a library like ethers.js. But How should one listen for a function call in the blockchain? Does it require something like examining blocks for relevant function signatures?
Hey @Apr_dev! For networks where the tracing API is available (eg mainnet), Sentinels fetch the trace of every transaction of every block, and inspect all calls from one contract to another, using the monitored function signatures to match on the calldata.
Where the trace API is not available, we do the same with the
data of every tx in every block, which is only able to capture direct calls to the monitored contracts.
Great, So it seems like alchemy provides access to those trace API methods. So I think I can use that for monitoring mainnet. Can you say, how should I go about if I'm trying to examine data of every tx in every block?
To examine data of every transaction in every block, you should do the following:
- Make use of either web3.js/ethers.js to fetch the latest block.
- Once the latest block is fetched, loop over the transactions array
- For each transaction filter out those having 'to' address of the contract whose method/function you are trying to get latest details of.
- Once above is filtered out use the method signature on the transaction data.
This would help you achieve what you are trying to do