Source code for bittensor.core.chain_data.delegate_info
from dataclasses import dataclass
from typing import Optional, Any
from scalecodec.utils.ss58 import ss58_encode
from bittensor.core.chain_data.utils import from_scale_encoding, ChainDataType
from bittensor.core.settings import SS58_FORMAT
from bittensor.utils import u16_normalized_float
from bittensor.utils.balance import Balance
[docs]
@dataclass
class DelegateInfo:
"""
Dataclass for delegate information. For a lighter version of this class, see :class:`bittensor.core.chain_data.delegate_info_lite`.
Args:
hotkey_ss58 (str): Hotkey of the delegate for which the information is being fetched.
total_stake (int): Total stake of the delegate.
nominators (list[tuple[str, int]]): List of nominators of the delegate and their stake.
take (float): Take of the delegate as a percentage.
owner_ss58 (str): Coldkey of the owner.
registrations (list[int]): List of subnets that the delegate is registered on.
validator_permits (list[int]): List of subnets that the delegate is allowed to validate on.
return_per_1000 (int): Return per 1000 TAO, for the delegate over a day.
total_daily_return (int): Total daily return of the delegate.
"""
hotkey_ss58: str # Hotkey of delegate
total_stake: Balance # Total stake of the delegate
nominators: list[
tuple[str, Balance]
] # List of nominators of the delegate and their stake
owner_ss58: str # Coldkey of owner
take: float # Take of the delegate as a percentage
validator_permits: list[
int
] # List of subnets that the delegate is allowed to validate on
registrations: tuple[int] # List of subnets that the delegate is registered on
return_per_1000: Balance # Return per 1000 tao of the delegate over a day
total_daily_return: Balance # Total daily return of the delegate
[docs]
@classmethod
def fix_decoded_values(cls, decoded: Any) -> "DelegateInfo":
"""Fixes the decoded values."""
return cls(
hotkey_ss58=ss58_encode(decoded["delegate_ss58"], SS58_FORMAT),
owner_ss58=ss58_encode(decoded["owner_ss58"], SS58_FORMAT),
take=u16_normalized_float(decoded["take"]),
nominators=[
(
ss58_encode(nom[0], SS58_FORMAT),
Balance.from_rao(nom[1]),
)
for nom in decoded["nominators"]
],
total_stake=Balance.from_rao(
sum([nom[1] for nom in decoded["nominators"]])
),
validator_permits=decoded["validator_permits"],
registrations=decoded["registrations"],
return_per_1000=Balance.from_rao(decoded["return_per_1000"]),
total_daily_return=Balance.from_rao(decoded["total_daily_return"]),
)
[docs]
@classmethod
def from_vec_u8(cls, vec_u8: list[int]) -> Optional["DelegateInfo"]:
"""Returns a DelegateInfo object from a ``vec_u8``."""
if len(vec_u8) == 0:
return None
decoded = from_scale_encoding(vec_u8, ChainDataType.DelegateInfo)
if decoded is None:
return None
return DelegateInfo.fix_decoded_values(decoded)
[docs]
@classmethod
def list_from_vec_u8(cls, vec_u8: list[int]) -> list["DelegateInfo"]:
"""Returns a list of DelegateInfo objects from a ``vec_u8``."""
decoded = from_scale_encoding(vec_u8, ChainDataType.DelegateInfo, is_vec=True)
if decoded is None:
return []
return [DelegateInfo.fix_decoded_values(d) for d in decoded]
[docs]
@classmethod
def delegated_list_from_vec_u8(
cls, vec_u8: list[int]
) -> list[tuple["DelegateInfo", "Balance"]]:
"""Returns a list of Tuples of DelegateInfo objects, and Balance, from a ``vec_u8``.
This is the list of delegates that the user has delegated to, and the amount of stake delegated.
"""
decoded = from_scale_encoding(vec_u8, ChainDataType.DelegatedInfo, is_vec=True)
if decoded is None:
return []
return [
(DelegateInfo.fix_decoded_values(d), Balance.from_rao(s))
for d, s in decoded
]