2023-02-21 09:05:16 +00:00
|
|
|
package cryptocompare
|
|
|
|
|
|
|
|
import (
|
2024-05-22 11:56:30 +00:00
|
|
|
"context"
|
2023-02-21 09:05:16 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-05-22 11:56:30 +00:00
|
|
|
"net/url"
|
2023-02-21 09:05:16 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty/utils"
|
|
|
|
)
|
|
|
|
|
2024-08-19 19:27:53 +00:00
|
|
|
const baseID = "cryptocompare"
|
2024-05-22 11:56:30 +00:00
|
|
|
const extraParamStatus = "Status.im"
|
2024-07-31 06:21:11 +00:00
|
|
|
const baseURL = "https://min-api.cryptocompare.com"
|
|
|
|
|
2023-02-21 09:05:16 +00:00
|
|
|
type HistoricalPricesContainer struct {
|
|
|
|
Aggregated bool `json:"Aggregated"`
|
|
|
|
TimeFrom int64 `json:"TimeFrom"`
|
|
|
|
TimeTo int64 `json:"TimeTo"`
|
|
|
|
HistoricalData []thirdparty.HistoricalPrice `json:"Data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type HistoricalPricesData struct {
|
|
|
|
Data HistoricalPricesContainer `json:"Data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type TokenDetailsContainer struct {
|
|
|
|
Data map[string]thirdparty.TokenDetails `json:"Data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type MarketValuesContainer struct {
|
|
|
|
Raw map[string]map[string]thirdparty.TokenMarketValues `json:"Raw"`
|
|
|
|
}
|
|
|
|
|
2024-07-30 13:48:22 +00:00
|
|
|
type Params struct {
|
2024-08-19 19:27:53 +00:00
|
|
|
ID string
|
2024-07-30 13:48:22 +00:00
|
|
|
URL string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:05:16 +00:00
|
|
|
type Client struct {
|
2024-08-19 19:27:53 +00:00
|
|
|
id string
|
2024-05-22 11:56:30 +00:00
|
|
|
httpClient *thirdparty.HTTPClient
|
2024-06-21 09:53:31 +00:00
|
|
|
baseURL string
|
2024-07-30 13:48:22 +00:00
|
|
|
creds *thirdparty.BasicCreds
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient() *Client {
|
2024-08-19 19:27:53 +00:00
|
|
|
return NewClientWithParams(Params{
|
|
|
|
ID: baseID,
|
|
|
|
URL: baseURL,
|
|
|
|
})
|
2024-06-21 09:53:31 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 13:48:22 +00:00
|
|
|
func NewClientWithParams(params Params) *Client {
|
|
|
|
var creds *thirdparty.BasicCreds
|
|
|
|
if params.User != "" {
|
|
|
|
creds = &thirdparty.BasicCreds{
|
|
|
|
User: params.User,
|
|
|
|
Password: params.Password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
return &Client{
|
2024-08-19 19:27:53 +00:00
|
|
|
id: params.ID,
|
2024-06-21 09:53:31 +00:00
|
|
|
httpClient: thirdparty.NewHTTPClient(),
|
2024-07-30 13:48:22 +00:00
|
|
|
baseURL: params.URL,
|
|
|
|
creds: creds,
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FetchPrices(symbols []string, currencies []string) (map[string]map[string]float64, error) {
|
2023-03-30 12:28:08 +00:00
|
|
|
chunks := utils.ChunkSymbols(symbols, 60)
|
2023-02-21 09:05:16 +00:00
|
|
|
result := make(map[string]map[string]float64)
|
|
|
|
realCurrencies := utils.RenameSymbols(currencies)
|
|
|
|
for _, smbls := range chunks {
|
|
|
|
realSymbols := utils.RenameSymbols(smbls)
|
|
|
|
|
2024-05-22 11:56:30 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("fsyms", strings.Join(realSymbols, ","))
|
|
|
|
params.Add("tsyms", strings.Join(realCurrencies, ","))
|
|
|
|
params.Add("extraParams", extraParamStatus)
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
url := fmt.Sprintf("%s/data/pricemulti", c.baseURL)
|
2024-07-30 13:48:22 +00:00
|
|
|
response, err := c.httpClient.DoGetRequest(context.Background(), url, params, c.creds)
|
2023-02-21 09:05:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
prices := make(map[string]map[string]float64)
|
2024-05-22 11:56:30 +00:00
|
|
|
err = json.Unmarshal(response, &prices)
|
2023-02-21 09:05:16 +00:00
|
|
|
if err != nil {
|
2024-05-22 11:56:30 +00:00
|
|
|
return nil, fmt.Errorf("%s - %s", err, string(response))
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, symbol := range smbls {
|
|
|
|
result[symbol] = map[string]float64{}
|
|
|
|
for _, currency := range currencies {
|
|
|
|
result[symbol][currency] = prices[utils.GetRealSymbol(symbol)][utils.GetRealSymbol(currency)]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FetchTokenDetails(symbols []string) (map[string]thirdparty.TokenDetails, error) {
|
2024-06-21 09:53:31 +00:00
|
|
|
url := fmt.Sprintf("%s/data/all/coinlist", c.baseURL)
|
2024-07-30 13:48:22 +00:00
|
|
|
response, err := c.httpClient.DoGetRequest(context.Background(), url, nil, c.creds)
|
2023-02-21 09:05:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
container := TokenDetailsContainer{}
|
2024-05-22 11:56:30 +00:00
|
|
|
err = json.Unmarshal(response, &container)
|
2023-02-21 09:05:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenDetails := make(map[string]thirdparty.TokenDetails)
|
|
|
|
|
|
|
|
for _, symbol := range symbols {
|
|
|
|
tokenDetails[symbol] = container.Data[utils.GetRealSymbol(symbol)]
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenDetails, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FetchTokenMarketValues(symbols []string, currency string) (map[string]thirdparty.TokenMarketValues, error) {
|
2023-03-30 12:28:08 +00:00
|
|
|
chunks := utils.ChunkSymbols(symbols)
|
2023-02-21 09:05:16 +00:00
|
|
|
realCurrency := utils.GetRealSymbol(currency)
|
|
|
|
item := map[string]thirdparty.TokenMarketValues{}
|
2023-03-30 12:28:08 +00:00
|
|
|
for _, smbls := range chunks {
|
|
|
|
realSymbols := utils.RenameSymbols(smbls)
|
2023-02-21 09:05:16 +00:00
|
|
|
|
2024-05-22 11:56:30 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("fsyms", strings.Join(realSymbols, ","))
|
|
|
|
params.Add("tsyms", realCurrency)
|
|
|
|
params.Add("extraParams", extraParamStatus)
|
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
url := fmt.Sprintf("%s/data/pricemultifull", c.baseURL)
|
2024-07-30 13:48:22 +00:00
|
|
|
response, err := c.httpClient.DoGetRequest(context.Background(), url, params, c.creds)
|
2023-03-30 12:28:08 +00:00
|
|
|
if err != nil {
|
2024-05-22 11:56:30 +00:00
|
|
|
return nil, err
|
2023-03-30 12:28:08 +00:00
|
|
|
}
|
2023-02-21 09:05:16 +00:00
|
|
|
|
2023-03-30 12:28:08 +00:00
|
|
|
container := MarketValuesContainer{}
|
2024-05-22 11:56:30 +00:00
|
|
|
err = json.Unmarshal(response, &container)
|
2024-04-25 12:15:07 +00:00
|
|
|
|
|
|
|
if len(container.Raw) == 0 {
|
2024-05-22 11:56:30 +00:00
|
|
|
return nil, fmt.Errorf("no data found - %s", string(response))
|
2024-04-25 12:15:07 +00:00
|
|
|
}
|
2023-03-30 12:28:08 +00:00
|
|
|
if err != nil {
|
2024-05-22 11:56:30 +00:00
|
|
|
return nil, fmt.Errorf("%s - %s", err, string(response))
|
2023-03-30 12:28:08 +00:00
|
|
|
}
|
2023-02-21 09:05:16 +00:00
|
|
|
|
2023-03-30 12:28:08 +00:00
|
|
|
for _, symbol := range smbls {
|
2024-07-03 13:10:10 +00:00
|
|
|
item[symbol] = container.Raw[utils.GetRealSymbol(symbol)][realCurrency]
|
2023-03-30 12:28:08 +00:00
|
|
|
}
|
2023-02-21 09:05:16 +00:00
|
|
|
}
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FetchHistoricalHourlyPrices(symbol string, currency string, limit int, aggregate int) ([]thirdparty.HistoricalPrice, error) {
|
|
|
|
item := []thirdparty.HistoricalPrice{}
|
|
|
|
|
2024-05-22 11:56:30 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("fsym", utils.GetRealSymbol(symbol))
|
|
|
|
params.Add("tsym", currency)
|
|
|
|
params.Add("aggregate", fmt.Sprintf("%d", aggregate))
|
|
|
|
params.Add("limit", fmt.Sprintf("%d", limit))
|
|
|
|
params.Add("extraParams", extraParamStatus)
|
2023-02-21 09:05:16 +00:00
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
url := fmt.Sprintf("%s/data/v2/histohour", c.baseURL)
|
2024-07-30 13:48:22 +00:00
|
|
|
response, err := c.httpClient.DoGetRequest(context.Background(), url, params, c.creds)
|
2023-02-21 09:05:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return item, err
|
|
|
|
}
|
|
|
|
|
|
|
|
container := HistoricalPricesData{}
|
2024-05-22 11:56:30 +00:00
|
|
|
err = json.Unmarshal(response, &container)
|
2023-02-21 09:05:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return item, err
|
|
|
|
}
|
|
|
|
|
|
|
|
item = container.Data.HistoricalData
|
|
|
|
|
|
|
|
return item, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) FetchHistoricalDailyPrices(symbol string, currency string, limit int, allData bool, aggregate int) ([]thirdparty.HistoricalPrice, error) {
|
|
|
|
item := []thirdparty.HistoricalPrice{}
|
|
|
|
|
2024-05-22 11:56:30 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("fsym", utils.GetRealSymbol(symbol))
|
|
|
|
params.Add("tsym", currency)
|
|
|
|
params.Add("aggregate", fmt.Sprintf("%d", aggregate))
|
|
|
|
params.Add("limit", fmt.Sprintf("%d", limit))
|
|
|
|
params.Add("allData", fmt.Sprintf("%v", allData))
|
|
|
|
params.Add("extraParams", extraParamStatus)
|
2023-02-21 09:05:16 +00:00
|
|
|
|
2024-06-21 09:53:31 +00:00
|
|
|
url := fmt.Sprintf("%s/data/v2/histoday", c.baseURL)
|
2024-07-30 13:48:22 +00:00
|
|
|
response, err := c.httpClient.DoGetRequest(context.Background(), url, params, c.creds)
|
2023-02-21 09:05:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return item, err
|
|
|
|
}
|
|
|
|
|
|
|
|
container := HistoricalPricesData{}
|
2024-05-22 11:56:30 +00:00
|
|
|
err = json.Unmarshal(response, &container)
|
2023-02-21 09:05:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return item, err
|
|
|
|
}
|
|
|
|
|
|
|
|
item = container.Data.HistoricalData
|
|
|
|
|
|
|
|
return item, nil
|
|
|
|
}
|
2024-07-02 17:58:55 +00:00
|
|
|
|
|
|
|
func (c *Client) ID() string {
|
2024-08-19 19:27:53 +00:00
|
|
|
return c.id
|
2024-07-02 17:58:55 +00:00
|
|
|
}
|