Skip to content

Commit

Permalink
fix: change the way of grouping allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarbacinski committed Aug 30, 2024
1 parent a3277d4 commit d9e93a1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
9 changes: 4 additions & 5 deletions backend/app/engine/projects/rewards/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from collections import namedtuple
from dataclasses import dataclass, field
from typing import List, Optional
from typing import List, Optional, Dict

from dataclass_wizard import JSONWizard

Expand Down Expand Up @@ -62,8 +62,7 @@ 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
self, allocations: Dict[str, List[AllocationItem]]
) -> AllocationsBelowThreshold:
return AllocationsBelowThreshold(
0, sum(map(lambda allocation: int(allocation.amount), allocations))
)
allocations_sum = sum(sum(allocations[project]) for project in allocations)
return AllocationsBelowThreshold(0, allocations_sum)
12 changes: 9 additions & 3 deletions backend/app/engine/projects/rewards/allocations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,24 @@ def _calc_allocations(
) -> Union[int, Decimal]:
...

def group_allocations_by_projects(
def segregate_allocations(
self, payload: ProjectAllocationsPayload
) -> (Dict[str, List], List[ProjectSumAllocationsDTO], Union[int, Decimal]):
result_allocations = []
) -> Dict[str, List]:
grouped_allocations = {
key: list(group)
for key, group in groupby(
sorted(payload.allocations, key=lambda a: a.project_address),
key=lambda a: a.project_address,
)
}
return grouped_allocations

def group_allocations_by_projects(
self, payload: ProjectAllocationsPayload
) -> (Dict[str, List], List[ProjectSumAllocationsDTO], Union[int, Decimal]):
grouped_allocations = self.segregate_allocations(payload)

result_allocations = []
total_plain_qf = 0
for project_address, project_allocations in grouped_allocations.items():
project_allocations = self._calc_allocations(project_allocations)
Expand Down
6 changes: 3 additions & 3 deletions backend/app/engine/projects/rewards/preliminary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import field, dataclass
from decimal import Decimal
from typing import List
from typing import List, Dict

from app.engine.projects.rewards import (
ProjectRewardsPayload,
Expand Down Expand Up @@ -45,10 +45,10 @@ def calculate_threshold(self, total_allocated: int, projects: List[str]) -> int:
)

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

def calculate_project_rewards(
Expand Down
7 changes: 4 additions & 3 deletions backend/app/engine/projects/rewards/threshold/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import List
from typing import List, Dict

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


@dataclass
Expand All @@ -19,6 +20,6 @@ def calculate_threshold(self, payload: ProjectThresholdPayload) -> int:

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

from app.engine.projects.rewards import AllocationItem, AllocationsBelowThreshold
from app.engine.projects.rewards import AllocationsBelowThreshold
from app.engine.projects.rewards.threshold import (
ProjectThreshold,
ProjectThresholdPayload,
)
from app.engine.projects.rewards import AllocationItem


@dataclass
Expand All @@ -23,17 +24,22 @@ def calculate_threshold(self, payload: ProjectThresholdPayload) -> int:
)

def get_total_allocations_below_threshold(
self, allocations: List[AllocationItem], no_projects: int
self, allocations: Dict[str, List[AllocationItem]]
) -> AllocationsBelowThreshold:
allocations_sum = sum(map(lambda x: int(x.amount), allocations))
summed_allocations = {
project: sum(map(lambda value: int(value.amount), values))
for project, values in allocations.items()
}
total_allocations = sum(summed_allocations.values())
no_projects = len(allocations.keys())

threshold = self.calculate_threshold(
ProjectThresholdPayload(allocations_sum, no_projects)
ProjectThresholdPayload(total_allocations, no_projects)
)

allocations_below_threshold = 0
for allocation in allocations:
amount = int(allocation.amount)
if amount < threshold:
allocations_below_threshold += amount
for allocations_sum_for_project in summed_allocations.values():
if allocations_sum_for_project < threshold:
allocations_below_threshold += allocations_sum_for_project

return AllocationsBelowThreshold(allocations_below_threshold, allocations_sum)
return AllocationsBelowThreshold(allocations_below_threshold, total_allocations)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from app.modules.dto import OctantRewardsDTO
from app.pydantic import Model
from app.engine.octant_rewards.leftover import LeftoverPayload
from app.engine.projects.rewards.allocations import ProjectAllocationsPayload


class FinalizedOctantRewards(Model):
Expand All @@ -31,8 +32,11 @@ def get_octant_rewards(self, context: Context) -> OctantRewardsDTO:
allocations_for_epoch = database.allocations.get_all_by_epoch(
context.epoch_details.epoch_num
)
grouped_allocations = context.epoch_settings.project.rewards.projects_allocations.segregate_allocations(
ProjectAllocationsPayload(allocations=allocations_for_epoch)
)
allocations_result = context.epoch_settings.project.rewards.get_total_allocations_below_threshold(
allocations_for_epoch, len(context.projects_details.projects)
grouped_allocations
)
allocations_sum = allocations_result.total
allocations_below_threshold = allocations_result.below_threshold
Expand Down

0 comments on commit d9e93a1

Please sign in to comment.