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

subscribe event logs code is outdated #64

Open
JoyCood opened this issue May 8, 2021 · 1 comment
Open

subscribe event logs code is outdated #64

JoyCood opened this issue May 8, 2021 · 1 comment
Labels
help wanted Extra attention is needed out-of-date-book-examples Book examples are out-of-date wontfix This will not be worked on

Comments

@JoyCood
Copy link

JoyCood commented May 8, 2021

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);
    }
}
@JoyCood JoyCood changed the title subscribe event logs code is outdate subscribe event logs code is outdated May 8, 2021
@miguelmota miguelmota added out-of-date-book-examples Book examples are out-of-date help wanted Extra attention is needed labels May 11, 2021
@stale
Copy link

stale bot commented Jun 10, 2022

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.

@stale stale bot added the wontfix This will not be worked on label Jun 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
help wanted Extra attention is needed out-of-date-book-examples Book examples are out-of-date wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants