Skip to content

Commit

Permalink
OCT-1837: Upgrade metics pie chart for unused MR
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarbacinski committed Aug 6, 2024
1 parent 44e57fa commit 311a936
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def save_snapshot(
leftover: int,
withdrawals_merkle_root: str = None,
total_withdrawals: int = 0,
used_matched_rewards: int = None,
):
snapshot = FinalizedEpochSnapshot(
epoch=epoch,
Expand All @@ -48,5 +49,6 @@ def save_snapshot(
patrons_rewards=str(patrons_rewards),
leftover=str(leftover),
total_withdrawals=str(total_withdrawals),
used_matched_rewards=str(used_matched_rewards),
)
db.session.add(snapshot)
3 changes: 3 additions & 0 deletions backend/app/infrastructure/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ class FinalizedEpochSnapshot(BaseModel):
leftover = Column(db.String, nullable=False)
withdrawals_merkle_root = Column(db.String)
total_withdrawals = Column(db.String)
used_matched_rewards = Column(
db.String, nullable=True
) # since a cap on matched rewards was introduced


class Deposit(BaseModel):
Expand Down
4 changes: 4 additions & 0 deletions backend/app/infrastructure/routes/epochs.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def get(self):
required=False,
description="Community fund for the given epoch. It's calculated from staking proceeds directly.",
),
"donatedToProjects": fields.String(
required=False,
description="The amount of funds donated to projects. Includes MR and allocations.",
),
},
)

Expand Down
3 changes: 3 additions & 0 deletions backend/app/modules/dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class FinalizedSnapshotDTO(JSONWizard):
total_withdrawals: int
leftover: int
merkle_root: str
used_matched_rewards: int = None # since a cap on matched rewards was introduced


@dataclass
Expand All @@ -53,6 +54,8 @@ class OctantRewardsDTO(JSONWizard):
# Data available starting from Epoch 3
ppf: Optional[int] = None
community_fund: Optional[int] = None
# Data needed since Epoch 4
donated_to_projects: Optional[int] = None


@dataclass(frozen=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def get_octant_rewards(self, context: Context) -> OctantRewardsDTO:
finalized_snapshot = database.finalized_epoch_snapshot.get_by_epoch(
context.epoch_details.epoch_num
)
allocations_sum = database.allocations.get_alloc_sum_by_epoch(
context.epoch_details.epoch_num
)
donated_to_projects = (
int(finalized_snapshot.used_matched_rewards) + allocations_sum
)

return OctantRewardsDTO(
staking_proceeds=int(pending_snapshot.eth_proceeds),
Expand All @@ -28,6 +34,7 @@ def get_octant_rewards(self, context: Context) -> OctantRewardsDTO:
total_withdrawals=int(finalized_snapshot.total_withdrawals),
ppf=pending_snapshot.validated_ppf,
community_fund=pending_snapshot.validated_community_fund,
donated_to_projects=donated_to_projects,
)

def get_leverage(self, context: Context) -> float:
Expand Down
31 changes: 23 additions & 8 deletions backend/app/modules/octant_rewards/general/service/pending.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def get_octant_rewards(self, context: Context) -> OctantRewardsDTO:
context.epoch_details.epoch_num
)
matched_rewards = self.octant_matched_rewards.get_matched_rewards(context)
project_rewards = self._get_project_rewards(context, matched_rewards)

return OctantRewardsDTO(
staking_proceeds=int(pending_snapshot.eth_proceeds),
Expand All @@ -63,7 +64,10 @@ def get_octant_rewards(self, context: Context) -> OctantRewardsDTO:
ppf=pending_snapshot.validated_ppf,
matched_rewards=matched_rewards,
patrons_rewards=self.patrons_mode.get_patrons_rewards(context),
leftover=self.get_leftover(context, pending_snapshot, matched_rewards),
leftover=self._get_leftover(
context, pending_snapshot, matched_rewards, project_rewards
),
donated_to_projects=self._get_donated_to_projects(project_rewards),
)

def get_matched_rewards(self, context: Context) -> int:
Expand All @@ -79,19 +83,14 @@ def get_leverage(self, context: Context) -> float:
matched_rewards, allocations_sum
)

def get_leftover(
def _get_leftover(
self,
context: Context,
pending_snapshot: PendingEpochSnapshot,
matched_rewards: int,
project_rewards: FinalizedProjectRewards,
) -> int:
allocations = database.allocations.get_all_with_uqs(
context.epoch_details.epoch_num
)
_, user_rewards = self.user_rewards.get_claimed_rewards(context)
project_rewards = self.project_rewards.get_finalized_project_rewards(
context, allocations, context.projects_details.projects, matched_rewards
)

return context.epoch_settings.octant_rewards.leftover.calculate_leftover(
LeftoverPayload(
Expand All @@ -106,3 +105,19 @@ def get_leftover(
used_matched_rewards=sum(r.matched for r in project_rewards.rewards),
)
)

def _get_donated_to_projects(self, project_rewards: FinalizedProjectRewards) -> int:
total_user_donations_with_used_matched_rewards = sum(
r.amount for r in project_rewards.rewards
)

return total_user_donations_with_used_matched_rewards

def _get_project_rewards(self, context: Context, matched_rewards: int):
allocations = database.allocations.get_all_with_uqs(
context.epoch_details.epoch_num
)
project_rewards = self.project_rewards.get_finalized_project_rewards(
context, allocations, context.projects_details.projects, matched_rewards
)
return project_rewards
6 changes: 4 additions & 2 deletions backend/app/modules/projects/rewards/service/finalizing.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

from app.context.manager import Context
from app.modules.common.project_rewards import get_projects_rewards, AllocationsPayload
from app.modules.dto import AllocationDTO, ProjectAccountFundsDTO
Expand All @@ -9,8 +11,8 @@ class FinalizingProjectRewards(Model):
def get_finalized_project_rewards(
self,
context: Context,
allocations: list[AllocationDTO],
all_projects: list[str],
allocations: List[AllocationDTO],
all_projects: List[str],
matched_rewards: int,
) -> FinalizedProjectRewards:
allocations_payload = AllocationsPayload(
Expand Down
1 change: 1 addition & 0 deletions backend/app/modules/snapshots/finalized/service/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _calculate_finalized_epoch_snapshot(
return FinalizedSnapshotDTO(
patrons_rewards=patrons_rewards,
matched_rewards=matched_rewards,
used_matched_rewards=used_matched_rewards,
projects_rewards=project_rewards.rewards,
user_rewards=user_rewards,
total_withdrawals=total_withdrawals,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ def _save_snapshot(self, epoch: int, dto: FinalizedSnapshotDTO):
leftover=dto.leftover,
withdrawals_merkle_root=dto.merkle_root,
total_withdrawals=dto.total_withdrawals,
used_matched_rewards=dto.used_matched_rewards,
)
2 changes: 2 additions & 0 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,7 @@ def mock_finalized_epoch_snapshot_db_since_epoch3(app, user_accounts):
NO_PATRONS_REWARDS,
LEFTOVER,
total_withdrawals=TOTAL_WITHDRAWALS,
used_matched_rewards=MATCHED_REWARDS_AFTER_OVERHAUL,
)

db.session.commit()
Expand All @@ -1275,6 +1276,7 @@ def mock_finalized_epoch_snapshot_db(app, user_accounts):
NO_PATRONS_REWARDS,
LEFTOVER,
total_withdrawals=TOTAL_WITHDRAWALS,
used_matched_rewards=MATCHED_REWARDS,
)

db.session.commit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
from app.modules.snapshots.finalized.service.finalizing import FinalizingSnapshots
from tests.helpers import make_user_allocation
from tests.helpers.allocations import make_user_allocation_with_uq_score
from tests.helpers.constants import MATCHED_REWARDS, USER2_BUDGET, LOW_UQ_SCORE
from tests.helpers.constants import (
MATCHED_REWARDS,
USER2_BUDGET,
LOW_UQ_SCORE,
MR_FUNDING_CAP_PERCENT,
)
from tests.helpers.context import get_context


Expand Down Expand Up @@ -97,6 +102,9 @@ def test_create_finalized_snapshots_with_rewards_and_user_uq_score(
)
assert snapshot.patrons_rewards == str(USER2_BUDGET)
assert snapshot.leftover == str(414362124463057389617)
assert snapshot.used_matched_rewards == str(
int(MR_FUNDING_CAP_PERCENT * MATCHED_REWARDS)
)
assert (
snapshot.withdrawals_merkle_root
== "0xb6bd0828b53f444a572c2da19f9f213a0e67109db91c2fbc458bbd42c4269775"
Expand Down

0 comments on commit 311a936

Please sign in to comment.