/
poloniex.go
67 lines (55 loc) · 1.35 KB
/
poloniex.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
// Package poloniex poloniex rest api package
package poloniex
import (
"io/ioutil"
"net/http"
"strings"
"github.com/Akagi201/cryptotrader/model"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
const (
API = "https://poloniex.com/public"
)
// Poloniex API data
type Poloniex struct {
AccessKey string
SecretKey string
}
// New create new Poloniex API data
func New(accessKey string, secretKey string) *Poloniex {
return &Poloniex{
AccessKey: accessKey,
SecretKey: secretKey,
}
}
// GetTicker 行情
func (pl *Poloniex) GetTicker(base string, quote string) (*model.Ticker, error) {
url := API + "?command=returnTicker"
log.Debugf("Request url: %v", url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
log.Debugf("Response body: %v", string(body))
tickers := gjson.ParseBytes(body).Map()
key := strings.ToUpper(base) + "_" + strings.ToUpper(quote)
if !tickers[key].Exists() {
return nil, errors.New("The ticker not exists")
}
v := tickers[key]
return &model.Ticker{
Buy: v.Get("highestBid").Float(),
Sell: v.Get("lowestAsk").Float(),
Last: v.Get("last").Float(),
Low: v.Get("low24hr").Float(),
High: v.Get("high24hr").Float(),
Vol: v.Get("baseVolume").Float(),
}, nil
}