-
Notifications
You must be signed in to change notification settings - Fork 0
/
binance.go
59 lines (49 loc) · 1.76 KB
/
binance.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
package binance
import (
"encoding/json"
"net/http"
"github.com/furyanprotocol/v4-chain/protocol/daemons/pricefeed/client/price_function"
"github.com/furyanprotocol/v4-chain/protocol/daemons/pricefeed/types"
)
// BinanceTicker is our representation of ticker information returned in Binance response.
// It implements interface `Ticker` in util.go.
type BinanceTicker struct {
Pair string `json:"symbol" validate:"required"`
AskPrice string `json:"askPrice" validate:"required,positive-float-string"`
BidPrice string `json:"bidPrice" validate:"required,positive-float-string"`
LastPrice string `json:"lastPrice" validate:"required,positive-float-string"`
}
// Ensure that BinanceTicker implements the Ticker interface at compile time.
var _ price_function.Ticker = (*BinanceTicker)(nil)
func (t BinanceTicker) GetPair() string {
// needs to be wrapped in quotes to be consistent with the API request format.
return t.Pair
}
func (t BinanceTicker) GetAskPrice() string {
return t.AskPrice
}
func (t BinanceTicker) GetBidPrice() string {
return t.BidPrice
}
func (t BinanceTicker) GetLastPrice() string {
return t.LastPrice
}
// BinancePriceFunction transforms an API response from Binance into a map of tickers to prices that have been
// shifted by a market specific exponent.
func BinancePriceFunction(
response *http.Response,
tickerToExponent map[string]int32,
resolver types.Resolver,
) (tickerToPrice map[string]uint64, unavailableTickers map[string]error, err error) {
// Unmarshal response body into a list of tickers.
var binanceTickers []BinanceTicker
err = json.NewDecoder(response.Body).Decode(&binanceTickers)
if err != nil {
return nil, nil, err
}
return price_function.GetMedianPricesFromTickers(
binanceTickers,
tickerToExponent,
resolver,
)
}