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

rpc: make ExampleClientSubscription work with the geth API #19483

Merged
merged 3 commits into from
Apr 27, 2020
Merged
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
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