Skip to content

Commit 85b4326

Browse files
committed
perf(api): remove redundant eb calculations
1 parent 8a0009f commit 85b4326

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

backend/pkg/api/data_access/mobile.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,13 @@ func (d *DataAccessService) GetValidatorDashboardMobileWidget(ctx context.Contex
261261
return nil
262262
})
263263

264+
investedAmount, err := d.GetValidatorDashboardEffectiveBalanceTotal(ctx, t.VDBId{Id: dashboardId}, true)
265+
if err != nil {
266+
return nil, err
267+
}
264268
retrieveApr := func(hours int, apr *float64) {
265269
eg.Go(func() error {
266-
incomeInfo, err := d.getElClAPR(ctx, wrappedDashboardId, -1, hours)
270+
incomeInfo, err := d.getElClAPR(ctx, wrappedDashboardId, -1, hours, investedAmount)
267271
if err != nil {
268272
return err
269273
}
@@ -274,7 +278,7 @@ func (d *DataAccessService) GetValidatorDashboardMobileWidget(ctx context.Contex
274278

275279
retrieveRewards := func(hours int, rewards *t.ClElValue[decimal.Decimal]) {
276280
eg.Go(func() error {
277-
incomeInfo, err := d.getElClAPR(ctx, wrappedDashboardId, -1, hours)
281+
incomeInfo, err := d.getElClAPR(ctx, wrappedDashboardId, -1, hours, investedAmount)
278282
if err != nil {
279283
return err
280284
}

backend/pkg/api/data_access/vdb_helpers.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ type IncomeInfo struct {
162162
Apr t.ClElValue[float64]
163163
}
164164

165-
func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId, groupId int64, hours int) (rewardsApr IncomeInfo, err error) {
165+
func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId, groupId int64, hours int, investedAmount uint64) (rewardsApr IncomeInfo, err error) {
166166
result := IncomeInfo{}
167167
table := ""
168168

@@ -233,15 +233,11 @@ func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId,
233233
aprDivisor = 90 * 24
234234
}
235235

236-
investedAmountUInt, err := d.GetValidatorDashboardEffectiveBalanceTotal(ctx, dashboardId, true)
237-
if err != nil {
238-
return IncomeInfo{}, fmt.Errorf("error retrieving total effective balance: %w", err)
239-
}
240236
// invested amount is wrong if the effective balance changed during the period (because of auto compound, consolidation, partial withdrawal etc.)
241237
// would need to split at eb changes and weigh results
242-
investedAmount := d.convertClToMain(decimal.NewFromUint64(investedAmountUInt))
238+
investedAmountDec := d.convertClToMain(decimal.NewFromUint64(investedAmount))
243239

244-
result.Apr.Cl = calcAPR(d.convertClToMain(decimal.NewFromInt(rewardsResultTable.Reward.Int64)), investedAmount, aprDivisor)
240+
result.Apr.Cl = calcAPR(d.convertClToMain(decimal.NewFromInt(rewardsResultTable.Reward.Int64)), investedAmountDec, aprDivisor)
245241

246242
result.Rewards.Cl = decimal.NewFromInt(rewardsResultTable.Reward.Int64).Mul(decimal.NewFromInt(1e9))
247243

@@ -293,7 +289,7 @@ func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId,
293289
return IncomeInfo{}, err
294290
}
295291

296-
result.Apr.El = calcAPR(d.convertElToMain(result.Rewards.El), investedAmount, aprDivisor)
292+
result.Apr.El = calcAPR(d.convertElToMain(result.Rewards.El), investedAmountDec, aprDivisor)
297293

298294
if hours == -1 {
299295
elTotalDs := elDs.

backend/pkg/api/data_access/vdb_management.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,10 +377,15 @@ func (d *DataAccessService) GetValidatorDashboardOverview(ctx context.Context, d
377377
return nil
378378
})
379379

380+
investedAmount, err := d.GetValidatorDashboardEffectiveBalanceTotal(ctx, dashboardId, true)
381+
if err != nil {
382+
return nil, err
383+
}
384+
380385
retrieveRewardsAndEfficiency := func(table string, hours int, rewards *t.ClElValue[decimal.Decimal], apr *t.ClElValue[float64], efficiency *float64) {
381386
// Rewards + APR
382387
eg.Go(func() error {
383-
incomeInfo, err := d.getElClAPR(ctx, dashboardId, -1, hours)
388+
incomeInfo, err := d.getElClAPR(ctx, dashboardId, -1, hours, investedAmount)
384389
if err != nil {
385390
return err
386391
}

backend/pkg/api/data_access/vdb_summary.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,11 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex
721721
ret.MissedRewards.ProposerRewards.Cl = utils.GWeiToWei(big.NewInt(totalMissedRewardsCl))
722722
ret.MissedRewards.ProposerRewards.El = decimal.NewFromFloat(totalMissedRewardsEl)
723723

724-
incomeInfo, err := d.getElClAPR(ctx, dashboardId, groupId, hours)
724+
investedAmount, err := d.GetValidatorDashboardEffectiveBalanceTotal(ctx, dashboardId, true)
725+
if err != nil {
726+
return nil, err
727+
}
728+
incomeInfo, err := d.getElClAPR(ctx, dashboardId, groupId, hours, investedAmount)
725729
if err != nil {
726730
return nil, err
727731
}

0 commit comments

Comments
 (0)