-
Notifications
You must be signed in to change notification settings - Fork 233
/
apis.go
65 lines (54 loc) · 1.56 KB
/
apis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package coingecko
import (
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strings"
"github.com/forbole/bdjuno/v4/types"
)
// GetCoinsList allows to fetch from the remote APIs the list of all the supported tokens
func GetCoinsList() (coins Tokens, err error) {
err = queryCoinGecko("/coins/list", &coins)
return coins, err
}
// GetTokensPrices queries the remote APIs to get the token prices of all the tokens having the given ids
func GetTokensPrices(ids []string) ([]types.TokenPrice, error) {
var prices []MarketTicker
query := fmt.Sprintf("/coins/markets?vs_currency=usd&ids=%s", strings.Join(ids, ","))
err := queryCoinGecko(query, &prices)
if err != nil {
return nil, err
}
return ConvertCoingeckoPrices(prices), nil
}
func ConvertCoingeckoPrices(prices []MarketTicker) []types.TokenPrice {
tokenPrices := make([]types.TokenPrice, len(prices))
for i, price := range prices {
tokenPrices[i] = types.NewTokenPrice(
price.Symbol,
price.CurrentPrice,
int64(math.Trunc(price.MarketCap)),
price.LastUpdated,
)
}
return tokenPrices
}
// queryCoinGecko queries the CoinGecko APIs for the given endpoint
func queryCoinGecko(endpoint string, ptr interface{}) error {
resp, err := http.Get("https://api.coingecko.com/api/v3" + endpoint)
if err != nil {
return err
}
defer resp.Body.Close()
bz, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error while reading response body: %s", err)
}
err = json.Unmarshal(bz, &ptr)
if err != nil {
return fmt.Errorf("error while unmarshaling response body: %s", err)
}
return nil
}