将函数调用记录为事件
通过note
修饰符提供通用函数调用日志记录,该修饰符触发数据捕获作为LogNote
事件包含:
msg.sig
(索引)msg.sender
(索引)- 第一个函数参数(索引)
- 第二个函数参数(索引)
msg.value
msg.data
使用修饰符修饰的函数note
将在使用区块链客户端可查询的索引字段调用时记录此信息。这涵盖了事件的大多数用例,使它成为一种快速将事件日志记录添加到 dapp 的简单方法。
/// note.sol -- the `note' modifier, for logging calls as events
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
pragma solidity >=0.4.23;
contract DSNote {
event LogNote(
bytes4 indexed sig,
address indexed guy,
bytes32 indexed foo,
bytes32 indexed bar,
uint256 wad,
bytes fax
) anonymous;
modifier note {
bytes32 foo;
bytes32 bar;
uint256 wad;
assembly {
foo := calldataload(4)
bar := calldataload(36)
wad := callvalue()
}
_;
emit LogNote(msg.sig, msg.sender, foo, bar, wad, msg.data);
}
}