-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdepth.go
125 lines (105 loc) · 2.53 KB
/
depth.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
package ant
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/shopspring/decimal"
)
type Order struct {
Price decimal.Decimal
Amount decimal.Decimal
Min decimal.Decimal
Max decimal.Decimal
}
type Depth struct {
Asks []Order `json:"asks"`
Bids []Order `json:"bids"`
}
type Ticker struct {
Base string `json:"echange_asset"`
Quote string `json:"base_asset"`
Price string `json:"price"`
Min string `json:"minimum_amount"`
Max string `json:"maximum_amount"`
}
func GetExinDepth(ctx context.Context, base, quote string) (*Depth, error) {
var depth Depth
if order, err := GetExinOrder(ctx, base, quote); err != nil {
return nil, err
} else {
order.Max = order.Max.Div(order.Price)
order.Min = order.Min.Div(order.Price)
depth.Asks = []Order{*order}
}
if order, err := GetExinOrder(ctx, quote, base); err != nil {
return nil, err
} else {
order.Price = decimal.NewFromFloat(1.0).Div(order.Price)
depth.Bids = []Order{*order}
}
return &depth, nil
}
func GetExinOrder(ctx context.Context, base, quote string) (*Order, error) {
url := "https://exinone.com/exincore/markets" + fmt.Sprintf("?&base_asset=%s", quote)
client := http.Client{
Timeout: 10 * time.Second,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var response struct {
Data map[string]Ticker `json:"data"`
}
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
for _, v := range response.Data {
if v.Base == base {
price, _ := decimal.NewFromString(v.Price)
min, _ := decimal.NewFromString(v.Min)
max, _ := decimal.NewFromString(v.Max)
return &Order{Price: price, Max: max, Min: min}, nil
}
}
return nil, fmt.Errorf("not found.")
}
func GetOceanDepth(ctx context.Context, base, quote string) (*Depth, error) {
url := "https://events.ocean.one/markets/" + fmt.Sprintf("%s-%s", base, quote) + "/book"
client := http.Client{
Timeout: 10 * time.Second,
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var response struct {
Data struct {
Depth `json:"data"`
} `json:"data"`
}
err = json.Unmarshal(body, &response)
return &response.Data.Depth, err
}