Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions bittensor/core/chain_data/info_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dataclasses import dataclass
from dataclasses import dataclass, fields
from typing import Any, TypeVar

from bittensor.core.errors import SubstrateRequestException
Expand All @@ -13,7 +13,13 @@ class InfoBase:
@classmethod
def from_dict(cls, decoded: dict) -> T:
try:
return cls._from_dict(decoded)
class_fields = {f.name for f in fields(cls)}
extra_keys = decoded.keys() - class_fields
instance = cls._from_dict(
{k: v for k, v in decoded.items() if k in class_fields}
)
[setattr(instance, k, decoded[k]) for k in extra_keys]
return instance
except KeyError as e:
raise SubstrateRequestException(
f"The {cls} structure is missing {e} from the chain.",
Expand Down
7 changes: 6 additions & 1 deletion bittensor/core/chain_data/metagraph_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from bittensor.core.chain_data.subnet_identity import SubnetIdentity
from bittensor.core.chain_data.utils import decode_account_id
from bittensor.utils import u64_normalized_float as u64tf, u16_normalized_float as u16tf
from bittensor.utils.balance import Balance
from bittensor.utils.balance import Balance, fixed_to_float


# to balance with unit (just shortcut)
Expand Down Expand Up @@ -68,6 +68,7 @@ class MetagraphInfo(InfoBase):
pending_alpha_emission: Balance # pending alpha to be distributed
pending_root_emission: Balance # pending tao for root divs to be distributed
subnet_volume: Balance # volume of the subnet in TAO
moving_price: Balance # subnet moving price.

# Hparams for epoch
rho: int # subnet rho param
Expand Down Expand Up @@ -168,6 +169,9 @@ def _from_dict(cls, decoded: dict) -> "MetagraphInfo":
)
decoded["pending_root_emission"] = _tbwu(decoded["pending_root_emission"])
decoded["subnet_volume"] = _tbwu(decoded["subnet_volume"], _netuid)
decoded["moving_price"] = Balance.from_tao(
fixed_to_float(decoded.get("moving_price"), 32)
)

# Hparams for epoch
decoded["kappa"] = u16tf(decoded["kappa"])
Expand Down Expand Up @@ -243,6 +247,7 @@ class MetagraphInfoPool:
alpha_in: float
tao_in: float
subnet_volume: float
moving_price: float


@dataclass
Expand Down
2 changes: 1 addition & 1 deletion bittensor/core/extrinsics/serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def publish_metadata(

def get_metadata(
subtensor: "Subtensor", netuid: int, hotkey: str, block: Optional[int] = None
) -> str:
) -> bytes:
"""Fetches metadata from the blockchain for a given hotkey and netuid."""
commit_data = subtensor.substrate.query(
module="Commitments",
Expand Down
1 change: 1 addition & 0 deletions bittensor/core/metagraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ def _apply_metagraph_info_mixin(self, metagraph_info: "MetagraphInfo"):
alpha_in=metagraph_info.alpha_in.tao,
tao_in=metagraph_info.tao_in.tao,
subnet_volume=metagraph_info.subnet_volume.tao,
moving_price=metagraph_info.moving_price.tao,
)
self.emissions = MetagraphInfoEmissions(
alpha_out_emission=metagraph_info.alpha_out_emission.tao,
Expand Down
12 changes: 7 additions & 5 deletions bittensor/utils/balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,16 +285,18 @@ class FixedPoint(TypedDict):
bits: int


def fixed_to_float(fixed: Union[FixedPoint, ScaleType]) -> float:
# Currently this is stored as a U64F64
def fixed_to_float(
fixed: Union[FixedPoint, ScaleType], frac_bits: int = 64, total_bits: int = 128
) -> float:
# By default, this is a U64F64
# which is 64 bits of integer and 64 bits of fractional
frac_bits = 64

data: int = fixed["bits"]

# Shift bits to extract integer part (assuming 64 bits for integer part)
integer_part = data >> frac_bits
# Logical and to get the fractional part; remaining is the integer part
fractional_part = data & (2**frac_bits - 1)
# Shift to get the integer part from the remaining bits
integer_part = data >> (total_bits - frac_bits)

frac_float = fractional_part / (2**frac_bits)

Expand Down