From 7e40dc8dd9c50f48a56e7cddea339905ef34662e Mon Sep 17 00:00:00 2001 From: invis-bitfly <162128378+invis-bitfly@users.noreply.github.com> Date: Thu, 9 Jan 2025 12:39:32 +0100 Subject: [PATCH] refactor(api): use new finalized rewards table --- backend/pkg/api/data_access/vdb_rewards.go | 65 ++++------------------ backend/pkg/api/data_access/vdb_summary.go | 31 ++--------- 2 files changed, 15 insertions(+), 81 deletions(-) diff --git a/backend/pkg/api/data_access/vdb_rewards.go b/backend/pkg/api/data_access/vdb_rewards.go index 4d4e4e266..8b253bf50 100644 --- a/backend/pkg/api/data_access/vdb_rewards.go +++ b/backend/pkg/api/data_access/vdb_rewards.go @@ -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. @@ -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 @@ -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. @@ -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")) // ------------------------------------------------------------------------------------------------------------------ diff --git a/backend/pkg/api/data_access/vdb_summary.go b/backend/pkg/api/data_access/vdb_summary.go index da45182d4..adde58e63 100644 --- a/backend/pkg/api/data_access/vdb_summary.go +++ b/backend/pkg/api/data_access/vdb_summary.go @@ -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")) @@ -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.