1
- from dataclasses import dataclass
2
- from typing import Optional , Any
1
+ import bt_decode
3
2
4
- from scalecodec .utils .ss58 import ss58_encode
3
+ from dataclasses import dataclass
4
+ from typing import Optional
5
5
6
- from bittensor .core .chain_data .utils import from_scale_encoding , ChainDataType
7
- from bittensor .core .settings import SS58_FORMAT
6
+ from bittensor .core .chain_data .utils import decode_account_id
8
7
from bittensor .utils import u16_normalized_float
9
8
from bittensor .utils .balance import Balance
10
9
@@ -24,7 +23,6 @@ class DelegateInfo:
24
23
validator_permits (list[int]): List of subnets that the delegate is allowed to validate on.
25
24
return_per_1000 (int): Return per 1000 TAO, for the delegate over a day.
26
25
total_daily_return (int): Total daily return of the delegate.
27
-
28
26
"""
29
27
30
28
hotkey_ss58 : str # Hotkey of delegate
@@ -37,69 +35,78 @@ class DelegateInfo:
37
35
validator_permits : list [
38
36
int
39
37
] # List of subnets that the delegate is allowed to validate on
40
- registrations : tuple [int ] # List of subnets that the delegate is registered on
38
+ registrations : list [int ] # list of subnets that the delegate is registered on
41
39
return_per_1000 : Balance # Return per 1000 tao of the delegate over a day
42
40
total_daily_return : Balance # Total daily return of the delegate
43
41
44
42
@classmethod
45
- def fix_decoded_values (cls , decoded : Any ) -> "DelegateInfo" :
46
- """Fixes the decoded values."""
47
-
48
- return cls (
49
- hotkey_ss58 = ss58_encode (decoded ["delegate_ss58" ], SS58_FORMAT ),
50
- owner_ss58 = ss58_encode (decoded ["owner_ss58" ], SS58_FORMAT ),
51
- take = u16_normalized_float (decoded ["take" ]),
52
- nominators = [
53
- (
54
- ss58_encode (nom [0 ], SS58_FORMAT ),
55
- Balance .from_rao (nom [1 ]),
56
- )
57
- for nom in decoded ["nominators" ]
58
- ],
59
- total_stake = Balance .from_rao (
60
- sum ([nom [1 ] for nom in decoded ["nominators" ]])
61
- ),
62
- validator_permits = decoded ["validator_permits" ],
63
- registrations = decoded ["registrations" ],
64
- return_per_1000 = Balance .from_rao (decoded ["return_per_1000" ]),
65
- total_daily_return = Balance .from_rao (decoded ["total_daily_return" ]),
43
+ def from_vec_u8 (cls , vec_u8 : bytes ) -> Optional ["DelegateInfo" ]:
44
+ decoded = bt_decode .DelegateInfo .decode (vec_u8 )
45
+ hotkey = decode_account_id (decoded .delegate_ss58 )
46
+ owner = decode_account_id (decoded .owner_ss58 )
47
+ nominators = [
48
+ (decode_account_id (x ), Balance .from_rao (y )) for x , y in decoded .nominators
49
+ ]
50
+ total_stake = sum ((x [1 ] for x in nominators )) if nominators else Balance (0 )
51
+ return DelegateInfo (
52
+ hotkey_ss58 = hotkey ,
53
+ total_stake = total_stake ,
54
+ nominators = nominators ,
55
+ owner_ss58 = owner ,
56
+ take = u16_normalized_float (decoded .take ),
57
+ validator_permits = decoded .validator_permits ,
58
+ registrations = decoded .registrations ,
59
+ return_per_1000 = Balance .from_rao (decoded .return_per_1000 ),
60
+ total_daily_return = Balance .from_rao (decoded .total_daily_return ),
66
61
)
67
62
68
63
@classmethod
69
- def from_vec_u8 (cls , vec_u8 : list [int ]) -> Optional ["DelegateInfo" ]:
70
- """Returns a DelegateInfo object from a ``vec_u8``."""
71
- if len (vec_u8 ) == 0 :
72
- return None
73
-
74
- decoded = from_scale_encoding (vec_u8 , ChainDataType .DelegateInfo )
75
- if decoded is None :
76
- return None
77
-
78
- return DelegateInfo .fix_decoded_values (decoded )
79
-
80
- @classmethod
81
- def list_from_vec_u8 (cls , vec_u8 : list [int ]) -> list ["DelegateInfo" ]:
82
- """Returns a list of DelegateInfo objects from a ``vec_u8``."""
83
- decoded = from_scale_encoding (vec_u8 , ChainDataType .DelegateInfo , is_vec = True )
84
-
85
- if decoded is None :
86
- return []
87
-
88
- return [DelegateInfo .fix_decoded_values (d ) for d in decoded ]
64
+ def list_from_vec_u8 (cls , vec_u8 : bytes ) -> list ["DelegateInfo" ]:
65
+ decoded = bt_decode .DelegateInfo .decode_vec (vec_u8 )
66
+ results = []
67
+ for d in decoded :
68
+ hotkey = decode_account_id (d .delegate_ss58 )
69
+ owner = decode_account_id (d .owner_ss58 )
70
+ nominators = [
71
+ (decode_account_id (x ), Balance .from_rao (y )) for x , y in d .nominators
72
+ ]
73
+ total_stake = sum ((x [1 ] for x in nominators )) if nominators else Balance (0 )
74
+ results .append (
75
+ DelegateInfo (
76
+ hotkey_ss58 = hotkey ,
77
+ total_stake = total_stake ,
78
+ nominators = nominators ,
79
+ owner_ss58 = owner ,
80
+ take = u16_normalized_float (d .take ),
81
+ validator_permits = d .validator_permits ,
82
+ registrations = d .registrations ,
83
+ return_per_1000 = Balance .from_rao (d .return_per_1000 ),
84
+ total_daily_return = Balance .from_rao (d .total_daily_return ),
85
+ )
86
+ )
87
+ return results
89
88
90
89
@classmethod
91
90
def delegated_list_from_vec_u8 (
92
- cls , vec_u8 : list [int ]
93
- ) -> list [tuple ["DelegateInfo" , "Balance" ]]:
94
- """Returns a list of Tuples of DelegateInfo objects, and Balance, from a ``vec_u8``.
95
-
96
- This is the list of delegates that the user has delegated to, and the amount of stake delegated.
97
- """
98
- decoded = from_scale_encoding (vec_u8 , ChainDataType .DelegatedInfo , is_vec = True )
99
- if decoded is None :
100
- return []
101
-
102
- return [
103
- (DelegateInfo .fix_decoded_values (d ), Balance .from_rao (s ))
104
- for d , s in decoded
105
- ]
91
+ cls , vec_u8 : bytes
92
+ ) -> list [tuple ["DelegateInfo" , Balance ]]:
93
+ decoded = bt_decode .DelegateInfo .decode_delegated (vec_u8 )
94
+ results = []
95
+ for d , b in decoded :
96
+ nominators = [
97
+ (decode_account_id (x ), Balance .from_rao (y )) for x , y in d .nominators
98
+ ]
99
+ total_stake = sum ((x [1 ] for x in nominators )) if nominators else Balance (0 )
100
+ delegate = DelegateInfo (
101
+ hotkey_ss58 = decode_account_id (d .delegate_ss58 ),
102
+ total_stake = total_stake ,
103
+ nominators = nominators ,
104
+ owner_ss58 = decode_account_id (d .owner_ss58 ),
105
+ take = u16_normalized_float (d .take ),
106
+ validator_permits = d .validator_permits ,
107
+ registrations = d .registrations ,
108
+ return_per_1000 = Balance .from_rao (d .return_per_1000 ),
109
+ total_daily_return = Balance .from_rao (d .total_daily_return ),
110
+ )
111
+ results .append ((delegate , Balance .from_rao (b )))
112
+ return results
0 commit comments