From 3364c159f4ba8aa3e34b51f33b4c4530fffaf193 Mon Sep 17 00:00:00 2001 From: Khushboo-dev-cpp <60327365+Khushboo-dev-cpp@users.noreply.github.com> Date: Mon, 10 Oct 2022 21:02:04 +0200 Subject: [PATCH] feat: Added API for getting historical data for a token (#2882) --- services/wallet/api.go | 10 ++++ services/wallet/cryptocompare.go | 80 ++++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/services/wallet/api.go b/services/wallet/api.go index 312b7dc77..fd8cb3ba0 100644 --- a/services/wallet/api.go +++ b/services/wallet/api.go @@ -305,6 +305,16 @@ func (api *API) FetchMarketValues(ctx context.Context, symbols []string, currenc return fetchTokenMarketValues(symbols, currency) } +func (api *API) GetHourlyMarketValues(ctx context.Context, symbol string, currency string, limit int, aggregate int) ([]TokenHistoricalPairs, error) { + log.Debug("call to GetHourlyMarketValues") + return fetchHourlyMarketValues(symbol, currency, limit, aggregate) +} + +func (api *API) GetDailyMarketValues(ctx context.Context, symbol string, currency string, limit int, allData bool, aggregate int) ([]TokenHistoricalPairs, error) { + log.Debug("call to GetDailyMarketValues") + return fetchDailyMarketValues(symbol, currency, limit, allData, aggregate) +} + func (api *API) FetchTokenDetails(ctx context.Context, symbols []string) (map[string]Coin, error) { log.Debug("call to FetchTokenDetails") return fetchCryptoCompareTokenDetails(symbols) diff --git a/services/wallet/cryptocompare.go b/services/wallet/cryptocompare.go index 13428e009..ac3394638 100644 --- a/services/wallet/cryptocompare.go +++ b/services/wallet/cryptocompare.go @@ -9,6 +9,8 @@ import ( "time" ) +const cryptocompareURL = "https://min-api.cryptocompare.com" + type Coin struct { ID string `json:"Id"` Name string `json:"Name"` @@ -32,6 +34,24 @@ type MarketCoinValues struct { CHANGE24HOUR string `json:"CHANGE24HOUR"` } +type TokenHistoricalPairs struct { + Timestamp int64 `json:"time"` + Value float64 `json:"close"` + Volumefrom float64 `json:"volumefrom"` + Volumeto float64 `json:"volumeto"` +} + +type HistoricalValuesContainer struct { + Aggregated bool `json:"Aggregated"` + TimeFrom int64 `json:"TimeFrom"` + TimeTo int64 `json:"TimeTo"` + HistoricalData []TokenHistoricalPairs `json:"Data"` +} + +type HistoricalValuesData struct { + Data HistoricalValuesContainer `json:"Data"` +} + type CoinsContainer struct { Data map[string]Coin `json:"Data"` } @@ -43,7 +63,7 @@ type MarketValuesContainer struct { func fetchCryptoComparePrices(symbols []string, currency string) (map[string]float64, error) { httpClient := http.Client{Timeout: time.Minute} - url := fmt.Sprintf("https://min-api.cryptocompare.com/data/pricemulti?fsyms=%s&tsyms=%s&extraParams=Status.im", strings.Join(symbols, ","), currency) + url := fmt.Sprintf("%s/data/pricemulti?fsyms=%s&tsyms=%s&extraParams=Status.im", cryptocompareURL, strings.Join(symbols, ","), currency) resp, err := httpClient.Get(url) if err != nil { return nil, err @@ -71,7 +91,7 @@ func fetchCryptoComparePrices(symbols []string, currency string) (map[string]flo func fetchCryptoCompareTokenDetails(symbols []string) (map[string]Coin, error) { httpClient := http.Client{Timeout: time.Minute} - url := "https://min-api.cryptocompare.com/data/all/coinlist" + url := fmt.Sprintf("%s/data/all/coinlist", cryptocompareURL) resp, err := httpClient.Get(url) if err != nil { return nil, err @@ -102,7 +122,7 @@ func fetchTokenMarketValues(symbols []string, currency string) (map[string]Marke item := map[string]MarketCoinValues{} httpClient := http.Client{Timeout: time.Minute} - url := fmt.Sprintf("https://min-api.cryptocompare.com/data/pricemultifull?fsyms=%s&tsyms=%s&extraParams=Status.im", strings.Join(symbols, ","), currency) + url := fmt.Sprintf("%s/data/pricemultifull?fsyms=%s&tsyms=%s&extraParams=Status.im", cryptocompareURL, strings.Join(symbols, ","), currency) resp, err := httpClient.Get(url) if err != nil { return item, err @@ -127,3 +147,57 @@ func fetchTokenMarketValues(symbols []string, currency string) (map[string]Marke return item, nil } + +func fetchHourlyMarketValues(symbol string, currency string, limit int, aggregate int) ([]TokenHistoricalPairs, error) { + item := []TokenHistoricalPairs{} + httpClient := http.Client{Timeout: time.Minute} + + url := fmt.Sprintf("%s/data/v2/histohour?fsym=%s&tsym=%s&aggregate=%d&limit=%d&extraParams=Status.im", cryptocompareURL, symbol, currency, aggregate, limit) + resp, err := httpClient.Get(url) + if err != nil { + return item, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return item, err + } + + container := HistoricalValuesData{} + err = json.Unmarshal(body, &container) + if err != nil { + return item, err + } + + item = container.Data.HistoricalData + + return item, nil +} + +func fetchDailyMarketValues(symbol string, currency string, limit int, allData bool, aggregate int) ([]TokenHistoricalPairs, error) { + item := []TokenHistoricalPairs{} + httpClient := http.Client{Timeout: time.Minute} + + url := fmt.Sprintf("%s/data/v2/histoday?fsym=%s&tsym=%s&aggregate=%d&limit=%d&allData=%v&extraParams=Status.im", cryptocompareURL, symbol, currency, aggregate, limit, allData) + resp, err := httpClient.Get(url) + if err != nil { + return item, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return item, err + } + + container := HistoricalValuesData{} + err = json.Unmarshal(body, &container) + if err != nil { + return item, err + } + + item = container.Data.HistoricalData + + return item, nil +}