-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgemini.go
294 lines (250 loc) · 8.35 KB
/
gemini.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
package gemini
import (
"bytes"
"crypto/hmac"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
const (
BASE_URL = "https://api.gemini.com"
SANDBOX_URL = "https://api.sandbox.gemini.com"
WS_BASE_URL = "wss://api.gemini.com"
WS_SANDBOX_URL = "wss://api.sandbox.gemini.com"
// public
SYMBOLS_URI = "/v1/symbols"
TICKER_URI = "/v1/pubticker/"
BOOK_URI = "/v1/book/"
TRADES_URI = "/v1/trades/"
AUCTION_URI = "/v1/auction/"
// authenticated
PAST_TRADES_URI = "/v1/mytrades"
TRADE_VOLUME_URI = "/v1/tradevolume"
ACTIVE_ORDERS_URI = "/v1/orders"
ORDER_STATUS_URI = "/v1/order/status"
NEW_ORDER_URI = "/v1/order/new"
CANCEL_ORDER_URI = "/v1/order/cancel"
CANCEL_ALL_URI = "/v1/order/cancel/all"
CANCEL_SESSION_URI = "/v1/order/cancel/session"
HEARTBEAT_URI = "/v1/heartbeat"
// fund mgmt
BALANCES_URI = "/v1/balances"
NEW_DEPOSIT_ADDRESS_URI = "/v1/deposit/"
WITHDRAW_FUNDS_URI = "/v1/withdraw/"
// websockets
ORDER_EVENTS_URI = "/v1/order/events"
MARKET_DATA_URI = "/v1/marketdata/"
)
type Api struct {
url string
key string
secret string
}
func New(live bool, key, secret string) *Api {
var url string
if url = SANDBOX_URL; live == true {
url = BASE_URL
}
return &Api{url: url, key: key, secret: secret}
}
type ApiError struct {
Reason string
Message string
}
func (e *ApiError) Error() string {
return fmt.Sprintf("[%v] %v", e.Reason, e.Message)
}
type GenericResponse struct {
Result string
ApiError
}
type Id string
// Id has a custom Unmarshal since it needs to handle unmarshalling from both
// string and int json types. This package takes the position that throughout
// ids should be strings and converted from json into strings where needed.
func (id *Id) UnmarshalJSON(b []byte) error {
if len(b) > 0 && b[0] == '"' {
b = b[1:]
}
l := len(b)
if l > 0 && b[l-1] == '"' {
b = b[:l-1]
}
*id = Id(b)
return nil
}
type Order struct {
OrderId Id `json:"order_id"`
ClientOrderId string `json:"client_order_id"`
Symbol string `json:"symbol"`
Side string `json:"side"`
Type string `json:"type"`
Timestamp int64 `json:"timestampms"`
IsLive bool `json:"is_live"`
IsCancelled bool `json:"is_cancelled"`
IsHidden bool `json:"is_hidden"`
WasForced bool `json:"was_forced"`
Price float64 `json:"price,string"`
ExecutedAmount float64 `json:"executed_amount,string"`
RemainingAmount float64 `json:"remaining_amount,string"`
OriginalAmount float64 `json:"original_amount,string"`
AvgExecutionPrice float64 `json:"avg_execution_price,string"`
}
type Trade struct {
OrderId Id `json:"order_id"`
TradeId Id `json:"tid"`
Timestamp int64 `json:"timestampms"`
Exchange string `json:"exchange"`
Type string `json:"type"`
FeeCurrency string `json:"fee_currency"`
FeeAmount float64 `json:"fee_amount,string"`
Amount float64 `json:"amount,string"`
Price float64 `json:"price,string"`
IsAuctionFill bool `json:"is_auction_fill"`
Aggressor bool `json:"aggressor"`
Broken bool `json:"broken"`
Break string `json:"break"`
}
type Ticker struct {
Bid float64 `json:"bid,string"`
Ask float64 `json:"ask,string"`
Last float64 `json:"last,string"`
Volume TickerVolume `json:"volume"`
}
type TickerVolume struct {
BTC float64 `json:",string"`
ETH float64 `json:",string"`
USD float64 `json:",string"`
Timestamp int64 `json:"timestamp"`
}
type TradeVolume struct {
AccountId Id `json:"account_id"`
Symbol string `json:"symbol"`
BaseCurrency string `json:"base_currency"`
NotionalCurrency string `json:"notional_currency"`
DataDate string `json:"data_date"`
TotalVolumeBase float64 `json:"total_volume_base"`
MakeBuySellRatio float64 `json:"maker_buy_sell_ratio"`
BuyMakerBase float64 `json:"buy_maker_base"`
BuyMakerNotional float64 `json:"buy_maker_notional"`
BuyMakerCount float64 `json:"buy_maker_count"`
SellMakerBase float64 `json:"sell_maker_base"`
SellMakerNotional float64 `json:"sell_maker_notional"`
SellMakerCount float64 `json:"sell_maker_count"`
BuyTakerBase float64 `json:"buy_taker_base"`
BuyTakerNotional float64 `json:"buy_taker_notional"`
BuyTakerCount float64 `json:"buy_taker_count"`
SellTakerBase float64 `json:"sell_taker_base"`
SellTakerNotional float64 `json:"sell_taker_notional"`
SellTakerCount float64 `json:"sell_taker_count"`
}
type CurrentAuction struct {
ClosedUntil int64 `json:"closed_until_ms"`
LastAuctionEid Id `json:"last_auction_eid"`
LastAuctionPrice float64 `json:"last_auction_price,string"`
LastAuctionQuantity float64 `json:"last_auction_quantity,string"`
LastHighestBidPrice float64 `json:"last_highest_bid_price,string"`
LastLowestAskPrice float64 `json:"last_lowest_ask_price,string"`
MostRecentIndicativePrice float64 `json:"most_recent_indicative_price,string"`
MostRecentIndicativeQuantity float64 `json:"most_recent_indicative_quantity,string"`
MostRecentHighestBidPrice float64 `json:"most_recent_highest_bid_price,string"`
MostRecentLowestAskPrice float64 `json:"most_recent_lowest_ask_price,string"`
NextUpdate int64 `json:"next_update_ms"`
NextAuction int64 `json:"next_auction_ms"`
}
type Auction struct {
Timestamp int64 `json:"timestampms"`
AuctionId Id `json:"auction_id"`
Eid Id `json:"eid"`
EventType string `json:"event_type"`
AuctionResult string `json:"auction_result"`
AuctionPrice float64 `json:"auction_price,string"`
AuctionQuantity float64 `json:"auction_quantity,string"`
HighestBidPrice float64 `json:"highest_bid_price,string"`
LowestAskPrice float64 `json:"lowest_ask_price,string"`
}
type CancelResult struct {
GenericResponse
Details CancelResultDetails
}
type CancelResultDetails struct {
CancelledOrders []Id
CancelRejects []Id
}
type FundBalance struct {
Type string `json:"type"`
Currency string `json:"currency"`
Amount float64 `json:"amount,string"`
Available float64 `json:"available,string"`
AvailableForWithdrawal float64 `json:"availableForWithdrawal,string"`
}
type DepositAddress struct {
Currency string `json:"currency"`
Address string `json:"address"`
Label string `json:"label"`
}
type WithdrawFundsResult struct {
Destination string `json:"destination"`
TxHash string `json:"txHash"`
Amount float64 `json:"amount,string"`
}
// Nonce returns a generic nonce based on unix timestamp
func Nonce() int64 {
return time.Now().UnixNano()
}
// BuildHeader handles the conversion of post parameters into headers formatted
// according to Gemini specification. Resulting headers include the API key,
// the payload and the signature.
func (api *Api) BuildHeader(req *map[string]interface{}) http.Header {
reqStr, _ := json.Marshal(req)
payload := base64.StdEncoding.EncodeToString([]byte(reqStr))
mac := hmac.New(sha512.New384, []byte(api.secret))
mac.Write([]byte(payload))
signature := hex.EncodeToString(mac.Sum(nil))
header := http.Header{}
header.Set("X-GEMINI-APIKEY", api.key)
header.Set("X-GEMINI-PAYLOAD", payload)
header.Set("X-GEMINI-SIGNATURE", signature)
return header
}
// request makes the HTTP request to Gemini and handles any returned errors
func (api *Api) request(verb, url string, params map[string]interface{}) ([]byte, error) {
req, err := http.NewRequest(verb, url, bytes.NewBuffer([]byte{}))
if err != nil {
return nil, err
}
if params != nil {
if verb == "GET" {
q := req.URL.Query()
for key, val := range params {
q.Add(key, val.(string))
}
req.URL.RawQuery = q.Encode()
} else {
req.Header = api.BuildHeader(¶ms)
}
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// read response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// check for error from Gemini
var res GenericResponse
json.Unmarshal(body, &res)
if res.Result == "error" {
return nil, &res.ApiError
}
return body, nil
}