Skip to content

Commit 8fbb0df

Browse files
committed
fix(tx): handle fee calculation in transaction build loop
- Update transaction build logic to handle fee calculation iteratively - Ensure no change UTxO is created for source address - Adjust assertions to verify correct balances for source and destination
1 parent 4e14809 commit 8fbb0df

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

cardano_node_tests/tests/test_tx_basic.py

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,9 @@ def test_build_no_change(
261261
262262
Uses `cardano-cli transaction build` command for building the transactions.
263263
264-
* try to build a Tx that sends all available funds, and extract fee amount
264+
* try to build a Tx that transfers all available funds, and extract fee amount
265265
from the error message
266-
* send all available funds minus fee from source address to destination address
266+
* transfer all available funds minus fee from source address to destination address
267267
* check that no change UTxO was created
268268
* (optional) check transactions in db-sync
269269
"""
@@ -279,32 +279,33 @@ def test_build_no_change(
279279
src_init_balance = cluster.g_query.get_address_balance(src_address)
280280

281281
tx_files = clusterlib.TxFiles(signing_key_files=[src_addr.skey_file])
282-
txouts_init = [clusterlib.TxOut(address=dst_address, amount=src_init_balance)]
283282

284-
with pytest.raises(clusterlib.CLIError) as excinfo:
285-
cluster.g_transaction.build_tx(
286-
src_address=src_address,
287-
tx_name=temp_template,
288-
tx_files=tx_files,
289-
txouts=txouts_init,
290-
)
291-
str_exc = str(excinfo.value)
292-
fee_match_old = re.search(r"negative: Lovelace \(-([0-9]*)\) lovelace", str_exc)
293-
fee_match_new = re.search(r"negative: -([0-9]*) Lovelace", str_exc) # cardano-node 8.10.0
294-
fee_match = fee_match_new or fee_match_old
295-
assert fee_match, f"The expected error message was not found: {str_exc}"
296-
297-
fee = int(fee_match.group(1))
298-
amount = src_init_balance - fee
299-
txouts = [clusterlib.TxOut(address=dst_address, amount=amount)]
283+
fee = 150000 # Initial fee value
284+
for i in range(5):
285+
txouts = [clusterlib.TxOut(address=dst_address, amount=src_init_balance - fee)]
286+
287+
try:
288+
tx_output = cluster.g_transaction.build_tx(
289+
src_address=src_address,
290+
tx_name=f"{temp_template}_{i}",
291+
tx_files=tx_files,
292+
txouts=txouts,
293+
change_address=src_address,
294+
)
295+
except clusterlib.CLIError as exc:
296+
str_exc = str(exc)
297+
if "negative" not in str_exc:
298+
raise
299+
300+
fee_match_old = re.search(r"negative: Lovelace \(-([0-9]*)\) lovelace", str_exc)
301+
# cardano-node 8.10.0+
302+
fee_match_new = re.search(r"negative: -([0-9]*) Lovelace", str_exc)
303+
fee_match = fee_match_new or fee_match_old
304+
assert fee_match, f"The expected error message was not found: {str_exc}"
305+
fee = fee + int(fee_match.group(1))
306+
else:
307+
break
300308

301-
tx_output = cluster.g_transaction.build_tx(
302-
src_address=src_address,
303-
tx_name=temp_template,
304-
tx_files=tx_files,
305-
txouts=txouts,
306-
change_address=src_address,
307-
)
308309
tx_signed = cluster.g_transaction.sign_tx(
309310
tx_body_file=tx_output.out_file,
310311
signing_key_files=tx_files.signing_key_files,
@@ -322,11 +323,12 @@ def test_build_no_change(
322323

323324
out_utxos = cluster.g_query.get_utxo(tx_raw_output=tx_output)
324325
assert not clusterlib.filter_utxos(utxos=out_utxos, address=src_address), (
325-
f"Incorrect balance for source address `{src_address}`"
326-
)
327-
assert clusterlib.filter_utxos(utxos=out_utxos, address=dst_address)[0].amount == amount, (
328-
f"Incorrect balance for destination address `{dst_address}`"
326+
f"Unexpected change UTxO created on source address `{src_address}`"
329327
)
328+
assert (
329+
clusterlib.filter_utxos(utxos=out_utxos, address=dst_address)[0].amount
330+
== src_init_balance - fee
331+
), f"Incorrect balance for destination address `{dst_address}`"
330332

331333
common.check_missing_utxos(cluster_obj=cluster, utxos=out_utxos)
332334

0 commit comments

Comments
 (0)