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
8 files
+ 204
160
Compare changes
  • Side-by-side
  • Inline
Files
8
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Literal
from abc import abstractmethod
from typing import TYPE_CHECKING, Any
from textual.containers import Horizontal
from textual.widgets import Label, Static
from textual.widgets import Static
from clive.__private.core.formatters.humanize import humanize_asset
from clive.__private.ui.data_providers.hive_power_data_provider import HivePowerDataProvider
from clive.__private.ui.get_css import get_css_from_relative_path
from clive.__private.ui.widgets.clive_data_table import CliveDataTable, CliveDataTableRow
from clive.__private.ui.widgets.clive_data_table import CliveDataTableRow
if TYPE_CHECKING:
from typing import Final
from textual.app import ComposeResult
from clive.models import Asset
BALANCE_CELL_CLASS: Final[str] = "shares-balance-cell"
HP_BALANCE_INDEX: Final[int] = 0
VESTS_BALANCE_INDEX: Final[int] = 1
class HpInformationTableRow(CliveDataTableRow):
class HpInfoTableRow(CliveDataTableRow):
"""Class created to not override the `provider` property five times, but only once."""
@property
def provider(self) -> Any:
return self.app.query_one(HivePowerDataProvider)
def sync_rows(self, content: Any) -> None:
if content is not None:
self._update_balance_row(self.cells, self.get_new_values(content))
def _update_balance_row(self, cells: tuple[Static, ...], pretty_balances: tuple[str, str]) -> None:
hp_cell = cells[HP_BALANCE_INDEX]
vests_cell = cells[VESTS_BALANCE_INDEX]
# In this table we are SURE that `HP` is first and `VESTS` second.
hp_cell.update(pretty_balances[HP_BALANCE_INDEX])
vests_cell.update(pretty_balances[VESTS_BALANCE_INDEX])
@abstractmethod
def get_new_values(self, content: Any) -> tuple[str, str]:
"""Should return the appropriate balance of HP and VESTS with sign prefix."""
class HpInformationTableHeader(Horizontal):
class HpInfoTableHeader(Horizontal):
def compose(self) -> ComposeResult:
yield Static("Voting Power", id="shares-name-header")
yield Static("Amount in HP", classes="shares-balance-header")
yield Static("Amount in VESTS", classes="shares-balance-header")
class HpInformationTableFirstRow(HpInformationTableRow):
class HpInfoTableOwnedRow(HpInfoTableRow):
def __init__(self) -> None:
super().__init__(
"Owned",
Static("loading...", classes=BALANCE_CELL_CLASS),
Static("loading...", classes=BALANCE_CELL_CLASS),
row_title="Owned",
dynamic=True,
classes="odd-row",
)
def sync_rows(self, content: Any) -> None:
if content is not None:
_update_balance_row(self.cells, (content.owned_balance.hp_balance, content.owned_balance.vests_balance))
def get_new_values(self, content: Any) -> tuple[str, str]:
hp_balance = humanize_asset(content.owned_balance.hp_balance, show_symbol=False)
vests_balance = humanize_asset(content.owned_balance.vests_balance, show_symbol=False)
return hp_balance, vests_balance
class HpInformationTableSecondRow(HpInformationTableRow):
class HpInfoTableReceivedRow(HpInfoTableRow):
def __init__(self) -> None:
super().__init__(
"Received",
Static("loading...", classes=BALANCE_CELL_CLASS),
Static("loading...", classes=BALANCE_CELL_CLASS),
row_title="Received",
dynamic=True,
classes="even-row",
)
def sync_rows(self, content: Any) -> None:
if content is not None:
_update_balance_row(
self.cells, (content.received_balance.hp_balance, content.received_balance.vests_balance), "+"
)
def get_new_values(self, content: Any) -> tuple[str, str]:
hp_balance = humanize_asset(content.received_balance.hp_balance, show_symbol=False, sign_prefix="+")
vests_balance = humanize_asset(content.received_balance.vests_balance, show_symbol=False, sign_prefix="+")
return hp_balance, vests_balance
class HpInformationTableThirdRow(HpInformationTableRow):
class HpInfoTableDelegatedRow(HpInfoTableRow):
def __init__(self) -> None:
super().__init__(
"Delegated",
Static("loading...", classes=BALANCE_CELL_CLASS),
Static("loading...", classes=BALANCE_CELL_CLASS),
row_title="Delegated",
dynamic=True,
classes="odd-row",
)
def sync_rows(self, content: Any) -> None:
if content is not None:
_update_balance_row(
self.cells, (content.delegated_balance.hp_balance, content.delegated_balance.vests_balance), "-"
)
def get_new_values(self, content: Any) -> tuple[str, str]:
hp_balance = humanize_asset(content.delegated_balance.hp_balance, show_symbol=False, sign_prefix="-")
vests_balance = humanize_asset(content.delegated_balance.vests_balance, show_symbol=False, sign_prefix="-")
return hp_balance, vests_balance
class HpInformationFourthRow(HpInformationTableRow):
class HpInfoTablePowerDownRow(HpInfoTableRow):
def __init__(self) -> None:
super().__init__(
"Power Down",
Static("loading...", classes=BALANCE_CELL_CLASS),
Static("loading...", classes=BALANCE_CELL_CLASS),
row_title="Power Down",
dynamic=True,
classes="even-row",
)
def sync_rows(self, content: Any) -> None:
if content is not None:
_update_balance_row(
self.cells, (content.next_power_down.hp_balance, content.next_power_down.vests_balance), "-"
)
def get_new_values(self, content: Any) -> tuple[str, str]:
hp_balance = humanize_asset(content.next_power_down.hp_balance, show_symbol=False, sign_prefix="-")
vests_balance = humanize_asset(content.next_power_down.vests_balance, show_symbol=False, sign_prefix="-")
return hp_balance, vests_balance
class HpInformationFifthRow(HpInformationTableRow):
class HpInfoTableEffectiveRow(HpInfoTableRow):
def __init__(self) -> None:
super().__init__(
"Effective",
Static("loading...", classes=BALANCE_CELL_CLASS),
Static("loading...", classes=BALANCE_CELL_CLASS),
row_title="Effective",
dynamic=True,
classes="odd-row",
)
def sync_rows(self, content: Any) -> None:
if content is not None:
_update_balance_row(self.cells, (content.total_balance.hp_balance, content.total_balance.vests_balance))
def _update_balance_row(
cells: tuple[Static | Label, ...], balances: tuple[Asset.Hive, Asset.Vests], sign_prefix: Literal["+", "-", ""] = ""
) -> None:
hp_cell = cells[HP_BALANCE_INDEX]
vests_cell = cells[VESTS_BALANCE_INDEX]
# In this table we are SURE that `HP` is first and `VESTS` second.
hp_cell.update(humanize_asset(balances[HP_BALANCE_INDEX], show_symbol=False, sign_prefix=sign_prefix))
vests_cell.update(humanize_asset(balances[VESTS_BALANCE_INDEX], show_symbol=False, sign_prefix=sign_prefix))
def get_new_values(self, content: Any) -> tuple[str, str]:
hp_balance = humanize_asset(content.total_balance.hp_balance, show_symbol=False)
vests_balance = humanize_asset(content.total_balance.vests_balance, show_symbol=False)
class HpInformationTable(CliveDataTable):
DEFAULT_CSS = get_css_from_relative_path(__file__)
def __init__(self) -> None:
self._table_rows = [
HpInformationTableFirstRow(),
HpInformationTableSecondRow(),
HpInformationTableThirdRow(),
HpInformationFourthRow(),
HpInformationFifthRow(),
]
super().__init__(table_rows=self._table_rows)
def create_table_headlines(self) -> ComposeResult:
yield HpInformationTableHeader()
return hp_balance, vests_balance
Loading