forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
141 lines (123 loc) · 4.02 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
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rpc
import (
"context"
"errors"
"io"
"net"
"net/http"
"time"
"github.com/giangcoy/solana-go/rpc/jsonrpc"
"github.com/klauspost/compress/gzhttp"
)
var ErrNotFound = errors.New("not found")
var ErrNotConfirmed = errors.New("not confirmed")
type Client struct {
rpcURL string
rpcClient JSONRPCClient
}
type JSONRPCClient interface {
CallForInto(ctx context.Context, out interface{}, method string, params []interface{}) error
CallWithCallback(ctx context.Context, method string, params []interface{}, callback func(*http.Request, *http.Response) error) error
CallBatch(ctx context.Context, requests jsonrpc.RPCRequests) (jsonrpc.RPCResponses, error)
}
// New creates a new Solana JSON RPC client.
// Client is safe for concurrent use by multiple goroutines.
func New(rpcEndpoint string) *Client {
opts := &jsonrpc.RPCClientOpts{
HTTPClient: newHTTP(),
}
rpcClient := jsonrpc.NewClientWithOpts(rpcEndpoint, opts)
return NewWithCustomRPCClient(rpcClient)
}
// New creates a new Solana JSON RPC client with the provided custom headers.
// The provided headers will be added to each RPC request sent via this RPC client.
func NewWithHeaders(rpcEndpoint string, headers map[string]string) *Client {
opts := &jsonrpc.RPCClientOpts{
HTTPClient: newHTTP(),
CustomHeaders: headers,
}
rpcClient := jsonrpc.NewClientWithOpts(rpcEndpoint, opts)
return NewWithCustomRPCClient(rpcClient)
}
// Close closes the client.
func (cl *Client) Close() error {
if cl.rpcClient == nil {
return nil
}
if c, ok := cl.rpcClient.(io.Closer); ok {
return c.Close()
}
return nil
}
// NewWithCustomRPCClient creates a new Solana RPC client
// with the provided RPC client.
func NewWithCustomRPCClient(rpcClient JSONRPCClient) *Client {
return &Client{
rpcClient: rpcClient,
}
}
var (
defaultMaxIdleConnsPerHost = 9
defaultTimeout = 5 * time.Minute
defaultKeepAlive = 180 * time.Second
)
func newHTTPTransport() *http.Transport {
return &http.Transport{
IdleConnTimeout: defaultTimeout,
MaxConnsPerHost: defaultMaxIdleConnsPerHost,
MaxIdleConnsPerHost: defaultMaxIdleConnsPerHost,
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: defaultTimeout,
KeepAlive: defaultKeepAlive,
DualStack: true,
}).DialContext,
ForceAttemptHTTP2: true,
// MaxIdleConns: 100,
TLSHandshakeTimeout: 10 * time.Second,
// ExpectContinueTimeout: 1 * time.Second,
}
}
// newHTTP returns a new Client from the provided config.
// Client is safe for concurrent use by multiple goroutines.
func newHTTP() *http.Client {
tr := newHTTPTransport()
return &http.Client{
Timeout: defaultTimeout,
Transport: gzhttp.Transport(tr),
}
}
// RPCCallForInto allows to access the raw RPC client and send custom requests.
func (cl *Client) RPCCallForInto(ctx context.Context, out interface{}, method string, params []interface{}) error {
return cl.rpcClient.CallForInto(ctx, out, method, params)
}
func (cl *Client) RPCCallWithCallback(
ctx context.Context,
method string,
params []interface{},
callback func(*http.Request, *http.Response) error,
) error {
return cl.rpcClient.CallWithCallback(ctx, method, params, callback)
}
func (cl *Client) RPCCallBatch(
ctx context.Context,
requests jsonrpc.RPCRequests,
) (jsonrpc.RPCResponses, error) {
return cl.rpcClient.CallBatch(ctx, requests)
}