Skip to content

Commit bafd6ce

Browse files
committed
fix comments
1 parent 1f27bd8 commit bafd6ce

File tree

18 files changed

+153
-97
lines changed

18 files changed

+153
-97
lines changed

test/functional/test_framework/wallet_cli_controller.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ async def create_from_cold_address(self, address: str, amount: int, selected_utx
301301
change_address_str = '' if change_address is None else f"--change {change_address}"
302302
return await self._write_command(f"transaction-create-from-cold-input {address} {amount} {str(selected_utxo)} {change_address_str}\n")
303303

304-
async def sweep_addresses(self, destination_address: str, from_addresses: List[str] = []) -> str:
305-
return await self._write_command(f"address-sweep-spendable {destination_address} {' '.join(from_addresses)}\n")
304+
async def sweep_addresses(self, destination_address: str, from_addresses: List[str] = [], all_addresses: bool = False) -> str:
305+
all_addresses_str = "--all" if all_addresses else ""
306+
return await self._write_command(f"address-sweep-spendable {destination_address} {' '.join(from_addresses)} {all_addresses_str}\n")
306307

307308
async def sweep_delegation(self, destination_address: str, delegation_id: str) -> str:
308309
return await self._write_command(f"staking-sweep-delegation {destination_address} {delegation_id}\n")

test/functional/test_framework/wallet_rpc_controller.py

+19-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import base64
2626
from operator import itemgetter
2727

28-
from typing import Optional, List, Union
28+
from typing import Optional, List, Union, TypedDict
2929

3030
from test_framework.util import assert_in, rpc_port
3131
from test_framework.wallet_controller_common import PartialSigInfo, TokenTxOutput, UtxoOutpoint, WalletCliControllerBase
@@ -46,6 +46,16 @@ def to_json(self):
4646
else:
4747
return {'Transfer': [ { 'Coin': {"atoms": str(self.atoms)} }, f"HexifiedDestination{{0x02{self.pub_key_hex}}}" ]}
4848

49+
@dataclass
50+
class Balances:
51+
coins: str
52+
tokens: dict
53+
54+
class NewTxResult(TypedDict):
55+
tx_id: str
56+
tx: str
57+
fees: Balances
58+
broadcasted: bool
4959

5060
@dataclass
5161
class PoolData:
@@ -295,30 +305,30 @@ async def issue_new_token(self,
295305
else:
296306
return None, result['error']
297307

298-
async def mint_tokens(self, token_id: str, address: str, amount: int) -> str:
308+
async def mint_tokens(self, token_id: str, address: str, amount: int) -> NewTxResult:
299309
return self._write_command("token_mint", [self.account, token_id, address, {'decimal': str(amount)}, {'in_top_x_mb': 5}])['result']
300310

301311
# Note: unlike mint_tokens, this function behaves identically both for wallet_cli_controller and wallet_rpc_controller.
302312
async def mint_tokens_or_fail(self, token_id: str, address: str, amount: int):
303313
# self.mint_tokens already fails on error
304314
await self.mint_tokens(token_id, address, amount)
305315

306-
async def unmint_tokens(self, token_id: str, amount: int) -> str:
316+
async def unmint_tokens(self, token_id: str, amount: int) -> NewTxResult:
307317
return self._write_command("token_unmint", [self.account, token_id, {'decimal': str(amount)}, {'in_top_x_mb': 5}])['result']
308318

309-
async def lock_token_supply(self, token_id: str) -> str:
319+
async def lock_token_supply(self, token_id: str) -> NewTxResult:
310320
return self._write_command("token_lock_supply", [self.account, token_id, {'in_top_x_mb': 5}])['result']
311321

312-
async def freeze_token(self, token_id: str, is_unfreezable: str) -> str:
322+
async def freeze_token(self, token_id: str, is_unfreezable: str) -> NewTxResult:
313323
return self._write_command("token_freeze", [self.account, token_id, is_unfreezable, {'in_top_x_mb': 5}])['result']
314324

315-
async def unfreeze_token(self, token_id: str) -> str:
325+
async def unfreeze_token(self, token_id: str) -> NewTxResult:
316326
return self._write_command("token_unfreeze", [self.account, token_id, {'in_top_x_mb': 5}])['result']
317327

318-
async def change_token_authority(self, token_id: str, new_authority: str) -> str:
328+
async def change_token_authority(self, token_id: str, new_authority: str) -> NewTxResult:
319329
return self._write_command("token_change_authority", [self.account, token_id, new_authority, {'in_top_x_mb': 5}])['result']
320330

321-
async def change_token_metadata_uri(self, token_id: str, new_metadata_uri: str) -> str:
331+
async def change_token_metadata_uri(self, token_id: str, new_metadata_uri: str) -> NewTxResult:
322332
return self._write_command("token_change_metadata_uri", [self.account, token_id, new_metadata_uri, {'in_top_x_mb': 5}])['result']
323333

324334
async def issue_new_nft(self,
@@ -571,7 +581,7 @@ async def create_htlc_transaction(self,
571581
secret_hash: str,
572582
spend_address: str,
573583
refund_address: str,
574-
refund_lock_for_blocks: int) -> str:
584+
refund_lock_for_blocks: int) -> NewTxResult:
575585
timelock = { "type": "ForBlockCount", "content": refund_lock_for_blocks }
576586
htlc = { "secret_hash": secret_hash, "spend_address": spend_address, "refund_address": refund_address, "refund_timelock": timelock }
577587
object = [self.account, {'decimal': str(amount)}, token_id, htlc, {'in_top_x_mb': 5}]

test/functional/wallet_htlc_refund.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ async def async_test(self):
128128
assert_equal(await wallet.get_best_block(), block_id)
129129

130130
balance = await wallet.get_balance()
131-
assert_in(f"Coins amount: 151", balance)
131+
assert_in("Coins amount: 151", balance)
132132
assert_not_in("Tokens", balance)
133133

134134
# issue a valid token
@@ -142,7 +142,7 @@ async def async_test(self):
142142
self.generate_block()
143143
assert_in("Success", await wallet.sync())
144144
balance = await wallet.get_balance()
145-
assert_in(f"Coins amount: 50", balance)
145+
assert_in("Coins amount: 50", balance)
146146
assert_not_in("Tokens", balance)
147147

148148
amount_to_mint = random.randint(1, 10000)
@@ -153,7 +153,7 @@ async def async_test(self):
153153
assert_in("Success", await wallet.sync())
154154
balance = await wallet.get_balance()
155155
print(balance)
156-
assert_in(f"Coins amount: 0", balance)
156+
assert_in("Coins amount: 0", balance)
157157
assert_in(f"Token: {token_id} amount: {amount_to_mint}", balance)
158158

159159
########################################################################################
@@ -165,7 +165,7 @@ async def async_test(self):
165165

166166
alice_amount_to_swap = amount_to_mint
167167
alice_htlc_tx = await wallet.create_htlc_transaction(alice_amount_to_swap, token_id, alice_secret_hash, bob_address, refund_address, 6)
168-
alice_signed_tx_obj = signed_tx_obj.decode(ScaleBytes("0x" + alice_htlc_tx))
168+
alice_signed_tx_obj = signed_tx_obj.decode(ScaleBytes("0x" + alice_htlc_tx['tx']))
169169
alice_htlc_outputs = alice_signed_tx_obj['transaction']['outputs']
170170
alice_htlc_change_dest = alice_htlc_outputs[1]['Transfer'][1]
171171
alice_htlc_tx_id = hash_object(base_tx_obj, alice_signed_tx_obj['transaction'])
@@ -200,7 +200,7 @@ async def async_test(self):
200200

201201
bob_amount_to_swap = 150
202202
bob_htlc_tx = await wallet.create_htlc_transaction(bob_amount_to_swap, None, alice_secret_hash, alice_address, refund_address, 6)
203-
bob_signed_tx_obj = signed_tx_obj.decode(ScaleBytes("0x" + bob_htlc_tx))
203+
bob_signed_tx_obj = signed_tx_obj.decode(ScaleBytes("0x" + bob_htlc_tx['tx']))
204204
bob_htlc_outputs = bob_signed_tx_obj['transaction']['outputs']
205205
bob_htlc_change_dest = bob_htlc_outputs[1]['Transfer'][1]
206206
bob_htlc_tx_id = hash_object(base_tx_obj, bob_signed_tx_obj['transaction'])
@@ -227,7 +227,7 @@ async def async_test(self):
227227
alice_refund_ptx = output.split('\n')[2]
228228

229229
# Alice's htlc tx can now be broadcasted
230-
output = await wallet.submit_transaction(alice_htlc_tx)
230+
output = await wallet.submit_transaction(alice_htlc_tx['tx'])
231231
assert_in("The transaction was submitted successfully", output)
232232

233233
# Alice signs Bob's refund
@@ -238,22 +238,22 @@ async def async_test(self):
238238
bob_refund_ptx = output.split('\n')[2]
239239

240240
# Bob's htlc tx can now be broadcasted
241-
output = await wallet.submit_transaction(bob_htlc_tx)
241+
output = await wallet.submit_transaction(bob_htlc_tx['tx'])
242242
assert_in("The transaction was submitted successfully", output)
243243

244244
self.generate_block()
245245
assert_in("Success", await wallet.sync())
246246

247247
# Check Alice's balance
248248
balance = await wallet.get_balance()
249-
assert_in(f"Coins amount: 0", balance)
249+
assert_in("Coins amount: 0", balance)
250250
assert_not_in("Tokens", balance)
251251

252252
# Check Bob's balance now
253253
await self.switch_to_wallet(wallet, 'bob_wallet')
254254
assert_in("Success", await wallet.sync())
255255
balance = await wallet.get_balance()
256-
assert_in(f"Coins amount: 0", balance)
256+
assert_in("Coins amount: 0", balance)
257257
assert_not_in("Tokens", balance)
258258

259259
########################################################################################
@@ -270,7 +270,7 @@ async def async_test(self):
270270
assert_in("Spending at height 9, locked until height 10", output)
271271

272272
balance = await wallet.get_balance()
273-
assert_in(f"Coins amount: 0", balance)
273+
assert_in("Coins amount: 0", balance)
274274
assert_not_in("Tokens", balance)
275275

276276
# Bob signs and spends the refund
@@ -286,7 +286,7 @@ async def async_test(self):
286286
assert_in("Spending at height 9, locked until height 10", output)
287287

288288
balance = await wallet.get_balance()
289-
assert_in(f"Coins amount: 0", balance)
289+
assert_in("Coins amount: 0", balance)
290290
assert_not_in("Tokens", balance)
291291

292292
########################################################################################
@@ -304,7 +304,7 @@ async def async_test(self):
304304
self.generate_block()
305305
assert_in("Success", await wallet.sync())
306306
balance = await wallet.get_balance()
307-
assert_in(f"Coins amount: 0", balance)
307+
assert_in("Coins amount: 0", balance)
308308
assert_not_in("Tokens", balance)
309309

310310
self.generate_block()
@@ -316,13 +316,13 @@ async def async_test(self):
316316
await self.switch_to_wallet(wallet, 'alice_wallet')
317317
assert_in("Success", await wallet.sync())
318318
balance = await wallet.get_balance()
319-
assert_in(f"Coins amount: 0", balance)
319+
assert_in("Coins amount: 0", balance)
320320
assert_in(f"Token: {token_id} amount: {alice_amount_to_swap}", balance)
321321

322322
await self.switch_to_wallet(wallet, 'bob_wallet')
323323
assert_in("Success", await wallet.sync())
324324
balance = await wallet.get_balance()
325-
assert_in(f"Coins amount: 150", balance)
325+
assert_in(f"Coins amount: {bob_amount_to_swap}", balance)
326326
assert_not_in("Tokens", balance)
327327

328328

test/functional/wallet_htlc_spend.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ async def async_test(self):
131131
assert_equal(await wallet.get_best_block(), block_id)
132132

133133
balance = await wallet.get_balance()
134-
assert_in(f"Coins amount: 151", balance)
134+
assert_in("Coins amount: 151", balance)
135135
assert_not_in("Tokens", balance)
136136

137137
# issue a valid token
@@ -142,7 +142,7 @@ async def async_test(self):
142142
self.generate_block()
143143
assert_in("Success", await wallet.sync())
144144
balance = await wallet.get_balance()
145-
assert_in(f"Coins amount: 50", balance)
145+
assert_in("Coins amount: 50", balance)
146146
assert_not_in("Tokens", balance)
147147

148148
amount_to_mint = random.randint(1, 10000)
@@ -152,7 +152,7 @@ async def async_test(self):
152152
self.generate_block()
153153
assert_in("Success", await wallet.sync())
154154
balance = await wallet.get_balance()
155-
assert_in(f"Coins amount: 0", balance)
155+
assert_in("Coins amount: 0", balance)
156156
assert_in(f"Token: {token_id} amount: {amount_to_mint}", balance)
157157

158158
########################################################################################
@@ -164,7 +164,7 @@ async def async_test(self):
164164
alice_amount_to_swap = amount_to_mint
165165
refund_address = await wallet.add_standalone_multisig_address(2, [alice_pub_key, bob_pub_key], None)
166166
alice_htlc_tx = await wallet.create_htlc_transaction(alice_amount_to_swap, token_id, alice_secret_hash, bob_address, refund_address, 2)
167-
output = await wallet.submit_transaction(alice_htlc_tx)
167+
output = await wallet.submit_transaction(alice_htlc_tx['tx'])
168168
alice_htlc_tx_id = output.split('\n')[2]
169169
self.generate_block()
170170
assert_in("Success", await wallet.sync())
@@ -175,7 +175,7 @@ async def async_test(self):
175175

176176
bob_amount_to_swap = 150
177177
bob_htlc_tx = await wallet.create_htlc_transaction(bob_amount_to_swap, None, alice_secret_hash, alice_address, refund_address, 2)
178-
output = await wallet.submit_transaction(bob_htlc_tx)
178+
output = await wallet.submit_transaction(bob_htlc_tx['tx'])
179179
bob_htlc_tx_id = output.split('\n')[2]
180180
self.generate_block()
181181
assert_in("Success", await wallet.sync())
@@ -189,7 +189,7 @@ async def async_test(self):
189189
random_secret_hex = random_secret.hex()
190190

191191
balance = await wallet.get_balance()
192-
assert_in(f"Coins amount: 0", balance)
192+
assert_in("Coins amount: 0", balance)
193193
assert_not_in("Tokens", balance)
194194

195195
# Alice can't spend Alice's htlc without a secret
@@ -214,7 +214,7 @@ async def async_test(self):
214214
assert_in("Success", await wallet.sync())
215215

216216
balance = await wallet.get_balance()
217-
assert_in(f"Coins amount: 0", balance)
217+
assert_in("Coins amount: 0", balance)
218218
assert_not_in("Tokens", balance)
219219

220220
# Bob can't spend it without secret
@@ -264,7 +264,7 @@ async def async_test(self):
264264
assert_in("Success", await wallet.sync())
265265

266266
balance = await wallet.get_balance()
267-
assert_in(f"Coins amount: 150", balance)
267+
assert_in(f"Coins amount: {bob_amount_to_swap}", balance)
268268
assert_not_in("Tokens", balance)
269269

270270
########################################################################################
@@ -283,7 +283,7 @@ async def async_test(self):
283283
assert_in("Success", await wallet.sync())
284284

285285
balance = await wallet.get_balance()
286-
assert_in(f"Coins amount: 0", balance)
286+
assert_in("Coins amount: 0", balance)
287287
assert_in(f"Token: {token_id} amount: {alice_amount_to_swap}", balance)
288288

289289

test/functional/wallet_sweep_address.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ def make_locked_output(pub_key_bytes):
149149
acc1_address = await wallet.new_address()
150150

151151
await wallet.select_account(0)
152-
assert_in("The transaction was submitted successfully", await wallet.sweep_addresses(acc1_address, addresses))
152+
assert_in("The transaction was submitted successfully", await wallet.sweep_addresses(acc1_address, all_addresses=True))
153153

154154
block_id = self.generate_block()
155155
assert_in("Success", await wallet.sync())
156156

157157
# check we sent all our coins
158158
balance = await wallet.get_balance()
159-
assert_in(f"Coins amount: 0", balance)
159+
assert_in("Coins amount: 0", balance)
160160
# check we still have the locked balance
161161

162162
balance = await wallet.get_balance('locked')

wallet/src/account/mod.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<K: AccountKeyChains> Account<K> {
201201
#[allow(clippy::too_many_arguments)]
202202
fn select_inputs_for_send_request(
203203
&mut self,
204-
request: SendRequest,
204+
mut request: SendRequest,
205205
input_utxos: SelectedInputs,
206206
selection_algo: Option<CoinSelectionAlgo>,
207207
change_addresses: BTreeMap<Currency, Address<Destination>>,
@@ -414,35 +414,31 @@ impl<K: AccountKeyChains> Account<K> {
414414
selection_result = selection_result.add_change(
415415
(total_fees_not_paid - new_total_fees_not_paid).unwrap_or(Amount::ZERO),
416416
)?;
417+
418+
request.add_fee(pay_fee_with_currency, new_total_fees_not_paid)?;
419+
} else {
420+
let new_total_fees_not_paid = current_fee_rate
421+
.compute_fee(total_tx_size)
422+
.map_err(|_| UtxoSelectorError::AmountArithmeticError)?
423+
.into();
424+
request.add_fee(pay_fee_with_currency, new_total_fees_not_paid)?;
417425
}
418426

419427
selected_inputs.insert(pay_fee_with_currency, selection_result);
420428

421429
// Check outputs against inputs and create change
422-
self.check_outputs_and_add_change(
423-
&pay_fee_with_currency,
424-
selected_inputs,
425-
change_addresses,
426-
db_tx,
427-
request,
428-
)
430+
self.check_outputs_and_add_change(selected_inputs, change_addresses, db_tx, request)
429431
}
430432

431433
fn check_outputs_and_add_change(
432434
&mut self,
433-
pay_fee_with_currency: &Currency,
434435
selected_inputs: BTreeMap<Currency, utxo_selector::SelectionResult>,
435436
mut change_addresses: BTreeMap<Currency, Address<Destination>>,
436437
db_tx: &mut impl WalletStorageWriteLocked,
437438
mut request: SendRequest,
438439
) -> Result<SendRequest, WalletError> {
439440
for (currency, currency_result) in selected_inputs.iter() {
440441
let change_amount = currency_result.get_change();
441-
let fees = currency_result.get_total_fees();
442-
443-
if fees > Amount::ZERO {
444-
request.add_fee(*pay_fee_with_currency, fees)?;
445-
}
446442

447443
if change_amount > Amount::ZERO {
448444
let change_address = if let Some(change_address) = change_addresses.remove(currency)
@@ -532,7 +528,7 @@ impl<K: AccountKeyChains> Account<K> {
532528
pub fn sweep_addresses(
533529
&mut self,
534530
destination: Destination,
535-
request: SendRequest,
531+
mut request: SendRequest,
536532
current_fee_rate: FeeRate,
537533
) -> WalletResult<SendRequest> {
538534
let mut grouped_inputs = group_preselected_inputs(
@@ -593,6 +589,7 @@ impl<K: AccountKeyChains> Account<K> {
593589
destination,
594590
);
595591
outputs.push(coin_output);
592+
request.add_fee(Currency::Coin, total_fee)?;
596593

597594
Ok(request.with_outputs(outputs))
598595
}

0 commit comments

Comments
 (0)