1

Is there a way to watch current or filter past function calls with their names in web3 or ethers, just like we can view past events using getPastEvents in web3?

I want to fetch only the transactions where a certain function is called within past few blocks or set a watcher for that function if possible.

1
  • Well, not in a straighforward way. That is exactly the purpose of events =) Commented Feb 17, 2022 at 11:53

1 Answer 1

1

I think that is possible but it is the worst way and also limited. Therefore u must use events.

Anyway suppose you have a contract with an external method myMethod(uint256,string):

  • first you need to get the function selector (in etherjs is probably https://docs.ethers.io/v5/api/utils/abi/interface/#Interface--selectors)

    web3.eth.abi.encodeFunctionSignature('myMethod(uint256,string)')
    > '0x24ee0097'
    
  • then you need to iterate over all the Transactions, filter the transactions by (to == your contract)

  • check if their inputs start with 0x24ee0097

This will only works for external transaction (A wallet(EOA) call the function directly in your contract). It will not works for internal txs( another function or contract call your function)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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