|
| 1 | +package exchange |
| 2 | + |
| 3 | +import ( |
| 4 | +"fmt" |
| 5 | +"github.com/preichenberger/go-coinbasepro/v2" |
| 6 | +"github.com/sirupsen/logrus" |
| 7 | +"math" |
| 8 | +"net/http" |
| 9 | +"sort" |
| 10 | +"strconv" |
| 11 | +"time" |
| 12 | +) |
| 13 | + |
| 14 | +type coinbaseClient struct { |
| 15 | +coinbasepro *coinbasepro.Client |
| 16 | +} |
| 17 | + |
| 18 | +//type coinbaseClient *coinbasepro.Client |
| 19 | + |
| 20 | +func NewCoinBaseClient(httpClient *http.Client) *coinbaseClient { |
| 21 | +client := coinbasepro.NewClient() |
| 22 | +client.HTTPClient = httpClient |
| 23 | +return &coinbaseClient{coinbasepro: client} |
| 24 | +} |
| 25 | + |
| 26 | +func (client *coinbaseClient) GetName() string { |
| 27 | +return "Coinbase" |
| 28 | +} |
| 29 | + |
| 30 | +func (client *coinbaseClient) GetPriceRightAfter(candles []coinbasepro.HistoricRate, after time.Time) (float64, error) { |
| 31 | +for _, candle := range candles { |
| 32 | +if after.Equal(candle.Time) || after.After(candle.Time) { |
| 33 | +// Assume candles are sorted in desc order, so the first less than or equal to is the candle looking for |
| 34 | +logrus.Debugf("%s - Kline for %v uses open price at %v", client.GetName(), after.Local(), candle.Time.Local()) |
| 35 | +return candle.Open, nil |
| 36 | +} |
| 37 | +} |
| 38 | +return 0, fmt.Errorf("no time found right after %v", after) |
| 39 | +} |
| 40 | + |
| 41 | +func (client *coinbaseClient) GetSymbolPrice(symbol string) (*SymbolPrice, error) { |
| 42 | +ticker, err := client.coinbasepro.GetTicker(symbol) |
| 43 | +if err != nil { |
| 44 | +return nil, err |
| 45 | +} |
| 46 | +currentPrice, err := strconv.ParseFloat(ticker.Price, 64) |
| 47 | +if err != nil { |
| 48 | +return nil, err |
| 49 | +} |
| 50 | + |
| 51 | +var percentChange1h, percentChange24h = math.MaxFloat64, math.MaxFloat64 |
| 52 | +candles, err := client.coinbasepro.GetHistoricRates(symbol, coinbasepro.GetHistoricRatesParams{ |
| 53 | +Granularity: 300, |
| 54 | +}) |
| 55 | +if err != nil { |
| 56 | +logrus.Warnf("%s - Failed to get kline ticks, error: %v", client.GetName(), err) |
| 57 | +} else { |
| 58 | +now := time.Now() |
| 59 | +sort.Slice(candles, func(i, j int) bool { return candles[i].Time.After(candles[j].Time) }) |
| 60 | + |
| 61 | +lastHour := now.Add(-1 * time.Hour) |
| 62 | +price1hAgo, err := client.GetPriceRightAfter(candles, lastHour) |
| 63 | +if err != nil { |
| 64 | +logrus.Warnf("%s - Failed to get price 1 hour ago, error: %v\n", client.GetName(), err) |
| 65 | +} else if price1hAgo != 0 { |
| 66 | +percentChange1h = (currentPrice - price1hAgo) / price1hAgo * 100 |
| 67 | +} |
| 68 | + |
| 69 | +last24Hour := now.Add(-24 * time.Hour) |
| 70 | +price24hAgo, err := client.GetPriceRightAfter(candles, last24Hour) |
| 71 | +if err != nil { |
| 72 | +logrus.Warnf("%s - Failed to get price 24 hours ago, error: %v\n", client.GetName(), err) |
| 73 | +} else if price24hAgo != 0 { |
| 74 | +percentChange24h = (currentPrice - price24hAgo) / price24hAgo * 100 |
| 75 | +} |
| 76 | +} |
| 77 | + |
| 78 | +return &SymbolPrice{ |
| 79 | +Symbol: symbol, |
| 80 | +Price: ticker.Price, |
| 81 | +UpdateAt: time.Time(ticker.Time), |
| 82 | +Source: client.GetName(), |
| 83 | +PercentChange1h: percentChange1h, |
| 84 | +PercentChange24h: percentChange24h, |
| 85 | +}, nil |
| 86 | +} |
| 87 | + |
| 88 | +func init() { |
| 89 | +register((&coinbaseClient{}).GetName(), func(client *http.Client) ExchangeClient { |
| 90 | +// Limited by type system in Go, I hate wrapper/adapter |
| 91 | +return NewCoinBaseClient(client) |
| 92 | +}) |
| 93 | +} |
0 commit comments