Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Just a question on token event reading. #18

Closed
yueawang opened this issue Nov 20, 2018 · 2 comments
Closed

Just a question on token event reading. #18

yueawang opened this issue Nov 20, 2018 · 2 comments

Comments

@yueawang
Copy link

Hi, @miguelmota

I am reading this github as I need to do something similar on ethereum, and I have to say this is a wonderful project and helps me a lot. 馃憤
Here I have a question while reading "Reading ERC-20 Token Event Logs" section. There we have code as below:

  	err := contractAbi.Unpack(&transferEvent, "Transfer", vLog.Data)
  	if err != nil {
  		log.Fatal(err)
  	}
  	
  	transferEvent.From = common.HexToAddress(vLog.Topics[1].Hex()) // ?
  	transferEvent.To = common.HexToAddress(vLog.Topics[2].Hex())     // ?
  	
  	fmt.Printf("From: %s\n", transferEvent.From.Hex())
  	fmt.Printf("To: %s\n", transferEvent.To.Hex())
  	fmt.Printf("Tokens: %s\n", transferEvent.Tokens.String()

My question is as this line
err := contractAbi.Unpack(&transferEvent, "Transfer", vLog.Data)
already parsed/unpacked the data (from/to fields from message), why we still need to get Hex data from vLog.Topics as below code shows ?

  	transferEvent.From = common.HexToAddress(vLog.Topics[1].Hex())
  	transferEvent.To = common.HexToAddress(vLog.Topics[2].Hex())

Based on my understanding, Topics stores the message type Hash, we may not get the correct from/to by convert Topics[*].Hex to address, correct me if I am wrong. :)
Thanks!

@miguelmota
Copy link
Owner

miguelmota commented Nov 20, 2018

@yueawang Thanks! and sorry it's not clear; the topics are indexed event arguments.

There can only be a maximum of 4 topics, the first topic is the event log signature hash.
The other 3 are optional and specified by the contract creator by using the indexed keyword.

Unpack unpacks the log data. The log data contains event arguments that are not indexed.
The log event signature is: event Transfer(address indexed from, address indexed to, uint tokens); and as you can see from and to are indexed while tokens is not.
So from and to are topics[1] and topics[2] respectively

Hope this makes sense

@yueawang
Copy link
Author

Thanks @miguelmota , that's crystal clear! 馃憤

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants