Skip to content
Snippets Groups Projects

Implement `Power up` part of hive power management

Merged Jakub Ziebinski requested to merge jziebinski/prepare-hp-management into develop
Compare and Show latest version
12 files
+ 122
203
Compare changes
  • Side-by-side
  • Inline
Files
12
@@ -16,10 +16,12 @@ if TYPE_CHECKING:
from schemas.apis.database_api.fundaments_of_reponses import VestingDelegationsFundament as VestingDelegation
from schemas.apis.database_api.fundaments_of_reponses import WithdrawVestingRoutesFundament as WithdrawRoute
_MAX_WITHDRAW_VESTING_ROUTES_LIMIT: Final[int] = 10
@dataclass
class HarvestedDataRaw:
dgpo: DynamicGlobalProperties | None = None
gdpo: DynamicGlobalProperties | None = None
core_account: FindAccounts | None = None
withdraw_routes: ListWithdrawVestingRoutes | None = None
delegations: FindVestingDelegations | None = None
@@ -27,7 +29,7 @@ class HarvestedDataRaw:
@dataclass
class SanitizedData:
dgpo: DynamicGlobalProperties
gdpo: DynamicGlobalProperties
core_account: SchemasAccount
withdraw_routes: list[WithdrawRoute]
delegations: list[VestingDelegation[Asset.Vests]]
@@ -53,7 +55,7 @@ class HivePowerData:
to_withdraw: SharesBalance
next_power_down: SharesBalance
current_hp_apr: str
dgpo: DynamicGlobalProperties
gdpo: DynamicGlobalProperties
@dataclass(kw_only=True)
@@ -67,14 +69,14 @@ class HivePowerDataRetrieval(CommandDataRetrieval[HarvestedDataRaw, SanitizedDat
await node.api.database_api.get_dynamic_global_properties(),
await node.api.database_api.find_accounts(accounts=[self.account_name]),
await node.api.database_api.list_withdraw_vesting_routes(
start=(self.account_name, ""), limit=10, order="by_withdraw_route"
start=(self.account_name, ""), limit=_MAX_WITHDRAW_VESTING_ROUTES_LIMIT, order="by_withdraw_route"
),
await node.api.database_api.find_vesting_delegations(account=self.account_name),
)
async def _sanitize_data(self, data: HarvestedDataRaw) -> SanitizedData:
return SanitizedData(
dgpo=self._assert_gdpo(data.dgpo),
gdpo=self._assert_gdpo(data.gdpo),
core_account=self._assert_core_account(data.core_account),
withdraw_routes=self._assert_withdraw_routes(data.withdraw_routes),
delegations=self._assert_delegations(data.delegations),
@@ -87,19 +89,19 @@ class HivePowerDataRetrieval(CommandDataRetrieval[HarvestedDataRaw, SanitizedDat
total_shares = owned_shares + received_shares - delegated_shares - data.core_account.vesting_withdraw_rate
return HivePowerData(
owned_balance=self._create_balance_representation(data.dgpo, owned_shares),
total_balance=self._create_balance_representation(data.dgpo, total_shares),
received_balance=self._create_balance_representation(data.dgpo, received_shares),
delegated_balance=self._create_balance_representation(data.dgpo, delegated_shares),
owned_balance=self._create_balance_representation(data.gdpo, owned_shares),
total_balance=self._create_balance_representation(data.gdpo, total_shares),
received_balance=self._create_balance_representation(data.gdpo, received_shares),
delegated_balance=self._create_balance_representation(data.gdpo, delegated_shares),
next_vesting_withdrawal=data.core_account.next_vesting_withdrawal,
withdraw_routes=[route for route in data.withdraw_routes if route.from_account == self.account_name],
delegations=data.delegations,
to_withdraw=self._create_balance_representation(
data.dgpo, Asset.vests(data.core_account.to_withdraw / 10**data.dgpo.total_vesting_shares.precision)
data.gdpo, Asset.vests(data.core_account.to_withdraw / 10**data.gdpo.total_vesting_shares.precision)
),
next_power_down=self._create_balance_representation(data.dgpo, data.core_account.vesting_withdraw_rate),
current_hp_apr=self._calculate_current_hp_apr(data.dgpo),
dgpo=data.dgpo,
next_power_down=self._create_balance_representation(data.gdpo, data.core_account.vesting_withdraw_rate),
current_hp_apr=self._calculate_current_hp_apr(data.gdpo),
gdpo=data.gdpo,
)
def _assert_gdpo(self, data: DynamicGlobalProperties | None) -> DynamicGlobalProperties:
@@ -122,8 +124,8 @@ class HivePowerDataRetrieval(CommandDataRetrieval[HarvestedDataRaw, SanitizedDat
assert data is not None, "FindVestingDelegations data is missing"
return data.delegations
def _create_balance_representation(self, dgdpo: DynamicGlobalProperties, vests_value: Asset.Vests) -> SharesBalance:
return SharesBalance(hp_balance=vests_to_hive(vests_value, dgdpo), vests_balance=vests_value)
def _create_balance_representation(self, gdpo: DynamicGlobalProperties, vests_value: Asset.Vests) -> SharesBalance:
return SharesBalance(hp_balance=vests_to_hive(vests_value, gdpo), vests_balance=vests_value)
def _calculate_current_hp_apr(self, gdpo: DynamicGlobalProperties) -> str:
# The inflation was set to 9.5% at block 7m
Loading