-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic.go
149 lines (130 loc) · 3.13 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
145
146
147
148
package btce
import (
"net/url"
"io/ioutil"
"encoding/json"
)
const (
PUBLIC_API_ENDPOINT = "https://btc-e.com/api/3/";
)
type btcePublic struct {
Client *clientTLS
}
type PublicInfo struct {
ServerTime float64 `json:"server_time"`
Pairs map[string] InfoPair `json:"pairs"`
}
type InfoPair struct {
DecimalPlaces int `json:"decimal_places"`
MinPrice float64 `json:"min_price"`
MaxPrice float64 `json:"max_price"`
MinAmount float64 `json:"min_amount"`
Hidden int `json:"hidden"`
Fee int `json:"fee"`
}
type Ticker map[string] TickerPair;
type TickerPair struct {
High float64 `json:"high"`
Low float64 `json:"low"`
AVG float64 `json:"avg"`
Vol float64 `json:"vol"`
VolCur float64 `json:"vol_cur"`
Last float64 `json:"last"`
Buy float64 `json:"buy"`
Sell float64 `json:"sell"`
Updated int `json:"updated"`
}
type Depth map[string] DepthPair;
type DepthPair struct {
Asks [][]float64 `json:"asks"`
Bids [][]float64 `json:"bids"`
}
type Trades map[string] TradesPair;
type TradesPair []TradesPairTrade;
type TradesPairTrade struct {
Type string `json:"type"`
Price float64 `json:"price"`
Amount float64 `json:"amount"`
TID int `json:"tid"`
Timestamp int `json:"timestamp"`
}
func NewPublic() *btcePublic {
client := NewClient("", "", false);
return &btcePublic{client};
}
func (this *btcePublic) Info() (*PublicInfo, error) {
data := url.Values{};
location := PUBLIC_API_ENDPOINT + "/info";
response, err := this.Client.Request(data, location);
if err != nil {
return nil, err;
}
defer response.Body.Close();
body, err := ioutil.ReadAll(response.Body);
if err != nil {
return nil, err;
}
Info := new(PublicInfo);
err = json.Unmarshal([]byte(body), &Info);
if err != nil {
return nil, err;
}
return Info, nil;
}
func (this *btcePublic) Ticker(pair string) (*Ticker, error) {
data := url.Values{};
location := PUBLIC_API_ENDPOINT + "ticker/" + pair;
response, err := this.Client.Request(data, location);
if err != nil {
return nil, err;
}
defer response.Body.Close();
body, err := ioutil.ReadAll(response.Body);
if err != nil {
return nil, err;
}
Ticker := new(Ticker);
err = json.Unmarshal([]byte(body), &Ticker);
if err != nil {
return nil, err;
}
return Ticker, nil;
}
func (this *btcePublic) Depth(pair string) (*Depth, error) {
data := url.Values{};
location := PUBLIC_API_ENDPOINT + "depth/" + pair;
response, err := this.Client.Request(data, location);
if err != nil {
return nil, err;
}
defer response.Body.Close();
body, err := ioutil.ReadAll(response.Body);
if err != nil {
return nil, err;
}
Depth := new(Depth);
err = json.Unmarshal([]byte(body), &Depth);
if err != nil {
return nil, err;
}
return Depth, nil;
}
func (this *btcePublic) Trades(pair string) (*Trades, error) {
data := url.Values{};
location := PUBLIC_API_ENDPOINT + "trades/" + pair;
response, err := this.Client.Request(data, location);
if err != nil {
return nil, err;
}
defer response.Body.Close();
body, err := ioutil.ReadAll(response.Body);
if err != nil {
return nil, err;
}
Trades := new(Trades);
err = json.Unmarshal([]byte(body), &Trades);
if err != nil {
return nil, err;
}
return Trades, nil;
}