forked from bitontop/gored
/
public.go
143 lines (116 loc) · 3.57 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
package gateio
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/chz8494/gored/exchange"
"github.com/chz8494/gored/utils"
)
func (e *Gateio) LoadPublicData(operation *exchange.PublicOperation) error {
switch operation.Type {
case exchange.TradeHistory:
return e.doTradeHistory(operation)
case exchange.Orderbook:
if operation.Wallet == exchange.SpotWallet {
return e.doSpotOrderBook(operation)
}
}
return fmt.Errorf("LoadPublicData :: Operation type invalid: %+v", operation.Type)
}
func (e *Gateio) doSpotOrderBook(op *exchange.PublicOperation) error {
orderBook := OrderBook{}
symbol := e.GetSymbolByPair(op.Pair)
maker := &exchange.Maker{
WorkerIP: utils.GetExternalIP(),
Source: exchange.EXCHANGE_API,
BeforeTimestamp: float64(time.Now().UnixNano() / 1e6),
}
/******
// strRequestUrl := fmt.Sprintf("/public/orderbook/%s", e.GetIDByPair(pair))
// strUrl := API3_URL + strRequestUrl
// jsonOrderbook := exchange.HttpGetRequest(strUrl, nil)
********/
//!------
get := &utils.HttpGet{
URI: fmt.Sprintf("%s/api2/1/orderBook/%s", API_URL, symbol),
Proxy: op.Proxy,
DebugMode: op.DebugMode,
}
if err := utils.HttpGetRequest(get); err != nil {
op.Error = err
return op.Error
}
jsonOrderbook := get.ResponseBody
//! ###
if err := json.Unmarshal([]byte(jsonOrderbook), &orderBook); err != nil {
return fmt.Errorf("%s Get Orderbook Json Unmarshal Err: %s %s", e.GetName(), err, jsonOrderbook)
} else if orderBook.Result != "true" {
return fmt.Errorf("%s Get Orderbook Failed: %s", e.GetName(), jsonOrderbook)
}
maker.AfterTimestamp = float64(time.Now().UnixNano() / 1e6)
var err error
for _, bid := range orderBook.Bids {
var buydata exchange.Order
//Modify according to type and structure
buydata.Rate, err = strconv.ParseFloat(bid[0], 64)
if err != nil {
return fmt.Errorf("GateIo Bids Rate ParseFloat error:%v", err)
}
buydata.Quantity, err = strconv.ParseFloat(bid[1], 64)
if err != nil {
return fmt.Errorf("GateIo Bids Quantity ParseFloat error:%v", err)
}
maker.Bids = append(maker.Bids, buydata)
}
for _, ask := range orderBook.Asks {
var selldata exchange.Order
//Modify according to type and structure
selldata.Rate, err = strconv.ParseFloat(ask[0], 64)
if err != nil {
return fmt.Errorf("GateIo Asks Rate ParseFloat error:%v", err)
}
selldata.Quantity, err = strconv.ParseFloat(ask[1], 64)
if err != nil {
return fmt.Errorf("GateIo Asks Quantity ParseFloat error:%v", err)
}
maker.Asks = append(maker.Asks, selldata)
}
op.Maker = maker
return nil
}
func (e *Gateio) doTradeHistory(operation *exchange.PublicOperation) error {
symbol := e.GetSymbolByPair(operation.Pair)
get := &utils.HttpGet{
URI: fmt.Sprintf("%s/api2/1/tradeHistory/%s", API_URL, symbol),
Proxy: operation.Proxy,
}
err := utils.HttpGetRequest(get)
if err != nil {
return err
} else {
tradeHistory := &TradeHistory{}
if err := json.Unmarshal(get.ResponseBody, &tradeHistory); err != nil {
return err
}
operation.TradeHistory = []*exchange.TradeDetail{}
for _, d := range tradeHistory.Data {
td := &exchange.TradeDetail{}
td.ID = d.TradeID
if d.Type == "buy" {
td.Direction = exchange.Buy
} else if d.Type == "sell" {
td.Direction = exchange.Sell
}
td.Quantity, err = strconv.ParseFloat(d.Amount, 64)
td.Rate, err = strconv.ParseFloat(d.Rate, 64)
t, err := strconv.ParseInt(d.Timestamp, 10, 64)
if err != nil {
return err
}
td.TimeStamp = t * 1000
operation.TradeHistory = append(operation.TradeHistory, td)
}
}
return nil
}