智能合约解析问题的事件数据与go-ethereum有关。

huangapple go评论110阅读模式
英文:

Event data from Smart Contract parsing issue with go-ethereum

问题

我无法通过使用types.Log通道获取SC事件发出的完整数据。有没有办法让我获取事件发出的所有数据?

我正在尝试解析的事件是:

  1. PairCreated(address indexed,address indexed,address,uint)

我的代码:

  1. for {
  2. select {
  3. case err := <-sub.Err():
  4. log.Fatal(err)
  5. case vLog := <-logs:
  6. fmt.Printf("Log Block Number: %d\n", vLog.BlockNumber)
  7. fmt.Printf("Log Index: %d\n", vLog.Index)
  8. event := make(map[string]interface{})
  9. err := contractAbi.UnpackIntoMap(event, "PairCreated", vLog.Data)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(event)
  14. }
  15. }

我只能解析事件的最后两个参数。

英文:

I am unable to get the full data emitted from SC event by using types.Log channel. Is there any way so that I can have all the data from an event emitted?

The event I'm trying to parse:

  1. PairCreated(address indexed,address indexed,address,uint)

My code:

  1. for {
  2. select {
  3. case err := &lt;-sub.Err():
  4. log.Fatal(err)
  5. case vLog := &lt;-logs:
  6. fmt.Printf(&quot;Log Block Number: %d\n&quot;, vLog.BlockNumber)
  7. fmt.Printf(&quot;Log Index: %d\n&quot;, vLog.Index)
  8. event := make(map[string]interface{})
  9. err := contractAbi.UnpackIntoMap(event, &quot;PairCreated&quot;, vLog.Data)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(event)
  14. }
  15. }

I could only parse the last two arguments of the event.

答案1

得分: 0

我理解了这里的问题。
如果一个参数被声明为indexed,那么该参数会被放在Topics而不是Data中。而且最多只能有3个主题。所以,我尝试解包主题,但失败了。并且通过以下方式成功了:

  1. token1 := common.HexToAddress(vLog.Topics[1].Hex())
  2. token2 := common.HexToAddress(vLog.Topics[2].Hex())

而配对信息在Data中。

所以,最终的代码是:

  1. for {
  2. select {
  3. case err := <-sub.Err():
  4. log.Fatal(err)
  5. case vLog := <-logs:
  6. fmt.Printf("Log Block Number: %d\n", vLog.BlockNumber)
  7. fmt.Printf("Log Index: %d\n", vLog.Index)
  8. event := make(map[string]interface{})
  9. err := contractAbi.UnpackIntoMap(event, "PairCreated", vLog.Data)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(event)
  14. token1 := common.HexToAddress(vLog.Topics[1].Hex())
  15. token2 := common.HexToAddress(vLog.Topics[2].Hex())
  16. }
  17. }
英文:

I understood what was wrong here.
If an argument is declared as indexed that argument goes to Topics instead of Data. And there can be at most 3 topics. So, I tried to unpack the topics but failed. And succeeded with the following way:

  1. token1 := common.HexToAddress(vLog.Topics[1].Hex())
  2. token2 := common.HexToAddress(vLog.Topics[2].Hex())

And pair was in Data

So, the final code is:

  1. for {
  2. select {
  3. case err := &lt;-sub.Err():
  4. log.Fatal(err)
  5. case vLog := &lt;-logs:
  6. fmt.Printf(&quot;Log Block Number: %d\n&quot;, vLog.BlockNumber)
  7. fmt.Printf(&quot;Log Index: %d\n&quot;, vLog.Index)
  8. event := make(map[string]interface{})
  9. err := contractAbi.UnpackIntoMap(event, &quot;PairCreated&quot;, vLog.Data)
  10. if err != nil {
  11. log.Fatal(err)
  12. }
  13. fmt.Println(event)
  14. token1 := common.HexToAddress(vLog.Topics[1].Hex())
  15. token2 := common.HexToAddress(vLog.Topics[2].Hex())
  16. }
  17. }

huangapple
  • 本文由 发表于 2023年1月21日 20:51:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75193452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定