Reputation: 56
event_filter = w3.eth.filter({
'address': contract_address,
'fromBlock':4916476,
'toBlock':4916576
# 'topics':[topic]
})
for event in event_filter.get_all_entries():
print(event)
I deployed a contract and wanted to scan the events using w3. The results returned are a bunch of 'attributesDict's containing 'address, topics, ..' etc. the problem is that the information is stored in 'data' which looks like this:
'data
': '0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000046164616d00000000000000000000000000000000000000000000000000000000
',
what do these 'data's from blockchain event contain? How should I decode this so I can have informations I want?
I'm using web3 5.22.0 btw
Upvotes: 0
Views: 287
Reputation: 56
Problem solved. I would like to share a few things I found and hopefully it can help the others.
Basically, the 'data' in the attributeDict that was retrieved by w3.eth.get_new/all_entries, is the information stored in the 'event' in the smart contract on block precisely. for example, I wrote this sample contract
struct UserInfo{
address userAddr;
uint userId;
}
UserInfo[] public userInfo;
event setUserEvent(address addrs, uint ids);
function setUser(address addr_, uint id_) public {
userInfo.push(UserInfo(addr_, id_));
emit setUserEvent(addr_, id_);
}
SetUser function will keep note of the address and id in the event 'setUserEvent'. And if I call the function properly, the 'data' retrieved would look like this:
'0x00000000000000000000000057384071e06f31aaaa039da92907a0000017691d20000000000000000000000000000000000000000000000000000000000000001'
This hex byte has 2 parts(for each part has 64 hexbytes, so in total i have 128 here) contains 2 piece of information(the address and the Id I stored in by calling setUser function). Obviously the address is
‘0x00000000000000000000000057384071e06f31aaaa039da92907a0000017691d2’
which is the first 64 hexbytes(the 0x don't count)
and The Id is actually
'0000000000000000000000000000000000000000000000000000000000000001'
one. which is the second part of the 'data', 64 hexbytes.
All I need to do now is remove the extra 0s in the address, give back the 0x at the beginning and turn the ID number into a regular number.
Btw, if you encountered some weird situation, like the data contains an extra '64' and some more info, like a single number, which is not a number you stored in event, is probably because it doesn't really support you to store characters in event, like userNames, etc. (string). It can be scanned, though, would look weird when you try to decode it. That single number should match the amount of characters of the string you stored in the event.
Upvotes: 2