forked from tonkeeper/tonapi-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
240 lines (218 loc) · 6.58 KB
/
websocket.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package tonapi
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"github.com/gorilla/websocket"
"golang.org/x/sync/errgroup"
)
// JsonRPCRequest represents a request in the JSON-RPC protocol supported by "/v2/websocket" endpoint.
type JsonRPCRequest struct {
ID uint64 `json:"id,omitempty"`
JSONRPC string `json:"jsonrpc,omitempty"`
Method string `json:"method,omitempty"`
Params []string `json:"params,omitempty"`
}
// JsonRPCResponse represents a response in the JSON-RPC protocol supported by "/v2/websocket" endpoint.
type JsonRPCResponse struct {
ID uint64 `json:"id,omitempty"`
JSONRPC string `json:"jsonrpc,omitempty"`
Method string `json:"method,omitempty"`
Result json.RawMessage `json:"result,omitempty"`
Params json.RawMessage `json:"params,omitempty"`
}
type websocketConnection struct {
// mu protects the handler fields below.
mu sync.Mutex
requestID uint64
conn *websocket.Conn
mempoolHandler MempoolHandler
transactionHandler TransactionHandler
traceHandler TraceHandler
blockHandler BlockHandler
}
func (w *websocketConnection) SubscribeToTransactions(accounts []string, operations []string) error {
params := accounts
if len(operations) > 0 {
params = make([]string, 0, len(accounts))
ops := fmt.Sprintf("operations=%s", strings.Join(operations, ","))
for _, account := range accounts {
params = append(params, fmt.Sprintf("%s;%s", account, ops))
}
}
request := JsonRPCRequest{ID: w.currentRequestID(), JSONRPC: "2.0", Method: "subscribe_account", Params: params}
w.mu.Lock()
defer w.mu.Unlock()
return w.conn.WriteJSON(request)
}
func (w *websocketConnection) UnsubscribeFromTransactions(accounts []string) error {
request := JsonRPCRequest{ID: w.currentRequestID(), JSONRPC: "2.0", Method: "unsubscribe_account", Params: accounts}
w.mu.Lock()
defer w.mu.Unlock()
return w.conn.WriteJSON(request)
}
func (w *websocketConnection) SubscribeToTraces(accounts []string) error {
request := JsonRPCRequest{ID: w.currentRequestID(), JSONRPC: "2.0", Method: "subscribe_trace", Params: accounts}
w.mu.Lock()
defer w.mu.Unlock()
return w.conn.WriteJSON(request)
}
func (w *websocketConnection) UnsubscribeFromTraces(accounts []string) error {
request := JsonRPCRequest{ID: w.currentRequestID(), JSONRPC: "2.0", Method: "unsubscribe_trace", Params: accounts}
w.mu.Lock()
defer w.mu.Unlock()
return w.conn.WriteJSON(request)
}
func (w *websocketConnection) SubscribeToMempool(accounts []string) error {
request := JsonRPCRequest{ID: w.currentRequestID(), JSONRPC: "2.0", Method: "subscribe_mempool"}
if len(accounts) > 0 {
request.Params = []string{
fmt.Sprintf("accounts=%s", strings.Join(accounts, ",")),
}
}
w.mu.Lock()
defer w.mu.Unlock()
return w.conn.WriteJSON(request)
}
func (w *websocketConnection) UnsubscribeFromMempool() error {
request := JsonRPCRequest{ID: w.currentRequestID(), JSONRPC: "2.0", Method: "unsubscribe_mempool"}
w.mu.Lock()
defer w.mu.Unlock()
return w.conn.WriteJSON(request)
}
func (w *websocketConnection) SubscribeToBlocks(workchain *int) error {
request := JsonRPCRequest{ID: w.currentRequestID(), JSONRPC: "2.0", Method: "subscribe_block"}
if workchain != nil {
request.Params = []string{
fmt.Sprintf("workchain=%d", *workchain),
}
}
w.mu.Lock()
defer w.mu.Unlock()
return w.conn.WriteJSON(request)
}
func (w *websocketConnection) UnsubscribeFromBlocks() error {
request := JsonRPCRequest{ID: w.currentRequestID(), JSONRPC: "2.0", Method: "unsubscribe_block"}
w.mu.Lock()
defer w.mu.Unlock()
return w.conn.WriteJSON(request)
}
func (w *websocketConnection) SetMempoolHandler(handler MempoolHandler) {
w.mu.Lock()
defer w.mu.Unlock()
w.mempoolHandler = handler
}
func (w *websocketConnection) SetTransactionHandler(handler TransactionHandler) {
w.mu.Lock()
defer w.mu.Unlock()
w.transactionHandler = handler
}
func (w *websocketConnection) SetTraceHandler(handler TraceHandler) {
w.mu.Lock()
defer w.mu.Unlock()
w.traceHandler = handler
}
func (w *websocketConnection) SetBlockHandler(handler BlockHandler) {
w.mu.Lock()
defer w.mu.Unlock()
w.blockHandler = handler
}
func websocketConnect(ctx context.Context, endpoint string, apiKey string) (*websocketConnection, error) {
header := http.Header{}
if len(apiKey) > 0 {
header.Set("Authorization", "bearer "+apiKey)
}
endpointUrl, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
switch endpointUrl.Scheme {
case "http":
endpointUrl.Scheme = "ws"
case "https":
endpointUrl.Scheme = "wss"
}
conn, _, err := websocket.DefaultDialer.DialContext(ctx, fmt.Sprintf("%s/v2/websocket", endpointUrl.String()), header)
if err != nil {
return nil, err
}
return &websocketConnection{
conn: conn,
mempoolHandler: func(data MempoolEventData) {},
transactionHandler: func(data TransactionEventData) {},
traceHandler: func(data TraceEventData) {},
blockHandler: func(data BlockEventData) {},
}, nil
}
func (w *websocketConnection) runJsonRPC(ctx context.Context, fn WebsocketConfigurator) error {
defer w.conn.Close()
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return fn(w)
})
g.Go(func() error {
for {
_, msg, err := w.conn.ReadMessage()
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
var response JsonRPCResponse
if err := json.Unmarshal(msg, &response); err != nil {
return err
}
switch response.Method {
case "trace":
var traceEvent TraceEventData
if err := json.Unmarshal(response.Params, &traceEvent); err != nil {
return err
}
w.processHandler(func() {
w.traceHandler(traceEvent)
})
case "account_transaction":
var txEvent TransactionEventData
if err := json.Unmarshal(response.Params, &txEvent); err != nil {
return err
}
w.processHandler(func() {
w.transactionHandler(txEvent)
})
case "mempool_message":
var mempoolEvent MempoolEventData
if err := json.Unmarshal(response.Params, &mempoolEvent); err != nil {
return err
}
w.processHandler(func() {
w.mempoolHandler(mempoolEvent)
})
case "block":
var block BlockEventData
if err := json.Unmarshal(response.Params, &block); err != nil {
return err
}
w.processHandler(func() {
w.blockHandler(block)
})
}
}
})
return g.Wait()
}
func (w *websocketConnection) currentRequestID() uint64 {
w.mu.Lock()
defer w.mu.Unlock()
w.requestID++
return w.requestID
}
func (w *websocketConnection) processHandler(fn func()) {
w.mu.Lock()
defer w.mu.Unlock()
fn()
}