miguelmota / ethereum-development-with-go-book

📖 A little guide book on Ethereum Development with Go (golang)
https://goethereumbook.org
Other
1.74k stars 427 forks source link

subscribe event logs code is outdated #64

Open JoyCood opened 3 years ago

JoyCood commented 3 years ago

when i follow this tutorial, and this code will generate an error when i miss Topics parameter:

ethereum.FilterQuery{
        Topics:    [][]common.Hash{}, //comment this line and an error will occur
        Addresses: []common.Address{i.contract},
    }

Errors encountered in param 1: Invalid value null supplied to : (RpcFilterRequest | undefined)/0: RpcFilterRequest/topics: (Array<(null | HASH | Array<(null | HASH)>)> | undefined)/0: Array<(null | HASH | Array<(null | HASH)>)>, Invalid value null supplied to : (RpcFilterRequest | undefined)/0: RpcFilterRequest/topics: (Array<(null | HASH | Array<(null | HASH)>)> | undefined)/1: undefined, Invalid value {"address":["0xa82ff9afd8f496c3d6ac40e2a0f282e47488cfc9"],"fromBlock":"0x0","toBlock":"latest","topics":null} supplied to : (RpcFilterRequest | undefined)/1: undefined

and when i test the code when i add Topics parameter in ethereum.FilterQuery{}, it still can‘t print any event logs here, below is my code:

package contracts

import (
    "context"
    "fmt"
    _ "fmt"
    //  "math/big"

    "github.com/ethereum/go-ethereum"
    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/golang/glog"
    "github.com/spf13/viper"
)

type ItemsObserver struct {
    client   *ethclient.Client
    contract common.Address
}

func NewItemsObserver(network, contract string) ItemsObserver {
    fmt.Printf("network: %s, contract: %s\n", network, contract)
    client, err := ethclient.Dial(network)
    if err != nil {
        glog.Fatalf("Fatal error ethclient, err: %s \n", err)
    }
    return ItemsObserver{
        client:   client,
        contract: common.HexToAddress(contract),
    }
}

func (i ItemsObserver) Start() {
    if i.client == nil {
        network := viper.GetString("network.localhost")
        fmt.Println("network:", network)
        client, err := ethclient.Dial(network)
        if err != nil {
            glog.Fatalf("Fatal error ")
        }
        i.client = client
    }
    if i.contract == common.HexToAddress("") {
        contract := viper.GetString("contracts.itemsDiamond")
        i.contract = common.HexToAddress(contract)
    }
    go i.run()
}

func (i ItemsObserver) run() {
    query := ethereum.FilterQuery{
        /*
            FromBlock: big.NewInt(0),
            ToBlock:   big.NewInt(1024),
            Topics:    [][]common.Hash{},
        */
        Addresses: []common.Address{i.contract},
    }
    logs := make(chan types.Log)
    defer close(logs)
    sub, err := i.client.SubscribeFilterLogs(context.Background(), query, logs)
    if err != nil {
        glog.Fatal(err)
    }
    for {
        select {
        case err := <-sub.Err():
            glog.Fatal(err)
        case vLog := <-logs:
            fmt.Println(vLog)
        }
    }
}

event.sol:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;

contract JustTest {
    event T(uint256 indexed x);

    function eventTest() external {
        emit T(2222);
    }
}
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.