Skip to content

Commit

Permalink
OCT-1354: move withdrawals to new arch (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoni-q authored Mar 14, 2024
1 parent 64a8471 commit 77572dd
Show file tree
Hide file tree
Showing 31 changed files with 548 additions and 663 deletions.
7 changes: 6 additions & 1 deletion backend/app/infrastructure/database/allocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def get_users_with_allocations(epoch_num: int) -> List[str]:
return [u.address for u in users]


def get_alloc_sum_by_epoch_and_user_address(epoch: int) -> List[AccountFundsDTO]:
def get_users_alloc_sum_by_epoch(epoch: int) -> List[AccountFundsDTO]:
allocations = (
db.session.query(User, Allocation)
.join(User, User.id == Allocation.user_id)
Expand Down Expand Up @@ -157,6 +157,11 @@ def get_alloc_sum_by_epoch(epoch: int) -> int:
return sum([int(a.amount) for a in allocations])


def get_user_alloc_sum_by_epoch(epoch: int, user_address: str) -> int:
allocations = get_all_by_user_addr_and_epoch(user_address, epoch)
return sum([int(a.amount) for a in allocations])


def add_all(epoch: int, user_id: int, nonce: int, allocations):
now = datetime.utcnow()

Expand Down
8 changes: 2 additions & 6 deletions backend/app/infrastructure/routes/withdrawals.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import dataclasses

from flask import current_app as app
from flask_restx import Namespace, fields

from app.legacy.controllers import withdrawals
from app.extensions import api
from app.infrastructure import OctantResource
from app.modules.withdrawals.controller import get_withdrawable_eth

ns = Namespace("withdrawals", description="Octant withdrawals")
api.add_namespace(ns)
Expand Down Expand Up @@ -49,9 +47,7 @@ class Withdrawals(OctantResource):
@ns.marshal_with(withdrawable_rewards_model)
def get(self, address):
app.logger.debug(f"Getting withdrawable eth for address: {address}")
result = [
dataclasses.asdict(w) for w in withdrawals.get_withdrawable_eth(address)
]
result = get_withdrawable_eth(address)
app.logger.debug(f"Withdrawable eth for address: {address}: {result}")

return result
2 changes: 1 addition & 1 deletion backend/app/legacy/controllers/rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def get_rewards_merkle_tree(epoch: int) -> RewardsMerkleTree:
if not core_epoch_snapshots.has_finalized_epoch_snapshot(epoch):
raise exceptions.InvalidEpoch

mt = merkle_tree.get_merkle_tree_for_epoch(epoch)
mt = merkle_tree.get_rewards_merkle_tree_for_epoch(epoch)
leaves = [
RewardsMerkleTreeLeaf(address=leaf.value[0], amount=leaf.value[1])
for leaf in mt.values
Expand Down
56 changes: 0 additions & 56 deletions backend/app/legacy/controllers/withdrawals.py

This file was deleted.

9 changes: 0 additions & 9 deletions backend/app/legacy/core/allocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,3 @@ def next_allocation_nonce(user: User | None) -> int:
if user.allocation_nonce is None:
return 0
return user.allocation_nonce + 1


def has_user_allocated_rewards(user_address: str, epoch: int) -> List[str]:
allocation_signature = (
database.allocations.get_allocation_request_by_user_and_epoch(
user_address, epoch
)
)
return allocation_signature is not None
35 changes: 0 additions & 35 deletions backend/app/legacy/core/user/rewards.py

This file was deleted.

7 changes: 1 addition & 6 deletions backend/app/modules/common/merkle_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@
LEAF_ENCODING: List[str] = ["address", "uint256"]


def get_proof_by_address_and_epoch(address: str, epoch: int) -> List[str]:
merkle_tree = get_merkle_tree_for_epoch(epoch)
return get_proof(merkle_tree, address)


def get_merkle_tree_for_epoch(epoch: int) -> StandardMerkleTree:
def get_rewards_merkle_tree_for_epoch(epoch: int) -> StandardMerkleTree:
leaves = [
AccountFundsDTO(r.address, int(r.amount))
for r in database.rewards.get_by_epoch(epoch)
Expand Down
14 changes: 14 additions & 0 deletions backend/app/modules/dto.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from decimal import Decimal
from enum import StrEnum
from typing import Optional, List

from dataclass_wizard import JSONWizard
Expand Down Expand Up @@ -55,3 +56,16 @@ class OctantRewardsDTO(JSONWizard):
@dataclass(frozen=True)
class AllocationDTO(AllocationPayload, JSONWizard):
user_address: Optional[str] = None


class WithdrawalStatus(StrEnum):
PENDING = "pending"
AVAILABLE = "available"


@dataclass(frozen=True)
class WithdrawableEth:
epoch: int
amount: int
proof: list[str]
status: WithdrawalStatus
16 changes: 11 additions & 5 deletions backend/app/modules/modules_factory/finalized.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
TotalEffectiveDeposits,
Leverage,
UserBudgets,
WithdrawalsService,
)
from app.modules.octant_rewards.service.finalized import FinalizedOctantRewards
from app.modules.user.allocations.service.saved import SavedUserAllocations
from app.modules.user.budgets.service.saved import SavedUserBudgets
from app.modules.user.deposits.service.saved import SavedUserDeposits
from app.modules.user.patron_mode.service.events_based import EventsBasedUserPatronMode
from app.modules.user.rewards.service.saved import SavedUserRewards
from app.modules.withdrawals.service.finalized import FinalizedWithdrawals
from app.pydantic import Model


Expand All @@ -34,22 +36,26 @@ class FinalizedServices(Model):
user_patron_mode_service: UserPatronMode
user_budgets_service: UserBudgets
user_rewards_service: UserRewards
withdrawals_service: WithdrawalsService

@staticmethod
def create() -> "FinalizedServices":
events_based_patron_mode = EventsBasedUserPatronMode()
saved_user_allocations = SavedUserAllocations()
saved_user_budgets = SavedUserBudgets()
user_rewards = SavedUserRewards(
user_budgets=saved_user_budgets,
patrons_mode=events_based_patron_mode,
allocations=saved_user_allocations,
)
withdrawals_service = FinalizedWithdrawals(user_rewards=user_rewards)

return FinalizedServices(
user_deposits_service=SavedUserDeposits(),
octant_rewards_service=FinalizedOctantRewards(),
user_allocations_service=saved_user_allocations,
user_patron_mode_service=events_based_patron_mode,
user_budgets_service=saved_user_budgets,
user_rewards_service=SavedUserRewards(
user_budgets=saved_user_budgets,
patrons_mode=events_based_patron_mode,
allocations=saved_user_allocations,
),
user_rewards_service=user_rewards,
withdrawals_service=withdrawals_service,
)
5 changes: 5 additions & 0 deletions backend/app/modules/modules_factory/finalizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Leverage,
UserBudgets,
CreateFinalizedSnapshots,
WithdrawalsService,
)
from app.modules.octant_rewards.service.pending import PendingOctantRewards
from app.modules.snapshots.finalized.service.finalizing import FinalizingSnapshots
Expand All @@ -18,6 +19,7 @@
from app.modules.user.deposits.service.saved import SavedUserDeposits
from app.modules.user.patron_mode.service.events_based import EventsBasedUserPatronMode
from app.modules.user.rewards.service.calculated import CalculatedUserRewards
from app.modules.withdrawals.service.pending import PendingWithdrawals
from app.pydantic import Model


Expand All @@ -37,6 +39,7 @@ class FinalizingServices(Model):
user_budgets_service: UserBudgets
user_rewards_service: UserRewards
finalized_snapshots_service: CreateFinalizedSnapshots
withdrawals_service: WithdrawalsService

@staticmethod
def create() -> "FinalizingServices":
Expand All @@ -54,6 +57,7 @@ def create() -> "FinalizingServices":
user_rewards=user_rewards,
patrons_mode=events_based_patron_mode,
)
withdrawals_service = PendingWithdrawals(user_rewards=user_rewards)

return FinalizingServices(
user_deposits_service=SavedUserDeposits(),
Expand All @@ -63,4 +67,5 @@ def create() -> "FinalizingServices":
user_budgets_service=saved_user_budgets,
user_rewards_service=user_rewards,
finalized_snapshots_service=finalized_snapshots_service,
withdrawals_service=withdrawals_service,
)
5 changes: 5 additions & 0 deletions backend/app/modules/modules_factory/pending.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
SimulateAllocation,
SimulateFinalizedSnapshots,
UserBudgets,
WithdrawalsService,
)
from app.modules.octant_rewards.service.pending import PendingOctantRewards
from app.modules.snapshots.finalized.service.simulated import (
Expand All @@ -21,6 +22,7 @@
from app.modules.user.deposits.service.saved import SavedUserDeposits
from app.modules.user.patron_mode.service.events_based import EventsBasedUserPatronMode
from app.modules.user.rewards.service.calculated import CalculatedUserRewards
from app.modules.withdrawals.service.pending import PendingWithdrawals
from app.pydantic import Model


Expand All @@ -44,6 +46,7 @@ class PendingServices(Model):
user_budgets_service: UserBudgets
user_rewards_service: UserRewards
finalized_snapshots_service: SimulateFinalizedSnapshots
withdrawals_service: WithdrawalsService

@staticmethod
def create() -> "PendingServices":
Expand All @@ -61,6 +64,7 @@ def create() -> "PendingServices":
user_rewards=user_rewards,
patrons_mode=events_based_patron_mode,
)
withdrawals_service = PendingWithdrawals(user_rewards=user_rewards)

return PendingServices(
user_deposits_service=SavedUserDeposits(),
Expand All @@ -70,4 +74,5 @@ def create() -> "PendingServices":
finalized_snapshots_service=finalized_snapshots_service,
user_budgets_service=saved_user_budgets,
user_rewards_service=user_rewards,
withdrawals_service=withdrawals_service,
)
15 changes: 14 additions & 1 deletion backend/app/modules/modules_factory/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
from app.context.manager import Context
from app.engine.projects.rewards import ProjectRewardDTO
from app.engine.user.effective_deposit import UserDeposit
from app.modules.dto import OctantRewardsDTO, AllocationDTO, FinalizedSnapshotDTO
from app.modules.dto import (
OctantRewardsDTO,
AllocationDTO,
FinalizedSnapshotDTO,
WithdrawableEth,
)


@runtime_checkable
Expand Down Expand Up @@ -94,3 +99,11 @@ def simulate_finalized_epoch_snapshot(
self, context: Context
) -> FinalizedSnapshotDTO:
...


@runtime_checkable
class WithdrawalsService(Protocol):
def get_withdrawable_eth(
self, context: Context, address: str
) -> list[WithdrawableEth]:
...
3 changes: 1 addition & 2 deletions backend/app/modules/snapshots/finalized/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from app.exceptions import InvalidEpoch
from app.modules.dto import FinalizedSnapshotDTO
from app.modules.modules_factory.finalizing import FinalizingServices
from app.modules.modules_factory.pending import PendingServices
from app.modules.registry import get_services


Expand All @@ -18,7 +17,7 @@ def create_finalized_epoch_snapshot() -> int | None:

def simulate_finalized_epoch_snapshot() -> FinalizedSnapshotDTO | None:
context = state_context(EpochState.PENDING)
services: PendingServices = get_services(EpochState.PENDING)
services = get_services(EpochState.PENDING)
return services.finalized_snapshots_service.simulate_finalized_epoch_snapshot(
context
)
18 changes: 3 additions & 15 deletions backend/app/modules/user/allocations/service/pending.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from app.context.manager import Context
from app.engine.projects.rewards import ProjectRewardDTO
from app.infrastructure import database
from app.modules.dto import AllocationDTO, AccountFundsDTO
from app.modules.dto import AllocationDTO
from app.modules.user.allocations import core
from app.pydantic import Model
from app.modules.user.allocations.service.saved import SavedUserAllocations


@runtime_checkable
Expand All @@ -14,21 +14,9 @@ def get_matched_rewards(self, context: Context) -> int:
...


class PendingUserAllocations(Model):
class PendingUserAllocations(SavedUserAllocations):
octant_rewards: OctantRewards

def get_all_donors_addresses(self, context: Context) -> List[str]:
return database.allocations.get_users_with_allocations(
context.epoch_details.epoch_num
)

def get_all_users_with_allocations_sum(
self, context: Context
) -> List[AccountFundsDTO]:
return database.allocations.get_alloc_sum_by_epoch_and_user_address(
context.epoch_details.epoch_num
)

def simulate_allocation(
self,
context: Context,
Expand Down
Loading

0 comments on commit 77572dd

Please sign in to comment.