Skip to content

Commit 10ab0d5

Browse files
committed
Helper function
1 parent f404ea5 commit 10ab0d5

File tree

5 files changed

+45
-52
lines changed

5 files changed

+45
-52
lines changed

bittensor/core/async_subtensor.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
torch,
9999
u16_normalized_float,
100100
u64_normalized_float,
101+
get_transfer_fn_params,
101102
)
102103
from bittensor.core.extrinsics.asyncex.liquidity import (
103104
add_liquidity_extrinsic,
@@ -3157,20 +3158,10 @@ async def get_transfer_fee(
31573158
wallet has sufficient funds to cover both the transfer amount and the associated costs. This function provides
31583159
a crucial tool for managing financial operations within the Bittensor network.
31593160
"""
3160-
call_params: dict[str, Union[int, str, bool]] = {"dest": dest}
3161-
if value is None:
3162-
call_function = "transfer_all"
3163-
if keep_alive:
3164-
call_params["keep_alive"] = True
3165-
else:
3166-
call_params["keep_alive"] = False
3167-
else:
3161+
if value is not None:
31683162
value = check_and_convert_to_balance(value)
3169-
call_params["value"] = value.rao
3170-
if keep_alive:
3171-
call_function = "transfer_keep_alive"
3172-
else:
3173-
call_function = "transfer_allow_death"
3163+
call_params: dict[str, Union[int, str, bool]]
3164+
call_function, call_params = get_transfer_fn_params(value, dest, keep_alive)
31743165

31753166
call = await self.substrate.compose_call(
31763167
call_module="Balances",

bittensor/core/extrinsics/asyncex/transfer.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
get_explorer_url_for_network,
77
is_valid_bittensor_address_or_public_key,
88
unlock_key,
9+
get_transfer_fn_params,
910
)
1011
from bittensor.utils.balance import Balance
1112
from bittensor.utils.btlogging import logging
@@ -45,19 +46,7 @@ async def _do_transfer(
4546
Returns:
4647
success, block hash, formatted error message
4748
"""
48-
call_params = {"dest": destination}
49-
if amount is None:
50-
call_function = "transfer_all"
51-
if keep_alive:
52-
call_params["keep_alive"] = True
53-
else:
54-
call_params["keep_alive"] = False
55-
else:
56-
call_params["value"] = amount.rao
57-
if keep_alive:
58-
call_function = "transfer_keep_alive"
59-
else:
60-
call_function = "transfer_allow_death"
49+
call_function, call_params = get_transfer_fn_params(amount, destination, keep_alive)
6150

6251
call = await subtensor.substrate.compose_call(
6352
call_module="Balances",

bittensor/core/extrinsics/transfer.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
is_valid_bittensor_address_or_public_key,
66
unlock_key,
77
get_explorer_url_for_network,
8+
get_transfer_fn_params,
89
)
910
from bittensor.utils.balance import Balance
1011
from bittensor.utils.btlogging import logging
@@ -44,19 +45,7 @@ def _do_transfer(
4445
Returns:
4546
success, block hash, formatted error message
4647
"""
47-
call_params = {"dest": destination}
48-
if amount is None:
49-
call_function = "transfer_all"
50-
if keep_alive:
51-
call_params["keep_alive"] = True
52-
else:
53-
call_params["keep_alive"] = False
54-
else:
55-
call_params["value"] = amount.rao
56-
if keep_alive:
57-
call_function = "transfer_keep_alive"
58-
else:
59-
call_function = "transfer_allow_death"
48+
call_function, call_params = get_transfer_fn_params(amount, destination, keep_alive)
6049

6150
call = subtensor.substrate.compose_call(
6251
call_module="Balances",

bittensor/core/subtensor.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
u16_normalized_float,
108108
u64_normalized_float,
109109
deprecated_message,
110+
get_transfer_fn_params,
110111
)
111112
from bittensor.utils.balance import (
112113
Balance,
@@ -2271,20 +2272,11 @@ def get_transfer_fee(
22712272
has sufficient funds to cover both the transfer amount and the associated costs. This function provides a
22722273
crucial tool for managing financial operations within the Bittensor network.
22732274
"""
2274-
call_params: dict[str, Union[int, str, bool]] = {"dest": dest}
2275-
if value is None:
2276-
call_function = "transfer_all"
2277-
if keep_alive:
2278-
call_params["keep_alive"] = True
2279-
else:
2280-
call_params["keep_alive"] = False
2281-
else:
2275+
if value is not None:
22822276
value = check_and_convert_to_balance(value)
2283-
call_params["value"] = value.rao
2284-
if keep_alive:
2285-
call_function = "transfer_keep_alive"
2286-
else:
2287-
call_function = "transfer_allow_death"
2277+
call_params: dict[str, Union[int, str, bool]]
2278+
call_function, call_params = get_transfer_fn_params(value, dest, keep_alive)
2279+
22882280
call = self.substrate.compose_call(
22892281
call_module="Balances",
22902282
call_function=call_function,

bittensor/utils/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
if TYPE_CHECKING:
2424
from bittensor_wallet import Wallet
25+
from bittensor.utils.balance import Balance
2526

2627
BT_DOCS_LINK = "https://docs.bittensor.com"
2728

@@ -445,3 +446,34 @@ def deprecated_message(message: str) -> None:
445446
"""Shows a deprecation warning message with the given message."""
446447
warnings.simplefilter("default", DeprecationWarning)
447448
warnings.warn(message=message, category=DeprecationWarning, stacklevel=2)
449+
450+
451+
def get_transfer_fn_params(
452+
amount: Optional["Balance"], destination: str, keep_alive: bool
453+
) -> tuple[str, dict[str, Union[str, int, bool]]]:
454+
"""
455+
Helper function to get the transfer call function and call params, depending on the value and keep_alive flag
456+
provided
457+
458+
Args:
459+
amount: the amount of Tao to transfer. `None` if transferring all.
460+
destination: the destination SS58 of the transfer
461+
keep_alive: whether to enforce a retention of the existential deposit in the account after transfer.
462+
463+
Returns:
464+
tuple[call function, call params]
465+
"""
466+
call_params = {"dest": destination}
467+
if amount is None:
468+
call_function = "transfer_all"
469+
if keep_alive:
470+
call_params["keep_alive"] = True
471+
else:
472+
call_params["keep_alive"] = False
473+
else:
474+
call_params["value"] = amount.rao
475+
if keep_alive:
476+
call_function = "transfer_keep_alive"
477+
else:
478+
call_function = "transfer_allow_death"
479+
return call_function, call_params

0 commit comments

Comments
 (0)