Transaction Balancing

cardano_error_t cardano_balance_transaction(cardano_transaction_t *unbalanced_tx, size_t foreign_signature_count, cardano_protocol_parameters_t *protocol_params, cardano_utxo_list_t *reference_inputs, cardano_utxo_list_t *pre_selected_utxo, cardano_input_to_redeemer_map_t *input_to_redeemer_map, cardano_utxo_list_t *available_utxo, cardano_coin_selector_t *coin_selector, cardano_address_t *change_address, cardano_utxo_list_t *available_collateral_utxo, cardano_address_t *collateral_change_address, cardano_tx_evaluator_t *evaluator, cardano_deferred_redeemer_list_t *deferred_redeemers)

Balances a Cardano transaction by adding necessary inputs, calculating change, and adjusting collateral.

This function balances an unbalanced transaction (unbalanced_tx) by:

  • Adding additional inputs if the transaction does not meet the required balance.

  • Computing the cost of script execution.

  • Calculating the change output to ensure the transaction has the correct total ADA and assets.

  • Adding collateral inputs if a redeemer exists in the transaction or in any of the sub transactions it carries.

When the transaction carries CIP-118 sub transactions the ledger checks value conservation over the whole batch, so the net imbalance of the sub transactions (see cardano_compute_sub_transaction_imbalance) is part of the value that coin selection must cover: a net deficit is funded by additional top level inputs and a net surplus ends up in the change outputs, and the result is verified with the whole batch check of cardano_is_transaction_balanced. A pool registered by several bodies of the batch pays the pool deposit once, as in cardano_compute_transaction_batch_imbalance.

The resolved UTXOs of the sub transactions, the ones they spend and optionally the ones they reference, are given in available_utxo, next to the UTXOs the top level transaction may spend. The role of each UTXO is taken from the bodies of the sub transactions: a UTXO that a sub transaction spends or references is never selected as a top level input. An input of a sub transaction is looked up in available_utxo first, then in reference_inputs and then in pre_selected_utxo, since a UTXO may be shared between the top level transaction and a sub transaction. Every input spent by a sub transaction must be resolved, while the reference inputs of the sub transactions that are not resolved are skipped.

A top level transaction that needs a PlutusV1, PlutusV2 or PlutusV3 script must also conserve value by itself, with the sub transactions removed. As the ledger does, the decision follows the scripts that run, not the scripts that are present: a script is needed when the transaction spends an input it locks, mints under its policy, withdraws from a reward account it controls, carries a certificate it must authorize, votes with it as the voter, carries a parameter change or treasury withdrawals proposal it guards as the guardrails script or lists it among its guards, and its language is taken from whatever source provides it, pooled over the whole batch: the witness sets of the transaction and of its sub transactions, or a reference script of a resolved reference input, of a pre selected input or of an input chosen by coin selection of the transaction, or of an input or a reference input of a sub transaction. A reference input of a sub transaction that none of available_utxo, reference_inputs and pre_selected_utxo resolves is not seen. A script that is only present and that the transaction does not need never triggers this rule, and a needed native or PlutusV4 script does not either. The check is made on every balancing iteration, with the inputs of that iteration. When the rule applies the sub transactions must balance between themselves, otherwise the function fails with CARDANO_ERROR_UNBALANCED_SUB_TRANSACTIONS and the fix is to add a balancing sub transaction, since top level change can not absorb the imbalance. In that mode the witness datums of the top level transaction are also checked with the sub transactions removed: each of them must be needed by an input the transaction spends, pre selected or coin selected, that is locked by a Plutus script among the pooled scripts, or match the datum hash of one of its outputs or of the output behind one of reference_inputs, otherwise the function fails with CARDANO_ERROR_NOT_ALLOWED_SUPPLEMENTAL_DATUMS.

The fee always includes the reference scripts the ledger charges for, whatever their language and whether or not the transaction has redeemers: every reference script found on the UTXOs of reference_inputs and on the UTXOs the transaction spends, pre selected or coin selected. A UTXO that is both referenced and spent is priced once, while the same script sitting on two different UTXOs is priced twice. The size of each script is the one reported by cardano_get_serialized_script_size, which is the size the ledger prices.

The top level transaction also pays the fee and posts the collateral of the whole batch. The fee covers the size of the sub transactions, the execution units of the redeemers of every sub transaction and the reference scripts of the sub transactions: the ones on the UTXOs each sub transaction spends and the ones on its resolved reference inputs. A UTXO that a sub transaction both references and spends is priced once, while a UTXO referenced by several sub transactions is priced once per sub transaction, as the ledger counts it when it prices the reference scripts of a batch. Collateral is added when a redeemer exists in the transaction or in any of its sub transactions, and it is sized from the fee of the top level transaction. The execution units the redeemers of the sub transactions declare are taken as final, since they are signed by the party that built each sub transaction. When the top level transaction carries redeemers, the resolved UTXOs of the sub transactions are handed to the transaction evaluator together with the ones of the top level transaction, so that it can run the scripts of every level, as the ledger does; a sub transaction script that fails then makes balancing fail with the error of the evaluator.

Usage Example:

cardano_transaction_t* tx = ...;                               // Unbalanced transaction
size_t foreign_signature_count = 2;                            // Example number of signatures
cardano_protocol_parameters_t* params = ...;                   // Protocol parameters
cardano_utxo_list_t* ref_inputs = ...;                         // Resolved reference inputs
cardano_utxo_list_t* preselected = ...;                        // Pre-selected UTXOs
cardano_input_to_redeemer_map_t* input_to_redeemer_map = ...;  // Input to redeemer map
cardano_utxo_list_t* available = ...;                          // Available UTXOs, including the UTXOs of the sub transactions
cardano_coin_selector_t* selector = ...;                       // Coin selector instance
cardano_address_t* change_addr = ...;                          // Change address
cardano_utxo_list_t* collateral_utxo = ...;                    // Available collateral UTXOs
cardano_address_t* collateral_change_addr = ...;               // Collateral change address
cardano_tx_evaluator_t* eval = ...;                            // Evaluator instance

cardano_error_t result = cardano_balance_transaction(tx, foreign_signature_count, params, ref_inputs, preselected, input_to_redeemer_map, available, selector, change_addr, collateral_utxo, collateral_change_addr, eval, NULL);

if (result == CARDANO_SUCCESS)
{
  // Transaction was balanced successfully
}

Note

This function assumes that the unbalanced_tx is a valid but incomplete transaction, missing necessary inputs to meet the target balance. After calling this function, the unbalanced_tx will be updated with additional inputs, collateral, and, if necessary, a change output.

Parameters:
cardano_transaction_t *unbalanced_tx

[inout] A pointer to the transaction that needs balancing.

size_t foreign_signature_count

[in] The number of expected extra signatures, not specified in the transaction.

cardano_protocol_parameters_t *protocol_params

[in] A pointer to the protocol parameters required for fee calculation and balancing.

cardano_utxo_list_t *reference_inputs

[in] A list of resolved reference inputs of the top level transaction that have already been included in the transaction. The reference scripts they carry are priced in the fee. An input of a sub transaction that is not found in available_utxo is also looked up in this list.

cardano_utxo_list_t *pre_selected_utxo

[in] A list of UTXOs that must be included in the transaction inputs. They must be disjoint from the inputs spent by the sub transactions the transaction carries. An overlap is rejected. The reference scripts they carry are priced in the fee. A reference input of a sub transaction that is not found in available_utxo or in reference_inputs is also looked up in this list.

cardano_input_to_redeemer_map_t *input_to_redeemer_map

[in] A map of inputs to redeemers. This map associates specific references of inputs to redeemers in the witness set. Balancing the transaction can add additional inputs and this can make inputs change positions in the input set. Redeemers must be updated to point to the correct input. If you provide redeemers for any pre-selected input, you must specify this association in this map.

cardano_utxo_list_t *available_utxo

[in] A list of available UTXOs to select from, if additional inputs are needed. The reference scripts carried by the ones that end up selected are priced in the fee. When the transaction carries sub transactions, the UTXOs they spend must be resolvable from this list or from reference_inputs, and supplying them in this list is the recommended way. This list may also hold the UTXOs they reference. The UTXOs a sub transaction spends or references are never selected, the value of the spent ones takes part in the value conservation of the batch and the reference scripts they carry are priced in the fee. The scripts of the UTXOs used only by sub transactions take no part in script evaluation or in the validation mode of the top level transaction.

cardano_coin_selector_t *coin_selector

[in] A pointer to the coin selector used for choosing appropriate UTXOs.

cardano_address_t *change_address

[in] The address where any remaining balance (change) will be sent.

cardano_utxo_list_t *available_collateral_utxo

[in] A list of available UTXOs to select from as collateral if a redeemer exists in the transaction or in any of its sub transactions. Collateral is required exactly when such a redeemer exists. This list can be NULL or empty only when collateral is not required, otherwise the function fails with CARDANO_ERROR_BALANCE_INSUFFICIENT. The UTXOs a sub transaction spends or references are never used as collateral, even when the batcher is also a party of the batch and offers them here. When every UTXO of this list is used by a sub transaction and collateral is required, the function also fails with CARDANO_ERROR_BALANCE_INSUFFICIENT.

cardano_address_t *collateral_change_address

[in] The address where any remaining collateral change will be sent, if applicable.

cardano_tx_evaluator_t *evaluator

[in] A transaction evaluator instance for determining the execution cost of scripts.

cardano_deferred_redeemer_list_t *deferred_redeemers

[in] An optional list of deferred redeemers to resolve on every balancing iteration, once the canonical input order, change outputs and fee of the draft transaction are known. Each entry’s callback produces the redeemer payload from the balanced draft. Can be NULL if the transaction has no deferred redeemers.

Returns:

CARDANO_SUCCESS if the transaction was balanced successfully, CARDANO_ERROR_ELEMENT_NOT_FOUND if an input spent by a sub transaction is not resolved, CARDANO_ERROR_DUPLICATED_KEY if a pre selected UTXO is also spent by a sub transaction, CARDANO_ERROR_UNBALANCED_SUB_TRANSACTIONS if the top level transaction needs a PlutusV1, PlutusV2 or PlutusV3 script and its sub transactions do not balance between themselves, CARDANO_ERROR_NOT_ALLOWED_SUPPLEMENTAL_DATUMS if the top level transaction needs a PlutusV1, PlutusV2 or PlutusV3 script and carries a witness datum that the ledger does not allow once its sub transactions are removed, CARDANO_ERROR_BALANCE_INSUFFICIENT if collateral is required and no collateral UTXO is available, CARDANO_ERROR_INTEGER_OVERFLOW if the withdrawals, deposits or reclaimed deposits add up to more than UINT64_MAX or the direct deposits, the produced coin or the consumed coin exceed INT64_MAX, or an appropriate error code indicating the type of failure.


cardano_error_t cardano_is_transaction_balanced(cardano_transaction_t *tx, cardano_utxo_list_t *resolved_inputs, cardano_protocol_parameters_t *protocol_params, bool *is_balanced)

Checks whether a Cardano transaction is balanced.

This function verifies if the specified transaction (tx) meets the balance requirements as per Cardano protocol rules. It considers the total inputs, outputs, fees, and execution costs to determine if the transaction is balanced.

Value conservation covers the whole CIP-118 batch: when the transaction carries sub transactions, the value they consume and produce is accounted together with the top level body, so the transaction is balanced when its cardano_compute_transaction_batch_imbalance is zero. A transaction without sub transactions is balanced when its own body is.

Usage Example:

cardano_transaction_t* tx = ...;                // Transaction to check
cardano_utxo_list_t* resolved_inputs = ...;     // List of resolved input UTXOs
cardano_protocol_parameters_t* params = ...;    // Protocol parameters
bool is_balanced = false;

cardano_error_t result = cardano_is_transaction_balanced(tx, resolved_inputs, params, &is_balanced);

if (result == CARDANO_SUCCESS && is_balanced)
{
  // Transaction is balanced
}
else
{
  // Transaction is not balanced, or an error occurred
}

Note

This function provides a quick way to verify that a transaction includes enough inputs to cover its outputs and all associated fees. It does not modify the transaction but provides a binary check on its balance state.

Parameters:
cardano_transaction_t *tx

[in] A pointer to the transaction to be checked.

cardano_utxo_list_t *resolved_inputs

[in] A list of UTXOs that have been selected and are expected to cover the transaction’s outputs and fees. It must resolve the inputs of the transaction and of every sub transaction it carries.

cardano_protocol_parameters_t *protocol_params

[in] Protocol parameters needed for fee calculation, including min-fee coefficients and other constraints.

bool *is_balanced

[out] A pointer to a boolean that will hold the result. Set to true if the transaction is balanced, or false otherwise.

Returns:

CARDANO_SUCCESS if the balance check was performed successfully, CARDANO_ERROR_INTEGER_OVERFLOW if the withdrawals, deposits or reclaimed deposits of the transaction or of one of its sub transactions add up to more than UINT64_MAX or their direct deposits, produced coin or consumed coin exceed INT64_MAX, or an appropriate error code indicating the type of failure.


cardano_error_t cardano_compute_transaction_imbalance(cardano_transaction_t *tx, cardano_utxo_list_t *resolved_inputs, cardano_protocol_parameters_t *protocol_params, cardano_value_t **imbalance)

Computes the imbalance of a Cardano transaction.

The imbalance is the value the transaction consumes minus the value it produces. Consumed value is the sum of the resolved input values, the reward withdrawals, the deposit refunds and the minted assets; produced value is the sum of the outputs, the fee, the deposits, the treasury donation, the direct deposits and the burned assets. The deposits and deposit refunds are the ones cardano_compute_implicit_coin reports for the body.

The imbalance covers the top level body only and ignores the sub transactions the transaction carries (body key 23). The figure the ledger checks for a CIP-118 batch is cardano_compute_transaction_batch_imbalance, which is also what cardano_is_transaction_balanced uses.

For a transaction without sub transactions, a zero imbalance means the transaction is balanced. A positive coin or asset amount means the transaction consumes more than it produces (a surplus that a change output or another transaction in a CIP-118 batch must absorb), while a negative amount means it produces more than it consumes (a deficit that other transactions in the batch must fund).

The returned value is independent of the transaction and of the resolved inputs: it owns its multi asset, so it may be modified freely (for example with cardano_value_add_asset) without altering the mint field of the body or the value of any resolved UTXO.

Usage Example:

cardano_transaction_t* tx = ...;                // Transaction to inspect
cardano_utxo_list_t* resolved_inputs = ...;     // List of resolved input UTXOs
cardano_protocol_parameters_t* params = ...;    // Protocol parameters
cardano_value_t* imbalance = NULL;

cardano_error_t result = cardano_compute_transaction_imbalance(tx, resolved_inputs, params, &imbalance);

if (result == CARDANO_SUCCESS)
{
  if (cardano_value_get_coin(imbalance) > 0)
  {
    // The transaction consumes more lovelace than it produces
  }

  cardano_value_unref(&imbalance);
}

Parameters:
cardano_transaction_t *tx

[in] A pointer to the transaction whose imbalance is computed.

cardano_utxo_list_t *resolved_inputs

[in] A list of UTXOs resolving every input of the transaction.

cardano_protocol_parameters_t *protocol_params

[in] Protocol parameters supplying the deposit amounts of the Shelley era certificates.

cardano_value_t **imbalance

[out] On success, a pointer to a newly created cardano_value_t holding the imbalance. The caller must release it with cardano_value_unref when it is no longer needed.

Returns:

CARDANO_SUCCESS if the imbalance was computed, CARDANO_ERROR_ELEMENT_NOT_FOUND if an input has no resolved UTXO, CARDANO_ERROR_INTEGER_OVERFLOW if the withdrawals, deposits or reclaimed deposits add up to more than UINT64_MAX or the direct deposits, the produced coin or the consumed coin exceed INT64_MAX, or an appropriate error code indicating the type of failure.


cardano_error_t cardano_compute_sub_transaction_imbalance(cardano_sub_transaction_t *sub_tx, cardano_utxo_list_t *resolved_inputs, cardano_protocol_parameters_t *protocol_params, cardano_value_t **imbalance)

Computes the imbalance of a CIP-118 sub transaction.

A sub transaction is an intent: it is deliberately unbalanced and its imbalance is what a batcher matches against the other transactions of the batch. The imbalance is the value the sub transaction consumes minus the value it produces, with the same terms and the same sign convention as cardano_compute_transaction_imbalance, except that a sub transaction body carries no fee.

The returned value is independent of the sub transaction and of the resolved inputs: it owns its multi asset, so a batcher may modify it freely (for example with cardano_value_add_asset) without altering the mint field of the sub transaction body or the value of any resolved UTXO.

Usage Example:

cardano_sub_transaction_t* sub_tx = ...;        // Sub transaction to inspect
cardano_utxo_list_t* resolved_inputs = ...;     // List of resolved input UTXOs
cardano_protocol_parameters_t* params = ...;    // Protocol parameters
cardano_value_t* imbalance = NULL;

cardano_error_t result = cardano_compute_sub_transaction_imbalance(sub_tx, resolved_inputs, params, &imbalance);

if (result == CARDANO_SUCCESS)
{
  // Match the imbalance against the rest of the batch
  cardano_value_unref(&imbalance);
}

Parameters:
cardano_sub_transaction_t *sub_tx

[in] A pointer to the sub transaction whose imbalance is computed.

cardano_utxo_list_t *resolved_inputs

[in] A list of UTXOs resolving every input of the sub transaction.

cardano_protocol_parameters_t *protocol_params

[in] Protocol parameters supplying the deposit amounts of the Shelley era certificates.

cardano_value_t **imbalance

[out] On success, a pointer to a newly created cardano_value_t holding the imbalance. The caller must release it with cardano_value_unref when it is no longer needed.

Returns:

CARDANO_SUCCESS if the imbalance was computed, CARDANO_ERROR_ELEMENT_NOT_FOUND if an input has no resolved UTXO, CARDANO_ERROR_INTEGER_OVERFLOW if the withdrawals, deposits or reclaimed deposits add up to more than UINT64_MAX or the direct deposits, the produced coin or the consumed coin exceed INT64_MAX, or an appropriate error code indicating the type of failure.


cardano_error_t cardano_compute_transaction_batch_imbalance(cardano_transaction_t *tx, cardano_utxo_list_t *resolved_inputs, cardano_protocol_parameters_t *protocol_params, cardano_value_t **imbalance)

Computes the imbalance of a whole CIP-118 batch.

The ledger checks value conservation over the whole batch: the value consumed and produced by the top level transaction body and by every sub transaction it carries is summed in a single check. This function returns the imbalance of the top level body (see cardano_compute_transaction_imbalance) plus the imbalance of each sub transaction (see cardano_compute_sub_transaction_imbalance), coin and multi assets included, with the same sign convention: a positive amount is value the batch still has left over, a negative amount is value the batch still needs, and a zero imbalance means the batch is balanced.

The figure is not a plain sum of the imbalances of the bodies for pool deposits: as the ledger does, a pool pays the pool deposit once across the whole batch, even when the top level body and its sub transactions, or several sub transactions, register it, so the pool deposit is given back once for every additional body that registers the same pool. A pool that is already registered on chain is still charged, as in cardano_compute_implicit_coin.

For a transaction that carries no sub transactions the result equals cardano_compute_transaction_imbalance.

The returned value is independent of the transaction, of its sub transactions and of the resolved inputs: it may be modified freely (for example with cardano_value_add_asset) without altering any of them.

Usage Example:

cardano_transaction_t* tx = ...;                // Top level transaction carrying the sub transactions
cardano_utxo_list_t* resolved_inputs = ...;     // Resolved input UTXOs of the top level body and the sub transactions
cardano_protocol_parameters_t* params = ...;    // Protocol parameters
cardano_value_t* imbalance = NULL;

cardano_error_t result = cardano_compute_transaction_batch_imbalance(tx, resolved_inputs, params, &imbalance);

if (result == CARDANO_SUCCESS)
{
  if (cardano_value_get_coin(imbalance) < 0)
  {
    // The batch still needs lovelace that the top level transaction must fund
  }

  cardano_value_unref(&imbalance);
}

Parameters:
cardano_transaction_t *tx

[in] A pointer to the top level transaction whose batch imbalance is computed.

cardano_utxo_list_t *resolved_inputs

[in] A list of UTXOs resolving every input of the top level transaction body and of every sub transaction it carries.

cardano_protocol_parameters_t *protocol_params

[in] Protocol parameters supplying the deposit amounts of the Shelley era certificates.

cardano_value_t **imbalance

[out] On success, a pointer to a newly created cardano_value_t holding the imbalance. The caller must release it with cardano_value_unref when it is no longer needed.

Returns:

CARDANO_SUCCESS if the imbalance was computed, CARDANO_ERROR_ELEMENT_NOT_FOUND if an input of the top level body or of a sub transaction has no resolved UTXO, CARDANO_ERROR_INTEGER_OVERFLOW if the withdrawals, deposits or reclaimed deposits of the top level body or of a sub transaction add up to more than UINT64_MAX or their direct deposits, produced coin or consumed coin exceed INT64_MAX, or an appropriate error code indicating the type of failure.