package
0.1.3
Repository: https://github.com/umbracle/ethgo.git
Documentation: pkg.go.dev

# README

Tracker

package main

import (
	"context"
	"encoding/binary"
	"flag"
	"fmt"
	"os"
	"os/signal"
	"syscall"

	"github.com/umbracle/ethgo"
	"github.com/umbracle/ethgo/abi"
	"github.com/umbracle/ethgo/jsonrpc"
	"github.com/umbracle/ethgo/tracker"

	boltdbStore "github.com/umbracle/ethgo/tracker/store/boltdb"
)

var depositEvent = abi.MustNewEvent(`DepositEvent(
	bytes pubkey,
	bytes whitdrawalcred,
	bytes amount,
	bytes signature,
	bytes index
)`)

func main() {
	var endpoint string
	var target string

	flag.StringVar(&endpoint, "endpoint", "", "")
	flag.StringVar(&target, "target", "", "")

	flag.Parse()

	provider, err := jsonrpc.NewClient(endpoint)
	if err != nil {
		fmt.Printf("[ERR]: %v", err)
		os.Exit(1)
	}

	store, err := boltdbStore.New("deposit.db")
	if err != nil {
		fmt.Printf("[ERR]: failted to start store %v", err)
		os.Exit(1)
	}

	tt, err := tracker.NewTracker(provider.Eth(),
		tracker.WithBatchSize(20000),
		tracker.WithStore(store),
		tracker.WithEtherscan(os.Getenv("ETHERSCAN_APIKEY")),
		tracker.WithFilter(&tracker.FilterConfig{
			Async: true,
			Address: []ethgo.Address{
				ethgo.HexToAddress(target),
			},
		}),
	)
	if err != nil {
		fmt.Printf("[ERR]: failed to create the tracker %v", err)
		os.Exit(1)
	}

	lastBlock, err := tt.GetLastBlock()
	if err != nil {
		fmt.Printf("[ERR]: failed to get last block %v", err)
		os.Exit(1)
	}
	if lastBlock != nil {
		fmt.Printf("Last block processed: %d\n", lastBlock.Number)
	}

	ctx, cancelFn := context.WithCancel(context.Background())
	go func() {
		go func() {
			if err := tt.Sync(ctx); err != nil {
				fmt.Printf("[ERR]: %v", err)
			}
		}()

		go func() {
			for {
				select {
				case evnt := <-tt.EventCh:
					for _, log := range evnt.Added {
						if depositEvent.Match(log) {
							vals, err := depositEvent.ParseLog(log)
							if err != nil {
								panic(err)
							}

							index := binary.LittleEndian.Uint64(vals["index"].([]byte))
							amount := binary.LittleEndian.Uint64(vals["amount"].([]byte))

							fmt.Printf("Deposit: Block %d Index %d Amount %d\n", log.BlockNumber, index, amount)
						}
					}
				case <-tt.DoneCh:
					fmt.Println("historical sync done")
				}
			}
		}()

	}()

	handleSignals(cancelFn)
}

func handleSignals(cancelFn context.CancelFunc) int {
	signalCh := make(chan os.Signal, 4)
	signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)

	<-signalCh

	gracefulCh := make(chan struct{})
	go func() {
		cancelFn()
		close(gracefulCh)
	}()

	select {
	case <-signalCh:
		return 1
	case <-gracefulCh:
		return 0
	}
}

You can query the ETH2.0 Deposit contract like so:

go run main.go --endpoint https://mainnet.infura.io/v3/... --target 0x00000000219ab540356cbb839cbe05303d7705fa

# Packages

No description provided by the author

# Functions

DefaultConfig returns the default tracker config.
NewTracker creates a new tracker.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

EventAdd happens when a new event is included in the chain.
EventDel may happen when there is a reorg and a past event is deleted.

# Structs

BlockEvent is an event emitted when a new block is included.
Config is the configuration of the tracker.
Event is an event emitted when a new log is included.
FilterConfig is a tracker filter configuration.
Tracker is a contract event tracker.

# Interfaces

Provider are the eth1x methods required by the tracker.

# Type aliases

No description provided by the author
EventType is the type of the event.