Skip to content

Commit

Permalink
fix: add ethBelowThreshold for computations
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarbacinski committed Aug 29, 2024
1 parent 1883d77 commit 0aaf45e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
13 changes: 13 additions & 0 deletions backend/app/engine/projects/rewards/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
from collections import namedtuple
from dataclasses import dataclass, field
from typing import List, Optional

Expand Down Expand Up @@ -41,6 +42,11 @@ class ProjectRewardsResult:
threshold: Optional[int] = None


AllocationsBelowThreshold = namedtuple(
"AllocationsBelowThreshold", ["below_threshold", "total"]
)


@dataclass
class ProjectRewards(ABC):
projects_allocations: ProjectAllocations = field(init=False)
Expand All @@ -54,3 +60,10 @@ def calculate_project_rewards(

def calculate_threshold(self, total_allocated: int, projects: List[str]) -> None:
return None

def get_total_allocations_below_threshold(
self, allocations: List[AllocationItem], no_projects: int
) -> AllocationsBelowThreshold:
return AllocationsBelowThreshold(
0, sum(map(lambda allocation: allocation.amount, allocations))
)
11 changes: 10 additions & 1 deletion backend/app/engine/projects/rewards/preliminary.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
ProjectRewardsResult,
ProjectRewards,
ProjectRewardDTO,
AllocationsBelowThreshold,
)
from app.engine.projects.rewards.allocations import (
ProjectAllocations,
ProjectAllocationsPayload,
AllocationItem,
)
from app.engine.projects.rewards.allocations.preliminary import (
PreliminaryProjectAllocations,
)
from app.engine.projects.rewards.leverage.preliminary import PreliminaryLeverage
from app.engine.projects.rewards.threshold import (
ProjectThreshold,
ProjectThresholdPayload,
)
from app.engine.projects.rewards.threshold.preliminary import (
PreliminaryProjectThreshold,
)
from app.engine.projects.rewards.leverage.preliminary import PreliminaryLeverage


@dataclass
Expand All @@ -42,6 +44,13 @@ def calculate_threshold(self, total_allocated: int, projects: List[str]) -> int:
)
)

def get_total_allocations_below_threshold(
self, allocations: List[AllocationItem], no_projects: int
) -> AllocationsBelowThreshold:
return self.projects_threshold.get_total_allocations_below_threshold(
allocations, no_projects
)

def calculate_project_rewards(
self, payload: ProjectRewardsPayload
) -> ProjectRewardsResult:
Expand Down
9 changes: 9 additions & 0 deletions backend/app/engine/projects/rewards/threshold/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import List

from app.engine.projects.rewards import AllocationItem, AllocationsBelowThreshold


@dataclass
Expand All @@ -13,3 +16,9 @@ class ProjectThreshold(ABC):
@abstractmethod
def calculate_threshold(self, payload: ProjectThresholdPayload) -> int:
pass

@abstractmethod
def get_total_allocations_below_threshold(
self, allocations: List[AllocationItem], no_projects: int
) -> AllocationsBelowThreshold:
pass
17 changes: 17 additions & 0 deletions backend/app/engine/projects/rewards/threshold/preliminary.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from dataclasses import dataclass
from typing import List

from app.engine.projects.rewards import AllocationItem, AllocationsBelowThreshold
from app.engine.projects.rewards.threshold import (
ProjectThreshold,
ProjectThresholdPayload,
Expand All @@ -19,3 +21,18 @@ def calculate_threshold(self, payload: ProjectThresholdPayload) -> int:
if payload.projects_count
else 0
)

def get_total_allocations_below_threshold(
self, allocations: List[AllocationItem], no_projects: int
) -> AllocationsBelowThreshold:
allocations_sum = sum(map(lambda x: x.amount, allocations))
threshold = self.calculate_threshold(
ProjectThresholdPayload(allocations_sum, no_projects)
)

allocations_below_threshold = 0
for allocation in allocations:
if allocation.amount < threshold:
allocations_below_threshold += allocation.amount

return AllocationsBelowThreshold(allocations_below_threshold, allocations_sum)
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ def get_octant_rewards(self, context: Context) -> OctantRewardsDTO:
int(finalized_snapshot.leftover), leftover_payload
)

allocations_sum = database.allocations.get_alloc_sum_by_epoch(
allocations_for_epoch = database.allocations.get_all_by_epoch(
context.epoch_details.epoch_num
)
allocations_result = context.epoch_settings.project.rewards.get_total_allocations_below_threshold(
allocations_for_epoch, len(context.projects_details.projects)
)
allocations_sum = allocations_result.total
allocations_below_threshold = allocations_result.below_threshold

donated_to_projects = (
int(finalized_snapshot.matched_rewards)
- unused_matched_rewards
+ allocations_sum
- allocations_below_threshold
)

return OctantRewardsDTO(
Expand Down

0 comments on commit 0aaf45e

Please sign in to comment.