|
| 1 | +""" |
| 2 | +This module defines the `DynamicInfo` data class and associated methods for handling and decoding |
| 3 | +dynamic information in the Bittensor network. |
| 4 | +""" |
| 5 | + |
| 6 | +from dataclasses import dataclass |
| 7 | +from typing import Optional, Union |
| 8 | + |
| 9 | +from scalecodec.utils.ss58 import ss58_encode |
| 10 | + |
| 11 | +from bittensor.core.chain_data.utils import ( |
| 12 | + ChainDataType, |
| 13 | + from_scale_encoding, |
| 14 | + SS58_FORMAT, |
| 15 | +) |
| 16 | +from bittensor.core.chain_data.subnet_identity import SubnetIdentity |
| 17 | +from bittensor.utils.balance import Balance |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class DynamicInfo: |
| 22 | + netuid: int |
| 23 | + owner_hotkey: str |
| 24 | + owner_coldkey: str |
| 25 | + subnet_name: str |
| 26 | + symbol: str |
| 27 | + tempo: int |
| 28 | + last_step: int |
| 29 | + blocks_since_last_step: int |
| 30 | + emission: Balance |
| 31 | + alpha_in: Balance |
| 32 | + alpha_out: Balance |
| 33 | + tao_in: Balance |
| 34 | + price: Balance |
| 35 | + k: float |
| 36 | + is_dynamic: bool |
| 37 | + alpha_out_emission: Balance |
| 38 | + alpha_in_emission: Balance |
| 39 | + tao_in_emission: Balance |
| 40 | + pending_alpha_emission: Balance |
| 41 | + pending_root_emission: Balance |
| 42 | + network_registered_at: int |
| 43 | + subnet_identity: Optional[SubnetIdentity] |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def from_vec_u8(cls, vec_u8: list[int]) -> Optional["DynamicInfo"]: |
| 47 | + if len(vec_u8) == 0: |
| 48 | + return None |
| 49 | + decoded = from_scale_encoding(vec_u8, ChainDataType.DynamicInfo) |
| 50 | + if decoded is None: |
| 51 | + return None |
| 52 | + return DynamicInfo.fix_decoded_values(decoded) |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def list_from_vec_u8(cls, vec_u8: Union[list[int], bytes]) -> list["DynamicInfo"]: |
| 56 | + decoded = from_scale_encoding( |
| 57 | + vec_u8, ChainDataType.DynamicInfo, is_vec=True, is_option=True |
| 58 | + ) |
| 59 | + if decoded is None: |
| 60 | + return [] |
| 61 | + decoded = [DynamicInfo.fix_decoded_values(d) for d in decoded] |
| 62 | + return decoded |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def fix_decoded_values(cls, decoded: dict) -> "DynamicInfo": |
| 66 | + """Returns a DynamicInfo object from a decoded DynamicInfo dictionary.""" |
| 67 | + |
| 68 | + netuid = int(decoded["netuid"]) |
| 69 | + symbol = bytes([int(b) for b in decoded["token_symbol"]]).decode() |
| 70 | + subnet_name = bytes([int(b) for b in decoded["subnet_name"]]).decode() |
| 71 | + |
| 72 | + is_dynamic = ( |
| 73 | + True if int(decoded["netuid"]) > 0 else False |
| 74 | + ) # Root is not dynamic |
| 75 | + |
| 76 | + owner_hotkey = ss58_encode(decoded["owner_hotkey"], SS58_FORMAT) |
| 77 | + owner_coldkey = ss58_encode(decoded["owner_coldkey"], SS58_FORMAT) |
| 78 | + |
| 79 | + emission = Balance.from_rao(decoded["emission"]).set_unit(0) |
| 80 | + alpha_in = Balance.from_rao(decoded["alpha_in"]).set_unit(netuid) |
| 81 | + alpha_out = Balance.from_rao(decoded["alpha_out"]).set_unit(netuid) |
| 82 | + tao_in = Balance.from_rao(decoded["tao_in"]).set_unit(0) |
| 83 | + alpha_out_emission = Balance.from_rao(decoded["alpha_out_emission"]).set_unit( |
| 84 | + netuid |
| 85 | + ) |
| 86 | + alpha_in_emission = Balance.from_rao(decoded["alpha_in_emission"]).set_unit( |
| 87 | + netuid |
| 88 | + ) |
| 89 | + tao_in_emission = Balance.from_rao(decoded["tao_in_emission"]).set_unit(0) |
| 90 | + pending_alpha_emission = Balance.from_rao( |
| 91 | + decoded["pending_alpha_emission"] |
| 92 | + ).set_unit(netuid) |
| 93 | + pending_root_emission = Balance.from_rao( |
| 94 | + decoded["pending_root_emission"] |
| 95 | + ).set_unit(0) |
| 96 | + |
| 97 | + price = ( |
| 98 | + Balance.from_tao(1.0) |
| 99 | + if netuid == 0 |
| 100 | + else Balance.from_tao(tao_in.tao / alpha_in.tao) |
| 101 | + if alpha_in.tao > 0 |
| 102 | + else Balance.from_tao(1) |
| 103 | + ) # Root always has 1-1 price |
| 104 | + |
| 105 | + if decoded.get("subnet_identity"): |
| 106 | + subnet_identity = SubnetIdentity( |
| 107 | + subnet_name=decoded["subnet_identity"]["subnet_name"], |
| 108 | + github_repo=decoded["subnet_identity"]["github_repo"], |
| 109 | + subnet_contact=decoded["subnet_identity"]["subnet_contact"], |
| 110 | + ) |
| 111 | + else: |
| 112 | + subnet_identity = None |
| 113 | + |
| 114 | + return cls( |
| 115 | + netuid=netuid, |
| 116 | + owner_hotkey=owner_hotkey, |
| 117 | + owner_coldkey=owner_coldkey, |
| 118 | + subnet_name=subnet_name, |
| 119 | + symbol=symbol, |
| 120 | + tempo=int(decoded["tempo"]), |
| 121 | + last_step=int(decoded["last_step"]), |
| 122 | + blocks_since_last_step=int(decoded["blocks_since_last_step"]), |
| 123 | + emission=emission, |
| 124 | + alpha_in=alpha_in, |
| 125 | + alpha_out=alpha_out, |
| 126 | + tao_in=tao_in, |
| 127 | + k=tao_in.rao * alpha_in.rao, |
| 128 | + is_dynamic=is_dynamic, |
| 129 | + price=price, |
| 130 | + alpha_out_emission=alpha_out_emission, |
| 131 | + alpha_in_emission=alpha_in_emission, |
| 132 | + tao_in_emission=tao_in_emission, |
| 133 | + pending_alpha_emission=pending_alpha_emission, |
| 134 | + pending_root_emission=pending_root_emission, |
| 135 | + network_registered_at=int(decoded["network_registered_at"]), |
| 136 | + subnet_identity=subnet_identity, |
| 137 | + ) |
0 commit comments