Skip to content

Commit

Permalink
refactor(api): use new finalized rewards table
Browse files Browse the repository at this point in the history
  • Loading branch information
invis-bitfly committed Jan 9, 2025
1 parent 60de595 commit 7e40dc8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 81 deletions.
65 changes: 11 additions & 54 deletions backend/pkg/api/data_access/vdb_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,11 @@ func (d *DataAccessService) GetValidatorDashboardRewards(ctx context.Context, da
elDs := goqu.Dialect("postgres").
Select(
goqu.L("b.epoch"),
goqu.L("SUM(COALESCE(rb.value, ep.fee_recipient_reward * 1e18, 0)) AS el_rewards")).
goqu.SUM(goqu.I("value")).As("el_rewards")).
From(goqu.L("users_val_dashboards_validators v")).
Where(goqu.L("b.epoch >= ?", startEpoch)).
LeftJoin(goqu.L("blocks b"), goqu.On(goqu.L("v.validator_index = b.proposer AND b.status = '1'"))).
LeftJoin(goqu.L("execution_payloads ep"), goqu.On(goqu.L("ep.block_hash = b.exec_block_hash"))).
LeftJoin(
goqu.Lateral(goqu.Dialect("postgres").
From("relays_blocks").
Select(
goqu.L("exec_block_hash"),
goqu.MAX("value").As("value")).
Where(goqu.L("relays_blocks.exec_block_hash = b.exec_block_hash")).
GroupBy("exec_block_hash")).As("rb"),
goqu.On(goqu.L("rb.exec_block_hash = b.exec_block_hash")),
)
LeftJoin(goqu.I("execution_rewards_finalized").As('b'), goqu.On(goqu.L("v.validator_index = b.proposer"))).
GroupBy(goqu.L("b.epoch"))

if dashboardId.Validators == nil {
rewardsDs = rewardsDs.
Expand Down Expand Up @@ -557,20 +547,9 @@ func (d *DataAccessService) GetValidatorDashboardGroupRewards(ctx context.Contex

elDs := goqu.Dialect("postgres").
Select(
goqu.L("COALESCE(SUM(COALESCE(rb.value, ep.fee_recipient_reward * 1e18, 0)), 0) AS blocks_el_reward")).
goqu.SUM(goqu.I("value")).As("blocks_el_rewards")).
From(goqu.L("users_val_dashboards_validators v")).
LeftJoin(goqu.L("blocks b"), goqu.On(goqu.L("v.validator_index = b.proposer AND b.status = '1'"))).
LeftJoin(goqu.L("execution_payloads ep"), goqu.On(goqu.L("ep.block_hash = b.exec_block_hash"))).
LeftJoin(
goqu.Lateral(goqu.Dialect("postgres").
From("relays_blocks").
Select(
goqu.L("exec_block_hash"),
goqu.MAX("value").As("value")).
Where(goqu.L("relays_blocks.exec_block_hash = b.exec_block_hash")).
GroupBy("exec_block_hash")).As("rb"),
goqu.On(goqu.L("rb.exec_block_hash = b.exec_block_hash")),
).
LeftJoin(goqu.I("execution_rewards_finalized").As('b'), goqu.On(goqu.L("v.validator_index = b.proposer"))).
Where(goqu.L("b.epoch = ?", epoch))

// handle the case when we have a list of validators
Expand Down Expand Up @@ -733,21 +712,11 @@ func (d *DataAccessService) GetValidatorDashboardRewardsChart(ctx context.Contex
elDs := goqu.Dialect("postgres").
Select(
goqu.L("b.epoch"),
goqu.L("SUM(COALESCE(rb.value, ep.fee_recipient_reward * 1e18, 0)) AS el_rewards")).
goqu.SUM(goqu.I("value")).As("el_rewards")).
From(goqu.L("users_val_dashboards_validators v")).
LeftJoin(goqu.L("blocks b"), goqu.On(goqu.L("v.validator_index = b.proposer AND b.status = '1'"))).
LeftJoin(goqu.L("execution_payloads ep"), goqu.On(goqu.L("ep.block_hash = b.exec_block_hash"))).
LeftJoin(
goqu.Lateral(goqu.Dialect("postgres").
From("relays_blocks").
Select(
goqu.L("exec_block_hash"),
goqu.MAX("value").As("value")).
Where(goqu.L("relays_blocks.exec_block_hash = b.exec_block_hash")).
GroupBy("exec_block_hash")).As("rb"),
goqu.On(goqu.L("rb.exec_block_hash = b.exec_block_hash")),
).
Where(goqu.L("b.epoch >= ?", startEpoch))
Where(goqu.L("b.epoch >= ?", startEpoch)).
LeftJoin(goqu.I("execution_rewards_finalized").As('b'), goqu.On(goqu.L("v.validator_index = b.proposer"))).
GroupBy(goqu.L("b.epoch"))

if dashboardId.Validators == nil {
rewardsDs = rewardsDs.
Expand Down Expand Up @@ -986,21 +955,9 @@ func (d *DataAccessService) GetValidatorDashboardDuties(ctx context.Context, das
elDs := goqu.Dialect("postgres").
Select(
goqu.L("b.proposer"),
goqu.L("SUM(COALESCE(rb.value, ep.fee_recipient_reward * 1e18, 0)) AS el_rewards")).
From(goqu.L("blocks b")).
LeftJoin(goqu.L("execution_payloads ep"), goqu.On(goqu.L("ep.block_hash = b.exec_block_hash"))).
LeftJoin(
goqu.Lateral(goqu.Dialect("postgres").
From("relays_blocks").
Select(
goqu.L("exec_block_hash"),
goqu.MAX("value").As("value")).
Where(goqu.L("relays_blocks.exec_block_hash = b.exec_block_hash")).
GroupBy("exec_block_hash")).As("rb"),
goqu.On(goqu.L("rb.exec_block_hash = b.exec_block_hash")),
).
goqu.SUM(goqu.I("value")).As("el_rewards")).
From(goqu.I("execution_rewards_finalized").As('b')).
Where(goqu.L("b.epoch = ?", epoch)).
Where(goqu.L("b.status = '1'")).
GroupBy(goqu.L("b.proposer"))

// ------------------------------------------------------------------------------------------------------------------
Expand Down
31 changes: 4 additions & 27 deletions backend/pkg/api/data_access/vdb_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,8 @@ func (d *DataAccessService) GetValidatorDashboardSummary(ctx context.Context, da
elRewards := make(map[int64]decimal.Decimal)
ds = goqu.Dialect("postgres").
Select(
goqu.L("SUM(COALESCE(rb.value, ep.fee_recipient_reward * 1e18, 0)) AS el_rewards")).
From(goqu.L("blocks b")).
LeftJoin(goqu.L("execution_payloads ep"), goqu.On(goqu.L("ep.block_hash = b.exec_block_hash"))).
LeftJoin(
goqu.Lateral(goqu.Dialect("postgres").
From("relays_blocks").
Select(
goqu.L("exec_block_hash"),
goqu.MAX("value").As("value")).
Where(goqu.L("relays_blocks.exec_block_hash = b.exec_block_hash")).
GroupBy("exec_block_hash")).As("rb"),
goqu.On(goqu.L("rb.exec_block_hash = b.exec_block_hash")),
).
goqu.SUM(goqu.I("value")).As("el_rewards")).
From(goqu.I("execution_rewards_finalized").As('b')).
Where(goqu.L("b.epoch >= ? AND b.epoch <= ? AND b.status = '1'", epochMin, epochMax)).
GroupBy(goqu.L("result_group_id"))

Expand Down Expand Up @@ -929,20 +918,8 @@ func (d *DataAccessService) internal_getElClAPR(ctx context.Context, dashboardId
}

elDs := goqu.Dialect("postgres").
Select(goqu.L("COALESCE(SUM(COALESCE(rb.value / 1e18, fee_recipient_reward)), 0) AS el_reward")).
From(goqu.L("blocks AS b")).
LeftJoin(goqu.L("execution_payloads AS ep"), goqu.On(goqu.L("b.exec_block_hash = ep.block_hash"))).
LeftJoin(
goqu.Lateral(goqu.Dialect("postgres").
From("relays_blocks").
Select(
goqu.L("exec_block_hash"),
goqu.MAX("value").As("value")).
Where(goqu.L("relays_blocks.exec_block_hash = b.exec_block_hash")).
GroupBy("exec_block_hash")).As("rb"),
goqu.On(goqu.L("rb.exec_block_hash = b.exec_block_hash")),
).
Where(goqu.L("b.status = '1'"))
Select(goqu.SUM(goqu.I("value"))).
From(goqu.I("execution_rewards_finalized")).As("b")

if len(dashboardId.Validators) > 0 {
elDs = elDs.
Expand Down

0 comments on commit 7e40dc8

Please sign in to comment.