Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@
u16_normalized_float,
_decode_hex_identity_dict,
)
from bittensor.utils.balance import Balance, fixed_to_float
from bittensor.utils.balance import (
Balance,
fixed_to_float,
check_and_convert_to_balance,
)
from bittensor.utils.btlogging import logging
from bittensor.utils.delegates_details import DelegatesDetails
from bittensor.utils.weight_utils import generate_weight_hash
Expand Down Expand Up @@ -2899,6 +2903,7 @@ async def add_stake(
This function enables neurons to increase their stake in the network, enhancing their influence and potential
rewards in line with Bittensor's consensus and reward mechanisms.
"""
amount = check_and_convert_to_balance(amount)
return await add_stake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -3078,7 +3083,7 @@ async def move_stake(
Returns:
success (bool): True if the stake movement was successful.
"""

amount = check_and_convert_to_balance(amount)
return await move_stake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -3480,9 +3485,7 @@ async def swap_stake(
Returns:
success (bool): True if the extrinsic was successful.
"""
if isinstance(amount, float):
amount = Balance.from_tao(amount)

amount = check_and_convert_to_balance(amount)
return await swap_stake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -3521,7 +3524,7 @@ async def transfer_stake(
Returns:
success (bool): True if the transfer was successful.
"""

amount = check_and_convert_to_balance(amount)
return await transfer_stake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -3560,7 +3563,7 @@ async def transfer(
Returns:
`True` if the transferring was successful, otherwise `False`.
"""

amount = check_and_convert_to_balance(amount)
return await transfer_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -3600,6 +3603,7 @@ async def unstake(
This function supports flexible stake management, allowing neurons to adjust their network participation and
potential reward accruals.
"""
amount = check_and_convert_to_balance(amount)
return await unstake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down
18 changes: 12 additions & 6 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@
u16_normalized_float,
_decode_hex_identity_dict,
)
from bittensor.utils.balance import Balance, fixed_to_float, FixedPoint
from bittensor.utils.balance import (
Balance,
fixed_to_float,
FixedPoint,
check_and_convert_to_balance,
)
from bittensor.utils.btlogging import logging
from bittensor.utils.weight_utils import generate_weight_hash

Expand Down Expand Up @@ -2186,6 +2191,7 @@ def add_stake(
This function enables neurons to increase their stake in the network, enhancing their influence and potential
rewards in line with Bittensor's consensus and reward mechanisms.
"""
amount = check_and_convert_to_balance(amount)
return add_stake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -2363,6 +2369,7 @@ def move_stake(
Returns:
success (bool): True if the stake movement was successful.
"""
amount = check_and_convert_to_balance(amount)
return move_stake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -2746,7 +2753,7 @@ def swap_stake(
Returns:
success (bool): True if the extrinsic was successful.
"""

amount = check_and_convert_to_balance(amount)
return swap_stake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -2784,9 +2791,7 @@ def transfer(
Returns:
`True` if the transferring was successful, otherwise `False`.
"""
if isinstance(amount, float):
amount = Balance.from_tao(amount)

amount = check_and_convert_to_balance(amount)
return transfer_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -2825,7 +2830,7 @@ def transfer_stake(
Returns:
success (bool): True if the transfer was successful.
"""

amount = check_and_convert_to_balance(amount)
return transfer_stake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down Expand Up @@ -2866,6 +2871,7 @@ def unstake(
This function supports flexible stake management, allowing neurons to adjust their network participation and
potential reward accruals.
"""
amount = check_and_convert_to_balance(amount)
return unstake_extrinsic(
subtensor=self,
wallet=wallet,
Expand Down
19 changes: 19 additions & 0 deletions bittensor/utils/balance.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from typing import Union, TypedDict

from scalecodec import ScaleType
Expand Down Expand Up @@ -781,3 +783,20 @@ def rao(amount: int) -> Balance:
Helper function to create a Balance object from an int (Rao)
"""
return Balance.from_rao(amount)


def check_and_convert_to_balance(amount: Union[float, int, Balance]) -> Balance:
"""
Helper function to check and convert the amount type to a Balance object.
This is used to support backwards compatibility while also providing a deprecation notice.
"""
if isinstance(amount, (float, int)):
warnings.simplefilter("default", DeprecationWarning)
warnings.warn(
"Detected a non-balance amount. Converting to Balance from Tao for backwards compatibility."
"Please update your code to use tao(amount) or Balance.from_tao(amount) for the main release 9.0.0.",
category=DeprecationWarning,
stacklevel=2,
)
amount = tao(amount)
return amount