packages feed

cardano-c-clib-0.1.1.0: vendor/include/cardano/transaction_builder/fee.h

/**
 * \file fee.h
 *
 * \author angel.castillo
 * \date   Oct 13, 2024
 *
 * Copyright 2024 Biglup Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef BIGLUP_LABS_INCLUDE_CARDANO_FEE_H
#define BIGLUP_LABS_INCLUDE_CARDANO_FEE_H

/* INCLUDES ******************************************************************/

#include <cardano/common/utxo.h>
#include <cardano/common/utxo_list.h>
#include <cardano/error.h>
#include <cardano/protocol_params/protocol_parameters.h>
#include <cardano/transaction/transaction.h>
#include <cardano/witness_set/redeemer.h>

#include <cardano/typedefs.h>

/* DECLARATIONS **************************************************************/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * \brief Computes the minimum transaction fee.
 *
 * This function computes the minimum required transaction fee for the provided transaction, based on
 * the resolved inputs and protocol parameters. The calculated fee considers factors such as the transaction
 * size, execution units consumed by plutus scripts and size of included reference scripts.
 *
 * Reference scripts are always priced, whatever their language and whether or not the transaction has redeemers: the
 * ledger charges for every reference script found on the outputs behind the reference inputs and the spend inputs of a
 * transaction, even if the script never runs. \p resolved_ref_inputs must therefore resolve both kinds of inputs.
 * They are priced with the reference script cost per byte, stride and multiplier of \p protocol_params, as
 * \ref cardano_compute_script_ref_fee_with_params documents: a stride or a multiplier that is not set, as in the
 * parameters of the eras before Dijkstra, takes its Conway value.
 *
 * A transaction that carries sub transactions pays the fee of the whole batch. The size of the sub transactions is part
 * of the size of the transaction, and the execution units of the redeemers of every sub transaction are added to the ones
 * of the transaction. The reference scripts of the sub transactions are included if the resolved UTXOs of their reference
 * inputs and spend inputs are given in \p resolved_ref_inputs, as the ledger prices the reference scripts of the whole
 * batch.
 *
 * The fee is the one the ledger requires: the size part is computed as \ref cardano_compute_min_fee_without_scripts
 * documents, without the is valid flag, and the script part as \ref cardano_compute_min_script_fee documents, in exact
 * arithmetic. The witness set is sized as it is, so to get the fee of the signed transaction, pass the transaction with
 * its signatures.
 *
 * \param[in] transaction The pointer to the \ref cardano_transaction_t object representing the transaction.
 * \param[in] resolved_ref_inputs A pointer to the \ref cardano_utxo_list_t containing every resolved UTXO whose reference script is priced: the ones behind
 *                                the reference inputs and the spend inputs of the transaction and of its sub transactions. Every entry is priced, so a UTXO
 *                                that a body both references and spends must be listed once, and a UTXO used by several bodies once per body.
 * \param[in] protocol_params The pointer to the \ref cardano_protocol_parameters_t structure that contains protocol-related parameters for fee calculation.
 * \param[out] fee A pointer to a uint64_t that will hold the computed transaction fee upon success.
 *
 * \return \ref CARDANO_SUCCESS if the fee was successfully computed, \ref CARDANO_ERROR_INVALID_ARGUMENT if the transaction
 *         has execution units to price and the price of the memory units or of the CPU steps has a zero denominator,
 *         \ref CARDANO_ERROR_INTEGER_OVERFLOW if the fee does not fit in 64 bits, or an appropriate error code indicating
 *         failure.
 *
 * \note The transaction size and the execution unit requirements for Plutus scripts influence the final fee.
 *       Ensure that the resolved UTXOs match the reference inputs and the spend inputs specified in the transaction to compute accurate fees.
 *
 * Usage Example:
 * \code{.c}
 * cardano_transaction_t* tx = ...;
 * cardano_utxo_list_t* resolved_ref_inputs = ...;
 * cardano_protocol_parameters_t* protocol_params = ...;
 * uint64_t computed_fee = 0;
 *
 * cardano_error_t result = cardano_compute_transaction_fee(
 *   tx,
 *   resolved_ref_inputs,
 *   protocol_params,
 *   &computed_fee);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // Successfully computed the fee
 *   // computed_fee contains the result
 * }
 * else
 * {
 *   // Handle error
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t cardano_compute_transaction_fee(
  cardano_transaction_t*         transaction,
  cardano_utxo_list_t*           resolved_ref_inputs,
  cardano_protocol_parameters_t* protocol_params,
  uint64_t*                      fee);

/**
 * \brief Computes the minimum ADA required for a transaction output.
 *
 * This function calculates the minimum amount of ADA (in lovelace) that must be present in a UTXO, based on its size
 * and the `coins_per_utxo_byte` parameter from the protocol. The size of the UTXO is determined by the provided
 * transaction output object, and the protocol parameter `coins_per_utxo_byte` specifies the cost per byte for UTXOs.
 *
 * \param[in] output The pointer to the \ref cardano_transaction_output_t object representing the transaction output for which the minimum ADA is being calculated.
 * \param[in] coins_per_utxo_byte The protocol parameter specifying the cost in lovelace per byte of the UTXO.
 * \param[out] lovelace_required A pointer to a uint64_t that will hold the calculated minimum amount of ADA (in lovelace) required for the UTXO.
 *
 * \return \ref CARDANO_SUCCESS if the minimum ADA was successfully computed, or an appropriate error code indicating failure.
 *
 * \note The minimum ADA ensures that the UTXO is valid and meets protocol requirements. It accounts for factors such as
 *       the size of the UTXO, including assets, scripts, and other elements within the output.
 *
 * Usage Example:
 * \code{.c}
 * cardano_transaction_output_t* output = ...;
 * uint64_t coins_per_utxo_byte = 4310;  // Example value from protocol parameters
 * uint64_t min_ada = 0;
 *
 * cardano_error_t result = cardano_compute_min_ada_required(output, coins_per_utxo_byte, &min_ada);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // Successfully computed the minimum ADA required for the UTXO
 *   // min_ada now holds the result
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t cardano_compute_min_ada_required(
  cardano_transaction_output_t* output,
  uint64_t                      coins_per_utxo_byte,
  uint64_t*                     lovelace_required);

/**
 * \brief Computes the script part of the minimum fee of a transaction: execution units and reference scripts.
 *
 * This function calculates the script part of the minimum fee of a transaction. The fee is based on:
 * - The execution units required by the scripts.
 * - The prices of execution units.
 * - The size of the reference scripts and the cost per reference script byte.
 *
 * The execution units are the total of the whole batch: the redeemers of the witness set of the transaction plus the
 * redeemers of the witness set of every sub transaction it carries, since the transaction that carries the sub
 * transactions pays for their script execution. A transaction without sub transactions only accounts for its own redeemers.
 *
 * Reference scripts are always priced, whatever their language: when the batch has no redeemers the execution unit part of
 * the fee is zero and the computed fee is the reference script fee alone. The reference scripts of the sub transactions
 * resolved in \p resolved_reference_inputs are included, as the ledger prices the reference scripts of the whole batch.
 *
 * The execution units are priced as the ledger prices them: the fee is the ceiling of the exact price of the memory
 * units plus the exact price of the CPU steps, computed from the numerators and the denominators of the prices in
 * integer arithmetic, so a fee whose exact value is an integer is never rounded up. A transaction without memory units
 * and CPU steps pays no execution units fee, whatever the prices are.
 *
 * The reference scripts are priced as \ref cardano_compute_script_ref_fee prices them, with the Conway values of the
 * reference script cost stride and multiplier. \ref cardano_compute_transaction_fee prices them with the stride and the
 * multiplier of its protocol parameters instead.
 *
 * \param[in] tx The pointer to the \ref cardano_transaction_t object representing the transaction for which the script fee is being calculated.
 * \param[in] prices The pointer to the \ref cardano_ex_unit_prices_t object containing the prices for execution units (memory and steps).
 * \param[in] resolved_reference_inputs A pointer to the \ref cardano_utxo_list_t object holding every resolved UTXO whose reference script is priced: the ones
 *                                      behind the reference inputs and the spend inputs of the transaction and of its sub transactions. Every entry
 *                                      is priced, so a UTXO that a body both references and spends must be listed once, and a UTXO used by several
 *                                      bodies once per body.
 * \param[in] coins_per_ref_script_byte The pointer to the \ref cardano_unit_interval_t object representing the cost per byte of reference scripts.
 * \param[out] min_fee A pointer to a uint64_t where the calculated minimum fee for the transaction will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the minimum script fee was successfully computed, \ref CARDANO_ERROR_INVALID_ARGUMENT if the
 *         transaction has execution units to price and the price of the memory units or of the CPU steps has a zero
 *         denominator, \ref CARDANO_ERROR_INTEGER_OVERFLOW if the fee does not fit in 64 bits, or an appropriate error code
 *         indicating failure.
 *
 * \note The calculated fee accounts for the execution requirements of all the Plutus scripts included and for the size of every reference script listed in
 *       \p resolved_reference_inputs, measured as \ref cardano_get_serialized_script_size documents.
 *
 * Usage Example:
 * \code{.c}
 * cardano_transaction_t* tx = ...;  // The transaction object
 * cardano_ex_unit_prices_t* prices = ...;  // Prices for execution units
 * cardano_utxo_list_t* resolved_reference_inputs = ...;  // List of resolved UTXOs
 * cardano_unit_interval_t* coins_per_ref_script_byte = ...;  // Cost per byte for reference scripts
 * uint64_t min_fee = 0;
 *
 * cardano_error_t result = cardano_compute_min_script_fee(tx, prices, resolved_reference_inputs, coins_per_ref_script_byte, &min_fee);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // Successfully computed the minimum fee for the transaction
 *   // min_fee now holds the calculated value
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_compute_min_script_fee(
  cardano_transaction_t*    tx,
  cardano_ex_unit_prices_t* prices,
  cardano_utxo_list_t*      resolved_reference_inputs,
  cardano_unit_interval_t*  coins_per_ref_script_byte,
  uint64_t*                 min_fee);

/**
 * \brief Computes the minimum fee required for a transaction without considering script costs.
 *
 * This function calculates the minimum transaction fee based on the serialized size of the transaction
 * and the protocol parameters for fee calculation. The fee is computed as:
 *
 * \f$ \text{min_fee} = \text{min_fee_constant} + (\text{min_fee_coefficient} \times \text{tx_size}) \f$
 *
 * where `tx_size` is the size of the transaction in bytes, measured as the ledger measures it for the fee: the size of
 * a three element array of the body, the witness set and the auxiliary data (or null when the transaction has none).
 * The is valid flag that the four element frame carries is not part of that size, so for such a transaction the size
 * is one byte lower than the one \ref cardano_get_serialized_transaction_size returns.
 *
 * \param[in] tx A pointer to the \ref cardano_transaction_t object representing the transaction.
 * \param[in] min_fee_constant The constant fee factor (A) from the protocol parameters.
 * \param[in] min_fee_coefficient The fee-per-byte coefficient (B) from the protocol parameters.
 * \param[out] min_fee A pointer to a uint64_t where the computed minimum fee will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the minimum fee was successfully computed, \ref CARDANO_ERROR_INTEGER_OVERFLOW if the fee
 *         does not fit in 64 bits, or an appropriate error code indicating failure.
 *
 * \note This function only computes the base fee without considering any costs related to scripts or smart contract execution.
 *
 * Usage Example:
 * \code{.c}
 * cardano_transaction_t* tx = ...;  // The transaction to calculate the fee for
 * uint64_t min_fee_constant = 155381;  // Example value from protocol parameters
 * uint64_t min_fee_coefficient = 44;   // Example value from protocol parameters
 * uint64_t min_fee = 0;
 *
 * cardano_error_t result = cardano_compute_min_fee_without_scripts(tx, min_fee_constant, min_fee_coefficient, &min_fee);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // Successfully computed the minimum fee
 *   // min_fee now holds the calculated fee
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_compute_min_fee_without_scripts(
  cardano_transaction_t* tx,
  uint64_t               min_fee_constant,
  uint64_t               min_fee_coefficient,
  uint64_t*              min_fee);

/**
 * \brief Computes the script reference fee for transaction inputs.
 *
 * This function calculates the fee component that is contributed by reference scripts on the inputs of a transaction.
 * The fee is computed based on the size of the reference scripts and the protocol parameter `coins_per_ref_script_byte`.
 * The total size is priced in tiers of 25600 bytes, the last one possibly partial, and every tier costs 1.2 times as much
 * per byte as the previous one. As the ledger does, the prices are exact fractions: the price of every tier is added
 * exactly and the fee is the floor of the total, taken once, so no tier is rounded on its own. The tier size and the
 * multiplier are the Conway values of the reference script cost stride and multiplier protocol parameters. To price
 * with the stride and the multiplier of a parameter set, as the Dijkstra ledger does, use
 * \ref cardano_compute_script_ref_fee_with_params.
 *
 * The fee is exact when the denominator of the price in lowest terms times 5 to the power of the number of tiers minus
 * one fits in 64 bits, and the integer parts of the prices of the tiers and the fee fit too. A price with a very large
 * denominator, such as one built with \ref cardano_unit_interval_from_double, or a very large price can break that
 * condition; the fee is then a conservative upper bound of the exact fee, which never falls below it and, for prices up
 * to about 50 lovelace per byte, is within a lovelace of it.
 *
 * The ledger charges for every reference script found on the outputs behind the reference inputs and the spend inputs of a
 * transaction, whatever the language of the script (native scripts included) and whether or not the script runs. The size
 * of each script is the one reported by \ref cardano_get_serialized_script_size, which is the size the ledger prices.
 *
 * The total size is not distinct: every entry of the list is counted, so a reference script that appears in several entries
 * is priced once per entry. The ledger takes the inputs of a body as a set, so a UTXO that a body both references and
 * spends must be listed once. Counting every entry matches how the ledger measures the reference script size of a batch, where
 * a UTXO referenced by the transaction and by one of its sub transactions counts twice. The ledger prices the total of the
 * whole batch with a single application of the tiered model, so the UTXOs of the sub transactions must be in the same list.
 *
 * \param[in] resolved_reference_inputs A pointer to the \ref cardano_utxo_list_t object holding every resolved UTXO whose reference script is priced: the
 *                            ones behind the reference inputs and the spend inputs of the transaction. Entries without a reference script add nothing.
 * \param[in] coins_per_ref_script_byte The fee cost per byte of reference script data, as defined by the protocol parameters.
 * \param[out] script_ref_fee A pointer to a uint64_t where the computed reference script fee will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the reference script fee was successfully computed, \ref CARDANO_ERROR_INVALID_ARGUMENT if there
 *         are reference scripts to price and \p coins_per_ref_script_byte has a zero denominator, \ref CARDANO_ERROR_INTEGER_OVERFLOW
 *         if the fee, or the upper bound computed in its place, does not fit in 64 bits, or another appropriate error code
 *         indicating failure. On failure \p script_ref_fee is set to zero.
 *
 * \note This function helps determine the additional cost of including reference scripts in a transaction's inputs based on the
 *       protocol's fee model.
 *
 * Usage Example:
 * \code{.c}
 * cardano_utxo_list_t* resolved_ref_inputs = ...;  // Resolved UTXO inputs, potentially containing reference scripts
 * cardano_unit_interval_t* coins_per_ref_script_byte = ...;  // Protocol parameter for fee per byte of reference script
 * uint64_t script_ref_fee = 0;
 *
 * cardano_error_t result = cardano_compute_script_ref_fee(resolved_ref_inputs, coins_per_ref_script_byte, &script_ref_fee);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // Successfully computed the script reference fee
 *   // script_ref_fee now holds the calculated fee value
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_compute_script_ref_fee(
  cardano_utxo_list_t*     resolved_reference_inputs,
  cardano_unit_interval_t* coins_per_ref_script_byte,
  uint64_t*                script_ref_fee);

/**
 * \brief Computes the script reference fee for transaction inputs with the reference script pricing of a set of
 * protocol parameters.
 *
 * This function prices the reference scripts of \p resolved_reference_inputs as \ref cardano_compute_script_ref_fee
 * does, with the tiered model of the ledger, but takes the price of a byte, the size of a tier and the multiplier from
 * \p protocol_params: the reference script cost per byte, the reference script cost stride and the reference script
 * cost multiplier. The ledger of Dijkstra reads the stride and the multiplier from its protocol parameters, while the
 * earlier eras use the fixed values of 25600 bytes and 6/5, which is what \ref cardano_compute_script_ref_fee uses.
 *
 * A stride of 0 or a multiplier that is not set means the parameter set does not carry it, as the parameters of the eras
 * before Dijkstra do not, and the Conway value is used in its place. When neither is set, the fee is the one
 * \ref cardano_compute_script_ref_fee computes. The multiplier is priced as the exact fraction it holds: the ledger
 * requires it to be positive, and a multiplier below 1 lowers the price of every further tier. A multiplier with a zero
 * numerator, which the ledger never produces, is not rejected and makes every tier after the first free.
 *
 * The fee is exact when the denominator of the price in lowest terms times the denominator of the multiplier in lowest
 * terms to the power of the number of tiers minus one fits in 64 bits, and the integer parts of the prices of the tiers
 * and the fee fit too. A price or a multiplier with a large denominator, such as one built with
 * \ref cardano_unit_interval_from_double, many tiers or a very large price can break that condition; the fee is then a
 * conservative upper bound of the exact fee, which never falls below it. The excess is a tiny fraction of the fee,
 * usually a few lovelace at most, when the denominators are small, and grows with them, up to about a thousandth of
 * the fee for a multiplier denominator around 10^15, as \ref cardano_unit_interval_from_double produces. With
 * denominators close to 2^64 the bound can be several times the exact fee.
 *
 * Every entry of the list is counted, as \ref cardano_compute_script_ref_fee documents, and the size of each script is
 * the one reported by \ref cardano_get_serialized_script_size.
 *
 * \param[in] resolved_reference_inputs A pointer to the \ref cardano_utxo_list_t object holding every resolved UTXO whose reference script is priced: the
 *                            ones behind the reference inputs and the spend inputs of the transaction. Entries without a reference script add nothing.
 * \param[in] protocol_params The pointer to the \ref cardano_protocol_parameters_t object that holds the reference script cost per byte, stride and multiplier.
 * \param[out] script_ref_fee A pointer to a uint64_t where the computed reference script fee will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the reference script fee was successfully computed, \ref CARDANO_ERROR_POINTER_IS_NULL if an
 *         argument is NULL or \p protocol_params has no reference script cost per byte, \ref CARDANO_ERROR_INVALID_ARGUMENT if
 *         there are reference scripts to price and the cost per byte or the multiplier has a zero denominator,
 *         \ref CARDANO_ERROR_INTEGER_OVERFLOW if the fee, or the upper bound computed in its place, does not fit in 64 bits, or
 *         another appropriate error code indicating failure. On failure after the arguments are checked, \p script_ref_fee is
 *         set to zero.
 *
 * Usage Example:
 * \code{.c}
 * cardano_utxo_list_t* resolved_ref_inputs = ...;  // Resolved UTXO inputs, potentially containing reference scripts
 * cardano_protocol_parameters_t* protocol_params = ...;  // Protocol parameters of the current era
 * uint64_t script_ref_fee = 0;
 *
 * cardano_error_t result = cardano_compute_script_ref_fee_with_params(resolved_ref_inputs, protocol_params, &script_ref_fee);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // Successfully computed the script reference fee
 *   // script_ref_fee now holds the calculated fee value
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_compute_script_ref_fee_with_params(
  cardano_utxo_list_t*           resolved_reference_inputs,
  cardano_protocol_parameters_t* protocol_params,
  uint64_t*                      script_ref_fee);

/**
 * \brief Computes the total execution units (exUnits) from a list of redeemers.
 *
 * This function aggregates the total execution units consumed by all redeemers in the provided \ref cardano_redeemer_list_t.
 * Execution units are a critical metric used in Plutus script evaluation, where both memory and CPU units are factored.
 *
 * \param[in] redeemers A pointer to the \ref cardano_redeemer_list_t object representing the list of redeemers.
 * \param[out] total A pointer to a \ref cardano_ex_units_t object where the total execution units will be stored.
 *                   This will contain the sum of the memory and CPU units from all redeemers.
 *
 * \return \ref CARDANO_SUCCESS if the total execution units were successfully computed, or an appropriate error code
 *         indicating failure.
 *
 * \note The `total` object must be managed by the caller. After use, it should be properly de-referenced
 *       with \ref cardano_ex_units_unref to avoid memory leaks.
 *
 * Usage Example:
 * \code{.c}
 * cardano_redeemer_list_t* redeemers = ...;  // Redeemer list for the transaction
 * cardano_ex_units_t* total_ex_units = NULL;
 *
 * cardano_error_t result = cardano_get_total_ex_units_in_redeemers(redeemers, &total_ex_units);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // Successfully computed the total execution units for the redeemers
 *   // total_ex_units now holds the summed execution units
 * }
 *
 * // Clean up after use
 * cardano_ex_units_unref(total_ex_units);
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_get_total_ex_units_in_redeemers(cardano_redeemer_list_t* redeemers, cardano_ex_units_t** total);

/**
 * \brief Computes the serialized size of a given amount of lovelace.
 *
 * This function calculates the size in bytes required to serialize a given amount of lovelace.
 *
 * \param[in] lovelace The amount of lovelace to calculate the serialized size for.
 * \param[out] size_in_bytes A pointer to a \c size_t where the size of the serialized lovelace in bytes will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the size was successfully computed, or an appropriate error code indicating failure.
 *
 * Usage Example:
 * \code{.c}
 * uint64_t lovelace = 1000000;  // Example amount of 1 ADA
 * size_t size_in_bytes = 0;
 *
 * cardano_error_t result = cardano_get_serialized_coin_size(lovelace, &size_in_bytes);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // The size_in_bytes now holds the serialized size of the lovelace amount
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_get_serialized_coin_size(uint64_t lovelace, size_t* size_in_bytes);

/**
 * \brief Computes the serialized size of a given transaction output.
 *
 * This function calculates the size in bytes required to serialize a given transaction output.
 *
 * \param[in] output A pointer to the \ref cardano_transaction_output_t object for which the serialized size is to be calculated.
 * \param[out] size_in_bytes A pointer to a \c size_t where the size of the serialized output in bytes will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the size was successfully computed, or an appropriate error code indicating failure.
 *
 * Usage Example:
 * \code{.c}
 * cardano_transaction_output_t* output = { ... };  // Assume this is initialized
 * size_t size_in_bytes = 0;
 *
 * cardano_error_t result = cardano_get_serialized_output_size(output, &size_in_bytes);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // The size_in_bytes now holds the serialized size of the output
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_get_serialized_output_size(cardano_transaction_output_t* output, size_t* size_in_bytes);

/**
 * \brief Computes the serialized size of a given script, the size the ledger prices for a reference script.
 *
 * This function calculates the size in bytes of the serialized script alone, without the two element array that holds the
 * language tag followed by the script in a script reference: for a Plutus script, the bytes of the script, the ones its
 * byte string holds, without the header of the byte string; for a native script, the bytes of its CBOR. A native script
 * decoded from CBOR is measured with the bytes it was decoded from, so a script stored on chain with a longer, non minimal
 * encoding (for example a slot written as an 8 byte integer) is measured with those bytes, as the ledger measures it.
 *
 * This is the size the ledger prices for a reference script, and the one \ref cardano_compute_script_ref_fee prices. The
 * encoding of the script with its language tag, as a script reference carries it, is the one \ref cardano_script_to_cbor
 * writes, which is 2 bytes larger for a native script and 3 to 7 bytes larger for a Plutus script.
 *
 * \param[in] script A pointer to the \ref cardano_script_t object for which the serialized size is to be calculated.
 * \param[out] size_in_bytes A pointer to a \c size_t where the size of the serialized script in bytes will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the size was successfully computed, or an appropriate error code indicating failure.
 *
 * Usage Example:
 * \code{.c}
 * cardano_script_t* script = { ... };  // Assume this is initialized
 * size_t size_in_bytes = 0;
 *
 * cardano_error_t result = cardano_get_serialized_script_size(script, &size_in_bytes);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // The size_in_bytes now holds the serialized size of the script
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_get_serialized_script_size(cardano_script_t* script, size_t* size_in_bytes);

/**
 * \brief Computes the serialized size of a given transaction.
 *
 * This function calculates the size in bytes required to serialize a given transaction, with the is valid flag when
 * the transaction is serialized in the four element frame. The ledger measures the size of a transaction for its fee
 * without that flag, as \ref cardano_compute_min_fee_without_scripts does.
 *
 * \param[in] transaction A pointer to the \ref cardano_transaction_t object for which the serialized size is to be calculated.
 * \param[out] size_in_bytes A pointer to a \c size_t where the size of the serialized transaction in bytes will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the size was successfully computed, or an appropriate error code indicating failure.
 *
 * Usage Example:
 * \code{.c}
 * cardano_transaction_t* transaction = { ... };  // Assume this is initialized
 * size_t size_in_bytes = 0;
 *
 * cardano_error_t result = cardano_get_serialized_transaction_size(transaction, &size_in_bytes);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // The size_in_bytes now holds the serialized size of the transaction
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_get_serialized_transaction_size(cardano_transaction_t* transaction, size_t* size_in_bytes);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif // BIGLUP_LABS_INCLUDE_CARDANO_FEE_H