简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM