Skip to content

Commit

Permalink
Merge pull request #146 from saratomaz/avoid_autobalance_on_build_com…
Browse files Browse the repository at this point in the history
…mand

Avoid autobalance on build command
  • Loading branch information
mkoura committed Apr 4, 2023
2 parents a0bf866 + 6296c16 commit 4f93fea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 4 additions & 0 deletions cardano_clusterlib/transaction_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ def build_tx( # noqa: C901
calc_script_cost_file: Optional[FileType] = None,
join_txouts: bool = True,
destination_dir: FileType = ".",
skip_asset_balancing: bool = False,
) -> structs.TxRawOutput:
"""Build a transaction.
Expand Down Expand Up @@ -721,6 +722,8 @@ def build_tx( # noqa: C901
join_txouts: A bool indicating whether to aggregate transaction outputs
by payment address (True by default).
destination_dir: A path to directory for storing artifacts (optional).
skip_asset_balancing: A bool indicating if assets balancing should be skipped
(`build` command balance the assets automatically in newer versions).
Returns:
structs.TxRawOutput: A tuple with transaction output details.
Expand Down Expand Up @@ -760,6 +763,7 @@ def build_tx( # noqa: C901
script_withdrawals=script_withdrawals,
deposit=deposit,
lovelace_balanced=True,
skip_asset_balancing=skip_asset_balancing,
)

required_signer_hashes = required_signer_hashes or []
Expand Down
17 changes: 14 additions & 3 deletions cardano_clusterlib/txtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def _balance_txouts( # noqa: C901
withdrawals: structs.OptionalTxOuts,
deposit: int = 0,
lovelace_balanced: bool = False,
skip_asset_balancing: bool = False,
) -> List[structs.TxOut]:
"""Balance the transaction by adding change output for each coin."""
# records for burning tokens, i.e. records with negative amount, are not allowed in `txouts`
Expand Down Expand Up @@ -180,7 +181,7 @@ def _balance_txouts( # noqa: C901
total_input_amount = functools.reduce(lambda x, y: x + y.amount, coin_txins, 0)
total_output_amount = functools.reduce(lambda x, y: x + y.amount, coin_txouts, 0)

if coin == consts.DEFAULT_COIN and lovelace_balanced:
if skip_asset_balancing or (coin == consts.DEFAULT_COIN and lovelace_balanced):
# balancing is done elsewhere (by the `transaction build` command)
pass
elif coin == consts.DEFAULT_COIN:
Expand Down Expand Up @@ -469,6 +470,7 @@ def _get_tx_ins_outs(
withdrawals: structs.OptionalTxOuts = (),
mint_txouts: structs.OptionalTxOuts = (),
lovelace_balanced: bool = False,
skip_asset_balancing: bool = False,
) -> Tuple[List[structs.UTXOData], List[structs.TxOut]]:
"""Return list of transaction's inputs and outputs.
Expand All @@ -483,12 +485,16 @@ def _get_tx_ins_outs(
withdrawals: A list (iterable) of `TxOuts`, specifying reward withdrawals (optional).
mint_txouts: A list (iterable) of `TxOuts`, specifying minted tokens (optional).
lovelace_balanced: A bool indicating whether Lovelace ins/outs are balanced
(by `build` command; optional)
(by `build` command).
skip_asset_balancing: A bool indicating if assets balancing should be skipped
(`build` command balance the assets automatically in newer versions).
Returns:
Tuple[list, list]: A tuple of list of transaction inputs and list of transaction
outputs.
"""
# pylint: disable=too-many-arguments

txouts_passed_db: Dict[str, List[structs.TxOut]] = _organize_tx_ins_outs_by_coin(txouts)
txouts_mint_db: Dict[str, List[structs.TxOut]] = _organize_tx_ins_outs_by_coin(mint_txouts)
outcoins_all = {consts.DEFAULT_COIN, *txouts_mint_db.keys(), *txouts_passed_db.keys()}
Expand Down Expand Up @@ -559,6 +565,7 @@ def _get_tx_ins_outs(
withdrawals=withdrawals,
deposit=tx_deposit,
lovelace_balanced=lovelace_balanced,
skip_asset_balancing=skip_asset_balancing,
)

return txins_filtered, txouts_balanced
Expand All @@ -578,6 +585,7 @@ def collect_data_for_build(
script_withdrawals: structs.OptionalScriptWithdrawals = (),
deposit: Optional[int] = None,
lovelace_balanced: bool = False,
skip_asset_balancing: bool = False,
) -> structs.DataForBuild:
"""Collect data (txins, txouts, withdrawals) needed for building a transaction.
Expand All @@ -598,7 +606,9 @@ def collect_data_for_build(
data (optional).
deposit: A deposit amount needed by the transaction (optional).
lovelace_balanced: A bool indicating whether Lovelace ins/outs are balanced
(by `build` command; optional)
(by `build` command).
skip_asset_balancing: A bool indicating if assets balancing should be skipped
(`build` command balance the assets automatically in newer versions).
Returns:
structs.DataForBuild: A tuple with data for build(-raw) commands.
Expand Down Expand Up @@ -641,6 +651,7 @@ def collect_data_for_build(
withdrawals=withdrawals_txouts,
mint_txouts=mint_txouts,
lovelace_balanced=lovelace_balanced,
skip_asset_balancing=skip_asset_balancing,
)

payment_txins = txins or txins_copy
Expand Down

0 comments on commit 4f93fea

Please sign in to comment.