-
Notifications
You must be signed in to change notification settings - Fork 0
/
latest_block_provider.go
57 lines (44 loc) · 1.33 KB
/
latest_block_provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package rpcfilters
import (
"errors"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/status-im/status-go/rpc"
)
type rpcProvider interface {
RPCClient() *rpc.Client
}
// blockInfo contains the hash and the number of the latest block
type blockInfo struct {
Hash common.Hash `json:"hash"`
NumberBytes hexutil.Bytes `json:"number"`
}
// Number returns a big.Int representation of the encoded block number.
func (i blockInfo) Number() *big.Int {
number := big.NewInt(0)
number.SetBytes(i.NumberBytes)
return number
}
// latestBlockProvider provides the latest block info from the blockchain
type latestBlockProvider interface {
GetLatestBlock() (blockInfo, error)
}
// latestBlockProviderRPC is an implementation of latestBlockProvider interface
// that requests a block using an RPC client provided
type latestBlockProviderRPC struct {
rpc rpcProvider
}
// GetLatestBlock returns the block info
func (p *latestBlockProviderRPC) GetLatestBlock() (blockInfo, error) {
rpcClient := p.rpc.RPCClient()
if rpcClient == nil {
return blockInfo{}, errors.New("no active RPC client: is the node running?")
}
var result blockInfo
err := rpcClient.Call(&result, "eth_getBlockByNumber", "latest", false)
if err != nil {
return blockInfo{}, err
}
return result, nil
}