-
Notifications
You must be signed in to change notification settings - Fork 0
/
huobi.go
69 lines (57 loc) · 2.04 KB
/
huobi.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
package huobi
import (
"encoding/json"
"errors"
"net/http"
"github.com/furyanprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/furyanprotocol/v4-chain/protocol/daemons/pricefeed/types"
)
// HuobiResponseBody defines the overall Huobi response.
type HuobiResponseBody struct {
Status string `json:"status" validate:"required"`
Tickers []HuobiTicker `json:"data" validate:"required"`
}
// HuobiTicker is our representation of ticker information returned in Huobi response.
// HuobiTicker implements interface `Ticker` in util.go.
type HuobiTicker struct {
Pair string `json:"symbol" validate:"required"`
AskPrice float64 `json:"ask" validate:"required,gt=0"`
BidPrice float64 `json:"bid" validate:"required,gt=0"`
LastPrice float64 `json:"close" validate:"required,gt=0"`
}
// Ensure that HuobiTicker implements the Ticker interface at compile time.
var _ price_function.Ticker = (*HuobiTicker)(nil)
func (t HuobiTicker) GetPair() string {
return t.Pair
}
func (t HuobiTicker) GetAskPrice() string {
return price_function.ConvertFloat64ToString(t.AskPrice)
}
func (t HuobiTicker) GetBidPrice() string {
return price_function.ConvertFloat64ToString(t.BidPrice)
}
func (t HuobiTicker) GetLastPrice() string {
return price_function.ConvertFloat64ToString(t.LastPrice)
}
// HuobiPriceFunction transforms an API response from Huobi into a map of tickers to prices that have been
// shifted by a market specific exponent.
func HuobiPriceFunction(
response *http.Response,
tickerToExponent map[string]int32,
resolver types.Resolver,
) (tickerToPrice map[string]uint64, unavailableTickers map[string]error, err error) {
// Unmarshal response body.
var huobiResponseBody HuobiResponseBody
err = json.NewDecoder(response.Body).Decode(&huobiResponseBody)
if err != nil {
return nil, nil, err
}
if huobiResponseBody.Status != "ok" {
return nil, nil, errors.New(`huobi response status is not "ok"`)
}
return price_function.GetMedianPricesFromTickers(
huobiResponseBody.Tickers,
tickerToExponent,
resolver,
)
}