0

I'm building a tool that requires listening to functions that are triggered by the developers of smart contracts. I'd like to know if on web3 or ethersjs there is a way to listen to a specific method on the smart contract being invoked. I was going to use the contract.on but that seems to only work on contract events, and I need it to listen to contract functions.

Any help is appreciated, thanks!

New contributor
Adam is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
8
  • another way is to look at first 4 bytes of tx input data, thats the signature of the function call. Oct 13 at 14:30
  • Make that function trigger an event . Or have a generic event that take name of the function as param
    – Majd TL
    Oct 13 at 14:39
  • @AndriyLysak this might actually help me achieve my goal, do you know how I can convert those 4 bytes into a string value to compare to a method name? for example, if the method name is sellTokens, how can I convert the initial 4 bytes of input data to display that string?
    – Adam
    Oct 13 at 15:07
  • @Adam ok first some theory, hash NEVER converts back to original input, You can make a burger out of a cow, but cant make a cow out of a burger. That being said: you have 2 options, convert the function name to the bytes yourself, its a keccak256 hash OR go to a block explorer like etherscan.io, find a TX that you know has that function call and just copy the first 4 bytes. Also relevant: 4byte.directory Oct 13 at 18:17
  • @Adam another wrinkle: if you arent 100% sure that other contracts wont delegate to this contract, you will need to keep an eye on INTERNAL transactions as well, same concept, look at first 4 bytes of input data. Oct 13 at 18:30

Your Answer

Adam is a new contributor. Be nice, and check out our Code of Conduct.

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

Browse other questions tagged or ask your own question.