-
Notifications
You must be signed in to change notification settings - Fork 1
/
public.go
306 lines (251 loc) · 8.8 KB
/
public.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
package valr
import (
"context"
"fmt"
"time"
)
// Currency is a fiat or crypto currency supported by VALR.
type Currency struct {
IsActive bool `json:"isActive"`
LongName string `json:"longName"`
ShortName string `json:"shortName"`
Symbol string `json:"symbol"`
}
// Currencies satisfies the PublicClient interface.
func (c *client) Currencies(ctx context.Context) ([]Currency, error) {
res, err := c.httpClient.Get(ctx, "/public/currencies", nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch currencies: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch currencies: %d status code "+
"received", res.StatusCode)
}
var currencies []Currency
if err = res.JSON(¤cies); err != nil {
return nil, fmt.Errorf("failed to unmarshal currencies: %w", err)
}
return currencies, nil
}
// CurrencyPair is a fiat/crypto or crypto/crypto pair supported by VALR.
type CurrencyPair struct {
Active bool `json:"active"`
BaseCurrency string `json:"baseCurrency"`
MinBaseAmount string `json:"minBaseAmount"`
MaxBaseAmount string `json:"maxBaseAmount"`
MinQuoteAmount string `json:"minQuoteAmount"`
MaxQuoteAmount string `json:"maxQuoteAmount"`
QuoteCurrency string `json:"quoteCurrency"`
ShortName string `json:"shortName"`
Symbol string `json:"symbol"`
}
// CurrencyPairs satisfies the PublicClient interface.
func (c *client) CurrencyPairs(ctx context.Context) ([]CurrencyPair, error) {
res, err := c.httpClient.Get(ctx, "/public/pairs", nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch currency pairs: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch currency pairs: %d status "+
"code received", res.StatusCode)
}
var pairs []CurrencyPair
if err = res.JSON(&pairs); err != nil {
return nil, fmt.Errorf("failed to fetch currency pairs: %w", err)
}
return pairs, nil
}
// MarketSummary contains a summary of a particular market pair on the
// exchange.
type MarketSummary struct {
AskPrice string `json:"askPrice"`
BaseVolume string `json:"baseVolume"`
BidPrice string `json:"bidPrice"`
ChangeFromPrevious string `json:"changeFromPrevious"`
CreatedAt time.Time `json:"created"`
CurrencyPair string `json:"currencyPair"`
HighPrice string `json:"highPrice"`
LastTradedPrice string `json:"lastTradedPrice"`
LowPrice string `json:"lowPrice"`
PreviousClosePrice string `json:"previousClosePrice"`
}
// MarketSummary satisfies the PublicClient interface.
func (c *client) MarketSummary(ctx context.Context) ([]MarketSummary, error) {
res, err := c.httpClient.Get(ctx, "/public/marketsummary", nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch the market summaries: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch market summary: %d status "+
"code received", res.StatusCode)
}
var summaries []MarketSummary
if err = res.JSON(&summaries); err != nil {
return nil, fmt.Errorf("failed to unmarshal market summaries: %w", err)
}
return summaries, nil
}
// MarketSummaryForCurrency satisfies the PublicClient interface.
func (c *client) MarketSummaryForCurrency(ctx context.Context, pair string) (
*MarketSummary, error) {
res, err := c.httpClient.Get(ctx, fmt.Sprintf("/public/%s/marketsummary",
pair), nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch the market summaries: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch market summaries: %d status "+
"code received", res.StatusCode)
}
var summary MarketSummary
if err = res.JSON(&summary); err != nil {
return nil, fmt.Errorf("failed to unmarshal market summaries: %w", err)
}
return &summary, nil
}
// OrderBook contains a list of the top 20 bids and asks. Ask orders are sorted
// by priceascending. Bid orders are sorted by price descending. Orders of the
// same price are aggregated.
type OrderBook struct {
Asks []OrderBookEntry `json:"Asks"`
Bids []OrderBookEntry `json:"Bids"`
}
// OrderBookEntry is a single entry in an order book.
type OrderBookEntry struct {
CurrencyPair string `json:"currencyPair"`
OrderCount int `json:"orderCount"`
Price string `json:"price"`
Quantity string `json:"quantity"`
Side string `json:"side"`
}
// OrderBook satisfies the PublicClient interface.
func (c *client) OrderBook(ctx context.Context, pair string) (*OrderBook,
error) {
res, err := c.httpClient.Get(ctx, fmt.Sprintf("/public/%s/orderbook", pair),
nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch order book: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch order book: %d status code "+
"received", res.StatusCode)
}
var book OrderBook
if err = res.JSON(&book); err != nil {
return nil, fmt.Errorf("failed to unmarshal order book: %w", err)
}
return &book, nil
}
// OrderType describes the kind of an order.
type OrderType string
const (
// Place a limit order on the exchange that will either be added to the
// order book or, should it match, be cancelled completely.
OrderTypePostOnly OrderType = "post-only limit"
// Place a limit order on the exchange.
OrderTypeLimit OrderType = "limit"
// Place a market order on the exchange (only crypto-to-ZAR pairs).
OrderTypeMarket OrderType = "market"
// Similar to market order, but allows for crypto-to-crypto pairs.
OrderTypeSimple OrderType = "simple"
)
// OrderTypes satisfies the PublicClient interface.
func (c *client) OrderTypes(ctx context.Context) (map[string]map[OrderType]bool,
error) {
res, err := c.httpClient.Get(ctx, "/public/ordertypes", nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch order types: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch order types: %d status code "+
"received", res.StatusCode)
}
types := []struct {
Pair string `json:"currencyPair"`
OrderTypes []OrderType `json:"orderTypes"`
}{}
if err = res.JSON(&types); err != nil {
return nil, fmt.Errorf("failed to unmarshal order types: %w", err)
}
typesMap := make(map[string]map[OrderType]bool)
for _, pair := range types {
typesMap[pair.Pair] = make(map[OrderType]bool)
for _, typ := range pair.OrderTypes {
typesMap[pair.Pair][typ] = true
}
}
return typesMap, nil
}
// OrderTypesForCurrency satisfies the PublicClient interface.
func (c *client) OrderTypesForCurrency(ctx context.Context, pair string) (
map[OrderType]bool, error) {
res, err := c.httpClient.Get(ctx, fmt.Sprintf("/public/%s/ordertypes",
pair), nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch order types for pair: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch order types: %d status code "+
"received", res.StatusCode)
}
var orderTypes []OrderType
if err = res.JSON(&orderTypes); err != nil {
return nil, fmt.Errorf("failed to unmarshal order types: %w", err)
}
typesMap := make(map[OrderType]bool)
for _, typ := range orderTypes {
typesMap[typ] = true
}
return typesMap, nil
}
// Server time contains the time on VALRs servers.
type ServerTime struct {
Epoch int64 `json:"epochTime"`
Time time.Time `json:"time"`
}
// ServerTime satisfies the PublicClient interface.
func (c *client) ServerTime(ctx context.Context) (*ServerTime, error) {
res, err := c.httpClient.Get(ctx, "/public/time", nil)
if err != nil {
return nil, fmt.Errorf("failed to fetch server time: %w", err)
}
if !res.IsSuccess() {
return nil, fmt.Errorf("failed to fetch server time: %d status code "+
"received", res.StatusCode)
}
var serverTime ServerTime
if err = res.JSON(&serverTime); err != nil {
return nil, fmt.Errorf("failed to unmarshal server time: %w", err)
}
return &serverTime, nil
}
// Status describes the current status of VALR.
type Status string
const (
// StatusUnknown implies that the call to VALR failed and we are unable to
// determine VALRs current status.
StatusUnknown Status = "unknown"
// StatusOnline implies that all functionality is available.
StatusOnline Status = "online"
// StatusReadOnly implies that only GET and OPTIONS requests are accepted.
// All other requests in read-only mode will respond with a 503 status code.
StatusReadOnly Status = "read-only"
)
// Status satisfies the PublicClient interface.
func (c *client) Status(ctx context.Context) (Status, error) {
res, err := c.httpClient.Get(ctx, "/public/status", nil)
if err != nil {
return StatusUnknown, fmt.Errorf("failed to fetch status: %w", err)
}
if !res.IsSuccess() {
return StatusUnknown, fmt.Errorf("failed to fetch status: %d status "+
"code received", res.StatusCode)
}
statusObj := struct {
Status Status
}{}
if err = res.JSON(&statusObj); err != nil {
return StatusUnknown, fmt.Errorf("failed to unmarshal status: %w", err)
}
return statusObj.Status, nil
}