forked from tensam/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstamphttp.go
328 lines (264 loc) · 8.08 KB
/
bitstamphttp.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
package main
import (
"errors"
"log"
"net/url"
"strconv"
"strings"
"time"
)
const (
BITSTAMP_API_URL = "https://www.bitstamp.net/api/"
BITSTAMP_API_VERSION = "0"
BITSTAMP_API_TICKER = "ticker/"
BITSTAMP_API_ORDERBOOK = "order_book/"
BITSTAMP_API_TRANSACTIONS = "transactions/"
BITSTAMP_API_EURUSD = "eur_usd/"
BITSTAMP_API_BALANCE = "balance/"
BITSTAMP_API_USER_TRANSACTIONS = "user_transactions/"
BITSTAMP_API_OPEN_ORDERS = "open_orders/"
BITSTAMP_API_CANCEL_ORDER = "cancel_order/"
BITSTAMP_API_BUY = "buy/"
BITSTAMP_API_SELL = "sell/"
BITSTAMP_API_WITHDRAWAL_REQUESTS = "withdrawal_requests/"
BITSTAMP_API_BITCOIN_WITHDRAWAL = "bitcoin_withdrawal/"
BITSTAMP_API_BITCOIN_DEPOSIT = "bitcoin_deposit_address/"
BITSTAMP_API_UNCONFIRMED_BITCOIN = "unconfirmed_btc/"
BITSTAMP_API_RIPPLE_WITHDRAWAL = "ripple_withdrawal/"
BITSTAMP_API_RIPPLE_DESPOIT = "ripple_address/"
)
type Bitstamp struct {
Name string
Enabled bool
Verbose bool
Websocket bool
RESTPollingDelay time.Duration
AuthenticatedAPISupport bool
ClientID, APIKey, APISecret string
Ticker BitstampTicker
Orderbook Orderbook
ConversionRate ConversionRate
Transactions []Transactions
Balance BitstampAccountBalance
TakerFee, MakerFee float64
BaseCurrencies []string
AvailablePairs []string
EnabledPairs []string
}
type BitstampTicker struct {
Last float64 `json:",string"`
High float64 `json:",string"`
Low float64 `json:",string"`
Vwap float64 `json:",string"`
Volume float64 `json:",string"`
Bid float64 `json:",string"`
Ask float64 `json:",string"`
}
type BitstampAccountBalance struct {
BTCReserved float64 `json:"usd_balance,string"`
Fee float64 `json:",string"`
BTCAvailable float64 `json:"btc_balance,string"`
USDReserved float64 `json:"usd_reserved,string"`
BTCBalance float64 `json:"btc_balance,string"`
USDBalance float64 `json:"usd_balance,string"`
USDAvailable float64 `json:"usd_available,string"`
}
type Orderbook struct {
Timestamp string
Bids [][]string
Asks [][]string
}
type Transactions struct {
Date, Price, Amount string
Tid int64
}
type ConversionRate struct {
Buy string
Sell string
}
func (b *Bitstamp) SetDefaults() {
b.Name = "Bitstamp"
b.Enabled = true
b.Verbose = false
b.Websocket = false
b.RESTPollingDelay = 10
}
func (b *Bitstamp) GetName() string {
return b.Name
}
func (b *Bitstamp) SetEnabled(enabled bool) {
b.Enabled = enabled
}
func (b *Bitstamp) IsEnabled() bool {
return b.Enabled
}
func (b *Bitstamp) GetFee() float64 {
return b.Balance.Fee
}
func (b *Bitstamp) SetAPIKeys(clientID, apiKey, apiSecret string) {
b.ClientID = clientID
b.APIKey = apiKey
b.APISecret = apiSecret
}
func (b *Bitstamp) Run() {
if b.Verbose {
log.Printf("%s Websocket: %s.", b.GetName(), IsEnabled(b.Websocket))
log.Printf("%s polling delay: %ds.\n", b.GetName(), b.RESTPollingDelay)
log.Printf("%s %d currencies enabled: %s.\n", b.GetName(), len(b.EnabledPairs), b.EnabledPairs)
}
if b.Websocket {
go b.PusherClient()
}
for b.Enabled {
for _, x := range b.EnabledPairs {
currency := x
go func() {
ticker := b.GetTicker()
log.Printf("Bitstamp %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume)
}()
}
time.Sleep(time.Second * b.RESTPollingDelay)
}
}
func (b *Bitstamp) GetTicker() BitstampTicker {
err := SendHTTPGetRequest(BITSTAMP_API_URL+BITSTAMP_API_TICKER, true, &b.Ticker)
if err != nil {
log.Println(err)
return BitstampTicker{}
}
return b.Ticker
}
func (b *Bitstamp) GetOrderbook() {
err := SendHTTPGetRequest(BITSTAMP_API_URL+BITSTAMP_API_ORDERBOOK, true, &b.Orderbook)
if err != nil {
log.Println(err)
return
}
}
func (b *Bitstamp) GetTransactions() {
err := SendHTTPGetRequest(BITSTAMP_API_URL+BITSTAMP_API_TRANSACTIONS, true, &b.Transactions)
if err != nil {
log.Println(err)
return
}
}
func (b *Bitstamp) GetEURUSDConversionRate() {
err := SendHTTPGetRequest(BITSTAMP_API_URL+BITSTAMP_API_EURUSD, true, &b.ConversionRate)
if err != nil {
log.Println(err)
return
}
}
func (b *Bitstamp) GetBalance() {
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_BALANCE, url.Values{}, &b.Balance)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) GetUserTransactions(offset, limit, sort int64) {
var req = url.Values{}
req.Add("offset", strconv.FormatInt(offset, 10))
req.Add("limit", strconv.FormatInt(limit, 10))
req.Add("sort", strconv.FormatInt(sort, 10))
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_USER_TRANSACTIONS, req, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) CancelOrder(OrderID int64) {
var req = url.Values{}
req.Add("id", strconv.FormatInt(OrderID, 10))
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_CANCEL_ORDER, req, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) GetOpenOrders() {
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_OPEN_ORDERS, url.Values{}, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) PlaceOrder(price float64, amount float64, Type int) {
var req = url.Values{}
req.Add("amount", strconv.FormatFloat(amount, 'f', 8, 64))
req.Add("price", strconv.FormatFloat(price, 'f', 2, 64))
orderType := BITSTAMP_API_BUY
if Type == 1 {
orderType = BITSTAMP_API_SELL
}
log.Printf("Placing %s order at price %f for %f amount.\n", orderType, price, amount)
err := b.SendAuthenticatedHTTPRequest(orderType, req, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) GetWithdrawalRequests() {
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_WITHDRAWAL_REQUESTS, url.Values{}, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) BitcoinWithdrawal(amount float64, address string) {
var req = url.Values{}
req.Add("amount", strconv.FormatFloat(amount, 'f', 8, 64))
req.Add("address", address)
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_BITCOIN_WITHDRAWAL, req, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) BitcoinDepositAddress() {
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_BITCOIN_DEPOSIT, url.Values{}, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) UnconfirmedBitcoin() {
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_UNCONFIRMED_BITCOIN, url.Values{}, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) RippleWithdrawal(amount float64, address, currency string) {
var req = url.Values{}
req.Add("amount", strconv.FormatFloat(amount, 'f', 8, 64))
req.Add("address", address)
req.Add("currency", currency)
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_RIPPLE_WITHDRAWAL, req, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) RippleDepositAddress() {
err := b.SendAuthenticatedHTTPRequest(BITSTAMP_API_RIPPLE_DESPOIT, url.Values{}, nil)
if err != nil {
log.Println(err)
}
}
func (b *Bitstamp) SendAuthenticatedHTTPRequest(path string, values url.Values, result interface{}) (err error) {
nonce := strconv.FormatInt(time.Now().UnixNano(), 10)
values.Set("key", b.APIKey)
values.Set("nonce", nonce)
hmac := GetHMAC(HASH_SHA256, []byte(nonce+b.ClientID+b.APIKey), []byte(b.APISecret))
values.Set("signature", strings.ToUpper(HexEncodeToString(hmac)))
path = BITSTAMP_API_URL + path
if b.Verbose {
log.Println("Sending POST request to " + path)
}
headers := make(map[string]string)
headers["Content-Type"] = "application/x-www-form-urlencoded"
resp, err := SendHTTPRequest("POST", path, headers, strings.NewReader(values.Encode()))
if err != nil {
return err
}
if b.Verbose {
log.Printf("Recieved raw: %s\n", resp)
}
err = JSONDecode([]byte(resp), &result)
if err != nil {
return errors.New("Unable to JSON Unmarshal response.")
}
return nil
}