Web3js
您如何解析 Web3 重新調整的 JSON 事件對象?
我正在使用 Web3 1.0 來監聽來自合約的事件,如下所示:
tokenContract.events.AddressList({ fromBlock: 6020474, toBlock: 'latest' }, (error, events) => { console.log(events.returnValues[0]) } );
我回來了:
{ transactionIndex: 12, id: 'log_0x18cb49c233bb0591f3b16eaa31c573a0171ce9d96390bd0639d741664d19f043', returnValues: { '0': '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '1': '0x6a57059920371e6819713c80a2c3E3E4fE21b7A6', '2': [BigNumber], src: '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', guy: '0x6a57059920371e6819713c80a2c3E3E4fE21b7A6', wad: [BigNumber] }, event: 'Approval', } ] [ { transactionIndex: 13, id: 'log_0xff56909901d0d7f2981eb2b51383a5f284dc9ffa3626876cc5a62c84dedf821b', returnValues: { '0': '0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4', '1': '0x6a57059920371e6819713c80a2c3E3E4fE21b7A6', '2': [BigNumber], src: '0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4', guy: '0x6a57059920371e6819713c80a2c3E3E4fE21b7A6', wad: [BigNumber] }, event: 'Approval', } }
我想將每個返回
events.returnValues[0]
的屬性(即欄位名稱'0:’)推送到這樣的數組:var addresses = [] addresses = events.returnValues[0] addresses = objArray.map(events => a.returnValues) //(Or maybe by iterating with a for loop somehow)
然後我想用類似的東西訪問各個地址
console.log(addresses[2])
單獨使用 Web3.js 有更好的方法嗎?解析 JSON 的所有答案都對我不起作用。我不知道從哪裡開始。
編輯:Web3 似乎返回的不是包含所有事件的 JSON 對象,而是單個對象。例如, Object.keys(AddressList).length 返回:
1 1 1 1 1
和 console.log(events.returnValues
$$ ‘0’ $$) 返回:
0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046 0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046 0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046 0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046 0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4
到目前為止,我得到的最好的是:
[ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ] [ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ] [ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ] [ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ] [ '0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4' ]
它只被推送到數組的第一個條目。我要地址
$$ 4 $$返回以 16F4 結尾的地址,並將地址本身返回:
['0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', 0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4]
編輯二。以下程式碼會導致異常且意外的返回:
let myArr = [] tokenContract.events.AddressList({ fromBlock: 6020474, toBlock: 'latest' }, (error, events) => { myArr.push(events.returnValues['0']) } ); [ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ] [ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ] [ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ] [ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ] [ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4' ]
試試這個
events.returnValues['0']
。或者你有沒有嘗試過這樣的事情:
let myArr = [] tokenContract.events.AddressList({ fromBlock: 6020474, toBlock: 'latest' }, (error, events) => { myArr.push(events.returnValues['0']) } );
或者你試過這個
tokenContract.events.getPastEvents(event[, options][, callback])