Safe Haskell | None |
---|---|
Language | Haskell2010 |
Fee calculation
Synopsis
- evaluateTransactionFee :: ShelleyBasedEra era -> PParams (ShelleyLedgerEra era) -> TxBody era -> Word -> Word -> Int -> Coin
- calculateMinTxFee :: ShelleyBasedEra era -> PParams (ShelleyLedgerEra era) -> UTxO era -> TxBody era -> Word -> Coin
- estimateTransactionKeyWitnessCount :: TxBodyContent BuildTx era -> Word
- evaluateTransactionExecutionUnits :: CardanoEra era -> SystemStart -> LedgerEpochInfo -> LedgerProtocolParameters era -> UTxO era -> TxBody era -> Either (TransactionValidityError era) (Map ScriptWitnessIndex (Either ScriptExecutionError (EvalTxExecutionUnitsLog, ExecutionUnits)))
- evaluateTransactionExecutionUnitsShelley :: ShelleyBasedEra era -> SystemStart -> LedgerEpochInfo -> LedgerProtocolParameters era -> UTxO era -> Tx (ShelleyLedgerEra era) -> Either (TransactionValidityError era) (Map ScriptWitnessIndex (Either ScriptExecutionError (EvalTxExecutionUnitsLog, ExecutionUnits)))
- data ScriptExecutionError
- = ScriptErrorMissingTxIn TxIn
- | ScriptErrorTxInWithoutDatum TxIn
- | ScriptErrorWrongDatum (Hash ScriptData)
- | ScriptErrorEvaluationFailed DebugPlutusFailure
- | ScriptErrorExecutionUnitsOverflow
- | ScriptErrorNotPlutusWitnessedTxIn ScriptWitnessIndex ScriptHash
- | ScriptErrorRedeemerPointsToUnknownScriptHash ScriptWitnessIndex
- | ScriptErrorMissingScript ScriptWitnessIndex ResolvablePointers
- | ScriptErrorMissingCostModel Language
- | (EraPlutusContext (ShelleyLedgerEra era), Show (ContextError (ShelleyLedgerEra era))) => ScriptErrorTranslationError (ContextError (ShelleyLedgerEra era))
- data TransactionValidityError era where
- TransactionValidityIntervalError :: forall era. PastHorizonException -> TransactionValidityError era
- TransactionValidityCostModelError :: forall era. Map AnyPlutusScriptVersion CostModel -> String -> TransactionValidityError era
- evaluateTransactionBalance :: ShelleyBasedEra era -> PParams (ShelleyLedgerEra era) -> Set PoolId -> Map StakeCredential Coin -> Map (Credential 'DRepRole StandardCrypto) Coin -> UTxO era -> TxBody era -> TxOutValue era
- estimateBalancedTxBody :: MaryEraOnwards era -> TxBodyContent BuildTx era -> PParams (ShelleyLedgerEra era) -> Set PoolId -> Map StakeCredential Coin -> Map (Credential 'DRepRole StandardCrypto) Coin -> Map ScriptWitnessIndex ExecutionUnits -> Coin -> Int -> Int -> Int -> AddressInEra era -> Value -> Either (TxFeeEstimationError era) (BalancedTxBody era)
- estimateOrCalculateBalancedTxBody :: ShelleyBasedEra era -> FeeEstimationMode era -> PParams (ShelleyLedgerEra era) -> TxBodyContent BuildTx era -> Set PoolId -> Map StakeCredential Coin -> Map (Credential 'DRepRole StandardCrypto) Coin -> AddressInEra era -> Either (AutoBalanceError era) (BalancedTxBody era)
- makeTransactionBodyAutoBalance :: ShelleyBasedEra era -> SystemStart -> LedgerEpochInfo -> LedgerProtocolParameters era -> Set PoolId -> Map StakeCredential Coin -> Map (Credential 'DRepRole StandardCrypto) Coin -> UTxO era -> TxBodyContent BuildTx era -> AddressInEra era -> Maybe Word -> Either (TxBodyErrorAutoBalance era) (BalancedTxBody era)
- calcReturnAndTotalCollateral :: AlonzoEraPParams (ShelleyLedgerEra era) => BabbageEraOnwards era -> Coin -> PParams (ShelleyLedgerEra era) -> TxInsCollateral era -> TxReturnCollateral CtxTx era -> TxTotalCollateral era -> AddressInEra era -> Value (ShelleyLedgerEra era) -> (TxReturnCollateral CtxTx era, TxTotalCollateral era)
- data AutoBalanceError era
- data BalancedTxBody era = BalancedTxBody (TxBodyContent BuildTx era) (TxBody era) (TxOut CtxTx era) Coin
- data FeeEstimationMode era
- newtype RequiredShelleyKeyWitnesses = RequiredShelleyKeyWitnesses {}
- newtype RequiredByronKeyWitnesses = RequiredByronKeyWitnesses {}
- newtype TotalReferenceScriptsSize = TotalReferenceScriptsSize {}
- data TxBodyErrorAutoBalance era
- = TxBodyError TxBodyError
- | TxBodyScriptExecutionError [(ScriptWitnessIndex, ScriptExecutionError)]
- | TxBodyScriptBadScriptValidity
- | TxBodyErrorAdaBalanceNegative Coin
- | TxBodyErrorAdaBalanceTooSmall TxOutInAnyEra Coin Coin
- | TxBodyErrorByronEraNotSupported
- | TxBodyErrorMissingParamMinUTxO
- | TxBodyErrorValidityInterval (TransactionValidityError era)
- | TxBodyErrorMinUTxONotMet TxOutInAnyEra Coin
- | TxBodyErrorNonAdaAssetsUnbalanced Value
- | TxBodyErrorScriptWitnessIndexMissingFromExecUnitsMap ScriptWitnessIndex (Map ScriptWitnessIndex ExecutionUnits)
- data TxFeeEstimationError era
- = TxFeeEstimationTransactionTranslationError (TransactionValidityError era)
- | TxFeeEstimationScriptExecutionError (TxBodyErrorAutoBalance era)
- | TxFeeEstimationBalanceError (TxBodyErrorAutoBalance era)
- | TxFeeEstimationxBodyError TxBodyError
- | TxFeeEstimationFinalConstructionError TxBodyError
- | TxFeeEstimationOnlyMaryOnwardsSupportedError
- calculateMinimumUTxO :: ShelleyBasedEra era -> TxOut CtxTx era -> PParams (ShelleyLedgerEra era) -> Coin
- data ResolvablePointers where
- ResolvablePointers :: forall era. (Era (ShelleyLedgerEra era), Show (PlutusPurpose AsIx (ShelleyLedgerEra era)), Show (PlutusPurpose AsItem (ShelleyLedgerEra era)), Show (PlutusScript (ShelleyLedgerEra era))) => ShelleyBasedEra era -> !(Map (PlutusPurpose AsIx (ShelleyLedgerEra era)) (PlutusPurpose AsItem (ShelleyLedgerEra era), Maybe (PlutusScriptBytes, Language), ScriptHash StandardCrypto)) -> ResolvablePointers
Transaction fees
evaluateTransactionFee Source #
:: ShelleyBasedEra era | |
-> PParams (ShelleyLedgerEra era) | |
-> TxBody era | |
-> Word | The number of Shelley key witnesses |
-> Word | The number of Byron key witnesses |
-> Int | Reference script size in bytes |
-> Coin |
Compute the transaction fee for a proposed transaction, with the assumption that there will be the given number of key witnesses (i.e. signatures).
Use calculateMinTxFee
if possible as that function is more accurate.
:: ShelleyBasedEra era | |
-> PParams (ShelleyLedgerEra era) | |
-> UTxO era | |
-> TxBody era | |
-> Word | The number of Shelley key witnesses |
-> Coin |
Estimate minimum transaction fee for a proposed transaction by looking into the transaction and figuring out how many and what kind of key witnesses this transaction needs.
It requires access to the portion of the UTxO
that is relevant for this
transaction in order to lookup any txins included in the transaction.
The only type of witnesses that it cannot figure out reliably is the witnesses needed for satisfying native scripts included in the transaction.
For this reason number of witnesses needed for native scripts must be supplied as an extra argument.
estimateTransactionKeyWitnessCount :: TxBodyContent BuildTx era -> Word Source #
Give an approximate count of the number of key witnesses (i.e. signatures) a transaction will need.
This is an estimate not a precise count in that it can over-estimate: it makes conservative assumptions such as all inputs are from distinct addresses, but in principle multiple inputs can use the same address and we only need a witness per address.
Similarly there can be overlap between the regular and collateral inputs, but we conservatively assume they are distinct.
TODO: it is worth us considering a more precise count that relies on the UTxO to resolve which inputs are for distinct addresses, and also to count the number of Shelley vs Byron style witnesses.
Script execution units
evaluateTransactionExecutionUnits :: CardanoEra era -> SystemStart -> LedgerEpochInfo -> LedgerProtocolParameters era -> UTxO era -> TxBody era -> Either (TransactionValidityError era) (Map ScriptWitnessIndex (Either ScriptExecutionError (EvalTxExecutionUnitsLog, ExecutionUnits))) Source #
Compute the ExecutionUnits
needed for each script in the transaction.
This works by running all the scripts and counting how many execution units are actually used.
evaluateTransactionExecutionUnitsShelley :: ShelleyBasedEra era -> SystemStart -> LedgerEpochInfo -> LedgerProtocolParameters era -> UTxO era -> Tx (ShelleyLedgerEra era) -> Either (TransactionValidityError era) (Map ScriptWitnessIndex (Either ScriptExecutionError (EvalTxExecutionUnitsLog, ExecutionUnits))) Source #
data ScriptExecutionError Source #
The different possible reasons that executing a script can fail,
as reported by evaluateTransactionExecutionUnits
.
The first three of these are about failures before we even get to execute the script, and two are the result of execution.
TODO: We should replace ScriptWitnessIndex with ledger's PlutusPurpose AsIx ledgerera. This would necessitate the parameterization of ScriptExecutionError.
ScriptErrorMissingTxIn TxIn | The script depends on a |
ScriptErrorTxInWithoutDatum TxIn | The |
ScriptErrorWrongDatum (Hash ScriptData) | The |
ScriptErrorEvaluationFailed DebugPlutusFailure | The script evaluation failed. This usually means it evaluated to an
error value. This is not a case of running out of execution units
(which is not possible for |
ScriptErrorExecutionUnitsOverflow | The execution units overflowed a 64bit word. Congratulations if you encounter this error. With the current style of cost model this would need a script to run for over 7 months, which is somewhat more than the expected maximum of a few milliseconds. |
ScriptErrorNotPlutusWitnessedTxIn ScriptWitnessIndex ScriptHash | An attempt was made to spend a key witnessed tx input with a script witness. |
ScriptErrorRedeemerPointsToUnknownScriptHash ScriptWitnessIndex | The redeemer pointer points to a script hash that does not exist in the transaction nor in the UTxO as a reference script" |
ScriptErrorMissingScript ScriptWitnessIndex ResolvablePointers | A redeemer pointer points to a script that does not exist. |
ScriptErrorMissingCostModel Language | A cost model was missing for a language which was used. |
(EraPlutusContext (ShelleyLedgerEra era), Show (ContextError (ShelleyLedgerEra era))) => ScriptErrorTranslationError (ContextError (ShelleyLedgerEra era)) |
Instances
Show ScriptExecutionError Source # | |
Defined in Cardano.Api.Fees | |
Error ScriptExecutionError Source # | |
Defined in Cardano.Api.Fees prettyError :: ScriptExecutionError -> Doc ann Source # |
data TransactionValidityError era where Source #
TransactionValidityIntervalError :: forall era. PastHorizonException -> TransactionValidityError era | The transaction validity interval is too far into the future. Transactions with Plutus scripts need to have a validity interval that is not so far in the future that we cannot reliably determine the UTC time corresponding to the validity interval expressed in slot numbers. This is because the Plutus scripts get given the transaction validity interval in UTC time, so that they are not sensitive to slot lengths. If either end of the validity interval is beyond the so called "time horizon" then the consensus algorithm is not able to reliably determine the relationship between slots and time. This is this situation in which this error is reported. For the Cardano mainnet the time horizon is 36 hours beyond the current time. This effectively means we cannot submit check or submit transactions that use Plutus scripts that have the end of their validity interval more than 36 hours into the future. |
TransactionValidityCostModelError :: forall era. Map AnyPlutusScriptVersion CostModel -> String -> TransactionValidityError era |
Instances
Show (TransactionValidityError era) Source # | |
Defined in Cardano.Api.Fees | |
Error (TransactionValidityError era) Source # | |
Defined in Cardano.Api.Fees prettyError :: TransactionValidityError era -> Doc ann Source # |
Transaction balance
evaluateTransactionBalance :: ShelleyBasedEra era -> PParams (ShelleyLedgerEra era) -> Set PoolId -> Map StakeCredential Coin -> Map (Credential 'DRepRole StandardCrypto) Coin -> UTxO era -> TxBody era -> TxOutValue era Source #
Compute the total balance of the proposed transaction. Ultimately a valid transaction must be fully balanced: that is have a total value of zero.
Finding the (non-zero) balance of partially constructed transaction is useful for adjusting a transaction to be fully balanced.
Automated transaction building
estimateBalancedTxBody Source #
:: MaryEraOnwards era | |
-> TxBodyContent BuildTx era | |
-> PParams (ShelleyLedgerEra era) | |
-> Set PoolId | The set of registered stake pools, that are being unregistered in this transaction. |
-> Map StakeCredential Coin | Map of all deposits for stake credentials that are being unregistered in this transaction |
-> Map (Credential 'DRepRole StandardCrypto) Coin | Map of all deposits for drep credentials that are being unregistered in this transaction |
-> Map ScriptWitnessIndex ExecutionUnits | Plutus script execution units |
-> Coin | Total potential collateral amount |
-> Int | The number of key witnesses still to be added to the transaction. |
-> Int | The number of Byron key witnesses still to be added to the transaction. |
-> Int | Size of all reference scripts in bytes |
-> AddressInEra era | Change address |
-> Value | Total value of UTxOs being spent |
-> Either (TxFeeEstimationError era) (BalancedTxBody era) |
Use when you do not have access to the UTxOs you intend to spend
estimateOrCalculateBalancedTxBody :: ShelleyBasedEra era -> FeeEstimationMode era -> PParams (ShelleyLedgerEra era) -> TxBodyContent BuildTx era -> Set PoolId -> Map StakeCredential Coin -> Map (Credential 'DRepRole StandardCrypto) Coin -> AddressInEra era -> Either (AutoBalanceError era) (BalancedTxBody era) Source #
makeTransactionBodyAutoBalance Source #
:: ShelleyBasedEra era | |
-> SystemStart | |
-> LedgerEpochInfo | |
-> LedgerProtocolParameters era | |
-> Set PoolId | The set of registered stake pools, that are being unregistered in this transaction. |
-> Map StakeCredential Coin | Map of all deposits for stake credentials that are being unregistered in this transaction |
-> Map (Credential 'DRepRole StandardCrypto) Coin | Map of all deposits for drep credentials that are being unregistered in this transaction |
-> UTxO era | Just the transaction inputs, not the entire |
-> TxBodyContent BuildTx era | |
-> AddressInEra era | Change address |
-> Maybe Word | Override key witnesses |
-> Either (TxBodyErrorAutoBalance era) (BalancedTxBody era) |
This is much like makeTransactionBody
but with greater automation to
calculate suitable values for several things.
In particular:
- It calculates the correct script
ExecutionUnits
(ignoring the provided values, which can thus be zero). - It calculates the transaction fees, based on the script
ExecutionUnits
, the currentProtocolParameters
, and an estimate of the number of key witnesses (i.e. signatures). There is an override for the number of key witnesses. - It accepts a change address, calculates the balance of the transaction and puts the excess change into the change output.
- It also checks that the balance is positive and the change is above the minimum threshold.
To do this it needs more information than makeTransactionBody
, all of
which can be queried from a local node.
calcReturnAndTotalCollateral Source #
:: AlonzoEraPParams (ShelleyLedgerEra era) | |
=> BabbageEraOnwards era | |
-> Coin | Fee |
-> PParams (ShelleyLedgerEra era) | |
-> TxInsCollateral era | From the initial TxBodyContent |
-> TxReturnCollateral CtxTx era | From the initial TxBodyContent |
-> TxTotalCollateral era | From the initial TxBodyContent |
-> AddressInEra era | Change address |
-> Value (ShelleyLedgerEra era) | Total available collateral (can include non-ada) |
-> (TxReturnCollateral CtxTx era, TxTotalCollateral era) |
data AutoBalanceError era Source #
AutoBalanceEstimationError (TxFeeEstimationError era) | |
AutoBalanceCalculationError (TxBodyErrorAutoBalance era) |
Instances
Show (AutoBalanceError era) Source # | |
Defined in Cardano.Api.Fees | |
Error (AutoBalanceError era) Source # | |
Defined in Cardano.Api.Fees prettyError :: AutoBalanceError era -> Doc ann Source # |
data BalancedTxBody era Source #
Instances
IsShelleyBasedEra era => Show (BalancedTxBody era) Source # | |
Defined in Cardano.Api.Fees |
data FeeEstimationMode era Source #
CalculateWithSpendableUTxO | Accurate fee calculation. |
| |
EstimateWithoutSpendableUTxO | Less accurate fee estimation. |
|
newtype RequiredShelleyKeyWitnesses Source #
Instances
Show RequiredShelleyKeyWitnesses Source # | |
Defined in Cardano.Api.Fees |
newtype RequiredByronKeyWitnesses Source #
Instances
Show RequiredByronKeyWitnesses Source # | |
Defined in Cardano.Api.Fees |
newtype TotalReferenceScriptsSize Source #
Instances
Show TotalReferenceScriptsSize Source # | |
Defined in Cardano.Api.Fees |
data TxBodyErrorAutoBalance era Source #
The possible errors that can arise from makeTransactionBodyAutoBalance
.
TxBodyError TxBodyError | The same errors that can arise from |
TxBodyScriptExecutionError [(ScriptWitnessIndex, ScriptExecutionError)] | One or more of the scripts fails to execute correctly. |
TxBodyScriptBadScriptValidity | One or more of the scripts were expected to fail validation, but none did. |
TxBodyErrorAdaBalanceNegative Coin | There is not enough ada to cover both the outputs and the fees. The transaction should be changed to provide more input ada, or otherwise adjusted to need less (e.g. outputs, script etc). |
TxBodyErrorAdaBalanceTooSmall | There is enough ada to cover both the outputs and the fees, but the resulting change is too small: it is under the minimum value for new UTxO entries. The transaction should be changed to provide more input ada. |
| |
TxBodyErrorByronEraNotSupported |
|
TxBodyErrorMissingParamMinUTxO | The |
TxBodyErrorValidityInterval (TransactionValidityError era) | The transaction validity interval is too far into the future.
See |
TxBodyErrorMinUTxONotMet | The minimum spendable UTxO threshold has not been met. |
| |
TxBodyErrorNonAdaAssetsUnbalanced Value | |
TxBodyErrorScriptWitnessIndexMissingFromExecUnitsMap ScriptWitnessIndex (Map ScriptWitnessIndex ExecutionUnits) |
Instances
Show (TxBodyErrorAutoBalance era) Source # | |
Defined in Cardano.Api.Fees | |
Error (TxBodyErrorAutoBalance era) Source # | |
Defined in Cardano.Api.Fees prettyError :: TxBodyErrorAutoBalance era -> Doc ann Source # |
data TxFeeEstimationError era Source #
Instances
Show (TxFeeEstimationError era) Source # | |
Defined in Cardano.Api.Fees | |
Error (TxFeeEstimationError era) Source # | |
Defined in Cardano.Api.Fees prettyError :: TxFeeEstimationError era -> Doc ann Source # |
Minimum UTxO calculation
calculateMinimumUTxO :: ShelleyBasedEra era -> TxOut CtxTx era -> PParams (ShelleyLedgerEra era) -> Coin Source #
Internal helpers
data ResolvablePointers where Source #
ResolvablePointers :: forall era. (Era (ShelleyLedgerEra era), Show (PlutusPurpose AsIx (ShelleyLedgerEra era)), Show (PlutusPurpose AsItem (ShelleyLedgerEra era)), Show (PlutusScript (ShelleyLedgerEra era))) => ShelleyBasedEra era -> !(Map (PlutusPurpose AsIx (ShelleyLedgerEra era)) (PlutusPurpose AsItem (ShelleyLedgerEra era), Maybe (PlutusScriptBytes, Language), ScriptHash StandardCrypto)) -> ResolvablePointers |
Instances
Show ResolvablePointers Source # | |
Defined in Cardano.Api.Fees |