Skip to content

Commit

Permalink
cr: alignements
Browse files Browse the repository at this point in the history
  • Loading branch information
kgarbacinski committed Dec 10, 2024
1 parent a76985b commit 84cf834
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 50 deletions.
8 changes: 8 additions & 0 deletions backend/v2/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ class AllocationWindowClosed(OctantException):

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


class EpochsNotFound(OctantException):
code = 404
description = "Epochs not found in Subgraph"

def __init__(self):
super().__init__(self.description, self.code)
10 changes: 6 additions & 4 deletions backend/v2/epochs/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from fastapi import Depends

from app.exceptions import InvalidEpoch
from app.modules.staking.proceeds.core import ESTIMATED_STAKING_REWARDS_RATE
from v2.core.dependencies import OctantSettings, Web3
from v2.core.exceptions import AllocationWindowClosed
Expand Down Expand Up @@ -61,14 +62,15 @@ async def get_current_epoch(epochs_contracts: GetEpochsContracts) -> int:


async def get_indexed_epoch(epochs_subgraph: GetEpochsSubgraph) -> int:
sg_epochs = await epochs_subgraph.get_epochs()
sg_epochs_sorted = sorted(sg_epochs, key=lambda d: d.epoch_num)

return sg_epochs_sorted[-1].epoch_num
latest_epoch = await epochs_subgraph.get_latest_epoch()
return latest_epoch.epoch_num


async def get_rewards_rate(epoch_number: int) -> float:
logging.debug(f"Getting rewards rate for epoch {epoch_number}")
if epoch_number <= 0:
raise InvalidEpoch()

return ESTIMATED_STAKING_REWARDS_RATE


Expand Down
48 changes: 25 additions & 23 deletions backend/v2/epochs/router.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@
from fastapi import APIRouter

from v2.epochs.dependencies import (
GetEpochsContracts,
GetCurrentEpoch,
GetIndexedEpoch,
GetRewardsRate,
)
from v2.epochs.schemas import (
CurrentEpochResponse,
IndexedEpochResponse,
EpochRewardsRateResponse,
CurrentEpochResponseV1,
IndexedEpochResponseV1,
EpochRewardsRateResponseV1,
)

api = APIRouter(prefix="/epochs", tags=["Epochs"])


@api.get("/current")
async def get_current_epoch_v1(
epochs_contract: GetEpochsContracts,
) -> CurrentEpochResponse:
current_epoch: GetCurrentEpoch,
) -> CurrentEpochResponseV1:
"""
Returns the current epoch number from the blockchain.
This endpoint queries the smart contract to get the current active epoch number.
The epoch number is used throughout the system to track allocation periods
and rewards distribution cycles.
Returns:
CurrentEpochResponse: Object containing the current epoch number
Args:
current_epoch (int): Injected dependency representing the current epoch number
retrieved from the blockchain.
"""
current_epoch = epochs_contract.get_current_epoch()
return CurrentEpochResponse(current_epoch=current_epoch)
return CurrentEpochResponseV1(current_epoch=current_epoch)


@api.get("/indexed")
async def get_indexed_epoch_v1(
current_epoch: GetCurrentEpoch, indexed_epoch: GetIndexedEpoch
) -> IndexedEpochResponse:
) -> IndexedEpochResponseV1:
"""
Returns the last indexed epoch number and current epoch number from the blockchain.
This endpoint provides information about the indexing status of epochs by returning both
the current epoch number and the last epoch that has been fully indexed in the system.
the current epoch number and the last epoch that has been fully indexed in the subgraph.
The indexed epoch will always be less than or equal to the current epoch.
If they differ, it means the subgraph is still being updated.
Returns:
IndexedEpochResponse: Object containing both the current epoch number and last indexed epoch number
Args:
current_epoch (int): Injected dependency representing the current epoch number
retrieved from the blockchain.
indexed_epoch (int): Injected dependency representing the last indexed epoch number
retrieved from the subgraph.
"""
return IndexedEpochResponse(
return IndexedEpochResponseV1(
current_epoch=current_epoch, indexed_epoch=indexed_epoch
)

Expand All @@ -68,18 +71,17 @@ async def get_indexed_epoch_v1(
@api.get("/rewards-rate/{epoch_number}")
async def get_epoch_rewards_rate_v1(
rewards_rate: GetRewardsRate,
) -> EpochRewardsRateResponse:
) -> EpochRewardsRateResponseV1:
"""
Returns the rewards rate for a specific epoch from the blockchain.
This endpoint queries the smart contract to get the rewards rate for the specified epoch number.
The rewards rate represents the percentage of rewards that will be distributed for that epoch,
and is used in calculating final reward amounts.
The rewards rate represents the percentage of rewards distributed for that epoch and is used
in calculating final reward amounts.
Args:
rewards_rate (float): The rewards rate retrieved from the blockchain via dependency injection
Returns:
EpochRewardsRateResponse: Object containing the rewards rate for the specified epoch
epoch_number (int): The epoch number in the blockchain to fetch rewards rate for.
rewards_rate (float): Injected dependency representing the rewards rate retrieved
from the blockchain.
"""
return EpochRewardsRateResponse(rewards_rate=rewards_rate)
return EpochRewardsRateResponseV1(rewards_rate=rewards_rate)
8 changes: 4 additions & 4 deletions backend/v2/epochs/schemas.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from pydantic import BaseModel


class CurrentEpochResponse(BaseModel):
class CurrentEpochResponseV1(BaseModel):
current_epoch: int


class IndexedEpochResponse(BaseModel):
class IndexedEpochResponseV1(BaseModel):
current_epoch: int
indexed_epoch: int


class EpochStatsResponse(BaseModel):
class EpochStatsResponseV1(BaseModel):
staking_proceeds: str
total_effective_deposit: str
total_rewards: str
Expand All @@ -25,5 +25,5 @@ class EpochStatsResponse(BaseModel):
donated_to_projects: str | None


class EpochRewardsRateResponse(BaseModel):
class EpochRewardsRateResponseV1(BaseModel):
rewards_rate: float
39 changes: 20 additions & 19 deletions backend/v2/epochs/subgraphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import backoff
from app import exceptions
from v2.core.exceptions import EpochsNotFound
from app.context.epoch.details import EpochDetails
from gql import Client, gql
from gql.transport.aiohttp import AIOHTTPTransport
Expand Down Expand Up @@ -112,14 +113,14 @@ async def get_epoch_by_number(self, epoch_number: int) -> EpochDetails:
remaining_sec=0,
)

async def get_epochs(self) -> list[EpochDetails]:
"""Get all epochs from the subgraph."""
logging.debug("[Subgraph] Getting list of all epochs")
async def get_latest_epoch(self) -> EpochDetails:
"""Get latest epoch from the subgraph."""
logging.debug("[Subgraph] Getting latest epoch from subgraph.")

query = gql(
"""
query {
epoches(first: 1000) {
epoches(first: 1, orderBy: epoch, orderDirection: desc) {
epoch
fromTs
toTs
Expand All @@ -132,23 +133,23 @@ async def get_epochs(self) -> list[EpochDetails]:
}
}
}
"""
"""
)

data = await self.gql_client.execute_async(query)
response = await self.gql_client.execute_async(query)
data = response["epoches"]

logging.debug(f"[Subgraph] Received epochs: {data}")
if not data:
logging.warning("[Subgraph] No epochs included in the subgraph.")
raise EpochsNotFound()

epoches = data["epoches"]
epoches_details = []
for epoch in epoches:
epoch_detail = EpochDetails(
epoch_num=epoch["epoch"],
start=epoch["fromTs"],
duration=epoch["duration"],
decision_window=epoch["decisionWindow"],
remaining_sec=0,
)
epoches_details.append(epoch_detail)
logging.debug(f"[Subgraph] Received the latest epoch: {data}")

return epoches_details
epoch_details = data[0]
return EpochDetails(
epoch_num=epoch_details["epoch"],
start=epoch_details["fromTs"],
duration=epoch_details["duration"],
decision_window=epoch_details["decisionWindow"],
remaining_sec=0,
)

0 comments on commit 84cf834

Please sign in to comment.