6
6
from bittensor .core import settings
7
7
8
8
9
+ def _check_currencies (self , other ):
10
+ """Checks that Balance objects have the same netuids to perform arithmetic operations."""
11
+ if self .netuid != other .netuid :
12
+ warnings .simplefilter ("default" , DeprecationWarning )
13
+ warnings .warn (
14
+ "Balance objects must have the same netuid (Alpha currency) to perform arithmetic operations. "
15
+ f"First balance is `{ self } `. Second balance is `{ other } `. " ,
16
+ category = DeprecationWarning ,
17
+ stacklevel = 2 ,
18
+ )
19
+
20
+
9
21
class Balance :
10
22
"""
11
23
Represents the bittensor balance of the wallet, stored as rao (int).
@@ -23,6 +35,7 @@ class Balance:
23
35
rao_unit : str = settings .RAO_SYMBOL # This is the rao unit
24
36
rao : int
25
37
tao : float
38
+ netuid : int = 0
26
39
27
40
def __init__ (self , balance : Union [int , float ]):
28
41
"""
@@ -78,7 +91,8 @@ def __eq__(self, other: Union[int, float, "Balance"]):
78
91
if other is None :
79
92
return False
80
93
81
- if hasattr (other , "rao" ):
94
+ if isinstance (other , Balance ):
95
+ _check_currencies (self , other )
82
96
return self .rao == other .rao
83
97
else :
84
98
try :
@@ -92,7 +106,8 @@ def __ne__(self, other: Union[int, float, "Balance"]):
92
106
return not self == other
93
107
94
108
def __gt__ (self , other : Union [int , float , "Balance" ]):
95
- if hasattr (other , "rao" ):
109
+ if isinstance (other , Balance ):
110
+ _check_currencies (self , other )
96
111
return self .rao > other .rao
97
112
else :
98
113
try :
@@ -103,7 +118,8 @@ def __gt__(self, other: Union[int, float, "Balance"]):
103
118
raise NotImplementedError ("Unsupported type" )
104
119
105
120
def __lt__ (self , other : Union [int , float , "Balance" ]):
106
- if hasattr (other , "rao" ):
121
+ if isinstance (other , Balance ):
122
+ _check_currencies (self , other )
107
123
return self .rao < other .rao
108
124
else :
109
125
try :
@@ -115,111 +131,129 @@ def __lt__(self, other: Union[int, float, "Balance"]):
115
131
116
132
def __le__ (self , other : Union [int , float , "Balance" ]):
117
133
try :
134
+ if isinstance (other , Balance ):
135
+ _check_currencies (self , other )
118
136
return self < other or self == other
119
137
except TypeError :
120
138
raise NotImplementedError ("Unsupported type" )
121
139
122
140
def __ge__ (self , other : Union [int , float , "Balance" ]):
123
141
try :
142
+ if isinstance (other , Balance ):
143
+ _check_currencies (self , other )
124
144
return self > other or self == other
125
145
except TypeError :
126
146
raise NotImplementedError ("Unsupported type" )
127
147
128
148
def __add__ (self , other : Union [int , float , "Balance" ]):
129
- if hasattr (other , "rao" ):
130
- return Balance .from_rao (int (self .rao + other .rao ))
149
+ if isinstance (other , Balance ):
150
+ _check_currencies (self , other )
151
+ return Balance .from_rao (int (self .rao + other .rao )).set_unit (self .netuid )
131
152
else :
132
153
try :
133
154
# Attempt to cast to int from rao
134
- return Balance .from_rao (int (self .rao + other ))
155
+ return Balance .from_rao (int (self .rao + other )). set_unit ( self . netuid )
135
156
except (ValueError , TypeError ):
136
157
raise NotImplementedError ("Unsupported type" )
137
158
138
159
def __radd__ (self , other : Union [int , float , "Balance" ]):
139
160
try :
161
+ if isinstance (other , Balance ):
162
+ _check_currencies (self , other )
140
163
return self + other
141
164
except TypeError :
142
165
raise NotImplementedError ("Unsupported type" )
143
166
144
167
def __sub__ (self , other : Union [int , float , "Balance" ]):
145
168
try :
169
+ if isinstance (other , Balance ):
170
+ _check_currencies (self , other )
146
171
return self + - other
147
172
except TypeError :
148
173
raise NotImplementedError ("Unsupported type" )
149
174
150
175
def __rsub__ (self , other : Union [int , float , "Balance" ]):
151
176
try :
177
+ if isinstance (other , Balance ):
178
+ _check_currencies (self , other )
152
179
return - self + other
153
180
except TypeError :
154
181
raise NotImplementedError ("Unsupported type" )
155
182
156
183
def __mul__ (self , other : Union [int , float , "Balance" ]):
157
- if hasattr (other , "rao" ):
158
- return Balance .from_rao (int (self .rao * other .rao ))
184
+ if isinstance (other , Balance ):
185
+ _check_currencies (self , other )
186
+ return Balance .from_rao (int (self .rao * other .rao )).set_unit (self .netuid )
159
187
else :
160
188
try :
161
189
# Attempt to cast to int from rao
162
- return Balance .from_rao (int (self .rao * other ))
190
+ return Balance .from_rao (int (self .rao * other )). set_unit ( self . netuid )
163
191
except (ValueError , TypeError ):
164
192
raise NotImplementedError ("Unsupported type" )
165
193
166
194
def __rmul__ (self , other : Union [int , float , "Balance" ]):
195
+ if isinstance (other , Balance ):
196
+ _check_currencies (self , other )
167
197
return self * other
168
198
169
199
def __truediv__ (self , other : Union [int , float , "Balance" ]):
170
- if hasattr (other , "rao" ):
171
- return Balance .from_rao (int (self .rao / other .rao ))
200
+ if isinstance (other , Balance ):
201
+ _check_currencies (self , other )
202
+ return Balance .from_rao (int (self .rao / other .rao )).set_unit (self .netuid )
172
203
else :
173
204
try :
174
205
# Attempt to cast to int from rao
175
- return Balance .from_rao (int (self .rao / other ))
206
+ return Balance .from_rao (int (self .rao / other )). set_unit ( self . netuid )
176
207
except (ValueError , TypeError ):
177
208
raise NotImplementedError ("Unsupported type" )
178
209
179
210
def __rtruediv__ (self , other : Union [int , float , "Balance" ]):
180
- if hasattr (other , "rao" ):
181
- return Balance .from_rao (int (other .rao / self .rao ))
211
+ if isinstance (other , Balance ):
212
+ _check_currencies (self , other )
213
+ return Balance .from_rao (int (other .rao / self .rao )).set_unit (self .netuid )
182
214
else :
183
215
try :
184
216
# Attempt to cast to int from rao
185
- return Balance .from_rao (int (other / self .rao ))
217
+ return Balance .from_rao (int (other / self .rao )). set_unit ( self . netuid )
186
218
except (ValueError , TypeError ):
187
219
raise NotImplementedError ("Unsupported type" )
188
220
189
221
def __floordiv__ (self , other : Union [int , float , "Balance" ]):
190
- if hasattr (other , "rao" ):
191
- return Balance .from_rao (int (self .tao // other .tao ))
222
+ if isinstance (other , Balance ):
223
+ _check_currencies (self , other )
224
+ return Balance .from_rao (int (self .tao // other .tao )).set_unit (self .netuid )
192
225
else :
193
226
try :
194
227
# Attempt to cast to int from rao
195
- return Balance .from_rao (int (self .rao // other ))
228
+ return Balance .from_rao (int (self .rao // other )). set_unit ( self . netuid )
196
229
except (ValueError , TypeError ):
197
230
raise NotImplementedError ("Unsupported type" )
198
231
199
232
def __rfloordiv__ (self , other : Union [int , float , "Balance" ]):
200
- if hasattr (other , "rao" ):
201
- return Balance .from_rao (int (other .rao // self .rao ))
233
+ if isinstance (other , Balance ):
234
+ _check_currencies (self , other )
235
+ return Balance .from_rao (int (other .rao // self .rao )).set_unit (self .netuid )
202
236
else :
203
237
try :
204
238
# Attempt to cast to int from rao
205
- return Balance .from_rao (int (other // self .rao ))
239
+ return Balance .from_rao (int (other // self .rao )). set_unit ( self . netuid )
206
240
except (ValueError , TypeError ):
207
241
raise NotImplementedError ("Unsupported type" )
208
242
209
243
def __nonzero__ (self ) -> bool :
210
244
return bool (self .rao )
211
245
212
246
def __neg__ (self ):
213
- return Balance .from_rao (- self .rao )
247
+ return Balance .from_rao (- self .rao ). set_unit ( self . netuid )
214
248
215
249
def __pos__ (self ):
216
- return Balance .from_rao (self .rao )
250
+ return Balance .from_rao (self .rao ). set_unit ( self . netuid )
217
251
218
252
def __abs__ (self ):
219
- return Balance .from_rao (abs (self .rao ))
253
+ return Balance .from_rao (abs (self .rao )). set_unit ( self . netuid )
220
254
221
255
@staticmethod
222
- def from_float (amount : float , netuid : int = 0 ):
256
+ def from_float (amount : float , netuid : int = 0 ) -> "Balance" :
223
257
"""
224
258
Given tao, return :func:`Balance` object with rao(``int``) and tao(``float``), where rao = int(tao*pow(10,9))
225
259
Args:
@@ -233,7 +267,7 @@ def from_float(amount: float, netuid: int = 0):
233
267
return Balance (rao_ ).set_unit (netuid )
234
268
235
269
@staticmethod
236
- def from_tao (amount : float , netuid : int = 0 ):
270
+ def from_tao (amount : float , netuid : int = 0 ) -> "Balance" :
237
271
"""
238
272
Given tao, return Balance object with rao(``int``) and tao(``float``), where rao = int(tao*pow(10,9))
239
273
@@ -248,7 +282,7 @@ def from_tao(amount: float, netuid: int = 0):
248
282
return Balance (rao_ ).set_unit (netuid )
249
283
250
284
@staticmethod
251
- def from_rao (amount : int , netuid : int = 0 ):
285
+ def from_rao (amount : int , netuid : int = 0 ) -> "Balance" :
252
286
"""
253
287
Given rao, return Balance object with rao(``int``) and tao(``float``), where rao = int(tao*pow(10,9))
254
288
@@ -262,7 +296,7 @@ def from_rao(amount: int, netuid: int = 0):
262
296
return Balance (amount ).set_unit (netuid )
263
297
264
298
@staticmethod
265
- def get_unit (netuid : int ):
299
+ def get_unit (netuid : int ) -> str :
266
300
base = len (units )
267
301
if netuid < base :
268
302
return units [netuid ]
@@ -274,6 +308,7 @@ def get_unit(netuid: int):
274
308
return result
275
309
276
310
def set_unit (self , netuid : int ):
311
+ self .netuid = netuid
277
312
self .unit = Balance .get_unit (netuid )
278
313
self .rao_unit = Balance .get_unit (netuid )
279
314
return self
@@ -777,18 +812,18 @@ def fixed_to_float(
777
812
]
778
813
779
814
780
- def tao (amount : float ) -> Balance :
815
+ def tao (amount : float , netuid : int = 0 ) -> Balance :
781
816
"""
782
817
Helper function to create a Balance object from a float (Tao)
783
818
"""
784
- return Balance .from_tao (amount )
819
+ return Balance .from_tao (amount ). set_unit ( netuid )
785
820
786
821
787
- def rao (amount : int ) -> Balance :
822
+ def rao (amount : int , netuid : int = 0 ) -> Balance :
788
823
"""
789
824
Helper function to create a Balance object from an int (Rao)
790
825
"""
791
- return Balance .from_rao (amount )
826
+ return Balance .from_rao (amount ). set_unit ( netuid )
792
827
793
828
794
829
def check_and_convert_to_balance (
0 commit comments