Skip to content

Commit 9cead4a

Browse files
committed
refactor: improved code style
1 parent 86abe3c commit 9cead4a

File tree

7 files changed

+66
-59
lines changed

7 files changed

+66
-59
lines changed

backend/pkg/api/data_access/mobile.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,22 @@ func (d *DataAccessService) GetValidatorDashboardMobileWidget(ctx context.Contex
263263

264264
retrieveApr := func(hours int, apr *float64) {
265265
eg.Go(func() error {
266-
_, elApr, _, clApr, err := d.getElClAPR(ctx, wrappedDashboardId, -1, hours)
266+
incomeInfo, err := d.getElClAPR(ctx, wrappedDashboardId, -1, hours)
267267
if err != nil {
268268
return err
269269
}
270-
*apr = elApr + clApr
270+
*apr = incomeInfo.Apr.El + incomeInfo.Apr.Cl
271271
return nil
272272
})
273273
}
274274

275275
retrieveRewards := func(hours int, rewards *t.ClElValue[decimal.Decimal]) {
276276
eg.Go(func() error {
277-
elRewards, _, clRewards, _, err := d.getElClAPR(ctx, wrappedDashboardId, -1, hours)
277+
incomeInfo, err := d.getElClAPR(ctx, wrappedDashboardId, -1, hours)
278278
if err != nil {
279279
return err
280280
}
281-
*rewards = t.ClElValue[decimal.Decimal]{
282-
El: elRewards,
283-
Cl: clRewards,
284-
}
281+
*rewards = incomeInfo.Rewards
285282
return nil
286283
})
287284
}

backend/pkg/api/data_access/validators.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,34 @@ func (d *DataAccessService) GetValidatorsEffectiveBalances(ctx context.Context,
4949
}
5050

5151
// exited
52-
if len(validatorExitEpochs) > 0 {
53-
ds := goqu.Dialect("postgres").
54-
Select(
55-
// goqu.SUM(goqu.I("balance_effective_end")).As("balance_effective_end"),
56-
goqu.I("validator_index"),
57-
goqu.I("balance_effective_end"),
58-
).
59-
From("validator_dashboard_data_epoch").
60-
Where(
61-
goqu.L("(epoch_timestamp, validator_index)").In(validatorExitEpochs),
62-
)
63-
query, args, err := ds.Prepared(true).ToSQL()
64-
if err != nil {
65-
return nil, err
66-
}
52+
if len(validatorExitEpochs) == 0 {
53+
return effectiveBalances, nil
54+
}
55+
ds := goqu.Dialect("postgres").
56+
Select(
57+
// goqu.SUM(goqu.I("balance_effective_end")).As("balance_effective_end"),
58+
goqu.I("validator_index"),
59+
goqu.I("balance_effective_end"),
60+
).
61+
From("validator_dashboard_data_epoch").
62+
Where(
63+
goqu.L("(epoch_timestamp, validator_index)").In(validatorExitEpochs),
64+
)
65+
query, args, err := ds.Prepared(true).ToSQL()
66+
if err != nil {
67+
return nil, err
68+
}
6769

68-
ebsBeforeExit := []struct {
69-
ValidatorIndex uint64 `db:"validator_index"`
70-
EffectiveBalance uint64 `db:"balance_effective_end"`
71-
}{}
72-
err = d.clickhouseReader.SelectContext(ctx, &ebsBeforeExit, query, args...)
73-
if err != nil && err != sql.ErrNoRows {
74-
return nil, err
75-
}
76-
for _, eb := range ebsBeforeExit {
77-
effectiveBalances[eb.ValidatorIndex] = eb.EffectiveBalance
78-
}
70+
ebsBeforeExit := []struct {
71+
ValidatorIndex uint64 `db:"validator_index"`
72+
EffectiveBalance uint64 `db:"balance_effective_end"`
73+
}{}
74+
err = d.clickhouseReader.SelectContext(ctx, &ebsBeforeExit, query, args...)
75+
if err != nil && err != sql.ErrNoRows {
76+
return nil, err
77+
}
78+
for _, eb := range ebsBeforeExit {
79+
effectiveBalances[eb.ValidatorIndex] = eb.EffectiveBalance
7980
}
8081
return effectiveBalances, nil
8182
}

backend/pkg/api/data_access/vdb_helpers.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,14 @@ func (d *DataAccessService) getTotalRewardsColumns() string {
156156
return rewardColumns
157157
}
158158

159-
func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId, groupId int64, hours int) (elIncome decimal.Decimal, elAPR float64, clIncome decimal.Decimal, clAPR float64, err error) {
159+
type IncomeInfo struct {
160+
Rewards t.ClElValue[decimal.Decimal]
161+
162+
Apr t.ClElValue[float64]
163+
}
164+
165+
func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId, groupId int64, hours int) (rewardsApr IncomeInfo, err error) {
166+
result := IncomeInfo{}
160167
table := ""
161168

162169
switch hours {
@@ -171,7 +178,7 @@ func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId,
171178
case -1:
172179
table = "validator_dashboard_data_rolling_90d"
173180
default:
174-
return decimal.Zero, 0, decimal.Zero, 0, fmt.Errorf("invalid hours value: %v", hours)
181+
return IncomeInfo{}, fmt.Errorf("invalid hours value: %v", hours)
175182
}
176183

177184
type RewardsResult struct {
@@ -209,16 +216,16 @@ func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId,
209216

210217
query, args, err := rewardsDs.Prepared(true).ToSQL()
211218
if err != nil {
212-
return decimal.Zero, 0, decimal.Zero, 0, fmt.Errorf("error preparing query: %w", err)
219+
return IncomeInfo{}, fmt.Errorf("error preparing query: %w", err)
213220
}
214221

215222
err = d.clickhouseReader.GetContext(ctx, &rewardsResultTable, query, args...)
216223
if err != nil || !rewardsResultTable.Reward.Valid {
217-
return decimal.Zero, 0, decimal.Zero, 0, err
224+
return IncomeInfo{}, err
218225
}
219226

220227
if rewardsResultTable.ValidatorCount == 0 {
221-
return decimal.Zero, 0, decimal.Zero, 0, nil
228+
return IncomeInfo{}, nil
222229
}
223230

224231
aprDivisor := hours
@@ -228,31 +235,31 @@ func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId,
228235

229236
investedAmountUInt, err := d.GetValidatorDashboardEffectiveBalanceTotal(ctx, dashboardId, true)
230237
if err != nil {
231-
return decimal.Zero, 0, decimal.Zero, 0, fmt.Errorf("error retrieving total effective balance: %w", err)
238+
return IncomeInfo{}, fmt.Errorf("error retrieving total effective balance: %w", err)
232239
}
233240
// invested amount is wrong if the effective balance changed during the period (because of auto compound, consolidation, partial withdrawal etc.)
234241
// would need to split at eb changes and weigh results
235242
investedAmount := d.convertClToMain(decimal.NewFromUint64(investedAmountUInt))
236243

237-
clAPR = calcAPR(d.convertClToMain(decimal.NewFromInt(rewardsResultTable.Reward.Int64)), investedAmount, aprDivisor)
244+
result.Apr.Cl = calcAPR(d.convertClToMain(decimal.NewFromInt(rewardsResultTable.Reward.Int64)), investedAmount, aprDivisor)
238245

239-
clIncome = decimal.NewFromInt(rewardsResultTable.Reward.Int64).Mul(decimal.NewFromInt(1e9))
246+
result.Rewards.Cl = decimal.NewFromInt(rewardsResultTable.Reward.Int64).Mul(decimal.NewFromInt(1e9))
240247

241248
if hours == -1 {
242249
rewardsDs = rewardsDs.
243250
From(goqu.L("validator_dashboard_data_rolling_total AS r FINAL"))
244251

245252
query, args, err = rewardsDs.Prepared(true).ToSQL()
246253
if err != nil {
247-
return decimal.Zero, 0, decimal.Zero, 0, fmt.Errorf("error preparing query: %w", err)
254+
return IncomeInfo{}, fmt.Errorf("error preparing query: %w", err)
248255
}
249256

250257
err = d.clickhouseReader.GetContext(ctx, &rewardsResultTotal, query, args...)
251258
if err != nil || !rewardsResultTotal.Reward.Valid {
252-
return decimal.Zero, 0, decimal.Zero, 0, err
259+
return IncomeInfo{}, err
253260
}
254261

255-
clIncome = decimal.NewFromInt(rewardsResultTotal.Reward.Int64).Mul(decimal.NewFromInt(1e9))
262+
result.Rewards.Cl = decimal.NewFromInt(rewardsResultTotal.Reward.Int64).Mul(decimal.NewFromInt(1e9))
256263
}
257264

258265
elDs := goqu.Dialect("postgres").
@@ -278,32 +285,32 @@ func (d *DataAccessService) getElClAPR(ctx context.Context, dashboardId t.VDBId,
278285

279286
query, args, err = elTableDs.Prepared(true).ToSQL()
280287
if err != nil {
281-
return decimal.Zero, 0, decimal.Zero, 0, fmt.Errorf("error preparing query: %w", err)
288+
return IncomeInfo{}, fmt.Errorf("error preparing query: %w", err)
282289
}
283290

284-
err = d.alloyReader.GetContext(ctx, &elIncome, query, args...)
291+
err = d.alloyReader.GetContext(ctx, &result.Rewards.El, query, args...)
285292
if err != nil {
286-
return decimal.Zero, 0, decimal.Zero, 0, err
293+
return IncomeInfo{}, err
287294
}
288295

289-
elAPR = calcAPR(d.convertElToMain(elIncome), investedAmount, aprDivisor)
296+
result.Apr.El = calcAPR(d.convertElToMain(result.Rewards.El), investedAmount, aprDivisor)
290297

291298
if hours == -1 {
292299
elTotalDs := elDs.
293300
Where(goqu.L("b.epoch >= ? AND b.epoch <= ?", rewardsResultTotal.EpochStart, rewardsResultTotal.EpochEnd))
294301

295302
query, args, err = elTotalDs.Prepared(true).ToSQL()
296303
if err != nil {
297-
return decimal.Zero, 0, decimal.Zero, 0, fmt.Errorf("error preparing query: %w", err)
304+
return IncomeInfo{}, fmt.Errorf("error preparing query: %w", err)
298305
}
299306

300-
err = d.alloyReader.GetContext(ctx, &elIncome, query, args...)
307+
err = d.alloyReader.GetContext(ctx, &result.Rewards.El, query, args...)
301308
if err != nil {
302-
return decimal.Zero, 0, decimal.Zero, 0, err
309+
return IncomeInfo{}, err
303310
}
304311
}
305312

306-
return elIncome, elAPR, clIncome, clAPR, nil
313+
return result, nil
307314
}
308315

309316
// precondition: invested amount and rewards are in the same currency

backend/pkg/api/data_access/vdb_management.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,12 @@ func (d *DataAccessService) GetValidatorDashboardOverview(ctx context.Context, d
381381
retrieveRewardsAndEfficiency := func(table string, hours int, rewards *t.ClElValue[decimal.Decimal], apr *t.ClElValue[float64], efficiency *float64) {
382382
// Rewards + APR
383383
eg.Go(func() error {
384-
(*rewards).El, (*apr).El, (*rewards).Cl, (*apr).Cl, err = d.getElClAPR(ctx, dashboardId, -1, hours)
384+
incomeInfo, err := d.getElClAPR(ctx, dashboardId, -1, hours)
385385
if err != nil {
386386
return err
387387
}
388+
*rewards = incomeInfo.Rewards
389+
*apr = incomeInfo.Apr
388390
return nil
389391
})
390392

backend/pkg/api/data_access/vdb_summary.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,10 +878,12 @@ func (d *DataAccessService) GetValidatorDashboardGroupSummary(ctx context.Contex
878878
ret.MissedRewards.ProposerRewards.Cl = utils.GWeiToWei(big.NewInt(totalMissedRewardsCl))
879879
ret.MissedRewards.ProposerRewards.El = decimal.NewFromFloat(totalMissedRewardsEl)
880880

881-
ret.Rewards.El, ret.Apr.El, ret.Rewards.Cl, ret.Apr.Cl, err = d.getElClAPR(ctx, dashboardId, groupId, hours)
881+
incomeInfo, err := d.getElClAPR(ctx, dashboardId, groupId, hours)
882882
if err != nil {
883883
return nil, err
884884
}
885+
ret.Rewards = incomeInfo.Rewards
886+
ret.Apr = incomeInfo.Apr
885887

886888
pastSyncPeriodCutoff := utils.SyncPeriodOfEpoch(rows[0].EpochStart)
887889
currentSyncPeriod := utils.SyncPeriodOfEpoch(latestEpoch)

backend/pkg/api/types/search.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ type SearchValidatorsByGraffiti struct {
3030
}
3131

3232
type SearchResult struct {
33-
Type string `json:"type"`
34-
ChainId uint64 `json:"chain_id"`
35-
TotalEffectiveBalance uint64 `json:"total_effective_balance"`
36-
Value interface{} `json:"value"`
33+
Type string `json:"type"`
34+
ChainId uint64 `json:"chain_id"`
35+
Value interface{} `json:"value"`
3736
}
3837

3938
type InternalPostSearchResponse struct {

frontend/types/api/search.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export interface SearchValidatorsByGraffiti {
2929
export interface SearchResult {
3030
type: string;
3131
chain_id: number /* uint64 */;
32-
total_effective_balance: number /* uint64 */;
3332
value: any;
3433
}
3534
export interface InternalPostSearchResponse {

0 commit comments

Comments
 (0)