0

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'
]

1 Answer 1

1

Try with this events.returnValues['0'].

Or did you try something like this:

let myArr = []
tokenContract.events.AddressList({
        fromBlock: 6020474,
        toBlock: 'latest'
    },
    (error, events) => { 
        myArr.push(events.returnValues['0'])
     }
);

Or did you try this tokenContract.events.getPastEvents(event[, options][, callback])

5
  • Adding quotes doesn't seem to change anything. :/ Still the same result of Web3 returning individual JSON objects I can parse into just the addresses, but can't yet put into an array. I tried to concatenate each JSON object returned as an array, but I can't figure out how to iterate through them as I don't know the length since they are objects, not arrays. Commented Jul 21, 2019 at 22:59
  • 1
    I edited my post. Did you try tokenContract.events.getPastEvents(event[, options][, callback])
    – n1c01a5
    Commented Jul 21, 2019 at 23:10
  • Yes, that was the first thing I tried before failing at mapping, and then iterating strategies. I edited the OP at the bottom to show the quite unexpected result. Additionally, addresses[4] returns undefined 4 times, then the correct address. I'm delirious now from trying this. Time to sleep. Will edit and update fresh. I think maybe can regex my way out of this, but I still can't figure out why Web3 returns things the way it does. Commented Jul 21, 2019 at 23:20
  • Fresh headed, and no closer. :/ I tried events.returnValues.src) instead of [0]&['0'], but that makes no difference. I am just absolutely stuck until I can put these values to an array. I'm shocked that nobody has done this before. I'm either really missing something simple, or people aren't diverse enough in what projects they are doing. I am going to have to maybe modify the way Web3 works. I wasn't hoping to get that deep. Commented Jul 22, 2019 at 9:40
  • I'm accepting your original response as the answer. It is in fact technically correct, but not the whole answer, which I hope someone else will provide someday, or be solved in Web3 2.0. The problem is that the callback/promise to retrieve the events has undocumented behavior causing drama. The workaround is to use the answer you provided, and to check the array for changes in a function outside the returned callback (also setting it to another variable). There is no parsing error or error in pushing. The array actually is created correctly. Commented Jul 22, 2019 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.