2

I am following the guide here but I am not able to replicate what is being done in the guide.

var transferEvent LogTransfer

err := contractAbi.Unpack(&transferEvent, "Transfer", vLog.Data)
if err != nil {
  log.Fatal(err)
}

First of all the Unpack function only takes two parameters, and using UnpackIntoInterface doesn't work for me neither.

I use the following code to try and feed the raw bytes into my struct using this Tx:

contractAbi, err := abi.JSON(strings.NewReader(string(uniswap.UniswapABI)))
            if err != nil {
                log.Fatal(err)
            }

            fmt.Println(txReceiptLogs[i].Data)

            eLog, err := contractAbi.Unpack("Transfer", txReceiptLogs[i].Data)
            if err != nil {
                log.Fatal("Error unpacking into interface: %v", err)
            }

            fmt.Println(eLog)

This actually works for me and prints out:

[491956807]
[301904282124331099]
[491956807]
[1007639403678612]

Which is correct. However, when I try to do the same for the Uniswap Sync event which has the following hash:

"0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1"

I get an empty array, or more precisely a struct filled with two empty values.

{<nil> <nil>}

What I would want to know is how to get the unpack function also working for the Uniswap Sync event in the logs (I use this ABI). And if an updated example could be provided for the Unpack function in general that would be very nice as well. Thanks!

1 Answer 1

1

You can use the UnpackIntoInterface() method which takes three parameters

event := struct {
    Key   [32]byte
    Value [32]byte
}{}
_ = contractAbi.UnpackIntoInterface(&event, "ItemSet", vLog.Data)
fmt.Println(string(event.Key[:]))   // foo
fmt.Println(string(event.Value[:])) // bar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.