BigTable: use CoinGecko "pro" api + key

Change-Id: I7f580cc462518f6eb713a1025850bd825bcbedfb

commit-id:39f708aa
This commit is contained in:
justinschuldt 2021-11-19 13:13:41 -06:00 committed by Leopold Schabel
parent 2c9455f4c9
commit c9222fe886
1 changed files with 31 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
@ -14,6 +15,7 @@ import (
)
const cgBaseUrl = "https://api.coingecko.com/api/v3/"
const cgProBaseUrl = "https://pro-api.coingecko.com/api/v3/"
type CoinGeckoCoin struct {
Id string `json:"id"`
@ -32,12 +34,21 @@ type CoinGeckoErrorRes struct {
}
func fetchCoinGeckoCoins() map[string][]CoinGeckoCoin {
url := fmt.Sprintf("%vcoins/list", cgBaseUrl)
baseUrl := cgBaseUrl
cgApiKey := os.Getenv("COINGECKO_API_KEY")
if cgApiKey != "" {
baseUrl = cgProBaseUrl
}
url := fmt.Sprintf("%vcoins/list", baseUrl)
req, reqErr := http.NewRequest("GET", url, nil)
if reqErr != nil {
log.Fatalf("failed coins request, err: %v", reqErr)
}
if cgApiKey != "" {
req.Header.Set("X-Cg-Pro-Api-Key", cgApiKey)
}
res, resErr := http.DefaultClient.Do(req)
if resErr != nil {
log.Fatalf("failed get coins response, err: %v", resErr)
@ -81,12 +92,20 @@ func chainIdToCoinGeckoPlatform(chain vaa.ChainID) string {
}
func fetchCoinGeckoCoinFromContract(chainId vaa.ChainID, address string) CoinGeckoCoin {
baseUrl := cgBaseUrl
cgApiKey := os.Getenv("COINGECKO_API_KEY")
if cgApiKey != "" {
baseUrl = cgProBaseUrl
}
platform := chainIdToCoinGeckoPlatform(chainId)
url := fmt.Sprintf("%vcoins/%v/contract/%v", cgBaseUrl, platform, address)
url := fmt.Sprintf("%vcoins/%v/contract/%v", baseUrl, platform, address)
req, reqErr := http.NewRequest("GET", url, nil)
if reqErr != nil {
log.Fatalf("failed contract request, err: %v\n", reqErr)
}
if cgApiKey != "" {
req.Header.Set("X-Cg-Pro-Api-Key", cgApiKey)
}
res, resErr := http.DefaultClient.Do(req)
if resErr != nil {
@ -166,11 +185,20 @@ func fetchCoinGeckoPrice(coinId string, timestamp time.Time) (float64, error) {
hourAgo := time.Now().Add(-time.Duration(1) * time.Hour)
withinLastHour := timestamp.After(hourAgo)
start, end := rangeFromTime(timestamp, 4)
url := fmt.Sprintf("%vcoins/%v/market_chart/range?vs_currency=usd&from=%v&to=%v", cgBaseUrl, coinId, start.Unix(), end.Unix())
baseUrl := cgBaseUrl
cgApiKey := os.Getenv("COINGECKO_API_KEY")
if cgApiKey != "" {
baseUrl = cgProBaseUrl
}
url := fmt.Sprintf("%vcoins/%v/market_chart/range?vs_currency=usd&from=%v&to=%v", baseUrl, coinId, start.Unix(), end.Unix())
req, reqErr := http.NewRequest("GET", url, nil)
if reqErr != nil {
log.Fatalf("failed coins request, err: %v\n", reqErr)
}
if cgApiKey != "" {
req.Header.Set("X-Cg-Pro-Api-Key", cgApiKey)
}
res, resErr := http.DefaultClient.Do(req)
if resErr != nil {