forked from Sagleft/uexchange-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade.go
147 lines (126 loc) · 3.58 KB
/
trade.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
package uexchange
import (
"encoding/json"
"errors"
)
func (c *Client) sendTradeTask(orderType string, pairSymbol string, amount, price float64) (int64, error) {
body, err := c.sendRequest(c.getAPIURL("market/"+orderType), mapTable{
"pair": pairSymbol,
"amount": amount,
"price": price,
})
if err != nil {
return 0, err
}
// decode response
var response APITradeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return 0, errors.New("failed to decode request response: " + err.Error())
}
if !response.Success {
return 0, errors.New("failed to send trade task") // TODO
}
return response.Result.OrderID, nil
}
// Buy currency. returns order ID
func (c *Client) Buy(pairSymbol string, amount, price float64) (int64, error) {
return c.sendTradeTask("buy", pairSymbol, amount, price)
}
// Sell currency. returns order ID
func (c *Client) Sell(pairSymbol string, amount, price float64) (int64, error) {
return c.sendTradeTask("sell", pairSymbol, amount, price)
}
// Hold or Unhold order
func (c *Client) Hold(orderID int64) error {
body, err := c.sendRequest(c.getAPIURL("market/hold"), mapTable{
"order_id": orderID,
})
if err != nil {
return err
}
// decode response
var response APITradeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return errors.New("failed to decode request response: " + err.Error())
}
if !response.Success {
return errors.New("failed to send trade task") // TODO
}
return nil
}
// Cancel the specified order
func (c *Client) Cancel(orderID int64) error {
body, err := c.sendRequest(c.getAPIURL("market/cancel"), mapTable{
"order_id": orderID,
})
if err != nil {
return err
}
// decode response
var response APITradeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return errors.New("failed to decode request response: " + err.Error())
}
if !response.Success {
return errors.New("failed to send trade task") // TODO
}
return nil
}
// GetPairs - get trading pairs list
func (c *Client) GetPairs() ([]PairsDataContainer, error) {
body, err := c.sendRequest(c.getAPIURL("market/pairs"), mapTable{})
if err != nil {
return nil, err
}
// decode response
var response APIPairsResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, errors.New("failed to decode request response: " + err.Error())
}
if !response.Success {
return nil, errors.New("failed to get pairs list") // TODO
}
return response.Result, nil
}
// GetOrderBook by trade pair
func (c *Client) GetOrderBook(pairSymbol string) (*BookValueDataContainer, error) {
body, err := c.sendRequest(c.getAPIURL("market/panel"), mapTable{
"pair": pairSymbol,
})
if err != nil {
return nil, err
}
// decode response
var response APIBookValueResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, errors.New("failed to decode request response: " + err.Error())
}
if !response.Success {
return nil, errors.New("failed to get order book by pair") // TODO
}
return &response.Result, nil
}
// GetMarketCurrenciesList - get exchange currencies list
func (c *Client) GetMarketCurrenciesList(pairSymbol string) (*CurrenciesListData, error) {
body, err := c.sendRequest(c.getAPIURL("market/curlist"), mapTable{
"pair": pairSymbol,
})
if err != nil {
return nil, err
}
// decode response
var response APICurrenciesListResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, errors.New("failed to decode request response: " + err.Error())
}
if !response.Success {
return nil, errors.New("failed to get currencies list") // TODO
}
return &response.Result, nil
}