1
1
from dataclasses import dataclass
2
- from typing import Any , Optional
2
+ from typing import Any , Optional , Union
3
3
4
4
from bittensor .core .chain_data .info_base import InfoBase
5
5
from bittensor .core .chain_data .utils import decode_account_id
@@ -15,20 +15,21 @@ class DelegateInfo(InfoBase):
15
15
Args:
16
16
hotkey_ss58 (str): Hotkey of the delegate for which the information is being fetched.
17
17
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.
19
19
take (float): Take of the delegate as a percentage.
20
20
owner_ss58 (str): Coldkey of the owner.
21
21
registrations (list[int]): List of subnets that the delegate is registered on.
22
22
validator_permits (list[int]): List of subnets that the delegate is allowed to validate on.
23
23
return_per_1000 (int): Return per 1000 TAO, for the delegate over a day.
24
24
total_daily_return (int): Total daily return of the delegate.
25
+ netuid (int): Netuid of the subnet.
25
26
"""
26
27
27
28
hotkey_ss58 : str # Hotkey of delegate
28
29
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
32
33
owner_ss58 : str # Coldkey of owner
33
34
take : float # Take of the delegate as a percentage
34
35
validator_permits : list [
@@ -37,36 +38,48 @@ class DelegateInfo(InfoBase):
37
38
registrations : list [int ] # list of subnets that the delegate is registered on
38
39
return_per_1000 : Balance # Return per 1000 tao of the delegate over a day
39
40
total_daily_return : Balance # Total daily return of the delegate
41
+ netuid : Optional [int ] = None
40
42
41
43
@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" ))
49
47
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 ,
52
66
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" )),
60
73
)
61
74
62
75
@classmethod
63
76
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