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

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

Event data from Smart Contract parsing issue with go-ethereum

问题

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

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

PairCreated(address indexed,address indexed,address,uint)

我的代码:

for {
    select {
    case err := <-sub.Err():
        log.Fatal(err)
    case vLog := <-logs:
        fmt.Printf("Log Block Number: %d\n", vLog.BlockNumber)
        fmt.Printf("Log Index: %d\n", vLog.Index)

        event := make(map[string]interface{})
        err := contractAbi.UnpackIntoMap(event, "PairCreated", vLog.Data)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(event)
    }
}

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

英文:

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:

PairCreated(address indexed,address indexed,address,uint)

My code:

for {
		select {
		case err := &lt;-sub.Err():
			log.Fatal(err)
		case vLog := &lt;-logs:
			fmt.Printf(&quot;Log Block Number: %d\n&quot;, vLog.BlockNumber)
			fmt.Printf(&quot;Log Index: %d\n&quot;, vLog.Index)

			event := make(map[string]interface{})
			err := contractAbi.UnpackIntoMap(event, &quot;PairCreated&quot;, vLog.Data)
			if err != nil {
				log.Fatal(err)
			}
			fmt.Println(event)
		}
	}

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

答案1

得分: 0

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

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

而配对信息在Data中。

所以,最终的代码是:

for {
    select {
    case err := <-sub.Err():
        log.Fatal(err)
    case vLog := <-logs:
        fmt.Printf("Log Block Number: %d\n", vLog.BlockNumber)
        fmt.Printf("Log Index: %d\n", vLog.Index)

        event := make(map[string]interface{})
        err := contractAbi.UnpackIntoMap(event, "PairCreated", vLog.Data)
        if err != nil {
            log.Fatal(err)
        }
        fmt.Println(event)

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

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:

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

And pair was in Data

So, the final code is:

for {
        select {
        case err := &lt;-sub.Err():
            log.Fatal(err)
        case vLog := &lt;-logs:
            fmt.Printf(&quot;Log Block Number: %d\n&quot;, vLog.BlockNumber)
            fmt.Printf(&quot;Log Index: %d\n&quot;, vLog.Index)

            event := make(map[string]interface{})
            err := contractAbi.UnpackIntoMap(event, &quot;PairCreated&quot;, vLog.Data)
            if err != nil {
                log.Fatal(err)
            }
            fmt.Println(event)

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

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:

确定