Skip to content

Commit d6acaa4

Browse files
committed
Updates DelegateInfo chain data
1 parent 4bebb26 commit d6acaa4

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed
Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Any, Optional
2+
from typing import Any, Optional, Union
33

44
from bittensor.core.chain_data.info_base import InfoBase
55
from bittensor.core.chain_data.utils import decode_account_id
@@ -15,20 +15,21 @@ class DelegateInfo(InfoBase):
1515
Args:
1616
hotkey_ss58 (str): Hotkey of the delegate for which the information is being fetched.
1717
total_stake (int): Total stake of the delegate.
18-
nominators (list[tuple[str, int]]): List of nominators of the delegate and their stake.
18+
nominators (dict[str, dict[int, Balance]]): List of nominators of the delegate and their stake.
1919
take (float): Take of the delegate as a percentage.
2020
owner_ss58 (str): Coldkey of the owner.
2121
registrations (list[int]): List of subnets that the delegate is registered on.
2222
validator_permits (list[int]): List of subnets that the delegate is allowed to validate on.
2323
return_per_1000 (int): Return per 1000 TAO, for the delegate over a day.
2424
total_daily_return (int): Total daily return of the delegate.
25+
netuid (int): Netuid of the subnet.
2526
"""
2627

2728
hotkey_ss58: str # Hotkey of delegate
2829
total_stake: Balance # Total stake of the delegate
29-
nominators: list[
30-
tuple[str, Balance]
31-
] # List of nominators of the delegate and their stake
30+
nominators: dict[
31+
str, dict[int, Balance]
32+
] # list of nominators of the delegate and their stake
3233
owner_ss58: str # Coldkey of owner
3334
take: float # Take of the delegate as a percentage
3435
validator_permits: list[
@@ -37,36 +38,48 @@ class DelegateInfo(InfoBase):
3738
registrations: list[int] # list of subnets that the delegate is registered on
3839
return_per_1000: Balance # Return per 1000 tao of the delegate over a day
3940
total_daily_return: Balance # Total daily return of the delegate
41+
netuid: Optional[int] = None
4042

4143
@classmethod
42-
def _from_dict(cls, decoded: dict) -> Optional["DelegateInfo"]:
43-
"""Returns a DelegateInfo object from decoded chain data."""
44-
nominators = [
45-
(decode_account_id(x), Balance.from_rao(y))
46-
for x, y in decoded["nominators"]
47-
]
48-
total_stake = sum((x[1] for x in nominators)) if nominators else Balance(0)
44+
def _from_dict(cls, decoded: Union[dict, tuple]) -> "DelegateInfo":
45+
hotkey = decode_account_id(decoded.get("delegate_ss58"))
46+
owner = decode_account_id(decoded.get("owner_ss58"))
4947

50-
return DelegateInfo(
51-
hotkey_ss58=decode_account_id(decoded["delegate_ss58"]),
48+
nominators = {}
49+
total_stake_by_netuid = {}
50+
for nominator in decoded.get("nominators", []):
51+
nominator_ss58 = decode_account_id(nominator[0])
52+
stakes = {
53+
int(netuid): Balance.from_rao(stake).set_unit(netuid)
54+
for netuid, stake in nominator[1]
55+
}
56+
nominators[nominator_ss58] = stakes
57+
58+
for netuid, stake in stakes.items():
59+
if netuid not in total_stake_by_netuid:
60+
total_stake_by_netuid[netuid] = Balance(0).set_unit(netuid)
61+
total_stake_by_netuid[netuid] += stake
62+
63+
return cls(
64+
hotkey_ss58=hotkey,
65+
total_stake=total_stake_by_netuid,
5266
nominators=nominators,
53-
owner_ss58=decode_account_id(decoded["owner_ss58"]),
54-
registrations=decoded["registrations"],
55-
return_per_1000=Balance.from_rao(decoded["return_per_1000"]),
56-
take=u16_normalized_float(decoded["take"]),
57-
total_daily_return=Balance.from_rao(decoded["total_daily_return"]),
58-
total_stake=total_stake,
59-
validator_permits=decoded["validator_permits"],
67+
owner_ss58=owner,
68+
take=u16_normalized_float(decoded.get("take")),
69+
validator_permits=list(decoded.get("validator_permits", [])),
70+
registrations=list(decoded.get("registrations", [])),
71+
return_per_1000=Balance.from_rao(decoded.get("return_per_1000")),
72+
total_daily_return=Balance.from_rao(decoded.get("total_daily_return")),
6073
)
6174

6275
@classmethod
6376
def delegated_list_from_dicts(
64-
cls, delegates: list[Any]
65-
) -> list[tuple["DelegateInfo", Balance]]:
66-
return [
67-
(
68-
DelegateInfo.from_dict(delegate),
69-
Balance.from_rao(balance),
70-
)
71-
for delegate, balance in delegates
72-
]
77+
cls, delegates_decoded: list[Any]
78+
) -> list["DelegateInfo"]:
79+
all_delegates = []
80+
for delegate, (netuid, stake) in delegates_decoded:
81+
instance = DelegateInfo.from_dict(delegate)
82+
instance.netuid = int(netuid)
83+
instance.total_stake = Balance.from_rao(int(stake)).set_unit(int(netuid))
84+
all_delegates.append(instance)
85+
return all_delegates

0 commit comments

Comments
 (0)