-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathclient.go
167 lines (142 loc) · 4.83 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package chain
import (
"context"
"math/big"
"sync"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/status-im/status-go/rpc"
"github.com/status-im/status-go/services/rpcstats"
)
var ChainClientInstances = make(map[uint64]*Client)
type Client struct {
eth *ethclient.Client
ChainID uint64
rpcClient *rpc.Client
IsConnected bool
IsConnectedLock sync.RWMutex
}
type FeeHistory struct {
BaseFeePerGas []string `json:"baseFeePerGas"`
}
func NewClient(rpc *rpc.Client, chainID uint64) (*Client, error) {
if client, ok := ChainClientInstances[chainID]; ok {
return client, nil
}
ethClient, err := rpc.EthClient(chainID)
if err != nil {
return nil, err
}
client := &Client{eth: ethClient, ChainID: chainID, rpcClient: rpc, IsConnected: true}
ChainClientInstances[chainID] = client
return client, nil
}
func NewLegacyClient(rpc *rpc.Client) (*Client, error) {
return NewClient(rpc, rpc.UpstreamChainID)
}
func NewClients(rpc *rpc.Client, chainIDs []uint64) (res []*Client, err error) {
for _, chainID := range chainIDs {
client, err := NewClient(rpc, chainID)
if err != nil {
return nil, err
}
res = append(res, client)
}
return res, nil
}
func (cc *Client) toggleIsConnected(err error) {
cc.IsConnectedLock.Lock()
defer cc.IsConnectedLock.Unlock()
if err != nil {
cc.IsConnected = false
} else {
cc.IsConnected = true
}
}
func (cc *Client) ToBigInt() *big.Int {
return big.NewInt(int64(cc.ChainID))
}
func (cc *Client) HeaderByHash(ctx context.Context, hash common.Hash) (*types.Header, error) {
rpcstats.CountCall("eth_getBlockByHash")
resp, err := cc.eth.HeaderByHash(ctx, hash)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error) {
rpcstats.CountCall("eth_getBlockByNumber")
resp, err := cc.eth.HeaderByNumber(ctx, number)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
rpcstats.CountCall("eth_getBlockByHash")
resp, err := cc.eth.BlockByHash(ctx, hash)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) BlockByNumber(ctx context.Context, number *big.Int) (*types.Block, error) {
rpcstats.CountCall("eth_getBlockByNumber")
resp, err := cc.eth.BlockByNumber(ctx, number)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) BalanceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (*big.Int, error) {
rpcstats.CountCall("eth_getBalance")
resp, err := cc.eth.BalanceAt(ctx, account, blockNumber)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) NonceAt(ctx context.Context, account common.Address, blockNumber *big.Int) (uint64, error) {
rpcstats.CountCall("eth_getTransactionCount")
resp, err := cc.eth.NonceAt(ctx, account, blockNumber)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error) {
rpcstats.CountCall("eth_getTransactionReceipt")
resp, err := cc.eth.TransactionReceipt(ctx, txHash)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) {
rpcstats.CountCall("eth_getTransactionByHash")
tx, isPending, err = cc.eth.TransactionByHash(ctx, hash)
defer cc.toggleIsConnected(err)
return tx, isPending, err
}
func (cc *Client) FilterLogs(ctx context.Context, q ethereum.FilterQuery) ([]types.Log, error) {
rpcstats.CountCall("eth_getLogs")
resp, err := cc.eth.FilterLogs(ctx, q)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) CodeAt(ctx context.Context, contract common.Address, blockNumber *big.Int) ([]byte, error) {
rpcstats.CountCall("eth_getCode")
resp, err := cc.eth.CodeAt(ctx, contract, blockNumber)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) CallContract(ctx context.Context, call ethereum.CallMsg, blockNumber *big.Int) ([]byte, error) {
rpcstats.CountCall("eth_call")
resp, err := cc.eth.CallContract(ctx, call, blockNumber)
defer cc.toggleIsConnected(err)
return resp, err
}
func (cc *Client) GetBaseFeeFromBlock(blockNumber *big.Int) (string, error) {
var feeHistory FeeHistory
err := cc.rpcClient.Call(&feeHistory, cc.ChainID, "eth_feeHistory", "0x1", (*hexutil.Big)(blockNumber), nil)
if err != nil {
if err.Error() == "the method eth_feeHistory does not exist/is not available" {
return "", nil
}
return "", err
}
var baseGasFee string = ""
if len(feeHistory.BaseFeePerGas) > 0 {
baseGasFee = feeHistory.BaseFeePerGas[0]
}
return baseGasFee, err
}