-
Notifications
You must be signed in to change notification settings - Fork 23
/
client.go
115 lines (98 loc) · 3.52 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
package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/ethergo/client"
"github.com/synapsecns/sanguine/ethergo/submitter"
"io"
"math/big"
"net/http"
)
// RPCClient is an interface for the omnirpc service.
type RPCClient interface {
submitter.ClientFetcher
// GetEndpoint returns the endpoint for the given chainID and confirmations.
GetEndpoint(chainID, confirmations int) string
// GetDefaultEndpoint returns the endpoint with the default confirmation count for the chain id.
GetDefaultEndpoint(chainID int) string
// GetConfirmationsClient returns a client for the given chainID and confirmations.
// TODO: consider making confirmations iota or uint8, can be easy to confuse these params
GetConfirmationsClient(ctx context.Context, chainID, confirmations int) (client.EVM, error)
// GetChainClient returns a client for the given chainID.
GetChainClient(ctx context.Context, chainID int) (client.EVM, error)
// GetChainIDs returns all chain ids.
GetChainIDs(ctx context.Context) ([]int, error)
}
type rpcClient struct {
config *rpcOptions
endpoint string
handler metrics.Handler
opts []client.Options
}
// NewOmnirpcClient creates a new RPCClient.
func NewOmnirpcClient(endpoint string, handler metrics.Handler, options ...OptionsArgsOption) RPCClient {
c := rpcClient{}
c.config = makeOptions(options)
c.endpoint = endpoint
c.handler = handler
c.opts = append(c.opts, client.Capture(c.config.captureReqRes))
return &c
}
func (c *rpcClient) GetClient(ctx context.Context, chainID *big.Int) (client.EVM, error) {
if !chainID.IsInt64() {
return nil, errors.New("chain id is not a uint64")
}
return c.GetChainClient(ctx, int(chainID.Uint64()))
}
func (c *rpcClient) GetEndpoint(chainID, confirmations int) string {
if confirmations == 0 {
return fmt.Sprintf("%s/rpc/%d", c.endpoint, chainID)
}
return fmt.Sprintf("%s/confirmations/%d/rpc/%d", c.endpoint, confirmations, chainID)
}
func (c *rpcClient) GetDefaultEndpoint(chainID int) string {
return c.GetEndpoint(chainID, c.config.confirmations)
}
func (c *rpcClient) GetConfirmationsClient(ctx context.Context, chainID, confirmations int) (client.EVM, error) {
endpoint := c.GetEndpoint(chainID, confirmations)
chainClient, err := client.DialBackend(ctx, endpoint, c.handler, c.opts...)
if err != nil {
return nil, fmt.Errorf("could not dial backend: %w", err)
}
return chainClient, nil
}
func (c *rpcClient) GetChainClient(ctx context.Context, chainID int) (client.EVM, error) {
endpoint := c.GetDefaultEndpoint(chainID)
chainClient, err := client.DialBackend(ctx, endpoint, c.handler, c.opts...)
if err != nil {
return nil, fmt.Errorf("could not dial backend: %w", err)
}
return chainClient, nil
}
func (c *rpcClient) GetChainIDs(ctx context.Context) (chainIDs []int, err error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/chain-ids", c.endpoint), nil)
if err != nil {
return nil, fmt.Errorf("could not create request: %w", err)
}
httpClient := new(http.Client)
c.handler.ConfigureHTTPClient(httpClient)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("could not get chain ids: %w", err)
}
defer func() {
_ = resp.Body.Close()
}()
readResp, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("could not get chain ids: %w", err)
}
err = json.Unmarshal(readResp, &chainIDs)
if err != nil {
return nil, fmt.Errorf("could not get chain ids: %w", err)
}
return chainIDs, nil
}