/
eth_client.go
113 lines (88 loc) · 3.03 KB
/
eth_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
package client
import (
"context"
"time"
"math/big"
"github.com/dan13ram/wpokt-validator/app"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
log "github.com/sirupsen/logrus"
)
const (
MAX_QUERY_BLOCKS int64 = 100000
)
type EthereumClient interface {
ValidateNetwork()
GetBlockNumber() (uint64, error)
GetChainId() (*big.Int, error)
GetClient() *ethclient.Client
GetTransactionByHash(txHash string) (*types.Transaction, bool, error)
GetTransactionReceipt(txHash string) (*types.Receipt, error)
}
type ethereumClient struct {
client *ethclient.Client
}
var Client EthereumClient = ðereumClient{}
func (c *ethereumClient) GetClient() *ethclient.Client {
return c.client
}
func (c *ethereumClient) GetBlockNumber() (uint64, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(app.Config.Ethereum.RPCTimeoutMillis)*time.Millisecond)
defer cancel()
blockNumber, err := c.client.BlockNumber(ctx)
if err != nil {
return 0, err
}
return blockNumber, nil
}
func (c *ethereumClient) GetChainId() (*big.Int, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(app.Config.Ethereum.RPCTimeoutMillis)*time.Millisecond)
defer cancel()
chainId, err := c.client.ChainID(ctx)
if err != nil {
return nil, err
}
return chainId, nil
}
func (c *ethereumClient) ValidateNetwork() {
log.Debugln("[ETH]", "Validating network")
log.Debugln("[ETH]", "uri", app.Config.Ethereum.RPCURL)
client, err := ethclient.Dial(app.Config.Ethereum.RPCURL)
if err != nil {
log.Fatalln("[ETH]", "Failed to connect to Ethereum node:", err)
}
c.client = client
chainId, err := c.GetChainId()
if err != nil {
log.Fatalln("[ETH]", "Failed to get chain ID:", err)
}
blockNumber, err := c.GetBlockNumber()
if err != nil {
log.Fatalln("[ETH]", "Failed to get block number:", err)
}
log.Debugln("[ETH]", "chainId", chainId.Uint64())
if chainId.String() != app.Config.Ethereum.ChainId {
log.Fatalln("[ETH]", "Chain ID Mismatch", "expected", app.Config.Ethereum.ChainId, "got", chainId.Uint64())
}
log.Debugln("[ETH]", "blockNumber", blockNumber)
log.Infoln("[ETH]", "Validated network")
}
func (c *ethereumClient) GetTransactionByHash(txHash string) (*types.Transaction, bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(app.Config.Ethereum.RPCTimeoutMillis)*time.Millisecond)
defer cancel()
tx, isPending, err := c.client.TransactionByHash(ctx, common.HexToHash(txHash))
return tx, isPending, err
}
func (c *ethereumClient) GetTransactionReceipt(txHash string) (*types.Receipt, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(app.Config.Ethereum.RPCTimeoutMillis)*time.Millisecond)
defer cancel()
receipt, err := c.client.TransactionReceipt(ctx, common.HexToHash(txHash))
return receipt, err
}
func NewClient() (EthereumClient, error) {
client, err := ethclient.Dial(app.Config.Ethereum.RPCURL)
return ðereumClient{
client: client,
}, err
}