|
| 1 | +package exchange |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "github.com/sirupsen/logrus" |
| 8 | + "io" |
| 9 | + "math" |
| 10 | + "net/http" |
| 11 | + "strconv" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// https://developer.big.one/ |
| 17 | +const bigOneBaseApi = "https://api.big.one/" |
| 18 | + |
| 19 | +type bigOneClient struct { |
| 20 | + exchangeBaseClient |
| 21 | + AccessKey string |
| 22 | + SecretKey string |
| 23 | +} |
| 24 | + |
| 25 | +type bigOneErrorResponse struct { |
| 26 | + Error *struct { |
| 27 | + Status int |
| 28 | + Code int |
| 29 | + Description string |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type bigOneMarketResponse struct { |
| 34 | + bigOneErrorResponse |
| 35 | + Data struct { |
| 36 | + Symbol string |
| 37 | + Ticker struct { |
| 38 | + Price float64 `json:",string"` |
| 39 | + } |
| 40 | + //Metrics map[string][]interface{} |
| 41 | + Metrics struct { |
| 42 | + // timestamp, open, close, high, low, volume |
| 43 | + Min1 [][]interface{} `json:"0000001"` |
| 44 | + Min5 [][]interface{} `json:"0000005"` |
| 45 | + Min15 [][]interface{} `json:"0000015"` |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func NewBigOneClient(httpClient *http.Client) *bigOneClient { |
| 51 | + return &bigOneClient{exchangeBaseClient: *newExchangeBase(bigOneBaseApi, httpClient)} |
| 52 | +} |
| 53 | + |
| 54 | +func (client *bigOneClient) GetName() string { |
| 55 | + return "BigONE" |
| 56 | +} |
| 57 | + |
| 58 | +func (client *bigOneClient) decodeResponse(body io.ReadCloser, respJSON zbCommonResponseProvider) error { |
| 59 | + defer body.Close() |
| 60 | + |
| 61 | + decoder := json.NewDecoder(body) |
| 62 | + if err := decoder.Decode(&respJSON); err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + // All I need is to get the common part, I don't like this |
| 67 | + commonResponse := respJSON.getCommonResponse() |
| 68 | + if commonResponse.Error != nil { |
| 69 | + return errors.New(*commonResponse.Error) |
| 70 | + } |
| 71 | + if commonResponse.Message != nil { |
| 72 | + return errors.New(*commonResponse.Message) |
| 73 | + } |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func (client *bigOneClient) SearchKlinePriceNear(klineIntervals [][]interface{}, after time.Time) (float64, error) { |
| 78 | + var intervalTime time.Time |
| 79 | + for _, interval := range klineIntervals { |
| 80 | + if ts, ok := interval[0].(float64); ok { |
| 81 | + intervalTime = time.Unix(int64(ts)/1000, 0) |
| 82 | + if after.Equal(intervalTime) || after.After(intervalTime) { |
| 83 | + // Assume candles are sorted in asc order, so the first less than or equal to is the candle looking for |
| 84 | + logrus.Debugf("%s - Kline for %v uses open price at %v", client.GetName(), after.Local(), intervalTime.Local()) |
| 85 | + if openStr, ok := interval[1].(string); ok { |
| 86 | + return strconv.ParseFloat(openStr, 64) |
| 87 | + } else { |
| 88 | + return 0, fmt.Errorf("cannot convert open price item %v of kline to string", interval[1]) |
| 89 | + } |
| 90 | + } |
| 91 | + } else { |
| 92 | + return 0, fmt.Errorf("cannot convert first item %v of kline to float64", interval[0]) |
| 93 | + } |
| 94 | + } |
| 95 | + return 0, fmt.Errorf("no time found right after %v, the last time in this interval is %v", after.Local(), intervalTime.Local()) |
| 96 | +} |
| 97 | + |
| 98 | +func (client *bigOneClient) GetSymbolPrice(symbol string) (*SymbolPrice, error) { |
| 99 | + // One api to get all |
| 100 | + resp, err := client.httpGet("markets/"+strings.ToUpper(symbol), nil) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + defer resp.Body.Close() |
| 105 | + |
| 106 | + var respJSON bigOneMarketResponse |
| 107 | + |
| 108 | + decoder := json.NewDecoder(resp.Body) |
| 109 | + if err := decoder.Decode(&respJSON); err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + if respJSON.Error != nil { |
| 114 | + return nil, errors.New(respJSON.Error.Description) |
| 115 | + } |
| 116 | + |
| 117 | + var ( |
| 118 | + now = time.Now() |
| 119 | + percentChange1h, percentChange24h = math.MaxFloat64, math.MaxFloat64 |
| 120 | + ) |
| 121 | + price1hAgo, err := client.SearchKlinePriceNear(respJSON.Data.Metrics.Min1, now.Add(-1*time.Hour)) |
| 122 | + if price1hAgo == 0 { |
| 123 | + // BigOne has a very low volume, that prices after certain amount are all zero, so enlarge intervals here. |
| 124 | + price1hAgo, err = client.SearchKlinePriceNear(respJSON.Data.Metrics.Min5, now.Add(-1*time.Hour)) |
| 125 | + } |
| 126 | + if err != nil { |
| 127 | + logrus.Warnf("%s - Failed to get price 1 hour ago, error: %v\n", client.GetName(), err) |
| 128 | + } else if price1hAgo != 0 { |
| 129 | + percentChange1h = (respJSON.Data.Ticker.Price - price1hAgo) / price1hAgo * 100 |
| 130 | + } |
| 131 | + |
| 132 | + price24hAgo, err := client.SearchKlinePriceNear(respJSON.Data.Metrics.Min15, now.Add(-24*time.Hour)) |
| 133 | + if err != nil { |
| 134 | + logrus.Warnf("%s - Failed to get price 24 hours ago, error: %v\n", client.GetName(), err) |
| 135 | + } else if price24hAgo != 0 { |
| 136 | + percentChange24h = (respJSON.Data.Ticker.Price - price24hAgo) / price24hAgo * 100 |
| 137 | + } |
| 138 | + |
| 139 | + return &SymbolPrice{ |
| 140 | + Symbol: symbol, |
| 141 | + Price: strconv.FormatFloat(respJSON.Data.Ticker.Price, 'f', -1, 64), |
| 142 | + UpdateAt: time.Now(), |
| 143 | + Source: client.GetName(), |
| 144 | + PercentChange1h: percentChange1h, |
| 145 | + PercentChange24h: percentChange24h, |
| 146 | + }, nil |
| 147 | +} |
| 148 | + |
| 149 | +func init() { |
| 150 | + register((&bigOneClient{}).GetName(), func(client *http.Client) ExchangeClient { |
| 151 | + // Limited by type system in Go, I hate wrapper/adapter |
| 152 | + return NewBigOneClient(client) |
| 153 | + }) |
| 154 | +} |
0 commit comments