Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RPC Subscription Support to the Client #154

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,20 @@ func (c *Client) Call(calls ...w3types.RPCCaller) error {
return c.CallCtx(context.Background(), calls...)
}

// SubscribeCtx creates a new subscription and returns a [rpc.ClientSubscription].
func (c *Client) SubscribeCtx(ctx context.Context, s w3types.RPCSubscriber) (*rpc.ClientSubscription, error) {
namespace, ch, params, err := s.CreateRequest()
if err != nil {
return nil, err
}
return c.client.Subscribe(ctx, namespace, ch, params...)
}

// Subscribe is like [Client.SubscribeCtx] with ctx equal to context.Background().
func (c *Client) Subscribe(s w3types.RPCSubscriber) (*rpc.ClientSubscription, error) {
return c.SubscribeCtx(context.Background(), s)
}

func (c *Client) rateLimit(ctx context.Context, batchElems []rpc.BatchElem) error {
if c.rl == nil {
return nil
Expand Down
22 changes: 22 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,28 @@ func ExampleCallErrors() {
// 0x00000000219ab540356cBB839Cbe05303d7705Fa: unknown symbol: execution reverted
}

func ExampleClient_Subscribe() {
client := w3.MustDial("wss://mainnet.gateway.tenderly.co")
defer client.Close()

txCh := make(chan *types.Transaction)
sub, err := client.Subscribe(eth.PendingTransactions(txCh))
if err != nil {
fmt.Printf("Failed to subscribe: %v\n", err)
return
}

for {
select {
case tx := <-txCh:
fmt.Printf("New pending tx: %s\n", tx.Hash())
case err := <-sub.Err():
fmt.Printf("Subscription error: %v\n", err)
return
}
}
}

func TestClientCall(t *testing.T) {
tests := []struct {
Buf *bytes.Buffer
Expand Down
33 changes: 33 additions & 0 deletions module/eth/subscribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package eth

import (
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/core/types"
"github.com/lmittmann/w3/w3types"
)

// NewHeads subscribes to notifications of updates to the blockchain head.
func NewHeads(ch chan<- *types.Header) w3types.RPCSubscriber {
return &ethSubscription[*types.Header]{ch, []any{"newHeads"}, nil}
}

// PendingTransactions subscribes to notifications about new pending transactions in the transaction pool.
func PendingTransactions(ch chan<- *types.Transaction) w3types.RPCSubscriber {
return &ethSubscription[*types.Transaction]{ch, []any{"newPendingTransactions", true}, nil}
}

// NewLogs subscribes to notifications about logs that match the given filter query.
func NewLogs(ch chan<- *types.Log, q ethereum.FilterQuery) w3types.RPCSubscriber {
arg, err := toFilterArg(q)
return &ethSubscription[*types.Log]{ch, []any{"logs", arg}, err}
}

type ethSubscription[T any] struct {
ch chan<- T
params []any
err error
}

func (s *ethSubscription[T]) CreateRequest() (string, any, []any, error) {
return "eth", s.ch, s.params, s.err
}
7 changes: 7 additions & 0 deletions w3types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ type RPCCallerFactory[T any] interface {
Returns(*T) RPCCaller
}

// RPCSubscriber is the interface that wraps the basic CreateRequest method.
type RPCSubscriber interface {
// CreateRequest returns the namespace, channel, params for starting a new
// subscription and an error if the request cannot be created.
CreateRequest() (namespace string, ch any, params []any, err error)
}

// Caller is the interface that groups the basic CreateRequest and
// HandleResponse methods.
//
Expand Down