forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
OKEx.go
171 lines (146 loc) · 4.54 KB
/
OKEx.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
package okex
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
. "github.com/nntaoli-project/GoEx"
"strings"
"sync"
"time"
)
type OKEx struct {
config *APIConfig
OKExSpot *OKExSpot
OKExFuture *OKExFuture
OKExSwap *OKExSwap
OKExWallet *OKExWallet
OKExMargin *OKExMargin
}
func NewOKEx(config *APIConfig) *OKEx {
okex := &OKEx{config: config}
okex.OKExSpot = &OKExSpot{okex}
okex.OKExFuture = &OKExFuture{OKEx: okex, Locker: new(sync.Mutex)}
okex.OKExWallet = &OKExWallet{okex}
okex.OKExMargin = &OKExMargin{okex}
return okex
}
func (ok *OKEx) GetExchangeName() string {
return OKEX
}
func (ok *OKEx) UUID() string {
return strings.Replace(uuid.New().String(), "-", "", 32)
}
func (ok *OKEx) DoRequest(httpMethod, uri, reqBody string, response interface{}) error {
url := ok.config.Endpoint + uri
sign, timestamp := ok.doParamSign(httpMethod, uri, reqBody)
//log.Println(sign, timestamp)
resp, err := NewHttpRequest(ok.config.HttpClient, httpMethod, url, reqBody, map[string]string{
CONTENT_TYPE: APPLICATION_JSON_UTF8,
ACCEPT: APPLICATION_JSON,
//COOKIE: LOCALE + "en_US",
OK_ACCESS_KEY: ok.config.ApiKey,
OK_ACCESS_PASSPHRASE: ok.config.ApiPassphrase,
OK_ACCESS_SIGN: sign,
OK_ACCESS_TIMESTAMP: fmt.Sprint(timestamp)})
if err != nil {
//log.Println(err)
return err
} else {
// log.Println(string(resp))
return json.Unmarshal(resp, &response)
}
}
func (ok *OKEx) adaptOrderState(state int) TradeStatus {
switch state {
case -2:
return ORDER_FAIL
case -1:
return ORDER_CANCEL
case 0:
return ORDER_UNFINISH
case 1:
return ORDER_PART_FINISH
case 2:
return ORDER_FINISH
case 3:
return ORDER_UNFINISH
case 4:
return ORDER_CANCEL_ING
}
return ORDER_UNFINISH
}
/*
Get a http request body is a json string and a byte array.
*/
func (ok *OKEx) BuildRequestBody(params interface{}) (string, *bytes.Reader, error) {
if params == nil {
return "", nil, errors.New("illegal parameter")
}
data, err := json.Marshal(params)
if err != nil {
//log.Println(err)
return "", nil, errors.New("json convert string error")
}
jsonBody := string(data)
binBody := bytes.NewReader(data)
return jsonBody, binBody, nil
}
func (ok *OKEx) doParamSign(httpMethod, uri, requestBody string) (string, string) {
timestamp := ok.IsoTime()
preText := fmt.Sprintf("%s%s%s%s", timestamp, strings.ToUpper(httpMethod), uri, requestBody)
//log.Println("preHash", preText)
sign, _ := GetParamHmacSHA256Base64Sign(ok.config.ApiSecretKey, preText)
return sign, timestamp
}
/*
Get a iso time
eg: 2018-03-16T18:02:48.284Z
*/
func (ok *OKEx) IsoTime() string {
utcTime := time.Now().UTC()
iso := utcTime.String()
isoBytes := []byte(iso)
iso = string(isoBytes[:10]) + "T" + string(isoBytes[11:23]) + "Z"
return iso
}
func (ok *OKEx) LimitBuy(amount, price string, currency CurrencyPair) (*Order, error) {
return ok.OKExSpot.LimitBuy(amount, price, currency)
}
func (ok *OKEx) LimitSell(amount, price string, currency CurrencyPair) (*Order, error) {
return ok.OKExSpot.LimitSell(amount, price, currency)
}
func (ok *OKEx) MarketBuy(amount, price string, currency CurrencyPair) (*Order, error) {
return ok.OKExSpot.MarketBuy(amount, price, currency)
}
func (ok *OKEx) MarketSell(amount, price string, currency CurrencyPair) (*Order, error) {
return ok.OKExSpot.MarketSell(amount, price, currency)
}
func (ok *OKEx) CancelOrder(orderId string, currency CurrencyPair) (bool, error) {
return ok.OKExSpot.OKExSpot.CancelOrder(orderId, currency)
}
func (ok *OKEx) GetOneOrder(orderId string, currency CurrencyPair) (*Order, error) {
return ok.OKExSpot.GetOneOrder(orderId, currency)
}
func (ok *OKEx) GetUnfinishOrders(currency CurrencyPair) ([]Order, error) {
return ok.OKExSpot.GetUnfinishOrders(currency)
}
func (ok *OKEx) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error) {
return ok.OKExSpot.GetOrderHistorys(currency, currentPage, pageSize)
}
func (ok *OKEx) GetAccount() (*Account, error) {
return ok.OKExSpot.GetAccount()
}
func (ok *OKEx) GetTicker(currency CurrencyPair) (*Ticker, error) {
return ok.OKExSpot.GetTicker(currency)
}
func (ok *OKEx) GetDepth(size int, currency CurrencyPair) (*Depth, error) {
return ok.OKExSpot.GetDepth(size, currency)
}
func (ok *OKEx) GetKlineRecords(currency CurrencyPair, period, size, since int) ([]Kline, error) {
return ok.OKExSpot.GetKlineRecords(currency, period, size, since)
}
func (ok *OKEx) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error) {
return ok.OKExSpot.GetTrades(currencyPair, since)
}