Skip to content

Commit 01f00e7

Browse files
committed
Add Error instance, need to fix pretty rendering
1 parent d00d7c6 commit 01f00e7

File tree

1 file changed

+50
-0
lines changed
  • cardano-api/src/Cardano/Api/Experimental/Tx/Internal

1 file changed

+50
-0
lines changed

cardano-api/src/Cardano/Api/Experimental/Tx/Internal/Fee.hs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ where
2424
import Cardano.Api.Address
2525
import Cardano.Api.Certificate.Internal
2626
import Cardano.Api.Era.Internal.Eon.Convert
27+
import Cardano.Api.Error
2728
import Cardano.Api.Experimental.AnyScriptWitness
2829
import Cardano.Api.Experimental.AnyScriptWitness qualified as Exp
2930
import Cardano.Api.Experimental.Era
@@ -38,11 +39,13 @@ import Cardano.Api.Plutus.Internal
3839
import Cardano.Api.Plutus.Internal.Script (fromAlonzoExUnits)
3940
import Cardano.Api.Plutus.Internal.Script qualified as Old
4041
import Cardano.Api.Plutus.Internal.ScriptData
42+
import Cardano.Api.Pretty
4143
import Cardano.Api.ProtocolParameters
4244
import Cardano.Api.Query.Internal.Type.QueryInMode
4345
import Cardano.Api.Tx.Internal.Body
4446
( CtxTx
4547
, ScriptWitnessIndex (..)
48+
, renderScriptWitnessIndex
4649
, toScriptIndex
4750
)
4851
import Cardano.Api.Tx.Internal.Fee
@@ -79,6 +82,7 @@ import Data.Set qualified as Set
7982
import GHC.Exts (IsList (..))
8083
import GHC.Stack
8184
import Lens.Micro ((.~), (^.))
85+
import Prettyprinter (punctuate)
8286

8387
data 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
116166
estimateBalancedTxBody
117167
:: HasCallStack

0 commit comments

Comments
 (0)