Skip to content

Commit aa1cf11

Browse files
committed
Transfers.
1 parent a71cf44 commit aa1cf11

File tree

4 files changed

+64
-24
lines changed

4 files changed

+64
-24
lines changed

bittensor/core/async_subtensor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5455,7 +5455,7 @@ async def transfer(
54555455
self,
54565456
wallet: "Wallet",
54575457
dest: str,
5458-
amount: Balance,
5458+
amount: Optional[Balance],
54595459
transfer_all: bool = False,
54605460
wait_for_inclusion: bool = True,
54615461
wait_for_finalization: bool = False,
@@ -5468,7 +5468,7 @@ async def transfer(
54685468
Arguments:
54695469
wallet: Source wallet for the transfer.
54705470
dest: Destination address for the transfer.
5471-
amount: Number of tokens to transfer.
5471+
amount: Number of tokens to transfer. `None` is transferring all.
54725472
transfer_all: Flag to transfer all tokens. Default is `False`.
54735473
wait_for_inclusion: Waits for the transaction to be included in a block. Defaults to `True`.
54745474
wait_for_finalization: Waits for the transaction to be finalized on the blockchain. Defaults to `False`.
@@ -5479,7 +5479,8 @@ async def transfer(
54795479
Returns:
54805480
`True` if the transferring was successful, otherwise `False`.
54815481
"""
5482-
amount = check_and_convert_to_balance(amount)
5482+
if amount is not None:
5483+
amount = check_and_convert_to_balance(amount)
54835484
return await transfer_extrinsic(
54845485
subtensor=self,
54855486
wallet=wallet,

bittensor/core/extrinsics/asyncex/transfer.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ async def _do_transfer(
1919
subtensor: "AsyncSubtensor",
2020
wallet: "Wallet",
2121
destination: str,
22-
amount: "Balance",
22+
amount: Optional[Balance],
23+
keep_alive: bool = True,
2324
wait_for_inclusion: bool = True,
2425
wait_for_finalization: bool = False,
2526
period: Optional[int] = None,
@@ -32,6 +33,7 @@ async def _do_transfer(
3233
wallet (bittensor_wallet.Wallet): Bittensor wallet object to make transfer from.
3334
destination (str): Destination public key address (ss58_address or ed25519) of recipient.
3435
amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance.
36+
keep_alive (bool): If `True`, will keep the existential deposit in the account.
3537
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning `True`, or returns
3638
`False` if the extrinsic fails to enter the block within the timeout.
3739
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning
@@ -43,10 +45,24 @@ async def _do_transfer(
4345
Returns:
4446
success, block hash, formatted error message
4547
"""
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["amount"] = amount.rao
57+
if keep_alive:
58+
call_function = "transfer_keep_alive"
59+
else:
60+
call_function = "transfer_allow_death"
61+
4662
call = await subtensor.substrate.compose_call(
4763
call_module="Balances",
48-
call_function="transfer_keep_alive",
49-
call_params={"dest": destination, "value": amount.rao},
64+
call_function=call_function,
65+
call_params=call_params,
5066
)
5167

5268
success, message = await subtensor.sign_and_send_extrinsic(
@@ -73,7 +89,7 @@ async def transfer_extrinsic(
7389
subtensor: "AsyncSubtensor",
7490
wallet: "Wallet",
7591
dest: str,
76-
amount: "Balance",
92+
amount: Optional[Balance],
7793
transfer_all: bool = False,
7894
wait_for_inclusion: bool = True,
7995
wait_for_finalization: bool = False,
@@ -86,7 +102,8 @@ async def transfer_extrinsic(
86102
subtensor (bittensor.core.async_subtensor.AsyncSubtensor): initialized AsyncSubtensor object used for transfer
87103
wallet (bittensor_wallet.Wallet): Bittensor wallet object to make transfer from.
88104
dest (str): Destination public key address (ss58_address or ed25519) of recipient.
89-
amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance.
105+
amount (Optional[bittensor.utils.balance.Balance]): Amount to stake as Bittensor balance. `None` if
106+
transferring all.
90107
transfer_all (bool): Whether to transfer all funds from this wallet to the destination address.
91108
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning `True`, or returns
92109
`False` if the extrinsic fails to enter the block within the timeout.
@@ -102,6 +119,11 @@ async def transfer_extrinsic(
102119
finalization / inclusion, the response is `True`, regardless of its inclusion.
103120
"""
104121
destination = dest
122+
123+
if amount is None and not transfer_all:
124+
logging.error("If not transferring all, `amount` must be specified.")
125+
return False
126+
105127
# Validate destination address.
106128
if not is_valid_bittensor_address_or_public_key(destination):
107129
logging.error(
@@ -137,12 +159,10 @@ async def transfer_extrinsic(
137159

138160
# Check if we have enough balance.
139161
if transfer_all is True:
140-
amount = account_balance - fee - existential_deposit
141-
if amount < Balance(0):
162+
if (account_balance - fee) < existential_deposit:
142163
logging.error("Not enough balance to transfer")
143164
return False
144-
145-
if account_balance < (amount + fee + existential_deposit):
165+
elif account_balance < (amount + fee + existential_deposit):
146166
logging.error(":cross_mark: [red]Not enough balance[/red]")
147167
logging.error(f"\t\tBalance:\t[blue]{account_balance}[/blue]")
148168
logging.error(f"\t\tAmount:\t[blue]{amount}[/blue]")

bittensor/core/extrinsics/transfer.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def _do_transfer(
1818
subtensor: "Subtensor",
1919
wallet: "Wallet",
2020
destination: str,
21-
amount: Balance,
21+
amount: Optional[Balance],
22+
keep_alive: bool = True,
2223
wait_for_inclusion: bool = True,
2324
wait_for_finalization: bool = False,
2425
period: Optional[int] = None,
@@ -30,7 +31,8 @@ def _do_transfer(
3031
subtensor (bittensor.core.subtensor.Subtensor): the Subtensor object used for transfer
3132
wallet (bittensor_wallet.Wallet): Bittensor wallet object to make transfer from.
3233
destination (str): Destination public key address (ss58_address or ed25519) of recipient.
33-
amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance.
34+
amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance. `None` if transferring all.
35+
keep_alive (bool): If `True`, will keep the existential deposit in the account.
3436
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning `True`, or returns
3537
`False` if the extrinsic fails to enter the block within the timeout.
3638
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning
@@ -42,10 +44,24 @@ def _do_transfer(
4244
Returns:
4345
success, block hash, formatted error message
4446
"""
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["amount"] = amount.rao
56+
if keep_alive:
57+
call_function = "transfer_keep_alive"
58+
else:
59+
call_function = "transfer_allow_death"
60+
4561
call = subtensor.substrate.compose_call(
4662
call_module="Balances",
47-
call_function="transfer_keep_alive",
48-
call_params={"dest": destination, "value": amount.rao},
63+
call_function=call_function,
64+
call_params=call_params,
4965
)
5066

5167
success, message = subtensor.sign_and_send_extrinsic(
@@ -72,7 +88,7 @@ def transfer_extrinsic(
7288
subtensor: "Subtensor",
7389
wallet: "Wallet",
7490
dest: str,
75-
amount: Balance,
91+
amount: Optional[Balance],
7692
transfer_all: bool = False,
7793
wait_for_inclusion: bool = True,
7894
wait_for_finalization: bool = False,
@@ -85,7 +101,7 @@ def transfer_extrinsic(
85101
subtensor (bittensor.core.subtensor.Subtensor): the Subtensor object used for transfer
86102
wallet (bittensor_wallet.Wallet): Bittensor wallet object to make transfer from.
87103
dest (str): Destination public key address (ss58_address or ed25519) of recipient.
88-
amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance.
104+
amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance. `None` if transferring all.
89105
transfer_all (bool): Whether to transfer all funds from this wallet to the destination address.
90106
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning `True`, or returns
91107
`False` if the extrinsic fails to enter the block within the timeout.
@@ -100,6 +116,10 @@ def transfer_extrinsic(
100116
success (bool): Flag is `True` if extrinsic was finalized or included in the block. If we did not wait for
101117
finalization / inclusion, the response is `True`, regardless of its inclusion.
102118
"""
119+
if amount is None and not transfer_all:
120+
logging.error("If not transferring all, `amount` must be specified.")
121+
return False
122+
103123
# Validate destination address.
104124
if not is_valid_bittensor_address_or_public_key(dest):
105125
logging.error(
@@ -131,12 +151,10 @@ def transfer_extrinsic(
131151

132152
# Check if we have enough balance.
133153
if transfer_all is True:
134-
amount = account_balance - fee - existential_deposit
135-
if amount < Balance(0):
154+
if (account_balance - fee) < existential_deposit:
136155
logging.error("Not enough balance to transfer")
137156
return False
138-
139-
if account_balance < (amount + fee + existential_deposit):
157+
elif account_balance < (amount + fee + existential_deposit):
140158
logging.error(":cross_mark: [red]Not enough balance[/red]")
141159
logging.error(f"\t\tBalance:\t[blue]{account_balance}[/blue]")
142160
logging.error(f"\t\tAmount:\t[blue]{amount}[/blue]")

bittensor/core/subtensor.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4266,7 +4266,7 @@ def transfer(
42664266
self,
42674267
wallet: "Wallet",
42684268
dest: str,
4269-
amount: Balance,
4269+
amount: Optional[Balance],
42704270
wait_for_inclusion: bool = True,
42714271
wait_for_finalization: bool = False,
42724272
transfer_all: bool = False,
@@ -4292,7 +4292,8 @@ def transfer(
42924292
Returns:
42934293
`True` if the transferring was successful, otherwise `False`.
42944294
"""
4295-
amount = check_and_convert_to_balance(amount)
4295+
if amount is not None:
4296+
amount = check_and_convert_to_balance(amount)
42964297
return transfer_extrinsic(
42974298
subtensor=self,
42984299
wallet=wallet,

0 commit comments

Comments
 (0)