diff --git a/services/wallet/api.go b/services/wallet/api.go index 62c37668f..a41d8b1f1 100644 --- a/services/wallet/api.go +++ b/services/wallet/api.go @@ -261,3 +261,8 @@ func (api *API) GetEthereumChains(ctx context.Context, onlyEnabled bool) ([]*par log.Debug("call to GetEthereumChains") return api.s.rpcClient.NetworkManager.Get(onlyEnabled) } + +func (api *API) FetchPrices(ctx context.Context, symbols []string, currency string) (map[string]float64, error) { + log.Debug("call to FetchPrices") + return fetchCryptoComparePrices(symbols, currency) +} diff --git a/services/wallet/price.go b/services/wallet/price.go new file mode 100644 index 000000000..3a053ce2b --- /dev/null +++ b/services/wallet/price.go @@ -0,0 +1,38 @@ +package wallet + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "strings" + "time" +) + +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", strings.Join(symbols, ","), currency) + resp, err := httpClient.Get(url) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + prices := make(map[string]map[string]float64) + err = json.Unmarshal(body, &prices) + if err != nil { + return nil, err + } + + result := make(map[string]float64) + for _, symbol := range symbols { + result[symbol] = prices[strings.ToUpper(symbol)][strings.ToUpper(currency)] + } + return result, nil +}