github.com/lmittmann/w3@v0.20.0/module/eth/subscribe.go (about)

     1  package eth
     2  
     3  import (
     4  	"github.com/ethereum/go-ethereum"
     5  	"github.com/ethereum/go-ethereum/core/types"
     6  	"github.com/lmittmann/w3/w3types"
     7  )
     8  
     9  // NewHeads subscribes to notifications of updates to the blockchain head.
    10  func NewHeads(ch chan<- *types.Header) w3types.RPCSubscriber {
    11  	return &ethSubscription[*types.Header]{ch, []any{"newHeads"}, nil}
    12  }
    13  
    14  // PendingTransactions subscribes to notifications about new pending transactions in the transaction pool.
    15  func PendingTransactions(ch chan<- *types.Transaction) w3types.RPCSubscriber {
    16  	return &ethSubscription[*types.Transaction]{ch, []any{"newPendingTransactions", true}, nil}
    17  }
    18  
    19  // NewLogs subscribes to notifications about logs that match the given filter query.
    20  func NewLogs(ch chan<- *types.Log, q ethereum.FilterQuery) w3types.RPCSubscriber {
    21  	arg, err := toFilterArg(q)
    22  	return &ethSubscription[*types.Log]{ch, []any{"logs", arg}, err}
    23  }
    24  
    25  type ethSubscription[T any] struct {
    26  	ch     chan<- T
    27  	params []any
    28  	err    error
    29  }
    30  
    31  func (s *ethSubscription[T]) CreateRequest() (string, any, []any, error) {
    32  	return "eth", s.ch, s.params, s.err
    33  }