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