RPC Client

w3.Client is a blazing fast RPC client, built on top of go-ethereum/rpc.Client. It is designed for batch requests and easy extendibility.

Get Started

w3.Client is a batch request focused RPC client that can be used to connect to an Ethereum node via HTTP, WebSocket, or IPC. Its modular API allows to create custom RPC method integrations that can be used alongside the common methods implemented by this package.

Connect to an RPC Endpoint

Connect to an RPC endpoint via HTTP, WebSocket, or IPC using w3.Dial or w3.MustDial.

client, err := w3.Dial("https://rpc.ankr.com/eth")
if err != nil {
    // ...
}
defer client.Close()

Make a Request

Make a single HTTP request that calls two RPC methods.

var (
    balance *big.Int
    nonce   uint64
)
if err := client.Call(
    eth.Balance(addr, nil).Returns(&balance),
    eth.Nonce(addr, nil).Returns(&nonce),
); err != nil {
    // ...
}

Why send batch requests?

Most of the time you need to call multiple RPC methods to get the data you need. When you make separate requests per RPC call you need a single round trip to the server for each call. This can be slow, especially for remote endpoints. Batching multiple RPC calls into a single request only requires a single round trip, and speeds up RPC calls significantly.

Call

Coming soon…

Call Contracts

Coming soon…

Subscribe

w3.Client supports subscriptions through the Client.Subscribe and Client.SubscribeCtx methods. Subscriptions can be used to listen to events, emitted by the Ethereum node.

Subscriptions

  • eth.NewHeads(ch chan<- *types.Header): Subscribe to new block headers.
  • eth.NewLogs(ch chan<- *types.Log, q ethereum.FilterQuery): Subscribe to new logs.
  • eth.PendingTransactions(ch chan<- *types.Transaction): Subscribe to new pending transactions.

Example: Subscribe to Pending Transactions

Subscribe to new pending transactions (Playground):

pendingTxCh := make(chan *types.Transaction)
sub, err := client.Subscribe(eth.PendingTransactions(pendingTxCh))
if err != nil {
    // ...
}
 
for {
    select {
    case tx := <-pendingTxCh:
        fmt.Printf("New pending tx: %s\n", tx.Hash())
    case err := <-sub.Err():
        fmt.Printf("Subscription error: %v\n", err)
        return
    }
}

Error Handling

If one or more calls in a batch request fail, Client.Call returns an error of type w3.CallErrors.

Example: w3.CallErrors

Check which RPC calls failed in a batch request (Playground)

var batchErr w3.CallErrors
if err := client.Call(calls...); errors.As(err, &batchErr) {
    // handle call errors
} else if err != nil {
    // handle other errors
}