github.com/diadata-org/diadata@v1.4.593/pkg/utils/datasource.go (about)

     1  package utils
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net/http"
     8  	"net/url"
     9  
    10  	"github.com/tidwall/gjson"
    11  )
    12  
    13  type ExternalDataSoure interface {
    14  	Price(string) (float64, error)
    15  }
    16  
    17  type Coingecko struct {
    18  	apiKey        string
    19  	assetsMapping map[string]string // key blockchain+address value coingecko name
    20  }
    21  
    22  func NewCoinGeckoProvider(apiKey string, assetsMapping map[string]string) *Coingecko {
    23  	return &Coingecko{apiKey: apiKey, assetsMapping: assetsMapping}
    24  }
    25  
    26  func (cg *Coingecko) Price(assetName string) (float64, error) {
    27  	cgName := cg.assetsMapping[assetName]
    28  	url := "https://pro-api.coingecko.com/api/v3/simple/price?ids=" + cgName + "&vs_currencies=usd&x_cg_pro_api_key=" + cg.apiKey
    29  	response, err := http.Get(url)
    30  
    31  	if err != nil {
    32  		return 0.0, err
    33  	}
    34  
    35  	defer response.Body.Close()
    36  	if 200 != response.StatusCode {
    37  		return 0.0, fmt.Errorf("Error on coingecko API call with return code %d", response.StatusCode)
    38  	}
    39  
    40  	contents, err := io.ReadAll(response.Body)
    41  	if err != nil {
    42  		return 0.0, err
    43  	}
    44  
    45  	price := gjson.Get(string(contents), cgName+".usd").Float()
    46  
    47  	return price, nil
    48  }
    49  
    50  // 5655df14-0213-429e-b375-51d338e8c79f
    51  
    52  type CoinMarketCap struct {
    53  	apiKey        string
    54  	assetsMapping map[string]string // key blockchain+address value CoinMarketCap name
    55  }
    56  
    57  func NewCoinMarketCapProvider(apiKey string, assetsMapping map[string]string) *CoinMarketCap {
    58  	return &CoinMarketCap{apiKey: apiKey, assetsMapping: assetsMapping}
    59  }
    60  
    61  func (cg *CoinMarketCap) Price(assetName string) (float64, error) {
    62  	cmcName := cg.assetsMapping[assetName]
    63  	urls := "https://pro-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest"
    64  	req, err := http.NewRequestWithContext(context.Background(), "GET", urls, nil)
    65  
    66  	if err != nil {
    67  		return 0.0, err
    68  	}
    69  	req.Header.Add("X-CMC_PRO_API_KEY", cg.apiKey)
    70  
    71  	q := url.Values{}
    72  	q.Add("symbol", cmcName)
    73  
    74  	req.URL.RawQuery = q.Encode()
    75  
    76  	response, statusCode, err := HTTPRequest(req)
    77  
    78  	if err != nil {
    79  		return 0.0, fmt.Errorf("Error on coinmarketcap API call with return code %d", statusCode)
    80  
    81  	}
    82  
    83  	if 200 != statusCode {
    84  
    85  		return 0.0, fmt.Errorf("Error on coinmarketcap API call with return code %d", statusCode)
    86  	}
    87  
    88  	price := gjson.Get(string(response), "data."+cmcName+".0.quote.USD.price").Float()
    89  
    90  	return price, nil
    91  }