forked from baibaratsky/go-poloniex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic.go
144 lines (115 loc) · 3.21 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
package poloniex
import (
"context"
"encoding/json"
"errors"
"github.com/shopspring/decimal"
)
const (
publicApiEndpoint = "https://poloniex.com/public"
// Order unmarshalling constants
orderParamsCount = 2
orderRateIndex = 0
orderAmountIndex = 1
)
type Market struct {
Id uint32
Last decimal.Decimal
LowestAsk decimal.Decimal
HighestBid decimal.Decimal
PercentChange decimal.Decimal
BaseVolume decimal.Decimal
QuoteVolume decimal.Decimal
IsFrozen convertibleBool
High24hr decimal.Decimal
Low24hr decimal.Decimal
}
type Ticker map[string]Market
func (client *Client) Ticker() (ticker Ticker, err error) {
err = client.publicApiRequest(&ticker, "returnTicker")
return
}
type Order struct {
Rate decimal.Decimal
Amount decimal.Decimal
Total decimal.Decimal
}
func (order *Order) UnmarshalJSON(data []byte) error {
var orderParams [orderParamsCount]decimal.Decimal
if err := json.Unmarshal(data, &orderParams); err != nil {
return err
}
order.Rate = orderParams[orderRateIndex]
order.Amount = orderParams[orderAmountIndex]
order.CalculateTotal()
return nil
}
func (order *Order) CalculateTotal() {
if order.Amount.Equal(decimal.Zero) {
return
}
order.Total = order.Rate.Mul(order.Amount)
}
type OrderBook struct {
Asks []Order `json:"asks"`
Bids []Order `json:"bids"`
IsFrozen convertibleBool `json:"isFrozen"`
Sequence uint `json:"seq"`
}
func (client *Client) OrderBook(currencyPair string) (orderBook OrderBook, err error) {
err = client.publicApiRequest(&orderBook, "returnOrderBook", Params{
"currencyPair": currencyPair,
})
return orderBook, err
}
func (client *Client) OrderBookAll() (orderBooks map[string]OrderBook, err error) {
err = client.publicApiRequest(&orderBooks, "returnOrderBook", Params{
"currencyPair": "all",
})
return orderBooks, err
}
func (client *Client) Currencies() (currencies map[string]Currency, err error) {
err = client.publicApiRequest(¤cies, "returnCurrencies", Params{})
return currencies, err
}
type Currency struct {
Id uint `json:"id"`
Name string `json:"name"`
TaxFee decimal.Decimal `json:"txFee"`
MinimumConf uint `json:"minConf"`
DepositAddress string `json:"depositAddress"`
Disabled convertibleBool `json:"disabled"`
Frozen convertibleBool `json:"frozen"`
Delisted convertibleBool `json:"delisted"`
}
func (client *Client) publicApiRequest(result interface{}, method string, params ...Params) error {
if len(params) > 1 {
return errors.New("too much arguments")
}
queryParams := Params{"command": method}
if len(params) == 1 {
for name, value := range params[0] {
queryParams[name] = value
}
}
err := client.limiter.Wait(context.TODO())
if err != nil {
return err
}
response, err := client.resty.R().
SetQueryParams(queryParams).
Get(publicApiEndpoint)
if err != nil {
return err
}
errorResponse := errorResponse{}
err = json.Unmarshal(response.Body(), &errorResponse)
if err != nil {
return err
}
if errorResponse.Error != nil {
return errors.New(*errorResponse.Error)
}
err = json.Unmarshal(response.Body(), result)
return err
}