繁体   English   中英

如何监控智能合约 function 何时被调用? 最好使用 ethers.js 或 web3.js

[英]How can I monitor when a smart contract function gets called? Preferably using ethers.js or web3.js

我有几个例子,我想监控智能合约 function 调用,但我不完全确定如何 go 这样做。 我已经阅读了 ethers.js 和 web3.js 文档,但还没有弄清楚任何事情。

例如,Etherscan 为与 function 调用对应的每个事务显示诸如“转移”、“批准”、“铸币”等方法。 如何在调用某个合约时监控特定合约?

你应该写一个事件。 例如:

event Transfer(
        address indexed _from,
        address indexed _to,
        uint _value
    );

然后在传输 function 调用它:

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;
    }

有关更多信息,请阅读什么是 Solidity 事件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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