Skip to content

Commit

Permalink
Removed detailed metrics repo usage, FetchIcePrice is used instead. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-myles authored Jun 24, 2024
1 parent 6924427 commit fbe5e38
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 40 deletions.
10 changes: 7 additions & 3 deletions tokenomics/balance_total_coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/redis/go-redis/v9"

dwh "github.com/ice-blockchain/freezer/bookkeeper/storage"
"github.com/ice-blockchain/freezer/tokenomics/detailed_coin_metrics"
"github.com/ice-blockchain/wintr/connectors/storage/v3"
"github.com/ice-blockchain/wintr/log"
"github.com/ice-blockchain/wintr/time"
Expand Down Expand Up @@ -148,14 +149,17 @@ func (r *repository) updateCachedBlockchainDetails(ctx context.Context) error {
return nil
}

detailedCoinMetrics, err := r.detailedMetricsRepo.ReadDetails(ctx)
stats, err := FetchICEPrice(ctx)
if err != nil {
return errors.Wrap(err, "failed to read detailedCoinMetrics")
return errors.Wrap(err, "failed to read ice stats")
}

err = storage.Set(ctx, r.db, &BlockchainDetails{
Timestamp: now,
Details: *detailedCoinMetrics,
Details: detailed_coin_metrics.Details{
CurrentPrice: stats.Price,
Volume24h: stats.TradingVolume24,
},
})

return errors.Wrap(err, "failed to update totalCoinStatsDetails")
Expand Down
17 changes: 3 additions & 14 deletions tokenomics/balance_total_coins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,10 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/stretchr/testify/require"

detailedCoinMetrics "github.com/ice-blockchain/freezer/tokenomics/detailed_coin_metrics"
appCfg "github.com/ice-blockchain/wintr/config"
"github.com/ice-blockchain/wintr/connectors/storage/v3"
)

type mockedDetailedCoinMetrics struct{}

func (*mockedDetailedCoinMetrics) ReadDetails(context.Context) (*detailedCoinMetrics.Details, error) {
return &detailedCoinMetrics.Details{
CurrentPrice: 1.42,
Volume24h: 42.42,
}, nil
}

func helperCreateRepoWithRedisOnly(t *testing.T) *repository {
t.Helper()

Expand All @@ -42,8 +32,7 @@ func helperCreateRepoWithRedisOnly(t *testing.T) *repository {
shutdown: func() error {
return multierror.Append(db.Close()).ErrorOrNil()
},
db: db,
detailedMetricsRepo: new(mockedDetailedCoinMetrics),
db: db,
}

return repo
Expand Down Expand Up @@ -87,8 +76,8 @@ func TestGetCoinStatsBlockchainDetails(t *testing.T) {
data, err := repo.loadCachedBlockchainDetails(context.TODO())
require.NoError(t, err)
require.NotNil(t, data)
require.Equal(t, 1.42, data.CurrentPrice)
require.Equal(t, 42.42, data.Volume24h)
require.Greater(t, data.CurrentPrice, 0.0)
require.Greater(t, data.Volume24h, 0.0)
require.NotNil(t, data.Timestamp)
})

Expand Down
9 changes: 8 additions & 1 deletion tokenomics/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ type (
RankingSummary struct {
GlobalRank uint64 `json:"globalRank" example:"12333"`
}
IceStats struct {
CirculatingSupply float64 `json:"circulatingSupply"`
TotalSupply float64 `json:"totalSupply"`
Price float64 `json:"price"`
MarketCap float64 `json:"marketCap"`
TradingVolume24 float64 `json:"24hTradingVolume"`
FullyDilutedMarketCap float64 `json:"fullyDilutedMarketCap"`
}
ReadRepository interface {
GetMiningBoostSummary(ctx context.Context, userID string) (*MiningBoostSummary, error)
GetBalanceSummary(ctx context.Context, userID string) (*BalanceSummary, error)
Expand Down Expand Up @@ -305,7 +313,6 @@ type (
dwh dwh.Client
mb messagebroker.Client
pictureClient picture.Client
detailedMetricsRepo detailedCoinMetrics.Repository
}

processor struct {
Expand Down
18 changes: 8 additions & 10 deletions tokenomics/mining_boost.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,17 @@ func (r *repository) startICEPriceSyncer(ctx context.Context) {
}

func (r *repository) syncICEPrice(ctx context.Context) error {
price, err := FetchICEPrice(ctx)
stats, err := FetchICEPrice(ctx)
if err != nil {
return errors.Wrap(err, "failed to fetchICEPrice")
}
r.cfg.MiningBoost.icePrice.Store(&price)
r.cfg.MiningBoost.icePrice.Store(&stats.Price)
r.cfg.MiningBoost.levels.Store(r.buildMiningBoostLevels())

return nil
}

func FetchICEPrice(ctx context.Context) (float64, error) {
func FetchICEPrice(ctx context.Context) (*IceStats, error) {
if resp, err := req.
SetContext(ctx).
SetRetryCount(25).
Expand All @@ -430,17 +430,15 @@ func FetchICEPrice(ctx context.Context) (float64, error) {
SetHeader("Pragma", "no-cache").
SetHeader("Expires", "0").
Get("https://data.ice.io/stats"); err != nil {
return 0, errors.Wrap(err, "failed to fetch https://data.ice.io/stats")
return nil, errors.Wrap(err, "failed to fetch https://data.ice.io/stats")
} else if data, err2 := resp.ToBytes(); err2 != nil {
return 0, errors.Wrap(err2, "failed to read body of https://data.ice.io/stats")
return nil, errors.Wrap(err2, "failed to read body of https://data.ice.io/stats")
} else {
var stats struct {
Price float64 `json:"price"`
}
var stats IceStats
if err3 := json.Unmarshal(data, &stats); err3 != nil {
return 0, errors.Wrapf(err3, "failed to unmarshal into %#v, data: `%v`", &stats, string(data))
return nil, errors.Wrapf(err3, "failed to unmarshal into %#v, data: `%v`", &stats, string(data))
} else {
return stats.Price, nil
return &stats, nil
}
}
}
Expand Down
21 changes: 9 additions & 12 deletions tokenomics/tokenomics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

dwh "github.com/ice-blockchain/freezer/bookkeeper/storage"
extrabonusnotifier "github.com/ice-blockchain/freezer/extra-bonus-notifier"
detailedCoinMetrics "github.com/ice-blockchain/freezer/tokenomics/detailed_coin_metrics"
appCfg "github.com/ice-blockchain/wintr/config"
messagebroker "github.com/ice-blockchain/wintr/connectors/message_broker"
storagev2 "github.com/ice-blockchain/wintr/connectors/storage/v2"
Expand All @@ -41,10 +40,9 @@ func New(ctx context.Context, _ context.CancelFunc) Repository {
shutdown: func() error {
return multierror.Append(db.Close(), dwhClient.Close()).ErrorOrNil()
},
db: db,
dwh: dwhClient,
pictureClient: picture.New(applicationYamlKey),
detailedMetricsRepo: detailedCoinMetrics.New(),
db: db,
dwh: dwhClient,
pictureClient: picture.New(applicationYamlKey),
}
go repo.startICEPriceSyncer(ctx)
go repo.startDisableAdvancedTeamCfgSyncer(ctx)
Expand All @@ -64,13 +62,12 @@ func StartProcessor(ctx context.Context, cancel context.CancelFunc) Processor {
appCfg.MustLoadFromKey(applicationYamlKey, &cfg)
dwhClient := dwh.MustConnect(ctx, applicationYamlKey)
prc := &processor{repository: &repository{
cfg: &cfg,
db: storage.MustConnect(context.Background(), applicationYamlKey),
globalDB: storagev2.MustConnect(context.Background(), globalDDL, applicationYamlKey),
mb: messagebroker.MustConnect(context.Background(), applicationYamlKey),
dwh: dwhClient,
pictureClient: picture.New(applicationYamlKey),
detailedMetricsRepo: detailedCoinMetrics.New(),
cfg: &cfg,
db: storage.MustConnect(context.Background(), applicationYamlKey),
globalDB: storagev2.MustConnect(context.Background(), globalDDL, applicationYamlKey),
mb: messagebroker.MustConnect(context.Background(), applicationYamlKey),
dwh: dwhClient,
pictureClient: picture.New(applicationYamlKey),
}}
//nolint:contextcheck // It's intended. Cuz we want to close everything gracefully.
mbConsumer := messagebroker.MustConnectAndStartConsuming(context.Background(), cancel, applicationYamlKey,
Expand Down

0 comments on commit fbe5e38

Please sign in to comment.