2424import Cardano.Api.Address
2525import Cardano.Api.Certificate.Internal
2626import Cardano.Api.Era.Internal.Eon.Convert
27+ import Cardano.Api.Error
2728import Cardano.Api.Experimental.AnyScriptWitness
2829import Cardano.Api.Experimental.AnyScriptWitness qualified as Exp
2930import Cardano.Api.Experimental.Era
@@ -38,11 +39,13 @@ import Cardano.Api.Plutus.Internal
3839import Cardano.Api.Plutus.Internal.Script (fromAlonzoExUnits )
3940import Cardano.Api.Plutus.Internal.Script qualified as Old
4041import Cardano.Api.Plutus.Internal.ScriptData
42+ import Cardano.Api.Pretty
4143import Cardano.Api.ProtocolParameters
4244import Cardano.Api.Query.Internal.Type.QueryInMode
4345import Cardano.Api.Tx.Internal.Body
4446 ( CtxTx
4547 , ScriptWitnessIndex (.. )
48+ , renderScriptWitnessIndex
4649 , toScriptIndex
4750 )
4851import Cardano.Api.Tx.Internal.Fee
@@ -79,6 +82,7 @@ import Data.Set qualified as Set
7982import GHC.Exts (IsList (.. ))
8083import GHC.Stack
8184import Lens.Micro ((.~) , (^.) )
85+ import Prettyprinter (punctuate )
8286
8387data TxBodyErrorAutoBalance era
8488 = -- | There is not enough ada and non-ada to cover both the outputs and the fees.
@@ -112,6 +116,52 @@ data TxBodyErrorAutoBalance era
112116 TxBodyScriptBadScriptValidity
113117 deriving Show
114118
119+ instance Error (TxBodyErrorAutoBalance era ) where
120+ prettyError = \ case
121+ TxBodyScriptExecutionError failures ->
122+ mconcat
123+ [ " The following scripts have execution failures:\n "
124+ , vsep
125+ [ mconcat
126+ [ " the script for " <> pretty (renderScriptWitnessIndex index)
127+ , " failed with: " <> " \n " <> prettyError failure
128+ ]
129+ | (index, failure) <- failures
130+ ]
131+ ]
132+ TxBodyScriptBadScriptValidity ->
133+ " One or more of the scripts were expected to fail validation, but none did."
134+ TxBodyErrorBalanceNegative lovelace assets ->
135+ mconcat $
136+ [ " The transaction does not balance in its use of assets. The net balance "
137+ , " of the transaction is negative: "
138+ ]
139+ <> punctuate " , " ([pretty lovelace] <> [pretty assets | assets /= mempty ])
140+ <> [ " . The usual solution is to provide more inputs, or inputs with more assets."
141+ ]
142+ TxBodyErrorAdaBalanceTooSmall changeOutput minUTxO balance ->
143+ mconcat
144+ [ " The transaction does balance in its use of ada, however the net "
145+ , " balance does not meet the minimum UTxO threshold. \n "
146+ , " Balance: " <> pretty balance <> " \n "
147+ , " Offending output (change output): " <> pretty (show changeOutput) <> " \n "
148+ , " Minimum UTxO threshold: " <> pretty minUTxO <> " \n "
149+ , " The usual solution is to provide more inputs, or inputs with more ada to "
150+ , " meet the minimum UTxO threshold"
151+ ]
152+ TxBodyErrorMinUTxONotMet txout minUTxO ->
153+ mconcat
154+ [ " Minimum UTxO threshold not met for tx output: " <> pretty (show txout) <> " \n "
155+ , " Minimum required UTxO: " <> pretty minUTxO
156+ ]
157+ TxBodyErrorNonAdaAssetsUnbalanced val ->
158+ " Non-Ada assets are unbalanced: " <> pretty (renderValue val)
159+ TxBodyErrorScriptWitnessIndexMissingFromExecUnitsMap sIndex eUnitsMap ->
160+ mconcat
161+ [ " ScriptWitnessIndex (redeemer pointer): " <> pshow sIndex <> " is missing from the execution "
162+ , " units (redeemer pointer) map: " <> pshow eUnitsMap
163+ ]
164+
115165-- | Use when you do not have access to the UTxOs you intend to spend
116166estimateBalancedTxBody
117167 :: HasCallStack
0 commit comments