-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use custom coingecko client (#9)
* feat: use custom coingecko client * chore: fixed linting errors * chore: pass chain to args
- Loading branch information
1 parent
783ebde
commit 49012a9
Showing
7 changed files
with
86 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,56 @@ | ||
package coingecko | ||
|
||
import ( | ||
"fmt" | ||
"main/pkg/config" | ||
"main/pkg/http" | ||
"main/pkg/types" | ||
"strings" | ||
|
||
"github.com/rs/zerolog" | ||
gecko "github.com/superoo7/go-gecko/v3" | ||
) | ||
|
||
type Response map[string]map[string]float64 | ||
|
||
type Coingecko struct { | ||
Client *gecko.Client | ||
Client *http.Client | ||
Config *config.Config | ||
Logger zerolog.Logger | ||
} | ||
|
||
func NewCoingecko(appConfig *config.Config, logger *zerolog.Logger) *Coingecko { | ||
func NewCoingecko(appConfig *config.Config, logger zerolog.Logger) *Coingecko { | ||
return &Coingecko{ | ||
Config: appConfig, | ||
Client: gecko.NewClient(nil), | ||
Client: http.NewClient(logger, "coingecko"), | ||
Logger: logger.With().Str("component", "coingecko").Logger(), | ||
} | ||
} | ||
|
||
func (c *Coingecko) FetchPrices(currencies []string) map[string]float64 { | ||
result, err := c.Client.SimplePrice(currencies, []string{"USD"}) | ||
func (c *Coingecko) FetchPrices(currencies []string) (map[string]float64, types.QueryInfo) { | ||
ids := strings.Join(currencies, ",") | ||
url := fmt.Sprintf("https://api.coingecko.com/api/v3/simple/price?ids=%s&vs_currencies=usd", ids) | ||
|
||
var response Response | ||
queryInfo, err := c.Client.Get(url, &response) | ||
if err != nil { | ||
c.Logger.Error().Err(err).Msg("Could not get rate") | ||
return map[string]float64{} | ||
return nil, queryInfo | ||
} | ||
|
||
prices := map[string]float64{} | ||
|
||
for currencyKey, currencyValue := range *result { | ||
for currencyKey, currencyValue := range response { | ||
for _, baseCurrencyValue := range currencyValue { | ||
chain, found := c.Config.FindChainByCoingeckoCurrency(currencyKey) | ||
if !found { | ||
c.Logger.Warn(). | ||
Str("currency", currencyKey). | ||
Msg("Could not find chain by coingecko currency, which should never happen.") | ||
} else { | ||
prices[chain.Name] = float64(baseCurrencyValue) | ||
prices[chain.Name] = baseCurrencyValue | ||
} | ||
} | ||
} | ||
|
||
return prices | ||
return prices, queryInfo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package http | ||
|
||
import ( | ||
"encoding/json" | ||
"main/pkg/types" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/rs/zerolog" | ||
) | ||
|
||
type Client struct { | ||
logger zerolog.Logger | ||
chain string | ||
} | ||
|
||
func NewClient(logger zerolog.Logger, chain string) *Client { | ||
return &Client{ | ||
logger: logger.With().Str("component", "http").Logger(), | ||
chain: chain, | ||
} | ||
} | ||
|
||
func (c *Client) Get(url string, target interface{}) (types.QueryInfo, error) { | ||
client := &http.Client{Timeout: 10 * 1000000000} | ||
start := time.Now() | ||
|
||
queryInfo := types.QueryInfo{ | ||
Success: false, | ||
Chain: c.chain, | ||
URL: url, | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodGet, url, nil) | ||
if err != nil { | ||
return queryInfo, err | ||
} | ||
|
||
req.Header.Set("User-Agent", "cosmos-wallets-exporter") | ||
|
||
c.logger.Debug().Str("url", url).Msg("Doing a query...") | ||
|
||
res, err := client.Do(req) | ||
queryInfo.Duration = time.Since(start) | ||
if err != nil { | ||
c.logger.Warn().Str("url", url).Err(err).Msg("Query failed") | ||
return queryInfo, err | ||
} | ||
defer res.Body.Close() | ||
|
||
c.logger.Debug().Str("url", url).Dur("duration", time.Since(start)).Msg("Query is finished") | ||
|
||
err = json.NewDecoder(res.Body).Decode(target) | ||
queryInfo.Success = err == nil | ||
|
||
return queryInfo, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters