gogo-ethereum

Event data from Smart Contract parsing issue with go-ethereum


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.


Solution

  • 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())
            }
        }