-
Notifications
You must be signed in to change notification settings - Fork 0
/
gate.go
58 lines (48 loc) · 1.65 KB
/
gate.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
package gate
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"
)
// GateTicker is our representation of ticker information returned in Gate response.
// Need to implement interface `Ticker` in util.go.
type GateTicker struct {
Pair string `json:"currency_pair" validate:"required"`
AskPrice string `json:"lowest_ask" validate:"required,positive-float-string"`
BidPrice string `json:"highest_bid" validate:"required,positive-float-string"`
LastPrice string `json:"last" validate:"required,positive-float-string"`
}
// Ensure that GateTicker implements the Ticker interface at compile time.
var _ price_function.Ticker = (*GateTicker)(nil)
func (t GateTicker) GetPair() string {
return t.Pair
}
func (t GateTicker) GetAskPrice() string {
return t.AskPrice
}
func (t GateTicker) GetBidPrice() string {
return t.BidPrice
}
func (t GateTicker) GetLastPrice() string {
return t.LastPrice
}
// GatePriceFunction transforms an API response from Gate into a map of tickers to prices that have been
// shifted by a market specific exponent.
func GatePriceFunction(
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 gateTickers []GateTicker
err = json.NewDecoder(response.Body).Decode(&gateTickers)
if err != nil {
return nil, nil, err
}
return price_function.GetMedianPricesFromTickers(
gateTickers,
tickerToExponent,
resolver,
)
}