Update list of available tokens (#1246)

* Update list of available tokens

Co-authored-by: walker-16 <agpazos85@gmail.com>

* Handle too many requests in notional jobs

---------

Co-authored-by: walker-16 <agpazos85@gmail.com>
This commit is contained in:
ftocal 2024-03-25 15:17:26 -03:00 committed by GitHub
parent 6958d5b78b
commit b19bc16dfb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 1237 additions and 1161 deletions

File diff suppressed because it is too large Load Diff

View File

@ -3,14 +3,15 @@ package main
import (
"context"
"encoding/json"
"github.com/wormhole-foundation/wormhole-explorer/common/dbconsts"
"github.com/wormhole-foundation/wormhole-explorer/jobs/jobs/protocols"
"github.com/wormhole-foundation/wormhole-explorer/jobs/jobs/protocols/repository"
"log"
"os"
"strings"
"time"
"github.com/wormhole-foundation/wormhole-explorer/common/dbconsts"
"github.com/wormhole-foundation/wormhole-explorer/jobs/jobs/protocols"
"github.com/wormhole-foundation/wormhole-explorer/jobs/jobs/protocols/repository"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/wormhole-foundation/wormhole-explorer/common/configuration"
@ -104,7 +105,7 @@ func main() {
// initNotionalJob initializes notional job.
func initNotionalJob(ctx context.Context, cfg *config.NotionalConfiguration, logger *zap.Logger) *notional.NotionalJob {
// init coingecko api client.
api := coingecko.NewCoingeckoAPI(cfg.CoingeckoURL)
api := coingecko.NewCoingeckoAPI(cfg.CoingeckoURL, logger)
// init redis client.
redisClient := redis.NewClient(&redis.Options{Addr: cfg.CacheURL})
// init token provider.

View File

@ -8,6 +8,7 @@ import (
"strings"
"github.com/shopspring/decimal"
"go.uber.org/zap"
)
// CoingeckoAPI is a client for the coingecko API
@ -15,14 +16,16 @@ type CoingeckoAPI struct {
url string
chunkSize int
client *http.Client
logger *zap.Logger
}
// NewCoingeckoAPI creates a new coingecko client
func NewCoingeckoAPI(url string) *CoingeckoAPI {
func NewCoingeckoAPI(url string, logger *zap.Logger) *CoingeckoAPI {
return &CoingeckoAPI{
url: url,
chunkSize: 200,
client: http.DefaultClient,
logger: logger,
}
}
@ -37,8 +40,11 @@ func (c *CoingeckoAPI) GetNotionalUSD(ids []string) (map[string]NotionalUSD, err
response := map[string]NotionalUSD{}
chunksIds := chunkChainIds(ids, c.chunkSize)
c.logger.Info("fetching notional value of assets", zap.Int("total_chunks", len(chunksIds)))
// iterate over chunks of ids.
for _, chunk := range chunksIds {
for i, chunk := range chunksIds {
notionalUrl := fmt.Sprintf("%s/simple/price?ids=%s&vs_currencies=usd", c.url, strings.Join(chunk, ","))
req, err := http.NewRequest(http.MethodGet, notionalUrl, nil)
@ -51,6 +57,11 @@ func (c *CoingeckoAPI) GetNotionalUSD(ids []string) (map[string]NotionalUSD, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
c.logger.Error("failed to get notional value of assets", zap.Int("statusCode", res.StatusCode), zap.Int("chunk", i))
return response, fmt.Errorf("failed to get notional value of assets, status code: %d", res.StatusCode)
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return response, err