listener

package
v1.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2024 License: MIT Imports: 18 Imported by: 8

README

Listener

logo.png

sequenceDiagram
    participant User
    participant EthereumNode
    participant SQLiteDB
    participant Listener

    User->>Listener: Start Listener
    Listener->>SQLiteDB: Connect and Migrate Models
    Listener->>EthereumNode: Connect to Ethereum Node
    Listener->>EthereumNode: Retrieve Latest Block
    Listener->>Listener: Listen for Logs
    Listener->>User: Process Log

Using Listener DB

There is a simple example here.

Migrating to ListenerDB

This guide outlines the steps to migrate your service to use the new ListenerDB interface. Follow these steps for each service that needs to be updated.

1. Remove Deprecated Methods

Delete any deprecated methods that are no longer needed with ListenerDB.

2. Update Interface Definitions

Add the ListenerDB interface to your Service interface:

type Service interface {
    Reader
    Writer
    SubmitterDB() submitterDB.Service
    listenerDB.ChainListenerDB
}

3. Update GetAllModels Function

Modify the GetAllModels function to include ListenerDB models:

func GetAllModels() (allModels []interface{}) {
    allModels = append(txdb.GetAllModels(), &YourModel{})
    allModels = append(allModels, listenerDB.GetAllModels()...)
    return allModels
}

4. Implement ListenerDB in Store

Update your Store struct and NewStore function:

type Store struct {
    db             *gorm.DB
    submitterStore submitterDB.Service
    listenerDB.ChainListenerDB
}

func NewStore(db *gorm.DB, metrics metrics.Handler) *Store {
    txDB := txdb.NewTXStore(db, metrics)
    listener := listenerDB.NewChainListenerStore(db, metrics)

    return &Store{
        db:              db,
        submitterStore:  txDB,
        ChainListenerDB: listener,
    }
}

6. Refactor Usage

Replace all calls to the removed methods with the appropriate methods from the ChainListenerDB interface.

7. Testing

After making these changes, thoroughly test your service to ensure all functionality works as expected with the new ListenerDB implementation.

By following these steps, you should successfully migrate your service to use the new ListenerDB interface. Remember to replace YourModel with the actual model name in your implementation. If you encounter any issues or need further clarification, please refer to the ListenerDB documentation or consult with your team.

Documentation

Overview

Package listener is the listener used to pull in chain events

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoLatestBlockForChainID is returned when no block exists for the chain.
	ErrNoLatestBlockForChainID = listenerDB.ErrNoLatestBlockForChainID
)

Functions

This section is empty.

Types

type ContractListener

type ContractListener interface {
	// Listen starts the listener and call HandleLog for each event
	Listen(ctx context.Context, handler HandleLog) error
	// LatestBlock gets the last recorded latest block from the rpc.
	// this is NOT last indexed. It is provided as a helper for checking confirmation count
	LatestBlock() uint64
	// Address gets the address of the contract this listener is listening to
	Address() common.Address
}

ContractListener listens for chain events and calls HandleLog.

func NewChainListener

func NewChainListener(omnirpcClient client.EVM, store listenerDB.ChainListenerDB, address common.Address, initialBlock uint64, handler metrics.Handler, options ...Option) (ContractListener, error)

NewChainListener creates a new chain listener.

type HandleLog

type HandleLog func(ctx context.Context, log types.Log) error

HandleLog is the handler for a log event in the event this errors, the range will be reparsed.

type NewBlockHandler added in v0.11.6

type NewBlockHandler func(ctx context.Context, block uint64) error

NewBlockHandler is a function that is called when a new block is detected.

type Option added in v0.11.6

type Option func(*chainListener)

Option is a functional option for chainListener.

func WithBlockWait added in v0.14.2

func WithBlockWait(wait uint64) Option

WithBlockWait sets the block wait.

func WithFinalityMode added in v0.14.2

func WithFinalityMode(mode string) Option

WithFinalityMode sets the finality mode.

func WithName added in v1.3.0

func WithName(name string) Option

WithName sets the listener name.

func WithNewBlockHandler added in v0.11.6

func WithNewBlockHandler(handler NewBlockHandler) Option

WithNewBlockHandler sets the new block handler.

func WithPollInterval added in v0.11.7

func WithPollInterval(interval time.Duration) Option

WithPollInterval sets the poll interval.

Directories

Path Synopsis
Package db provides the database layer for the chain listener.
Package db provides the database layer for the chain listener.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL