once - 单次订阅合约事件
订阅合约指定的事件,并在第一次触发后或发生错误后立即取消订阅。一个事件仅触发一次。
调用:
myContract.once(event[, options], callback)
参数:
event
- String: 要订阅的合约事件名,或者使用"allEvents" 订阅所有事件options
- Object: 可选,用于部署的选项,包含以下字段:filter
- Object : 可选,用于根据索引参数过滤事件。例如 {filter: {myNumber: [12,13]}} 表示“myNumber” 为12或13的所有事件topics
- Array : 可选,用于手动设置事件过滤器的主题。如果制订了filter属性和事件签名,将不会自动设置(topic[0])
- callback - Function: 该回调函数触发时,其第一个参数为错误对象,第二个参数为事件对象
返回值:
未定义
示例代码:
myContract.once('MyEvent', {
filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23
fromBlock: 0
}, function(error, event){ console.log(event); });
// event output example
> {
returnValues: {
myIndexedParam: 20,
myOtherIndexedParam: '0x123456789...',
myNonIndexParam: 'My String'
},
raw: {
data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
topics: ['0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7', '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385']
},
event: 'MyEvent',
signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
logIndex: 0,
transactionIndex: 0,
transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
blockNumber: 1234,
address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
}