I am using Web3 1.0 to listen to events from a contract like this:
tokenContract.events.AddressList({
fromBlock: 6020474,
toBlock: 'latest'
},
(error, events) => {
console.log(events.returnValues[0])
}
);
I get back this:
{
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',
}
}
I want to push each returned
events.returnValues[0]
property (which is the field name '0:') to an array like this:
var addresses = []
addresses = events.returnValues[0]
addresses = objArray.map(events => a.returnValues)
//(Or maybe by iterating with a for loop somehow)
Then I want to access the individual addresses with something like
console.log(addresses[2])
Is there a better way to do this with Web3.js alone? All of the answers parsing JSON have not worked for me. I don't know where to begin.
Edit: It appears that Web3 returns not a JSON object containing all events, but individual objects. For example, Object.keys(AddressList).length returns:
1
1
1
1
1
And console.log(events.returnValues['0']) returns:
0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046
0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046
0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046
0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046
0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4
The best I've got so far was this:
[ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ]
[ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ]
[ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ]
[ '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046' ]
[ '0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4' ]
Which gets pushed only to the first entry of the array altogether. I want addresses[4] to return the address ending in 16F4, and addresses itself to return:
['0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', '0xd204E0f9c07768D91eA2f27Dc01d48DC485e5046', 0xE59B0539f3eC6a8b70D60fEf5FcEDFB2C07516F4]
Edit two. The following code results in a wonky and unexpected return:
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'
]