-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarantex.go
186 lines (161 loc) · 4.36 KB
/
garantex.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
package marketdata
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
type garantex struct {
baseurl, api, dom, history string
}
func Garantex() garantex {
return garantex{
baseurl: "https://garantex.org",
api: "api/v2",
dom: "/depth",
history: "/trades",
}
}
//####################################################################
// Depth Of Market
//####################################################################
// GetDOM recieves depth of market
//
// Availible markets: btcrub, usdtrub, dairub, ethrub, usdcrub, btcusdt, ethbtc, ethusdt, usdcusdt
func (g garantex) GetDOM(market string) (DOM, error) {
u, _ := url.ParseRequestURI(g.baseurl)
u.Path = g.api + g.dom
params := url.Values{"market": []string{market}}
u.RawQuery = params.Encode()
resp, err := http.Get(u.String())
if err != nil {
return DOM{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return DOM{}, fmt.Errorf("request returned bad status code %s", resp.Status)
}
respBody, _ := io.ReadAll(resp.Body)
respBytes := []byte(respBody)
var model garantexDOMResponse
err = json.Unmarshal(respBytes, &model)
if err != nil {
return DOM{}, err
}
result := model.toEntity()
return result, nil
}
// DOM Response
type garantexDOMResponse struct {
Timestamp float64 `json:"timestamp"`
Bids []garantexDOMPosition `json:"bids"`
Asks []garantexDOMPosition `json:"asks"`
}
type garantexDOMPosition struct {
Price string `json:"price"`
Volume string `json:"volume"`
Amount string `json:"amount"`
Type string `json:"type"`
Factor string `json:"factor"`
}
func (b garantexDOMResponse) toEntity() DOM {
bids := make([]DOMPosition, 0, len(b.Bids))
asks := make([]DOMPosition, 0, len(b.Asks))
for _, p := range b.Bids {
bids = append(bids, p.toEntity())
}
for _, p := range b.Asks {
asks = append(asks, p.toEntity())
}
return DOM{
Date: time.Unix(int64(b.Timestamp), 0),
Bids: bids,
Asks: asks,
}
}
func (p garantexDOMPosition) toEntity() DOMPosition {
return DOMPosition{
Price: p.Price,
Amount: p.Amount,
}
}
// History
type GarantexHistoryConfig struct {
Market string // btcrub, usdtrub, dairub, ethrub, usdcrub, btcusdt, ethbtc, ethusdt, usdcusdt
Limit int // optional records amount (default 50, max 1000)
From int // optional trade ID to get data from (but not including)
To int // optional trade ID to get data to (but not including)
Order string // optional sorting order ASC DESC
}
func (c GarantexHistoryConfig) toParams() url.Values {
values := make(url.Values)
values.Add("market", c.Market)
if c.Limit != 0 {
values.Add("limit", strconv.Itoa(c.Limit))
}
if c.From != 0 {
values.Add("from", strconv.Itoa(c.From))
}
if c.To != 0 {
values.Add("to", strconv.Itoa(c.To))
}
if c.Order != "" {
values.Add("order_by", c.Order)
}
return values
}
// GetHistory recieves market trading history.
func (g garantex) GetHistory(config GarantexHistoryConfig) ([]HistoryPosition, error) {
raw, err := g.getHistory(config.toParams())
if err != nil {
return nil, err
}
return raw.toEntity(), nil
}
func (g garantex) getHistory(params url.Values) (garantexHistoryResponce, error) {
u, _ := url.ParseRequestURI(g.baseurl)
u.Path = g.api + g.history
u.RawQuery = params.Encode()
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request returned bad status code %s", resp.Status)
}
respBody, _ := io.ReadAll(resp.Body)
respBytes := []byte(respBody)
var model garantexHistoryResponce
err = json.Unmarshal(respBytes, &model)
return model, err
}
// History response
type garantexHistoryResponce []garantexHistoryResponcePosition
func (r garantexHistoryResponce) toEntity() []HistoryPosition {
data := make([]HistoryPosition, 0, len(r))
for _, el := range r {
data = append(data, el.toEntity())
}
return data
}
// History responce position
type garantexHistoryResponcePosition struct {
ID float64 `json:"id"`
Date string `json:"created_at"`
Price string `json:"price"`
Volume string `json:"volume"`
Funds string `json:"funds"`
}
func (p garantexHistoryResponcePosition) toEntity() HistoryPosition {
date, _ := time.Parse(time.RFC3339, p.Date)
return HistoryPosition{
ID: uint(p.ID),
Date: date,
Price: p.Price,
Volume: p.Volume,
}
}