Skip to content

Commit fa2993b

Browse files
committed
BalancingError with explicit script evaluation errors
* Makes script evaluation errors explicit by wrapping those, and only those, in 'ScriptExecutionError' * Should, in turn, allow using classy prisms to extract just the script execution errors. * The errors themselves are, again, shown as strings, since the error hierarchy allows json encoding, which is not allowed by the actual script execution error.
1 parent 501d639 commit fa2993b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/coin-selection/lib/Convex/CoinSelection.hs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{-# LANGUAGE BangPatterns #-}
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE DeriveAnyClass #-}
4-
{-# LANGUAGE DerivingStrategies #-}
4+
{-# LANGUAGE DerivingVia #-}
55
{-# LANGUAGE FlexibleInstances #-}
66
{-# LANGUAGE FunctionalDependencies #-}
77
{-# LANGUAGE GADTs #-}
@@ -136,7 +136,7 @@ import Convex.Utxos (
136136
import Convex.Utxos qualified as Utxos
137137
import Convex.Wallet (Wallet)
138138
import Convex.Wallet qualified as Wallet
139-
import Data.Aeson (FromJSON, ToJSON)
139+
import Data.Aeson (FromJSON (..), ToJSON (..))
140140
import Data.Bifunctor (Bifunctor (..))
141141
import Data.Default (Default (..))
142142
import Data.Function (on)
@@ -216,8 +216,14 @@ makeClassyPrisms ''CoinSelectionError
216216
bodyError :: C.TxBodyError -> CoinSelectionError
217217
bodyError = BodyError . Text.pack . C.docToString . C.prettyError
218218

219+
-- | Balancing errors, including the *special* 'BalancingScriptExecutionErr'.
219220
data BalancingError era
220221
= BalancingError Text
222+
| -- | A single type of balancing error is treated specially: the type with
223+
-- 'C.ScriptExecutionError's.
224+
-- TODO: I would like to retain the actual error structure, but this collides (quite massively)
225+
-- with the required JSON encoding / decoding.
226+
ScriptExecutionErr Text -- [(C.ScriptWitnessIndex, C.ScriptExecutionError)]
221227
| CheckMinUtxoValueError (C.TxOut C.CtxTx era) C.Quantity
222228
| BalanceCheckError (BalancingError era)
223229
| ComputeBalanceChangeError
@@ -226,8 +232,16 @@ data BalancingError era
226232

227233
makeClassyPrisms ''BalancingError
228234

235+
{- | Sort *most* balancing errors into 'BalancingError', but script execution errors into their own
236+
data constructor.
237+
-}
229238
balancingError :: (MonadError (BalancingError era) m) => Either (C.TxBodyErrorAutoBalance era) a -> m a
230-
balancingError = either (throwError . BalancingError . Text.pack . C.docToString . C.prettyError) pure
239+
balancingError = \case
240+
Right a -> pure a
241+
Left err@(C.TxBodyScriptExecutionError _es) -> throwError $ ScriptExecutionErr (asText err)
242+
Left err -> throwError . BalancingError $ asText err
243+
where
244+
asText = Text.pack . C.docToString . C.prettyError
231245

232246
-- | Messages that are produced during coin selection and balancing
233247
data TxBalancingMessage

0 commit comments

Comments
 (0)