Skip to content

Commit

Permalink
Merge branch 'fix/oct-1569' of github.com:golemfoundation/octant into…
Browse files Browse the repository at this point in the history
… fix/oct-1569
  • Loading branch information
aziolek committed Apr 19, 2024
2 parents 07a30e5 + 55ca979 commit 14b5c1f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
from dataclasses import dataclass
from decimal import Decimal

from app.exceptions import InvalidMatchedRewardsStrategy


@dataclass
class PercentageMatchedRewards(MatchedRewards):
MATCHED_REWARDS_PERCENT: Decimal

def calculate_matched_rewards(self, payload: MatchedRewardsPayload) -> int:
if payload.locked_ratio > payload.tr_percent:
raise InvalidMatchedRewardsStrategy()

if payload.locked_ratio < payload.ire_percent:
return int(
self.MATCHED_REWARDS_PERCENT * payload.staking_proceeds
Expand Down
10 changes: 10 additions & 0 deletions backend/app/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,13 @@ class InvalidMultisigAddress(OctantException):

def __init__(self):
super().__init__(self.description, self.code)


class InvalidMatchedRewardsStrategy(OctantException):
code = 500
description = (
"Can't calculate matched rewards when locked ratio is greater than TR percent"
)

def __init__(self):
super().__init__(self.description, self.code)
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Update PPF after invalid calculations for Epoch3
Revision ID: 8e72b01c43a7
Revises: fc7a01c20be5
Create Date: 2024-04-19 00:48:12.133033
"""
from alembic import op
from decimal import Decimal

revision = "8e72b01c43a7"
down_revision = "fc7a01c20be5"
branch_labels = None
depends_on = None

individual_rewards = Decimal(
"146655862334541166188"
) # Got directly from the database for Pending Snapshot in Epoch3
staking_proceeds = Decimal(
"959848624830407629247"
) # Got directly from the database for Pending Snapshot in Epoch3
ppf = int(staking_proceeds * Decimal("0.35") - individual_rewards)


def upgrade():
query = f"UPDATE pending_epoch_snapshots SET ppf = '{ppf}' WHERE epoch = 3 AND all_individual_rewards = '{individual_rewards}' AND eth_proceeds = '{staking_proceeds}';"
op.execute(query)


def downgrade():
old_ppf = "335947018690642670236"

query = f"UPDATE pending_epoch_snapshots SET ppf = '{old_ppf}' WHERE epoch = 3 AND all_individual_rewards = '{individual_rewards}' AND eth_proceeds = '{staking_proceeds}' AND ppf = '{ppf}';"

print(query, flush=True)

op.execute(query)
43 changes: 42 additions & 1 deletion backend/tests/engine/octant_rewards/test_matched_rewards.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
from decimal import Decimal

import pytest

from app.engine.octant_rewards.matched.preliminary import PreliminaryMatchedRewards
from app.engine.octant_rewards.matched import MatchedRewardsPayload
from tests.helpers.constants import TOTAL_REWARDS, ALL_INDIVIDUAL_REWARDS
from app.engine.octant_rewards import (
PercentageMatchedRewards,
OctantRewardsDefaultValues,
)
from app.exceptions import InvalidMatchedRewardsStrategy
from tests.helpers.constants import (
TOTAL_REWARDS,
ALL_INDIVIDUAL_REWARDS,
ETH_PROCEEDS,
USER2_BUDGET,
)
from tests.modules.octant_rewards.helpers.overhaul_formulas import (
IRE_PERCENT,
TR_PERCENT,
)


def test_preliminary_matched_rewards():
Expand All @@ -13,3 +31,26 @@ def test_preliminary_matched_rewards():
result = uut.calculate_matched_rewards(payload)

assert result == 220115925184490486394


def test_matched_rewards_when_locked_ratio_greater_than_tr_percent():
payload = MatchedRewardsPayload(
staking_proceeds=ETH_PROCEEDS,
patrons_rewards=USER2_BUDGET,
locked_ratio=TR_PERCENT + Decimal("0.01"),
ire_percent=IRE_PERCENT,
tr_percent=TR_PERCENT,
)

uut = PercentageMatchedRewards(
MATCHED_REWARDS_PERCENT=OctantRewardsDefaultValues.MATCHED_REWARDS_PERCENT
)

with pytest.raises(InvalidMatchedRewardsStrategy) as exc:
uut.calculate_matched_rewards(payload)

assert exc.value.status_code == 500
assert (
exc.value.description
== "Can't calculate matched rewards when locked ratio is greater than TR percent"
)
31 changes: 30 additions & 1 deletion backend/tests/engine/user/test_budget.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from app.engine.user.budget.preliminary import PreliminaryUserBudget
from app.engine.user.budget.with_ppf import UserBudgetWithPPF
from app.engine.user.budget import UserBudgetPayload
from tests.helpers.constants import USER1_ED, TOTAL_ED, ALL_INDIVIDUAL_REWARDS
from tests.helpers.constants import USER1_ED, TOTAL_ED, ALL_INDIVIDUAL_REWARDS, PPF


def test_preliminary_user_budget():
Expand Down Expand Up @@ -30,3 +31,31 @@ def test_preliminary_user_budget_total_effective_is_none():
result = uut.calculate_budget(payload)

assert result == 0


def test_user_budget_with_ppf():
payload = UserBudgetPayload(
user_effective_deposit=USER1_ED,
all_individual_rewards=ALL_INDIVIDUAL_REWARDS,
ppf=PPF,
total_effective_deposit=TOTAL_ED,
)
uut = UserBudgetWithPPF()

result = uut.calculate_budget(payload)

assert result == 1819523568520052


def test_user_budget_with_ppf_as_null():
payload = UserBudgetPayload(
user_effective_deposit=USER1_ED,
all_individual_rewards=ALL_INDIVIDUAL_REWARDS,
ppf=0,
total_effective_deposit=TOTAL_ED,
)
uut = UserBudgetWithPPF()

result = uut.calculate_budget(payload)

assert result == 1526868989237987

0 comments on commit 14b5c1f

Please sign in to comment.