0

i have a problem to get the Logs. Here is the Code:

query := ethereum.FilterQuery{
    Addresses: []common.Address{contractAddress},
}

ctx := context.Background()
    ethereumLogsCh := make(chan types.Log)
sub, err := ethClient.SubscribeFilterLogs(ctx, query, ethereumLogsCh)
if err != nil {
    log.Println("Subscribe:", err)
    return err
}

for {
    select {
    case err := <-sub.Err():
        log.Fatal(err)
    case ethLog := <-ethereumLogsCh:
        log.Println("Log:", ethLog)
    }
}

System information: Geth version: 1.8.0-unstable OS: Linux

Expected behaviour: I would expect to get a types.Log object when an event is triggered from the contract.

Actual behaviour: Im getting an Erro: cannot unmarshal array into Go value of type types.Log. What i'am doing wrong? Does the blockchain (in my case a local parity BC) send more than one log object?

Thx for the help.

0

Here's a full of example of how to subscribe to event logs using Go.

import (
    "context"
    "fmt"
    "log"

    "github.com/ethereum/go-ethereum"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/ethclient"
)

func main() {
    client, err := ethclient.Dial("wss://rinkeby.infura.io/ws")
    if err != nil {
        log.Fatal(err)
    }

    contractAddress := common.HexToAddress("0x147B8eb97fD247D06C4006D269c90C1908Fb5D54")
    query := ethereum.FilterQuery{
        Addresses: []common.Address{contractAddress},
    }

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

    for {
        select {
        case err := <-sub.Err():
            log.Fatal(err)
        case vLog := <-logs:
            fmt.Println(vLog) // pointer to event log
        }
    }
}

Your Answer

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

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