Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
gusin13 committed Dec 3, 2024
1 parent ceba534 commit bb1def5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
7 changes: 5 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ func (cfg *Config) Validate() error {
}
}

if err := cfg.ExternalAPIs.Validate(); err != nil {
return err
// ExternalAPIs is optional
if cfg.ExternalAPIs != nil {
if err := cfg.ExternalAPIs.Validate(); err != nil {
return err
}
}

return nil
Expand Down
10 changes: 6 additions & 4 deletions internal/db/model/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ func Setup(ctx context.Context, cfg *config.Config) error {
}
}

// Create TTL index for BTC price collection
if err := createTTLIndexes(ctx, database, cfg.ExternalAPIs.CoinMarketCap.CacheTTL); err != nil {
log.Error().Err(err).Msg("Failed to create TTL index for BTC price")
return err
// If external APIs are configured, create TTL index for BTC price collection
if cfg.ExternalAPIs != nil {
if err := createTTLIndexes(ctx, database, cfg.ExternalAPIs.CoinMarketCap.CacheTTL); err != nil {
log.Error().Err(err).Msg("Failed to create TTL index for BTC price")
return err
}
}

log.Info().Msg("Collections and Indexes created successfully.")
Expand Down
32 changes: 19 additions & 13 deletions internal/services/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
)

type OverallStatsPublic struct {
ActiveTvl int64 `json:"active_tvl"`
TotalTvl int64 `json:"total_tvl"`
ActiveDelegations int64 `json:"active_delegations"`
TotalDelegations int64 `json:"total_delegations"`
TotalStakers uint64 `json:"total_stakers"`
UnconfirmedTvl uint64 `json:"unconfirmed_tvl"`
PendingTvl uint64 `json:"pending_tvl"`
BtcPriceUsd float64 `json:"btc_price_usd"`
ActiveTvl int64 `json:"active_tvl"`
TotalTvl int64 `json:"total_tvl"`
ActiveDelegations int64 `json:"active_delegations"`
TotalDelegations int64 `json:"total_delegations"`
TotalStakers uint64 `json:"total_stakers"`
UnconfirmedTvl uint64 `json:"unconfirmed_tvl"`
PendingTvl uint64 `json:"pending_tvl"`
BtcPriceUsd *float64 `json:"btc_price_usd,omitempty"` // Optional field
}

type StakerStatsPublic struct {
Expand Down Expand Up @@ -186,10 +186,16 @@ func (s *Services) GetOverallStats(
pendingTvl = unconfirmedTvl - confirmedTvl
}

btcPriceUsd, err := s.GetLatestBtcPriceUsd(ctx)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("error while fetching latest btc price")
return nil, types.NewInternalServiceError(err)
// Only fetch BTC price if external APIs are configured
var btcPrice *float64
if s.cfg.ExternalAPIs != nil {
price, err := s.GetLatestBtcPriceUsd(ctx)
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("error while fetching latest btc price")
return nil, types.NewInternalServiceError(err)
}
roundedPrice := math.Round(price*100) / 100
btcPrice = &roundedPrice
}

return &OverallStatsPublic{
Expand All @@ -200,7 +206,7 @@ func (s *Services) GetOverallStats(
TotalStakers: stats.TotalStakers,
UnconfirmedTvl: unconfirmedTvl,
PendingTvl: pendingTvl,
BtcPriceUsd: math.Round(btcPriceUsd*100) / 100, // round to 2 decimal places
BtcPriceUsd: btcPrice,
}, nil
}

Expand Down

0 comments on commit bb1def5

Please sign in to comment.