1

I am trying to subscribe to certain smart contract using go-ethereum. Here is the example of my function

client, err := ethclient.Dial(config.AppConfig.SmartcontractHost)
if err != nil {
    log.Fatal(err)
}

contractAddress := common.HexToAddress(config.AppConfig.SmartContractAddress)
query := ethereum.FilterQuery{
    Addresses: []common.Address{contractAddress},
    FromBlock: big.NewInt(11102868),
    ToBlock:   nil,
}

logs := make(chan types.Log)
sub, err := client.SubscribeFilterLogs(context.Background(), query, logs)
if err != nil {
    log.Fatal(err)
}

abiPath, _ := filepath.Abs(fmt.Sprintf("%v/Abi.abi", config.GetDataDirectory()))
fmt.Println(abiPath)
file, err := ioutil.ReadFile(abiPath)

if err != nil {
    fmt.Println("Failed to read file:", err)
}
abiFile, err := abi.JSON(strings.NewReader(string(file)))

go func() {
    for {
        select {
        case err := <-sub.Err():
            log.Fatal(err)
        case vLog := <-logs:
            fmt.Printf("Data %v\n", vLog.Data)
            _, err := abiFile.Unpack("Transfer", vLog.Data)
            if err != nil {
                log.Panic(err)
            }

        }
    }
}()

But unfortunately I get free vLog.Data when receiveing any kind of event. How can I solve this error?

To check the host and smart contract in general I have tried to do the same using web3.js and everything was ok.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.