英文:
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 := <-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 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 := <-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())
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论