@ -19,9 +19,9 @@ package rpc_test
import (
import (
"context"
"context"
"fmt"
"fmt"
"math/big"
"time"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/rpc"
)
)
@ -31,16 +31,16 @@ import (
// eth_getBlockByNumber("latest", {})
// eth_getBlockByNumber("latest", {})
// returns the latest block object.
// returns the latest block object.
//
//
// eth_subscribe("newBlock s")
// eth_subscribe("newHead s")
// creates a subscription which fires block objects when new blocks arrive.
// creates a subscription which fires block objects when new blocks arrive.
type Block struct {
type Block struct {
Number * big . Int
Number * hexutil . Big
}
}
func ExampleClientSubscription ( ) {
func ExampleClientSubscription ( ) {
// Connect the client.
// Connect the client.
client , _ := rpc . Dial ( "ws://127.0.0.1:848 5" )
client , _ := rpc . Dial ( "ws://127.0.0.1:85 45" )
subch := make ( chan Block )
subch := make ( chan Block )
// Ensure that subch receives the latest block.
// Ensure that subch receives the latest block.
@ -75,7 +75,8 @@ func subscribeBlocks(client *rpc.Client, subch chan Block) {
// The connection is established now.
// The connection is established now.
// Update the channel with the current block.
// Update the channel with the current block.
var lastBlock 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 )
fmt . Println ( "can't get latest block:" , err )
return
return
}
}