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
6 files
+ 66
59
Compare changes
  • Side-by-side
  • Inline
Files
6
from __future__ import annotations
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any
from textual.containers import Horizontal
from textual.widgets import Static
@@ -15,6 +15,12 @@ if TYPE_CHECKING:
from textual.app import ComposeResult
class HpInformationTableRow(CliveDataTableRow):
@property
def provider(self) -> Any:
return self.app.query_one(HivePowerDataProvider)
class HpInformationTableHeader(Horizontal):
def compose(self) -> ComposeResult:
yield Static(classes="empty-header-column")
@@ -24,22 +30,14 @@ class HpInformationTableHeader(Horizontal):
yield Static("Additional info", classes="additional-info-header")
class HpInformationTableBaseRow(CliveDataTableRow):
"""Row of the clive data table with provider property."""
@property
def provider(self) -> HivePowerDataProvider:
return self.app.query_one(HivePowerDataProvider)
class HpInformationTableFirstRow(HpInformationTableBaseRow):
def create_row(self) -> CliveTableRowWrapper | None:
if self.provider.is_content_set:
class HpInformationTableFirstRow(HpInformationTableRow):
def create_row(self, content: Any) -> CliveTableRowWrapper | None:
if content is not None:
return CliveTableRowWrapper(
[
"Owned",
f"{Asset.pretty_amount(self.provider.content.owned_balance.hp_balance)}",
f"{Asset.pretty_amount(self.provider.content.owned_balance.vests_balance)}",
create_balance_representation(content.owned_balance.hp_balance),
create_balance_representation(content.owned_balance.vests_balance),
"",
"Next withdrawal",
],
@@ -54,16 +52,16 @@ class HpInformationTableFirstRow(HpInformationTableBaseRow):
return None
class HpInformationTableSecondRow(HpInformationTableBaseRow):
def create_row(self) -> CliveTableRowWrapper | None:
if self.provider.is_content_set:
class HpInformationTableSecondRow(HpInformationTableRow):
def create_row(self, content: Any) -> CliveTableRowWrapper | None:
if content is not None:
return CliveTableRowWrapper(
[
"Received",
f"+{Asset.pretty_amount(self.provider.content.received_balance.hp_balance)}",
f"+{Asset.pretty_amount(self.provider.content.received_balance.vests_balance)}",
create_balance_representation(content.received_balance.hp_balance, "+"),
create_balance_representation(content.received_balance.vests_balance, "+"),
"",
f"{humanize_datetime(self.provider.content.next_vesting_withdrawal)}",
f"{humanize_datetime(content.next_vesting_withdrawal)}",
],
cell_classes=[
"balance-row-title",
@@ -76,14 +74,14 @@ class HpInformationTableSecondRow(HpInformationTableBaseRow):
return None
class HpInformationTableThirdRow(HpInformationTableBaseRow):
def create_row(self) -> CliveTableRowWrapper | None:
if self.provider.is_content_set:
class HpInformationTableThirdRow(HpInformationTableRow):
def create_row(self, content: Any) -> CliveTableRowWrapper | None:
if content is not None:
return CliveTableRowWrapper(
[
"Delegated",
f"-{Asset.pretty_amount(self.provider.content.delegated_balance.hp_balance)}",
f"-{Asset.pretty_amount(self.provider.content.delegated_balance.vests_balance)}",
create_balance_representation(content.delegated_balance.hp_balance, "-"),
create_balance_representation(content.delegated_balance.vests_balance, "-"),
"",
"To withdraw",
],
@@ -98,16 +96,16 @@ class HpInformationTableThirdRow(HpInformationTableBaseRow):
return None
class HpInformationFourthRow(HpInformationTableBaseRow):
def create_row(self) -> CliveTableRowWrapper | None:
if self.provider.is_content_set:
class HpInformationFourthRow(HpInformationTableRow):
def create_row(self, content: Any) -> CliveTableRowWrapper | None:
if content is not None:
return CliveTableRowWrapper(
[
"Power down",
f"-{Asset.pretty_amount(self.provider.content.next_power_down.hp_balance)}",
f"-{Asset.pretty_amount(self.provider.content.next_power_down.vests_balance)}",
create_balance_representation(content.next_power_down.hp_balance, "-"),
create_balance_representation(content.next_power_down.vests_balance, "-"),
"",
f"{Asset.pretty_amount(self.provider.content.to_withdraw.hp_balance)} HP",
f"{Asset.pretty_amount(content.to_withdraw.hp_balance)} HP",
],
cell_classes=[
"balance-row-title",
@@ -120,16 +118,16 @@ class HpInformationFourthRow(HpInformationTableBaseRow):
return None
class HpInformationFifthRow(HpInformationTableBaseRow):
def create_row(self) -> CliveTableRowWrapper | None:
if self.provider.is_content_set:
class HpInformationFifthRow(HpInformationTableRow):
def create_row(self, content: Any) -> CliveTableRowWrapper | None:
if content is not None:
return CliveTableRowWrapper(
[
"Total",
f"{Asset.pretty_amount(self.provider.content.total_balance.hp_balance)}",
f"{Asset.pretty_amount(self.provider.content.total_balance.vests_balance)}",
create_balance_representation(content.total_balance.hp_balance),
create_balance_representation(content.total_balance.vests_balance),
"",
f"{Asset.pretty_amount(self.provider.content.to_withdraw.vests_balance)} VESTS",
f"{create_balance_representation(content.to_withdraw.vests_balance)} VESTS",
],
cell_classes=[
"balance-row-title",
@@ -142,11 +140,11 @@ class HpInformationFifthRow(HpInformationTableBaseRow):
return None
class HpInformationSixthRow(HpInformationTableBaseRow):
def create_row(self) -> CliveTableRowWrapper | None:
if self.provider.is_content_set:
class HpInformationSixthRow(HpInformationTableRow):
def create_row(self, content: Any) -> CliveTableRowWrapper | None:
if content is not None:
return CliveTableRowWrapper(
["", f"APR interest for HP/VESTS ≈ {self.provider.content.current_hp_apr} %"],
["", f"APR interest for HP/VESTS ≈ {content.current_hp_apr} %"],
cell_classes=["empty-column-fifth-row", "row-with-apr"],
)
return None
@@ -168,3 +166,12 @@ class HpInformationTable(CliveDataTable):
def create_table_headlines(self) -> ComposeResult:
yield HpInformationTableHeader()
def create_balance_representation(asset_balance: Asset.Hive | Asset.Vests, sign_prefix: str = "") -> str:
"""Create prettier asset balance representation."""
pretty_asset = Asset.pretty_amount(asset_balance)
if sign_prefix and int(asset_balance.amount) != 0:
# To not allow display + or - if balance is equal to zero.
return f"{sign_prefix}{pretty_asset}"
return pretty_asset
Loading