-
Notifications
You must be signed in to change notification settings - Fork 10
/
types.go
79 lines (72 loc) · 1.44 KB
/
types.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
package cryptocom
import (
"encoding/json"
"errors"
"fmt"
"time"
)
const (
// Types
AuthRequest = iota + 1
SubscribeRequest
HeartBeat
OrderRequest
RestOrderRequest
RestBalanceRequest
RestTradesRequest
RestOpenOrdersRequest
)
type Request struct {
Id int
Type uint8
Method string
ApiKey string
Signature string
Nonce string
Params map[string]interface{}
}
type Response struct {
Id int
Method string
Code int
Message string
Result map[string]interface{}
}
func generateNonce() string {
return fmt.Sprintf("%d", time.Now().Unix()*1000)
}
func (r *Request) Encode() ([]byte, error) {
switch r.Type {
case AuthRequest:
return json.Marshal(map[string]interface{}{
"id": r.Id,
"method": r.Method,
"api_key": r.ApiKey,
"sig": r.Signature,
"nonce": r.Nonce,
})
case RestOrderRequest, RestBalanceRequest, RestTradesRequest, RestOpenOrdersRequest:
return json.Marshal(map[string]interface{}{
"id": r.Id,
"method": r.Method,
"params": r.Params,
"api_key": r.ApiKey,
"sig": r.Signature,
"nonce": r.Nonce,
})
case SubscribeRequest, OrderRequest:
return json.Marshal(map[string]interface{}{
"id": r.Id,
"method": r.Method,
"params": r.Params,
"nonce": r.Nonce,
})
case HeartBeat:
return json.Marshal(map[string]interface{}{
"id": r.Id,
"method": r.Method,
})
default:
return nil, errors.New("invalid type")
}
}