Skip to content

Commit

Permalink
feat(balancing): make build autobalance assets
Browse files Browse the repository at this point in the history
When using `transaction build`, don't perform balancing of assets (both
Lovelace and multi-assets) in the library and leave the balancing to the the CLI.

The `lovelace_balanced` flag has been removed from various functions
including `_balance_txouts`, `_get_tx_ins_outs`, and `collect_data_for_build`.
This change simplifies the code by relying on the `skip_asset_balancing`
for both Lovelace and multi-assets.
  • Loading branch information
mkoura committed Sep 4, 2024
1 parent 3ee1a5d commit 1fe158f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cardano_clusterlib/transaction_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ def build_raw_tx(
script_withdrawals=script_withdrawals,
deposit=deposit,
treasury_donation=treasury_donation,
skip_asset_balancing=False,
)

if (
Expand Down Expand Up @@ -829,7 +830,7 @@ def build_tx( # noqa: C901
calc_script_cost_file: tp.Optional[itp.FileType] = None,
join_txouts: bool = True,
destination_dir: itp.FileType = ".",
skip_asset_balancing: bool = False,
skip_asset_balancing: bool = True,
) -> structs.TxRawOutput:
"""Build a transaction.
Expand Down Expand Up @@ -923,7 +924,6 @@ def build_tx( # noqa: C901
script_withdrawals=script_withdrawals,
deposit=deposit,
treasury_donation=treasury_donation,
lovelace_balanced=True,
skip_asset_balancing=skip_asset_balancing,
)

Expand Down
11 changes: 1 addition & 10 deletions cardano_clusterlib/txtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ def _balance_txouts( # noqa: C901
withdrawals: structs.OptionalTxOuts,
deposit: int = 0,
treasury_donation: int = 0,
lovelace_balanced: bool = False,
skip_asset_balancing: bool = False,
) -> tp.List[structs.TxOut]:
"""Balance the transaction by adding change output for each coin."""
Expand Down Expand Up @@ -185,7 +184,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 skip_asset_balancing or (coin == consts.DEFAULT_COIN and lovelace_balanced):
if skip_asset_balancing:
# balancing is done elsewhere (by the `transaction build` command)
pass
elif coin == consts.DEFAULT_COIN:
Expand Down Expand Up @@ -497,7 +496,6 @@ def _get_tx_ins_outs(
treasury_donation: tp.Optional[int] = None,
withdrawals: structs.OptionalTxOuts = (),
mint_txouts: structs.OptionalTxOuts = (),
lovelace_balanced: bool = False,
skip_asset_balancing: bool = False,
) -> tp.Tuple[tp.List[structs.UTXOData], tp.List[structs.TxOut]]:
"""Return list of transaction's inputs and outputs.
Expand All @@ -513,8 +511,6 @@ def _get_tx_ins_outs(
treasury_donation: A donation to the treasury to perform (optional).
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).
skip_asset_balancing: A bool indicating if assets balancing should be skipped
(`build` command balance the assets automatically in newer versions).
Expand Down Expand Up @@ -603,7 +599,6 @@ def _get_tx_ins_outs(
withdrawals=withdrawals,
deposit=tx_deposit,
treasury_donation=tx_treasury_donation,
lovelace_balanced=lovelace_balanced,
skip_asset_balancing=skip_asset_balancing,
)

Expand All @@ -625,7 +620,6 @@ def collect_data_for_build(
script_withdrawals: structs.OptionalScriptWithdrawals = (),
deposit: tp.Optional[int] = None,
treasury_donation: tp.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 @@ -649,8 +643,6 @@ def collect_data_for_build(
data (optional).
deposit: A deposit amount needed by the transaction (optional).
treasury_donation: A donation to the treasury to perform (optional).
lovelace_balanced: A bool indicating whether Lovelace ins/outs are balanced
(by `build` command).
skip_asset_balancing: A bool indicating if assets balancing should be skipped
(`build` command balance the assets automatically in newer versions).
Expand Down Expand Up @@ -701,7 +693,6 @@ def collect_data_for_build(
treasury_donation=treasury_donation,
withdrawals=withdrawals_txouts,
mint_txouts=mint_txouts,
lovelace_balanced=lovelace_balanced,
skip_asset_balancing=skip_asset_balancing,
)

Expand Down

0 comments on commit 1fe158f

Please sign in to comment.