Events

如何從方法 getPastEvents 中獲取 returnValues?

  • March 16, 2022

// 這是我獲取事件資訊的地方。

const myEvent = myContract.getPastEvents('nftCreated', {fromBlock: 10230636})

// 那麼日誌是這樣的:

Promise {                                                                                                            
 <pending>,                                                                                                         
 [Symbol(async_id_symbol)]: 8614,                                                                                   
 [Symbol(trigger_async_id_symbol)]: 2656,                                                                           
 [Symbol(destroyed)]: { destroyed: false }                                                                          
}                                                                                                                    
> [                                                                                                                  
 {                                                                                                                  
   address: '0xF8C3b71BfE0bEEA2cF11F51C4B6370d96bD5c98d',                                                           
   blockHash: '0x239f98cab3616bd0e3218eed61a8c871c96d043f5594662364169680752e6b60',                                 
   blockNumber: 10230636,                                                                                           
   logIndex: 1,                                                                                                     
   removed: false,                                                                                                  
   transactionHash: '0x885a008946923b39b560d465614e119b668b5117e2acf47abe50d41cd286948f',                           
   transactionIndex: 0,                                                                                             
   id: 'log_185ce0cc',                                                                                              
   returnValues: Result {                                                                                           
     '0': 'xxxxxxxxxx',                                                             
     '1': '54366324124',                                                                                            
     '2': '0x3ac1b7722278da916f3996f09357ebdcb17ad8c54a9424b3826f8326c75a0582',                                     
     '3': '1645806587',                                                                                             
     owner: 'xxxxxxxxx',                                                           
     idToken: '54366324124',                                                                                        
     shaCode: '0x3ac1b7722278da916f3996f09357ebdcb17ad8c54a9424b3826f8326c75a0582',                                 
     timestamp: '1645806587'                                                                                        
   },                                                                                                               
   event: 'nftCreated',                                                                                             
   signature: 'xxxxxxxxxxxx',                                 
   raw: {                                                                                                           
     data: '0x000000000000000000000000e3bca80f9dfdc7f72e0e648cdb83c790b20ee45100000000000000000000000000000000000000
00000000000000000ca87c459c3ac1b7722278da916f3996f09357ebdcb17ad8c54a9424b3826f8326c75a0582000000000000000000000000000
00000000000000000000000000000621903fb',                                                                              
     topics: [Array]                                                                                                
   }                                                                                                                
 },                                                                                                                 

getPastEvents返回承諾,你必須像下面這樣使用

try {
 const nftCreatedEvents = await myContract.getPastEvents('nftCreated', {fromBlock: 10230636});

console.log(nftCreatedEvents.map(_ev => _ev.returnValues))
}catch(e) {
  console.log(e)
}

引用自:https://ethereum.stackexchange.com/questions/123015