packages feed

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

/**
 * \file implicit_coin.h
 *
 * \author angel.castillo
 * \date   Nov 02, 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_IMPLICIT_COIN_H
#define BIGLUP_LABS_INCLUDE_CARDANO_IMPLICIT_COIN_H

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

#include <cardano/error.h>
#include <cardano/protocol_params/protocol_parameters.h>
#include <cardano/transaction/sub_transaction.h>
#include <cardano/transaction/transaction.h>
#include <cardano/typedefs.h>

/* STRUCTURES *****************************************************************/

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

/**
 * \brief Represents the implicit coin values in a transaction.
 */
typedef struct cardano_implicit_coin_t
{
    /**
     * \brief The reward withdrawals in the transaction.
     */
    uint64_t withdrawals;

    /**
     * \brief The deposits paid by the certificates and the proposal procedures.
     */
    uint64_t deposits;

    /**
     * \brief The deposits returned by the stake credential and DRep unregistration certificates.
     */
    uint64_t reclaim_deposits;
} cardano_implicit_coin_t;

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

/**
 * \brief Computes the implicit coin balance for a transaction.
 *
 * The `cardano_compute_implicit_coin` function calculates the implicit coin balance for a given transaction.
 *
 * Deposits and refunds follow the ledger rules for the certificates of the body:
 * - A pool registration pays the pool deposit of the protocol parameters, once per pool: a later registration of the
 *   same pool, identified by its operator key hash, in the same body updates the pool parameters and pays nothing.
 * - A pool retirement reclaims nothing. The ledger returns the pool deposit to the reward account of the pool at the
 *   epoch boundary where the pool retires, not in the retiring transaction.
 *
 * This function does not know which pools are already registered on chain, so a registration that updates the
 * parameters of an already registered pool is charged the pool deposit, which the ledger does not charge, and a
 * transaction balanced with it does not conserve value for the ledger. The caller must account for that deposit.
 *
 * \param[in] tx A pointer to the \ref cardano_transaction_t object representing the transaction.
 * \param[in] protocol_params A pointer to \ref cardano_protocol_parameters_t containing the protocol parameters necessary for accurate computation.
 * \param[out] implicit_coin A pointer to \ref cardano_implicit_coin_t where the computed implicit coin balance will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the implicit coin balance was successfully computed, \ref CARDANO_ERROR_INTEGER_OVERFLOW if the
 *         withdrawals, the deposits or the reclaimed deposits add up to more than UINT64_MAX, or an appropriate error code
 *         indicating failure.
 *
 * Usage Example:
 * \code{.c}
 * cardano_transaction_t* transaction = ...;             // Transaction object
 * cardano_protocol_parameters_t* protocol_params = ...; // Protocol parameters
 * cardano_implicit_coin_t implicit_coin = { 0 };
 *
 * cardano_error_t result = cardano_compute_implicit_coin(transaction, protocol_params, &implicit_coin);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // The implicit coin balance was successfully computed and stored in `implicit_coin`
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_compute_implicit_coin(
  cardano_transaction_t*         tx,
  cardano_protocol_parameters_t* protocol_params,
  cardano_implicit_coin_t*       implicit_coin);

/**
 * \brief Computes the implicit coin balance for a sub transaction.
 *
 * The `cardano_compute_sub_transaction_implicit_coin` function calculates the implicit coin balance of a CIP-118 sub transaction
 * from the withdrawals, certificates and proposal procedures of its body, applying the same rules as \ref cardano_compute_implicit_coin
 * does for a top level transaction. The pool deposits are charged per body: a pool registered by another body of the
 * batch is treated like a pool that is not registered yet. The ledger charges the pool deposit once per pool across a
 * whole CIP-118 batch, and \ref cardano_compute_transaction_batch_imbalance counts it that way.
 *
 * \param[in] sub_tx A pointer to the \ref cardano_sub_transaction_t object representing the sub transaction.
 * \param[in] protocol_params A pointer to \ref cardano_protocol_parameters_t containing the protocol parameters necessary for accurate computation.
 * \param[out] implicit_coin A pointer to \ref cardano_implicit_coin_t where the computed implicit coin balance will be stored.
 *
 * \return \ref CARDANO_SUCCESS if the implicit coin balance was successfully computed, \ref CARDANO_ERROR_INTEGER_OVERFLOW if the
 *         withdrawals, the deposits or the reclaimed deposits add up to more than UINT64_MAX, or an appropriate error code
 *         indicating failure.
 *
 * Usage Example:
 * \code{.c}
 * cardano_sub_transaction_t* sub_transaction = ...;     // Sub transaction object
 * cardano_protocol_parameters_t* protocol_params = ...; // Protocol parameters
 * cardano_implicit_coin_t implicit_coin = { 0 };
 *
 * cardano_error_t result = cardano_compute_sub_transaction_implicit_coin(sub_transaction, protocol_params, &implicit_coin);
 *
 * if (result == CARDANO_SUCCESS)
 * {
 *   // The implicit coin balance was successfully computed and stored in `implicit_coin`
 * }
 * \endcode
 */
CARDANO_NODISCARD
CARDANO_EXPORT cardano_error_t
cardano_compute_sub_transaction_implicit_coin(
  cardano_sub_transaction_t*     sub_tx,
  cardano_protocol_parameters_t* protocol_params,
  cardano_implicit_coin_t*       implicit_coin);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif // BIGLUP_LABS_INCLUDE_CARDANO_IMPLICIT_COIN_H