/
common.go
399 lines (338 loc) · 8.42 KB
/
common.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package myokxapi
import (
"bytes"
"compress/gzip"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"io"
"sync"
"time"
"net/http"
"net/url"
"reflect"
"strconv"
"strings"
jsoniter "github.com/json-iterator/go"
"github.com/shopspring/decimal"
"github.com/sirupsen/logrus"
)
const (
BIT_BASE_10 = 10
BIT_SIZE_64 = 64
BIT_SIZE_32 = 32
)
type RequestType string
const (
GET = "GET"
POST = "POST"
DELETE = "DELETE"
PUT = "PUT"
)
var NIL_REQBODY = []byte{}
var json = jsoniter.ConfigCompatibleWithStandardLibrary
var log = logrus.New()
func SetLogger(logger *logrus.Logger) {
log = logger
}
func GetPointer[T any](v T) *T {
return &v
}
func HmacSha256(secret, data string) []byte {
h := hmac.New(sha256.New, []byte(secret))
h.Write([]byte(data))
return h.Sum(nil)
}
// Request 发送请求
func Request(url string, reqBody []byte, method string, isGzip bool) ([]byte, error) {
return RequestWithHeader(url, reqBody, method, map[string]string{}, isGzip)
}
func RequestWithHeader(url string, reqBody []byte, method string, headerMap map[string]string, isGzip bool) ([]byte, error) {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return nil, err
}
for k, v := range headerMap {
req.Header.Set(k, v)
}
req.Header.Set("Content-Type", "application/json")
//ok模拟盘额外参数
if NowNetType == TEST_NET {
req.Header.Set("x-simulated-trading", "1")
}
client := &http.Client{}
if isGzip { // 请求 header 添加 gzip
req.Header.Add("Content-Encoding", "gzip")
req.Header.Add("Accept-Encoding", "gzip")
}
req.Close = true
req.Body = io.NopCloser(bytes.NewBuffer(reqBody))
log.Debug("reqURL: ", req.URL.String())
if reqBody != nil && len(reqBody) > 0 {
log.Debug("reqBody: ", string(reqBody))
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body := resp.Body
if resp.Header.Get("Content-Encoding") == "gzip" {
body, err = gzip.NewReader(resp.Body)
if err != nil {
log.Error(err)
return nil, err
}
}
data, err := io.ReadAll(body)
// log.Debug(string(data))
return data, err
}
type MyOkx struct {
}
const (
OKX_API_HTTP = "www.okx.com"
OKX_API_WEBSOCKET = "ws.okx.com:8443"
OKX_API_HTTP_AWS = "aws.okx.com"
OKX_API_WEBSOCKET_AWS = "wsaws.okx.com:8443"
IS_GZIP = true
)
type ServerType int
const (
BASE ServerType = iota
AWS
)
var SERVER_TYPE = BASE
func SetServerType(serverType ServerType) {
SERVER_TYPE = serverType
}
type NetType int
const (
MAIN_NET NetType = iota
TEST_NET
)
var NowNetType = MAIN_NET
func SetNetType(netType NetType) {
NowNetType = netType
}
type APIType int
const (
REST APIType = iota
WS_PUBLIC
WS_PRIVATE
WS_BUSINESS
)
// 产品类型 SPOT:币币 MARGIN:币币杠杆 SWAP:永续合约 FUTURES:交割合约 OPTION : 期权 ANY: 全部
type InstType string
const (
SPOT InstType = "SPOT"
MARGIN InstType = "MARGIN"
SWAP InstType = "SWAP"
FUTURES InstType = "FUTURES"
OPTION InstType = "OPTION"
ANY InstType = "ANY"
)
type Client struct {
APIKey string
SecretKey string
Passphrase string
}
type RestClient struct {
c *Client
}
type PublicRestClient RestClient
type PrivateRestClient RestClient
func (*MyOkx) NewRestClient(APIKey, SecretKey, Passphrase string) *RestClient {
client := &RestClient{
c: &Client{
APIKey: APIKey,
SecretKey: SecretKey,
Passphrase: Passphrase,
},
}
return client
}
func (c *RestClient) PublicRestClient() *PublicRestClient {
return &PublicRestClient{
c: c.c,
}
}
func (c *RestClient) PrivateRestClient() *PrivateRestClient {
return &PrivateRestClient{
c: c.c,
}
}
// 通用接口调用
func okxCallAPI[T any](client *Client, url url.URL, reqBody []byte, method string) (*OkxRestRes[T], error) {
body, err := Request(url.String(), reqBody, method, IS_GZIP)
if err != nil {
return nil, err
}
res, err := handlerCommonRest[T](body)
if err != nil {
return nil, err
}
return res, res.handlerError()
}
// 通用鉴权接口调用
func okxCallAPIWithSecret[T any](client *Client, url url.URL, reqBody []byte, method string) (*OkxRestRes[T], error) {
timestamp := time.Now().UTC().Format("2006-01-02T15:04:05.000Z")
requestPath := url.Path
query := url.RawQuery
hmacSha256Data := timestamp + method + requestPath
if query != "" {
hmacSha256Data += "?" + query
}
if len(reqBody) != 0 {
hmacSha256Data += string(reqBody)
}
sign := base64.StdEncoding.EncodeToString(HmacSha256(client.SecretKey, hmacSha256Data))
// log.Warn("timestamp: ", timestamp)
// log.Warn("method: ", method)
// log.Warn("requestPath: ", requestPath)
// log.Warn("query: ", query)
// log.Warn("reqBody: ", string(reqBody))
// log.Warn("hmacSha256Data: ", hmacSha256Data)
// log.Warn("sign: ", sign)
body, err := RequestWithHeader(url.String(), reqBody, method,
map[string]string{
"OK-ACCESS-KEY": client.APIKey,
"OK-ACCESS-SIGN": sign,
"OK-ACCESS-TIMESTAMP": timestamp,
"OK-ACCESS-PASSPHRASE": client.Passphrase,
}, IS_GZIP)
if err != nil {
return nil, err
}
res, err := handlerCommonRest[T](body)
if err != nil {
return nil, err
}
return res, res.handlerError()
}
// URL标准封装 带路径参数
func okxHandlerRequestAPIWithPathQueryParam[T any](apiType APIType, request *T, name string) url.URL {
query := okxHandlerReq(request)
u := url.URL{
Scheme: "https",
Host: OkxGetRestHostByAPIType(apiType),
Path: name,
RawQuery: query,
}
return u
}
// URL标准封装 不带路径参数
func okxHandlerRequestAPIWithoutPathQueryParam(apiType APIType, name string) url.URL {
// query := okxHandlerReq(request)
u := url.URL{
Scheme: "https",
Host: OkxGetRestHostByAPIType(apiType),
Path: name,
RawQuery: "",
}
return u
}
func okxHandlerReq[T any](req *T) string {
var argBuffer bytes.Buffer
t := reflect.TypeOf(req)
v := reflect.ValueOf(req)
if v.IsNil() {
return ""
}
t = t.Elem()
v = v.Elem()
count := v.NumField()
for i := 0; i < count; i++ {
argName := t.Field(i).Tag.Get("json")
switch v.Field(i).Elem().Kind() {
case reflect.String:
argBuffer.WriteString(argName + "=" + v.Field(i).Elem().String() + "&")
case reflect.Int, reflect.Int64:
argBuffer.WriteString(argName + "=" + strconv.FormatInt(v.Field(i).Elem().Int(), BIT_BASE_10) + "&")
case reflect.Float32, reflect.Float64:
argBuffer.WriteString(argName + "=" + decimal.NewFromFloat(v.Field(i).Elem().Float()).String() + "&")
case reflect.Bool:
argBuffer.WriteString(argName + "=" + strconv.FormatBool(v.Field(i).Elem().Bool()) + "&")
case reflect.Struct:
sv := reflect.ValueOf(v.Field(i).Interface())
ToStringMethod := sv.MethodByName("String")
args := make([]reflect.Value, 0)
result := ToStringMethod.Call(args)
argBuffer.WriteString(argName + "=" + result[0].String() + "&")
case reflect.Slice:
s := v.Field(i).Interface()
d, _ := json.Marshal(s)
argBuffer.WriteString(argName + "=" + url.QueryEscape(string(d)) + "&")
case reflect.Invalid:
default:
log.Errorf("req type error %s:%s", argName, v.Field(i).Elem().Kind())
}
}
return strings.TrimRight(argBuffer.String(), "&")
}
func OkxGetRestHostByAPIType(apiType APIType) string {
switch apiType {
case REST:
switch SERVER_TYPE {
case BASE:
return OKX_API_HTTP
case AWS:
return OKX_API_HTTP_AWS
}
default:
}
return ""
}
func interfaceStringToFloat64(inter interface{}) float64 {
return stringToFloat64(inter.(string))
}
func interfaceStringToInt64(inter interface{}) int64 {
return int64(inter.(float64))
}
func stringToFloat64(str string) float64 {
f, _ := strconv.ParseFloat(str, BIT_SIZE_64)
return f
}
type MySyncMap[K any, V any] struct {
smap sync.Map
}
func NewMySyncMap[K any, V any]() MySyncMap[K, V] {
return MySyncMap[K, V]{
smap: sync.Map{},
}
}
func (m *MySyncMap[K, V]) Load(k K) (V, bool) {
v, ok := m.smap.Load(k)
if ok {
return v.(V), true
}
var resv V
return resv, false
}
func (m *MySyncMap[K, V]) Store(k K, v V) {
m.smap.Store(k, v)
}
func (m *MySyncMap[K, V]) Delete(k K) {
m.smap.Delete(k)
}
func (m *MySyncMap[K, V]) Range(f func(k K, v V) bool) {
m.smap.Range(func(k, v any) bool {
return f(k.(K), v.(V))
})
}
func (m *MySyncMap[K, V]) Length() int {
length := 0
m.Range(func(k K, v V) bool {
length += 1
return true
})
return length
}
func (m *MySyncMap[K, V]) MapValues(f func(k K, v V) V) *MySyncMap[K, V] {
var res = NewMySyncMap[K, V]()
m.Range(func(k K, v V) bool {
res.Store(k, f(k, v))
return true
})
return &res
}