wormhole-explorer/analytics/cmd/prices/run.go

56 lines
1.4 KiB
Go

package prices
import (
"fmt"
"os"
"time"
"github.com/wormhole-foundation/wormhole-explorer/analytics/coingecko"
"github.com/wormhole-foundation/wormhole-explorer/common/domain"
"github.com/wormhole-foundation/wormhole-explorer/common/logger"
"go.uber.org/zap"
)
// go througth the symbol list provided by wormhole
// and fetch the history from coingecko
// and save it to a file
func RunPrices(output string) {
// build logger
logger := logger.New("wormhole-explorer-analytics")
logger.Info("starting wormhole-explorer-analytics ...")
cg := coingecko.NewCoinGeckoAPI("")
pricesOutput, err := os.Create(output)
if err != nil {
logger.Fatal("creating file", zap.Error(err))
}
defer pricesOutput.Close()
tokens := domain.GetAllTokens()
logger.Info("found tokens", zap.Int("count", len(tokens)))
for index, token := range tokens {
logger.Info("processing token",
zap.String("coingeckoID", token.CoingeckoID),
zap.Stringer("symbol", token.Symbol),
zap.Int("index", index+1), zap.Int("count", len(tokens)))
r, err := cg.GetSymbolDailyPrice(token.CoingeckoID)
if err != nil {
fmt.Println(err)
continue
}
for _, p := range r.Prices {
pricesOutput.WriteString(fmt.Sprintf("%d,%s,%s,%s,%s\n", token.TokenChain, token.CoingeckoID, token.Symbol, p[0], p[1]))
}
time.Sleep(5 * time.Second) // 10 requests per second
}
logger.Info("finished wormhole-explorer-analytics")
}