mirror of
https://github.com/status-im/status-go.git
synced 2025-02-16 16:56:53 +00:00
feat: move fetch prices to status-go (#2595)
This commit is contained in:
parent
de2b8df033
commit
b629d6fa74
@ -261,3 +261,8 @@ func (api *API) GetEthereumChains(ctx context.Context, onlyEnabled bool) ([]*par
|
|||||||
log.Debug("call to GetEthereumChains")
|
log.Debug("call to GetEthereumChains")
|
||||||
return api.s.rpcClient.NetworkManager.Get(onlyEnabled)
|
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
38
services/wallet/price.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user