-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
201 lines (169 loc) · 4.05 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
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
package goftx
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
"github.com/pkg/errors"
)
const (
apiUrl = "https://ftx.com/api"
apiOtcUrl = "https://otc.ftx.com/api"
keyHeader = "FTX-KEY"
signHeader = "FTX-SIGN"
tsHeader = "FTX-TS"
subAccountHeader = "FTX-SUBACCOUNT"
)
type Option func(c *Client)
func WithHTTPClient(client *http.Client) Option {
return func(c *Client) {
c.client = client
}
}
func WithAuth(key, secret string, subAccount ...string) Option {
return func(c *Client) {
c.apiKey = key
c.secret = secret
if len(subAccount) > 0 {
c.subAccount = url.PathEscape(subAccount[0])
}
}
}
type Client struct {
client *http.Client
apiKey string
secret string
subAccount string
serverTimeDiff time.Duration
SubAccounts
Markets
Account
Orders
Fills
Converts
Futures
SpotMargin
}
func New(opts ...Option) *Client {
client := &Client{
client: http.DefaultClient,
}
for _, opt := range opts {
opt(client)
}
client.SubAccounts = SubAccounts{client: client}
client.Markets = Markets{client: client}
client.Account = Account{client: client}
client.Orders = Orders{client: client}
client.Fills = Fills{client: client}
client.Converts = Converts{client: client}
client.Futures = Futures{client: client}
client.SpotMargin = SpotMargin{client: client}
return client
}
func (c *Client) SetServerTimeDiff() error {
serverTime, err := c.GetServerTime()
if err != nil {
return errors.WithStack(err)
}
c.serverTimeDiff = serverTime.Sub(time.Now().UTC())
return nil
}
type Response struct {
Success bool `json:"success"`
Result json.RawMessage `json:"result"`
Error string `json:"error,omitempty"`
}
type Request struct {
Auth bool
Method string
URL string
Headers map[string]string
Params map[string]string
Body []byte
}
func (c *Client) prepareRequest(request Request) (*http.Request, error) {
req, err := http.NewRequest(request.Method, request.URL, bytes.NewBuffer(request.Body))
if err != nil {
return nil, errors.WithStack(err)
}
query := req.URL.Query()
for k, v := range request.Params {
query.Add(k, v)
}
req.URL.RawQuery = query.Encode()
if request.Auth {
nonce := strconv.FormatInt(time.Now().UTC().Add(c.serverTimeDiff).Unix()*1000, 10)
payload := nonce + req.Method + req.URL.Path
if req.URL.RawQuery != "" {
payload += "?" + req.URL.RawQuery
}
if len(request.Body) > 0 {
payload += string(request.Body)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set(keyHeader, c.apiKey)
req.Header.Set(signHeader, c.getSignature(payload))
req.Header.Set(tsHeader, nonce)
if c.subAccount != "" {
req.Header.Set(subAccountHeader, c.subAccount)
}
}
for k, v := range request.Headers {
req.Header.Set(k, v)
}
return req, nil
}
func (c *Client) do(req *http.Request) ([]byte, error) {
resp, err := c.client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, errors.WithStack(err)
}
res, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithStack(err)
}
var response Response
err = json.Unmarshal(res, &response)
if err != nil {
return nil, errors.WithStack(err)
}
if !response.Success {
return nil, errors.Errorf("Status Code: %d Error: %v", resp.StatusCode, response.Error)
}
return response.Result, nil
}
func (c *Client) getSignature(payload string) string {
mac := hmac.New(sha256.New, []byte(c.secret))
mac.Write([]byte(payload))
return hex.EncodeToString(mac.Sum(nil))
}
func (c *Client) GetServerTime() (*time.Time, error) {
request, err := c.prepareRequest(Request{
Method: http.MethodGet,
URL: fmt.Sprintf("%s/time", apiOtcUrl),
})
if err != nil {
return nil, errors.WithStack(err)
}
response, err := c.do(request)
if err != nil {
return nil, errors.WithStack(err)
}
var result time.Time
err = json.Unmarshal(response, &result)
if err != nil {
return nil, errors.WithStack(err)
}
return &result, nil
}