2023-05-23 07:27:23 -07:00
|
|
|
package prices
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-01-02 05:38:03 -08:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/common/coingecko"
|
2023-05-30 07:14:19 -07:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/common/domain"
|
2023-06-01 12:32:00 -07:00
|
|
|
"github.com/wormhole-foundation/wormhole-explorer/common/logger"
|
|
|
|
"go.uber.org/zap"
|
2023-05-23 07:27:23 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// go througth the symbol list provided by wormhole
|
|
|
|
// and fetch the history from coingecko
|
|
|
|
// and save it to a file
|
2024-01-02 05:38:03 -08:00
|
|
|
func RunPrices(output, p2pNetwork, coingeckoUrl, coingeckoHeaderKey, coingeckoApiKey string) {
|
2023-05-23 07:27:23 -07:00
|
|
|
|
2023-06-01 12:32:00 -07:00
|
|
|
// build logger
|
|
|
|
logger := logger.New("wormhole-explorer-analytics")
|
|
|
|
|
|
|
|
logger.Info("starting wormhole-explorer-analytics ...")
|
|
|
|
|
2024-01-02 05:38:03 -08:00
|
|
|
cg := coingecko.NewCoinGeckoAPI(coingeckoUrl, coingeckoHeaderKey, coingeckoApiKey)
|
2023-05-23 07:27:23 -07:00
|
|
|
|
|
|
|
pricesOutput, err := os.Create(output)
|
|
|
|
if err != nil {
|
2023-06-01 12:32:00 -07:00
|
|
|
logger.Fatal("creating file", zap.Error(err))
|
2023-05-23 07:27:23 -07:00
|
|
|
}
|
|
|
|
defer pricesOutput.Close()
|
|
|
|
|
2023-11-28 05:16:40 -08:00
|
|
|
// create token provider
|
|
|
|
tokenProvider := domain.NewTokenProvider(p2pNetwork)
|
|
|
|
tokens := tokenProvider.GetAllTokens()
|
2023-06-01 12:32:00 -07:00
|
|
|
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)))
|
|
|
|
|
2024-01-02 05:38:03 -08:00
|
|
|
r, err := cg.GetSymbolDailyPrice(token.CoingeckoID, "max")
|
2023-05-23 07:27:23 -07:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, p := range r.Prices {
|
2023-05-30 07:14:19 -07:00
|
|
|
pricesOutput.WriteString(fmt.Sprintf("%d,%s,%s,%s,%s\n", token.TokenChain, token.CoingeckoID, token.Symbol, p[0], p[1]))
|
2023-05-23 07:27:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(5 * time.Second) // 10 requests per second
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-06-01 12:32:00 -07:00
|
|
|
logger.Info("finished wormhole-explorer-analytics")
|
|
|
|
|
2023-05-23 07:27:23 -07:00
|
|
|
}
|