'How can I monitor when a smart contract function gets called? Preferably using ethers.js or web3.js
I have a few instances where I want to monitor smart contract function calls, but I'm not totally sure how to go about doing that. I've read through the ethers.js and web3.js docs but haven't figured anything out yet.
For example, Etherscan shows methods such as 'transfer', 'approve', 'mint', etc for every transaction that corresponds to a function call. How can I monitor a specific contract for when a certain contract is called?
Solution 1:[1]
You should write an event. For example:
event Transfer(
address indexed _from,
address indexed _to,
uint _value
);
then in transfer function call it:
function transfer(address _to, uint _value) public returns (bool success){
// run some logic
// if you reached here, means everyting went ok
emit Transfer(msg.sender,_to,_value);
return true;
}
For more, read What are Solidity Events
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yilmaz |
