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
+
12
+ A warning is raised if the netuids differ.
13
+
14
+ Example:
15
+ >>> balance1 = Balance.from_rao(1000).set_unit(12)
16
+ >>> balance2 = Balance.from_rao(500).set_unit(12)
17
+ >>> balance1 + balance2 # No warning
18
+
19
+ >>> balance3 = Balance.from_rao(200).set_unit(15)
20
+ >>> balance1 + balance3 # Raises DeprecationWarning
21
+
22
+ In this example:
23
+ - `from_rao` creates a Balance instance from the amount in rao (smallest unit).
24
+ - `set_unit(12)` sets the unit to correspond to subnet 12 (i.e., Alpha from netuid 12).
25
+ """
26
+ if self .netuid != other .netuid :
27
+ warnings .simplefilter ("default" , DeprecationWarning )
28
+ warnings .warn (
29
+ "Balance objects must have the same netuid (Alpha currency) to perform arithmetic operations.\n "
30
+ f"First balance is `{ self } `. Second balance is `{ other } `.\n \n "
31
+ "To create a Balance instance with the correct netuid, use:\n "
32
+ "Balance.from_rao(1000).set_unit(12) # 1000 rao in subnet 12" ,
33
+ category = DeprecationWarning ,
34
+ stacklevel = 2 ,
35
+ )
36
+
37
+
9
38
class Balance :
10
39
"""
11
40
Represents the bittensor balance of the wallet, stored as rao (int).
@@ -23,6 +52,7 @@ class Balance:
23
52
rao_unit : str = settings .RAO_SYMBOL # This is the rao unit
24
53
rao : int
25
54
tao : float
55
+ netuid : int = 0
26
56
27
57
def __init__ (self , balance : Union [int , float ]):
28
58
"""
@@ -78,7 +108,8 @@ def __eq__(self, other: Union[int, float, "Balance"]):
78
108
if other is None :
79
109
return False
80
110
81
- if hasattr (other , "rao" ):
111
+ if isinstance (other , Balance ):
112
+ _check_currencies (self , other )
82
113
return self .rao == other .rao
83
114
else :
84
115
try :
@@ -92,7 +123,8 @@ def __ne__(self, other: Union[int, float, "Balance"]):
92
123
return not self == other
93
124
94
125
def __gt__ (self , other : Union [int , float , "Balance" ]):
95
- if hasattr (other , "rao" ):
126
+ if isinstance (other , Balance ):
127
+ _check_currencies (self , other )
96
128
return self .rao > other .rao
97
129
else :
98
130
try :
@@ -103,7 +135,8 @@ def __gt__(self, other: Union[int, float, "Balance"]):
103
135
raise NotImplementedError ("Unsupported type" )
104
136
105
137
def __lt__ (self , other : Union [int , float , "Balance" ]):
106
- if hasattr (other , "rao" ):
138
+ if isinstance (other , Balance ):
139
+ _check_currencies (self , other )
107
140
return self .rao < other .rao
108
141
else :
109
142
try :
@@ -115,111 +148,129 @@ def __lt__(self, other: Union[int, float, "Balance"]):
115
148
116
149
def __le__ (self , other : Union [int , float , "Balance" ]):
117
150
try :
151
+ if isinstance (other , Balance ):
152
+ _check_currencies (self , other )
118
153
return self < other or self == other
119
154
except TypeError :
120
155
raise NotImplementedError ("Unsupported type" )
121
156
122
157
def __ge__ (self , other : Union [int , float , "Balance" ]):
123
158
try :
159
+ if isinstance (other , Balance ):
160
+ _check_currencies (self , other )
124
161
return self > other or self == other
125
162
except TypeError :
126
163
raise NotImplementedError ("Unsupported type" )
127
164
128
165
def __add__ (self , other : Union [int , float , "Balance" ]):
129
- if hasattr (other , "rao" ):
130
- return Balance .from_rao (int (self .rao + other .rao ))
166
+ if isinstance (other , Balance ):
167
+ _check_currencies (self , other )
168
+ return Balance .from_rao (int (self .rao + other .rao )).set_unit (self .netuid )
131
169
else :
132
170
try :
133
171
# Attempt to cast to int from rao
134
- return Balance .from_rao (int (self .rao + other ))
172
+ return Balance .from_rao (int (self .rao + other )). set_unit ( self . netuid )
135
173
except (ValueError , TypeError ):
136
174
raise NotImplementedError ("Unsupported type" )
137
175
138
176
def __radd__ (self , other : Union [int , float , "Balance" ]):
139
177
try :
178
+ if isinstance (other , Balance ):
179
+ _check_currencies (self , other )
140
180
return self + other
141
181
except TypeError :
142
182
raise NotImplementedError ("Unsupported type" )
143
183
144
184
def __sub__ (self , other : Union [int , float , "Balance" ]):
145
185
try :
186
+ if isinstance (other , Balance ):
187
+ _check_currencies (self , other )
146
188
return self + - other
147
189
except TypeError :
148
190
raise NotImplementedError ("Unsupported type" )
149
191
150
192
def __rsub__ (self , other : Union [int , float , "Balance" ]):
151
193
try :
194
+ if isinstance (other , Balance ):
195
+ _check_currencies (self , other )
152
196
return - self + other
153
197
except TypeError :
154
198
raise NotImplementedError ("Unsupported type" )
155
199
156
200
def __mul__ (self , other : Union [int , float , "Balance" ]):
157
- if hasattr (other , "rao" ):
158
- return Balance .from_rao (int (self .rao * other .rao ))
201
+ if isinstance (other , Balance ):
202
+ _check_currencies (self , other )
203
+ return Balance .from_rao (int (self .rao * other .rao )).set_unit (self .netuid )
159
204
else :
160
205
try :
161
206
# Attempt to cast to int from rao
162
- return Balance .from_rao (int (self .rao * other ))
207
+ return Balance .from_rao (int (self .rao * other )). set_unit ( self . netuid )
163
208
except (ValueError , TypeError ):
164
209
raise NotImplementedError ("Unsupported type" )
165
210
166
211
def __rmul__ (self , other : Union [int , float , "Balance" ]):
212
+ if isinstance (other , Balance ):
213
+ _check_currencies (self , other )
167
214
return self * other
168
215
169
216
def __truediv__ (self , other : Union [int , float , "Balance" ]):
170
- if hasattr (other , "rao" ):
171
- return Balance .from_rao (int (self .rao / other .rao ))
217
+ if isinstance (other , Balance ):
218
+ _check_currencies (self , other )
219
+ return Balance .from_rao (int (self .rao / other .rao )).set_unit (self .netuid )
172
220
else :
173
221
try :
174
222
# Attempt to cast to int from rao
175
- return Balance .from_rao (int (self .rao / other ))
223
+ return Balance .from_rao (int (self .rao / other )). set_unit ( self . netuid )
176
224
except (ValueError , TypeError ):
177
225
raise NotImplementedError ("Unsupported type" )
178
226
179
227
def __rtruediv__ (self , other : Union [int , float , "Balance" ]):
180
- if hasattr (other , "rao" ):
181
- return Balance .from_rao (int (other .rao / self .rao ))
228
+ if isinstance (other , Balance ):
229
+ _check_currencies (self , other )
230
+ return Balance .from_rao (int (other .rao / self .rao )).set_unit (self .netuid )
182
231
else :
183
232
try :
184
233
# Attempt to cast to int from rao
185
- return Balance .from_rao (int (other / self .rao ))
234
+ return Balance .from_rao (int (other / self .rao )). set_unit ( self . netuid )
186
235
except (ValueError , TypeError ):
187
236
raise NotImplementedError ("Unsupported type" )
188
237
189
238
def __floordiv__ (self , other : Union [int , float , "Balance" ]):
190
- if hasattr (other , "rao" ):
191
- return Balance .from_rao (int (self .tao // other .tao ))
239
+ if isinstance (other , Balance ):
240
+ _check_currencies (self , other )
241
+ return Balance .from_rao (int (self .tao // other .tao )).set_unit (self .netuid )
192
242
else :
193
243
try :
194
244
# Attempt to cast to int from rao
195
- return Balance .from_rao (int (self .rao // other ))
245
+ return Balance .from_rao (int (self .rao // other )). set_unit ( self . netuid )
196
246
except (ValueError , TypeError ):
197
247
raise NotImplementedError ("Unsupported type" )
198
248
199
249
def __rfloordiv__ (self , other : Union [int , float , "Balance" ]):
200
- if hasattr (other , "rao" ):
201
- return Balance .from_rao (int (other .rao // self .rao ))
250
+ if isinstance (other , Balance ):
251
+ _check_currencies (self , other )
252
+ return Balance .from_rao (int (other .rao // self .rao )).set_unit (self .netuid )
202
253
else :
203
254
try :
204
255
# Attempt to cast to int from rao
205
- return Balance .from_rao (int (other // self .rao ))
256
+ return Balance .from_rao (int (other // self .rao )). set_unit ( self . netuid )
206
257
except (ValueError , TypeError ):
207
258
raise NotImplementedError ("Unsupported type" )
208
259
209
260
def __nonzero__ (self ) -> bool :
210
261
return bool (self .rao )
211
262
212
263
def __neg__ (self ):
213
- return Balance .from_rao (- self .rao )
264
+ return Balance .from_rao (- self .rao ). set_unit ( self . netuid )
214
265
215
266
def __pos__ (self ):
216
- return Balance .from_rao (self .rao )
267
+ return Balance .from_rao (self .rao ). set_unit ( self . netuid )
217
268
218
269
def __abs__ (self ):
219
- return Balance .from_rao (abs (self .rao ))
270
+ return Balance .from_rao (abs (self .rao )). set_unit ( self . netuid )
220
271
221
272
@staticmethod
222
- def from_float (amount : float , netuid : int = 0 ):
273
+ def from_float (amount : float , netuid : int = 0 ) -> "Balance" :
223
274
"""
224
275
Given tao, return :func:`Balance` object with rao(``int``) and tao(``float``), where rao = int(tao*pow(10,9))
225
276
Args:
@@ -233,7 +284,7 @@ def from_float(amount: float, netuid: int = 0):
233
284
return Balance (rao_ ).set_unit (netuid )
234
285
235
286
@staticmethod
236
- def from_tao (amount : float , netuid : int = 0 ):
287
+ def from_tao (amount : float , netuid : int = 0 ) -> "Balance" :
237
288
"""
238
289
Given tao, return Balance object with rao(``int``) and tao(``float``), where rao = int(tao*pow(10,9))
239
290
@@ -248,7 +299,7 @@ def from_tao(amount: float, netuid: int = 0):
248
299
return Balance (rao_ ).set_unit (netuid )
249
300
250
301
@staticmethod
251
- def from_rao (amount : int , netuid : int = 0 ):
302
+ def from_rao (amount : int , netuid : int = 0 ) -> "Balance" :
252
303
"""
253
304
Given rao, return Balance object with rao(``int``) and tao(``float``), where rao = int(tao*pow(10,9))
254
305
@@ -262,7 +313,7 @@ def from_rao(amount: int, netuid: int = 0):
262
313
return Balance (amount ).set_unit (netuid )
263
314
264
315
@staticmethod
265
- def get_unit (netuid : int ):
316
+ def get_unit (netuid : int ) -> str :
266
317
base = len (units )
267
318
if netuid < base :
268
319
return units [netuid ]
@@ -274,6 +325,7 @@ def get_unit(netuid: int):
274
325
return result
275
326
276
327
def set_unit (self , netuid : int ):
328
+ self .netuid = netuid
277
329
self .unit = Balance .get_unit (netuid )
278
330
self .rao_unit = Balance .get_unit (netuid )
279
331
return self
@@ -777,18 +829,18 @@ def fixed_to_float(
777
829
]
778
830
779
831
780
- def tao (amount : float ) -> Balance :
832
+ def tao (amount : float , netuid : int = 0 ) -> Balance :
781
833
"""
782
834
Helper function to create a Balance object from a float (Tao)
783
835
"""
784
- return Balance .from_tao (amount )
836
+ return Balance .from_tao (amount ). set_unit ( netuid )
785
837
786
838
787
- def rao (amount : int ) -> Balance :
839
+ def rao (amount : int , netuid : int = 0 ) -> Balance :
788
840
"""
789
841
Helper function to create a Balance object from an int (Rao)
790
842
"""
791
- return Balance .from_rao (amount )
843
+ return Balance .from_rao (amount ). set_unit ( netuid )
792
844
793
845
794
846
def check_and_convert_to_balance (
0 commit comments