5
5
is_valid_bittensor_address_or_public_key ,
6
6
unlock_key ,
7
7
get_explorer_url_for_network ,
8
+ get_transfer_fn_params ,
8
9
)
9
10
from bittensor .utils .balance import Balance
10
11
from bittensor .utils .btlogging import logging
@@ -18,10 +19,11 @@ def _do_transfer(
18
19
subtensor : "Subtensor" ,
19
20
wallet : "Wallet" ,
20
21
destination : str ,
21
- amount : Balance ,
22
+ amount : Optional [ Balance ] ,
22
23
wait_for_inclusion : bool = True ,
23
24
wait_for_finalization : bool = False ,
24
25
period : Optional [int ] = None ,
26
+ keep_alive : bool = True ,
25
27
) -> tuple [bool , str , str ]:
26
28
"""
27
29
Makes transfer from wallet to destination public key address.
@@ -30,22 +32,25 @@ def _do_transfer(
30
32
subtensor (bittensor.core.subtensor.Subtensor): the Subtensor object used for transfer
31
33
wallet (bittensor_wallet.Wallet): Bittensor wallet object to make transfer from.
32
34
destination (str): Destination public key address (ss58_address or ed25519) of recipient.
33
- amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance.
35
+ amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance. `None` if transferring all.
34
36
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning `True`, or returns
35
37
`False` if the extrinsic fails to enter the block within the timeout.
36
38
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning
37
39
`True`, or returns `False` if the extrinsic fails to be finalized within the timeout.
38
40
period (Optional[int]): The number of blocks during which the transaction will remain valid after it's submitted.
39
41
If the transaction is not included in a block within that number of blocks, it will expire and be rejected.
40
42
You can think of it as an expiration date for the transaction.
43
+ keep_alive (bool): If `True`, will keep the existential deposit in the account.
41
44
42
45
Returns:
43
46
success, block hash, formatted error message
44
47
"""
48
+ call_function , call_params = get_transfer_fn_params (amount , destination , keep_alive )
49
+
45
50
call = subtensor .substrate .compose_call (
46
51
call_module = "Balances" ,
47
- call_function = "transfer_keep_alive" ,
48
- call_params = { "dest" : destination , "value" : amount . rao } ,
52
+ call_function = call_function ,
53
+ call_params = call_params ,
49
54
)
50
55
51
56
success , message = subtensor .sign_and_send_extrinsic (
@@ -72,7 +77,7 @@ def transfer_extrinsic(
72
77
subtensor : "Subtensor" ,
73
78
wallet : "Wallet" ,
74
79
dest : str ,
75
- amount : Balance ,
80
+ amount : Optional [ Balance ] ,
76
81
transfer_all : bool = False ,
77
82
wait_for_inclusion : bool = True ,
78
83
wait_for_finalization : bool = False ,
@@ -85,7 +90,7 @@ def transfer_extrinsic(
85
90
subtensor (bittensor.core.subtensor.Subtensor): the Subtensor object used for transfer
86
91
wallet (bittensor_wallet.Wallet): Bittensor wallet object to make transfer from.
87
92
dest (str): Destination public key address (ss58_address or ed25519) of recipient.
88
- amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance.
93
+ amount (bittensor.utils.balance.Balance): Amount to stake as Bittensor balance. `None` if transferring all.
89
94
transfer_all (bool): Whether to transfer all funds from this wallet to the destination address.
90
95
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning `True`, or returns
91
96
`False` if the extrinsic fails to enter the block within the timeout.
@@ -100,6 +105,10 @@ def transfer_extrinsic(
100
105
success (bool): Flag is `True` if extrinsic was finalized or included in the block. If we did not wait for
101
106
finalization / inclusion, the response is `True`, regardless of its inclusion.
102
107
"""
108
+ if amount is None and not transfer_all :
109
+ logging .error ("If not transferring all, `amount` must be specified." )
110
+ return False
111
+
103
112
# Validate destination address.
104
113
if not is_valid_bittensor_address_or_public_key (dest ):
105
114
logging .error (
@@ -127,16 +136,16 @@ def transfer_extrinsic(
127
136
else :
128
137
existential_deposit = subtensor .get_existential_deposit (block = block )
129
138
130
- fee = subtensor .get_transfer_fee (wallet = wallet , dest = dest , value = amount )
139
+ fee = subtensor .get_transfer_fee (
140
+ wallet = wallet , dest = dest , value = amount , keep_alive = keep_alive
141
+ )
131
142
132
143
# Check if we have enough balance.
133
144
if transfer_all is True :
134
- amount = account_balance - fee - existential_deposit
135
- if amount < Balance (0 ):
145
+ if (account_balance - fee ) < existential_deposit :
136
146
logging .error ("Not enough balance to transfer" )
137
147
return False
138
-
139
- if account_balance < (amount + fee + existential_deposit ):
148
+ elif account_balance < (amount + fee + existential_deposit ):
140
149
logging .error (":cross_mark: [red]Not enough balance[/red]" )
141
150
logging .error (f"\t \t Balance:\t [blue]{ account_balance } [/blue]" )
142
151
logging .error (f"\t \t Amount:\t [blue]{ amount } [/blue]" )
@@ -149,6 +158,7 @@ def transfer_extrinsic(
149
158
wallet = wallet ,
150
159
destination = dest ,
151
160
amount = amount ,
161
+ keep_alive = keep_alive ,
152
162
wait_for_finalization = wait_for_finalization ,
153
163
wait_for_inclusion = wait_for_inclusion ,
154
164
period = period ,
0 commit comments