{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}

module Cardano.Api.Experimental.Tx.Internal.Fee
  ( estimateBalancedTxBody
  )
where

import Cardano.Api.Address
import Cardano.Api.Certificate.Internal
import Cardano.Api.Era.Internal.Eon.Convert
import Cardano.Api.Experimental.Era
import Cardano.Api.Ledger.Internal.Reexport qualified as L
import Cardano.Api.Plutus
import Cardano.Api.Tx.Internal.Body
import Cardano.Api.Tx.Internal.Fee qualified as Fee
import Cardano.Api.Value.Internal

import Cardano.Ledger.Alonzo.Core qualified as Ledger
import Cardano.Ledger.Credential as Ledger (Credential)

import Data.Map.Strict (Map)
import Data.Map.Strict qualified as Map
import Data.Set (Set)
import GHC.Stack

-- | Use when you do not have access to the UTxOs you intend to spend
estimateBalancedTxBody
  :: HasCallStack
  => Era era
  -> TxBodyContent BuildTx era
  -> L.PParams (LedgerEra era)
  -> Set PoolId
  -- ^ The set of registered stake pools, being
  --   unregistered in this transaction.
  -> Map StakeCredential L.Coin
  -- ^ A map of all deposits for stake credentials that are being
  --   unregistered in this transaction.
  -> Map (Ledger.Credential Ledger.DRepRole) L.Coin
  -- ^ A map of all deposits for DRep credentials that are being
  --   unregistered in this transaction.
  -> Map (Ledger.PlutusPurpose Ledger.AsIx (LedgerEra era)) ExecutionUnits
  -- ^ Plutus script execution units.
  -> Coin
  -- ^ Total potential collateral amount.
  -> Int
  -- ^ The number of key witnesses to be added to the transaction.
  -> Int
  -- ^ The number of Byron key witnesses to be added to the transaction.
  -> Int
  -- ^ The size of all reference scripts in bytes.
  -> AddressInEra era
  -- ^ Change address.
  -> Value
  -- ^ Total value of UTXOs being spent.
  -> Either (Fee.TxFeeEstimationError era) (Fee.BalancedTxBody era)
estimateBalancedTxBody :: forall era.
HasCallStack =>
Era era
-> TxBodyContent BuildTx era
-> PParams (LedgerEra era)
-> Set PoolId
-> Map StakeCredential Coin
-> Map (Credential 'DRepRole) Coin
-> Map (PlutusPurpose AsIx (LedgerEra era)) ExecutionUnits
-> Coin
-> Int
-> Int
-> Int
-> AddressInEra era
-> Value
-> Either (TxFeeEstimationError era) (BalancedTxBody era)
estimateBalancedTxBody
  Era era
w
  TxBodyContent BuildTx era
txbodycontent
  PParams (LedgerEra era)
pparams
  Set PoolId
poolids
  Map StakeCredential Coin
stakeDelegDeposits
  Map (Credential 'DRepRole) Coin
drepDelegDeposits
  Map (PlutusPurpose AsIx (LedgerEra era)) ExecutionUnits
exUnitsMap =
    Era era
-> (EraCommonConstraints era =>
    Coin
    -> Int
    -> Int
    -> Int
    -> AddressInEra era
    -> Value
    -> Either (TxFeeEstimationError era) (BalancedTxBody era))
-> Coin
-> Int
-> Int
-> Int
-> AddressInEra era
-> Value
-> Either (TxFeeEstimationError era) (BalancedTxBody era)
forall era a. Era era -> (EraCommonConstraints era => a) -> a
obtainCommonConstraints Era era
w ((EraCommonConstraints era =>
  Coin
  -> Int
  -> Int
  -> Int
  -> AddressInEra era
  -> Value
  -> Either (TxFeeEstimationError era) (BalancedTxBody era))
 -> Coin
 -> Int
 -> Int
 -> Int
 -> AddressInEra era
 -> Value
 -> Either (TxFeeEstimationError era) (BalancedTxBody era))
-> (EraCommonConstraints era =>
    Coin
    -> Int
    -> Int
    -> Int
    -> AddressInEra era
    -> Value
    -> Either (TxFeeEstimationError era) (BalancedTxBody era))
-> Coin
-> Int
-> Int
-> Int
-> AddressInEra era
-> Value
-> Either (TxFeeEstimationError era) (BalancedTxBody era)
forall a b. (a -> b) -> a -> b
$
      MaryEraOnwards era
-> TxBodyContent BuildTx era
-> PParams (ShelleyLedgerEra era)
-> Set PoolId
-> Map StakeCredential Coin
-> Map (Credential 'DRepRole) Coin
-> Map ScriptWitnessIndex ExecutionUnits
-> Coin
-> Int
-> Int
-> Int
-> AddressInEra era
-> Value
-> Either (TxFeeEstimationError era) (BalancedTxBody era)
forall era.
HasCallStack =>
MaryEraOnwards era
-> TxBodyContent BuildTx era
-> PParams (ShelleyLedgerEra era)
-> Set PoolId
-> Map StakeCredential Coin
-> Map (Credential 'DRepRole) Coin
-> Map ScriptWitnessIndex ExecutionUnits
-> Coin
-> Int
-> Int
-> Int
-> AddressInEra era
-> Value
-> Either (TxFeeEstimationError era) (BalancedTxBody era)
Fee.estimateBalancedTxBody
        (Era era -> MaryEraOnwards era
forall era. Era era -> MaryEraOnwards era
forall a (f :: a -> *) (g :: a -> *) (era :: a).
Convert f g =>
f era -> g era
convert Era era
w)
        TxBodyContent BuildTx era
txbodycontent
        PParams (ShelleyLedgerEra era)
PParams (LedgerEra era)
pparams
        Set PoolId
poolids
        Map StakeCredential Coin
stakeDelegDeposits
        Map (Credential 'DRepRole) Coin
drepDelegDeposits
        ((PlutusPurpose AsIx (LedgerEra era) -> ScriptWitnessIndex)
-> Map (PlutusPurpose AsIx (LedgerEra era)) ExecutionUnits
-> Map ScriptWitnessIndex ExecutionUnits
forall k2 k1 a. Ord k2 => (k1 -> k2) -> Map k1 a -> Map k2 a
Map.mapKeys (AlonzoEraOnwards era
-> PlutusPurpose AsIx (ShelleyLedgerEra era) -> ScriptWitnessIndex
forall era.
AlonzoEraOnwards era
-> PlutusPurpose AsIx (ShelleyLedgerEra era) -> ScriptWitnessIndex
toScriptIndex (Era era -> AlonzoEraOnwards era
forall era. Era era -> AlonzoEraOnwards era
forall a (f :: a -> *) (g :: a -> *) (era :: a).
Convert f g =>
f era -> g era
convert Era era
w)) Map (PlutusPurpose AsIx (LedgerEra era)) ExecutionUnits
exUnitsMap)