|
| 1 | +package exchange |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "github.com/sirupsen/logrus" |
| 7 | + "math" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// https://api.hitbtc.com/#market-data |
| 15 | +const hitBtcBaseApi = "https://api.hitbtc.com/api/2/" |
| 16 | + |
| 17 | +// ZB api is very similar to OKEx, who copied whom? |
| 18 | + |
| 19 | +type hitBtcClient struct { |
| 20 | + exchangeBaseClient |
| 21 | + AccessKey string |
| 22 | + SecretKey string |
| 23 | +} |
| 24 | + |
| 25 | +type hitBtcCommonResponse struct { |
| 26 | + Error *struct { |
| 27 | + Code int |
| 28 | + Message string |
| 29 | + Description string |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type hitBtcTickerResponse struct { |
| 34 | + hitBtcCommonResponse |
| 35 | + Last float64 `json:",string"` |
| 36 | + Open float64 `json:",string"` |
| 37 | + Timestamp string |
| 38 | +} |
| 39 | + |
| 40 | +type hitBtcKlineResponse struct { |
| 41 | + hitBtcCommonResponse |
| 42 | + Timestamp string |
| 43 | + Open float64 `json:",string"` |
| 44 | +} |
| 45 | + |
| 46 | +func (resp *hitBtcTickerResponse) getCommonResponse() hitBtcCommonResponse { |
| 47 | + return resp.hitBtcCommonResponse |
| 48 | +} |
| 49 | + |
| 50 | +func (resp *hitBtcKlineResponse) getCommonResponse() hitBtcCommonResponse { |
| 51 | + return resp.hitBtcCommonResponse |
| 52 | +} |
| 53 | + |
| 54 | +// Any way to hold the common response, instead of adding an interface here? |
| 55 | +type hitBtcCommonResponseProvider interface { |
| 56 | + getCommonResponse() hitBtcCommonResponse |
| 57 | +} |
| 58 | + |
| 59 | +func NewHitBtcClient(httpClient *http.Client) *hitBtcClient { |
| 60 | + return &hitBtcClient{exchangeBaseClient: *newExchangeBase(hitBtcBaseApi, httpClient)} |
| 61 | +} |
| 62 | + |
| 63 | +func (client *hitBtcClient) GetName() string { |
| 64 | + return "HitBTC" |
| 65 | +} |
| 66 | + |
| 67 | +func (client *hitBtcClient) decodeResponse(resp *http.Response, respJSON hitBtcCommonResponseProvider) error { |
| 68 | + defer resp.Body.Close() |
| 69 | + |
| 70 | + decoder := json.NewDecoder(resp.Body) |
| 71 | + if err := decoder.Decode(&respJSON); err != nil { |
| 72 | + if resp.StatusCode != 200 { |
| 73 | + return errors.New(resp.Status) |
| 74 | + } |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + // All I need is to get the common part, I don't like this |
| 79 | + commonResponse := respJSON.getCommonResponse() |
| 80 | + if commonResponse.Error != nil { |
| 81 | + return errors.New(commonResponse.Error.Message + " - " + commonResponse.Error.Description) |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (client *hitBtcClient) GetKlinePrice(symbol, period string, limit int) (float64, error) { |
| 87 | + symbol = strings.ToLower(symbol) |
| 88 | + resp, err := client.httpGet("public/candles/"+strings.ToUpper(symbol), map[string]string{ |
| 89 | + "period": period, |
| 90 | + "limit": strconv.Itoa(limit), |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + return 0, err |
| 94 | + } |
| 95 | + |
| 96 | + if resp.StatusCode != 200 { |
| 97 | + var respJSON hitBtcKlineResponse |
| 98 | + return 0, client.decodeResponse(resp, &respJSON) |
| 99 | + } |
| 100 | + var respJSON []hitBtcKlineResponse |
| 101 | + defer resp.Body.Close() |
| 102 | + |
| 103 | + decoder := json.NewDecoder(resp.Body) |
| 104 | + if err := decoder.Decode(&respJSON); err != nil { |
| 105 | + if resp.StatusCode != 200 { |
| 106 | + return 0, errors.New(resp.Status) |
| 107 | + } |
| 108 | + return 0, err |
| 109 | + } |
| 110 | + logrus.Debugf("%s - Kline for %s*%v uses price at %s", client.GetName(), period, limit, respJSON[0].Timestamp) |
| 111 | + return respJSON[0].Open, nil |
| 112 | +} |
| 113 | + |
| 114 | +func (client *hitBtcClient) GetSymbolPrice(symbol string) (*SymbolPrice, error) { |
| 115 | + resp, err := client.httpGet("/public/ticker/"+strings.ToUpper(symbol), nil) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + var respJSON hitBtcTickerResponse |
| 121 | + err = client.decodeResponse(resp, &respJSON) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + updated, err := time.Parse(time.RFC3339, respJSON.Timestamp) |
| 126 | + if err != nil { |
| 127 | + return nil, err |
| 128 | + } |
| 129 | + |
| 130 | + var percentChange1h, percentChange24h = math.MaxFloat64, math.MaxFloat64 |
| 131 | + price1hAgo, err := client.GetKlinePrice(symbol, "M1", 60) |
| 132 | + if err != nil { |
| 133 | + logrus.Warnf("%s - Failed to get price 1 hour ago, error: %v\n", client.GetName(), err) |
| 134 | + } else if price1hAgo != 0 { |
| 135 | + percentChange1h = (respJSON.Last - price1hAgo) / price1hAgo * 100 |
| 136 | + } |
| 137 | + |
| 138 | + //price24hAgo_, err := client.GetKlinePrice(symbol, "M3", 480) |
| 139 | + //logrus.Warnf("%s - %s", price24hAgo_, respJSON.Open) |
| 140 | + |
| 141 | + price24hAgo := respJSON.Open |
| 142 | + percentChange24h = (respJSON.Last - price24hAgo) / price24hAgo * 100 |
| 143 | + |
| 144 | + return &SymbolPrice{ |
| 145 | + Symbol: symbol, |
| 146 | + Price: strconv.FormatFloat(respJSON.Last, 'f', -1, 64), |
| 147 | + UpdateAt: updated, |
| 148 | + Source: client.GetName(), |
| 149 | + PercentChange1h: percentChange1h, |
| 150 | + PercentChange24h: percentChange24h, |
| 151 | + }, nil |
| 152 | +} |
| 153 | + |
| 154 | +func init() { |
| 155 | + register((&hitBtcClient{}).GetName(), func(client *http.Client) ExchangeClient { |
| 156 | + // Limited by type system in Go, I hate wrapper/adapter |
| 157 | + return NewHitBtcClient(client) |
| 158 | + }) |
| 159 | +} |
0 commit comments