1
1
from dataclasses import dataclass
2
- from typing import Any , Optional , Union
2
+ from typing import 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
8
8
9
9
10
10
@dataclass
11
- class DelegateInfo (InfoBase ):
12
- """
13
- Dataclass for delegate information. For a lighter version of this class, see ``DelegateInfoLite``.
11
+ class DelegateInfoBase (InfoBase ):
12
+ """Base class containing common delegate information fields.
14
13
15
- Args:
16
- hotkey_ss58 (str): Hotkey of the delegate for which the information is being fetched.
17
- total_stake (int): Total stake of the delegate.
18
- nominators (dict[str, dict[int, Balance]]): List of nominators of the delegate and their stake.
14
+ Attributes:
15
+ hotkey_ss58 (str): Hotkey of delegate.
16
+ owner_ss58 (str): Coldkey of owner.
19
17
take (float): Take of the delegate as a percentage.
20
- owner_ss58 (str): Coldkey of the owner.
21
- registrations (list[int]): List of subnets that the delegate is registered on.
22
18
validator_permits (list[int]): List of subnets that the delegate is allowed to validate on.
23
- return_per_1000 ( int): Return per 1000 TAO, for the delegate over a day .
24
- total_daily_return (int ): Total daily return of the delegate.
25
- netuid (int ): Netuid of the subnet .
19
+ registrations (list[ int] ): List of subnets that the delegate is registered on .
20
+ return_per_1000 (Balance ): Return per 1000 tao of the delegate over a day .
21
+ total_daily_return (Balance ): Total daily return of the delegate .
26
22
"""
27
23
28
24
hotkey_ss58 : str # Hotkey of delegate
29
- total_stake : Balance # Total stake of the delegate
30
- nominators : dict [
31
- str , dict [int , Balance ]
32
- ] # list of nominators of the delegate and their stake
33
25
owner_ss58 : str # Coldkey of owner
34
26
take : float # Take of the delegate as a percentage
35
27
validator_permits : list [
@@ -38,20 +30,36 @@ class DelegateInfo(InfoBase):
38
30
registrations : list [int ] # list of subnets that the delegate is registered on
39
31
return_per_1000 : Balance # Return per 1000 tao of the delegate over a day
40
32
total_daily_return : Balance # Total daily return of the delegate
41
- netuid : Optional [int ] = None
33
+
34
+
35
+ @dataclass
36
+ class DelegateInfo (DelegateInfoBase ):
37
+ """
38
+ Dataclass for delegate information.
39
+
40
+ Additional Attributes:
41
+ total_stake (dict[int, Balance]): Total stake of the delegate mapped by netuid.
42
+ nominators (dict[str, dict[int, Balance]]): Mapping of nominator SS58 addresses to their stakes per subnet.
43
+ """
44
+
45
+ total_stake : dict [int , Balance ] # Total stake of the delegate by netuid and stake
46
+ nominators : dict [
47
+ str , dict [int , Balance ]
48
+ ] # Mapping of nominator addresses to their stakes per subnet
42
49
43
50
@classmethod
44
- def _from_dict (cls , decoded : Union [dict , tuple ]) -> "DelegateInfo" :
51
+ def _from_dict (cls , decoded : Union [dict , tuple ]) -> Optional [ "DelegateInfo" ] :
45
52
hotkey = decode_account_id (decoded .get ("delegate_ss58" ))
46
53
owner = decode_account_id (decoded .get ("owner_ss58" ))
47
54
48
55
nominators = {}
49
56
total_stake_by_netuid = {}
50
- for nominator in decoded .get ("nominators" , []):
51
- nominator_ss58 = decode_account_id (nominator [0 ])
57
+
58
+ for raw_nominator , raw_stakes in decoded .get ("nominators" , []):
59
+ nominator_ss58 = decode_account_id (raw_nominator )
52
60
stakes = {
53
- int (netuid ): Balance .from_rao (stake ).set_unit (netuid )
54
- for netuid , stake in nominator [ 1 ]
61
+ int (netuid ): Balance .from_rao (stake_amt ).set_unit (int ( netuid ) )
62
+ for ( netuid , stake_amt ) in raw_stakes
55
63
}
56
64
nominators [nominator_ss58 ] = stakes
57
65
@@ -72,14 +80,38 @@ def _from_dict(cls, decoded: Union[dict, tuple]) -> "DelegateInfo":
72
80
total_daily_return = Balance .from_rao (decoded .get ("total_daily_return" )),
73
81
)
74
82
83
+
84
+ @dataclass
85
+ class DelegatedInfo (DelegateInfoBase ):
86
+ """
87
+ Dataclass for delegated information. This class represents a delegate's information
88
+ specific to a particular subnet.
89
+
90
+ Additional Attributes:
91
+ netuid (int): Network ID of the subnet.
92
+ stake (Balance): Stake amount for this specific delegation.
93
+ """
94
+
95
+ netuid : int
96
+ stake : Balance
97
+
75
98
@classmethod
76
- def delegated_list_from_dicts (
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
99
+ def _from_dict (
100
+ cls , decoded : tuple [dict , tuple [int , int ]]
101
+ ) -> Optional ["DelegatedInfo" ]:
102
+ delegate_info , (netuid , stake ) = decoded
103
+ hotkey = decode_account_id (delegate_info .get ("delegate_ss58" ))
104
+ owner = decode_account_id (delegate_info .get ("owner_ss58" ))
105
+ return cls (
106
+ hotkey_ss58 = hotkey ,
107
+ owner_ss58 = owner ,
108
+ take = u16_normalized_float (delegate_info .get ("take" )),
109
+ validator_permits = list (delegate_info .get ("validator_permits" , [])),
110
+ registrations = list (delegate_info .get ("registrations" , [])),
111
+ return_per_1000 = Balance .from_rao (delegate_info .get ("return_per_1000" )),
112
+ total_daily_return = Balance .from_rao (
113
+ delegate_info .get ("total_daily_return" )
114
+ ),
115
+ netuid = int (netuid ),
116
+ stake = Balance .from_rao (int (stake )).set_unit (int (netuid )),
117
+ )
0 commit comments