/
node_client.go
67 lines (57 loc) · 1.52 KB
/
node_client.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
58
59
60
61
62
63
64
65
66
67
package client
import (
"context"
"math/rand"
"time"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/samber/lo"
)
type NodeClient struct {
ethClients []*ethclient.Client
limitChan chan struct{}
}
func NewNodeClientsWithEndpoints(endpoints []string) (*NodeClient, error) {
ethClients := make([]*ethclient.Client, 0)
for _, endpoint := range endpoints {
ethClient, err := ethclient.Dial(endpoint)
if err != nil {
return nil, err
}
ethClients = append(ethClients, ethClient)
}
return &NodeClient{
ethClients: ethClients,
limitChan: make(chan struct{}, 10),
}, nil
}
func (n *NodeClient) ETHClient() *ethclient.Client {
if len(n.ethClients) == 0 {
return nil
}
rand.NewSource(time.Now().Unix())
return n.ethClients[rand.Intn(len(n.ethClients))]
}
// Latest Client returns the client with the latest block number.
// It only works on specific node, like 'http://192.xxx.xxx.xx:8045'.
// It will NOT work when connected to a gateway like 'https://ankr.com/eth',
// because the request itself will be sent to one of the nodes randomly.
func (n *NodeClient) LatestClient() *ethclient.Client {
if len(n.ethClients) == 0 {
return nil
}
var (
latestBlockNumber uint64
latestClient *ethclient.Client
)
lo.ForEach[*ethclient.Client](n.ethClients, func(cli *ethclient.Client, _ int) {
blockNumber, err := cli.BlockNumber(context.Background())
if err != nil {
return
}
if blockNumber > latestBlockNumber {
latestBlockNumber = blockNumber
latestClient = cli
}
})
return latestClient
}