Skip to content
This repository was archived by the owner on Nov 19, 2022. It is now read-only.
This repository was archived by the owner on Nov 19, 2022. It is now read-only.

subscribe event logs code is outdated #64

@JoyCood

Description

@JoyCood

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);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions