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 0d6e735
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 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)
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)
10 changes: 9 additions & 1 deletion backend/app/modules/octant_rewards/general/service/finalized.py
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,15 @@ 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.group_allocations_by_projects(
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 0d6e735

Please sign in to comment.