Skip to content

Commit

Permalink
rpc: make ExampleClientSubscription work with the geth API (ethereum#…
Browse files Browse the repository at this point in the history
…19483)

This corrects the call to eth_getBlockByNumber, which previously
returned this error:

  can't get latest block: missing value for required argument 1

Co-authored-by: Felix Lange <fjl@twurst.com>
  • Loading branch information
2 people authored and enriquefynn committed Feb 15, 2021
1 parent de38d9e commit a3a8801
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions rpc/client_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package rpc_test
import (
"context"
"fmt"
"math/big"
"time"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
)

Expand All @@ -31,16 +31,16 @@ import (
// eth_getBlockByNumber("latest", {})
// returns the latest block object.
//
// eth_subscribe("newBlocks")
// eth_subscribe("newHeads")
// creates a subscription which fires block objects when new blocks arrive.

type Block struct {
Number *big.Int
Number *hexutil.Big
}

func ExampleClientSubscription() {
// Connect the client.
client, _ := rpc.Dial("ws://127.0.0.1:8485")
client, _ := rpc.Dial("ws://127.0.0.1:8545")
subch := make(chan Block)

// Ensure that subch receives the latest block.
Expand Down Expand Up @@ -75,7 +75,8 @@ func subscribeBlocks(client *rpc.Client, subch chan Block) {
// The connection is established now.
// Update the channel with the current block.
var lastBlock Block
if err := client.CallContext(ctx, &lastBlock, "eth_getBlockByNumber", "latest"); err != nil {
err = client.CallContext(ctx, &lastBlock, "eth_getBlockByNumber", "latest", false)
if err != nil {
fmt.Println("can't get latest block:", err)
return
}
Expand Down

0 comments on commit a3a8801

Please sign in to comment.