feat: move fetch prices to status-go (#2595)

This commit is contained in:
Anthony Laibe 2022-03-23 08:35:58 +00:00 committed by GitHub
parent de2b8df033
commit b629d6fa74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 0 deletions

View File

@ -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)
}

38
services/wallet/price.go Normal file
View File

@ -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
}