@@ -55,7 +55,7 @@ import Data.Text qualified as Text
5555import GHC.Real (Ratio ((:%) ))
5656import Ledger qualified
5757import Ledger.Ada qualified as Ada
58- import Ledger.Address (Address (.. ))
58+ import Ledger.Address (Address (.. ), PaymentPubKeyHash ( PaymentPubKeyHash ) )
5959import Ledger.Constraints.OffChain (UnbalancedTx (.. ))
6060import Ledger.Crypto (PubKeyHash )
6161import Ledger.Interval (
@@ -66,6 +66,7 @@ import Ledger.Interval (
6666 )
6767import Ledger.Time (POSIXTimeRange )
6868import Ledger.Tx (
69+ ToCardanoError (InvalidValidityRange ),
6970 Tx (.. ),
7071 TxIn (.. ),
7172 TxInType (.. ),
@@ -83,6 +84,7 @@ import Plutus.V1.Ledger.Api (
8384
8485import Ledger.Constraints.OffChain qualified as Constraints
8586import Prettyprinter (pretty , viaShow , (<+>) )
87+ import Wallet.API qualified as WAPI
8688import Prelude
8789
8890-- Config for balancing a `Tx`.
@@ -106,7 +108,7 @@ balanceTxIO ::
106108 PABConfig ->
107109 PubKeyHash ->
108110 UnbalancedTx ->
109- Eff effs (Either Text Tx )
111+ Eff effs (Either WAPI. WalletAPIError Tx )
110112balanceTxIO = balanceTxIO' @ w defaultBalanceConfig
111113
112114-- | `balanceTxIO'` is more flexible version of `balanceTxIO`, this lets us specify custom `BalanceConfig`.
@@ -117,12 +119,12 @@ balanceTxIO' ::
117119 PABConfig ->
118120 PubKeyHash ->
119121 UnbalancedTx ->
120- Eff effs (Either Text Tx )
122+ Eff effs (Either WAPI. WalletAPIError Tx )
121123balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx' =
122124 runEitherT $
123125 do
124126 updatedOuts <-
125- firstEitherT ( Text. pack . show ) $
127+ firstEitherT WAPI. OtherError $
126128 newEitherT $
127129 sequence <$> traverse (minUtxo @ w ) (unbalancedTx' ^. Constraints. tx . Tx. outputs)
128130
@@ -136,7 +138,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx' =
136138 pabConf
137139 changeAddr
138140
139- privKeys <- newEitherT $ Files. readPrivateKeys @ w pabConf
141+ privKeys <- firstEitherT WAPI. OtherError $ newEitherT $ Files. readPrivateKeys @ w pabConf
140142
141143 let utxoIndex :: Map TxOutRef TxOut
142144 utxoIndex = fmap Tx. toTxOut utxos <> unBalancedTxUtxoIndex unbalancedTx
@@ -163,14 +165,14 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx' =
163165 if bcHasScripts balanceCfg
164166 then
165167 maybe
166- (throwE " Tx uses script but no collateral was provided." )
168+ (throwE $ WAPI. OtherError " Tx uses script but no collateral was provided." )
167169 (hoistEither . addSignatories ownPkh privKeys requiredSigs . flip addTxCollaterals tx)
168170 mcollateral
169171 else hoistEither $ addSignatories ownPkh privKeys requiredSigs tx
170172
171173 -- Balance the tx
172174 balancedTx <- balanceTxLoop utxoIndex privKeys preBalancedTx
173- changeTxOutWithMinAmt <- newEitherT $ addOutput @ w changeAddr balancedTx
175+ changeTxOutWithMinAmt <- firstEitherT WAPI. OtherError $ newEitherT $ addOutput @ w changeAddr balancedTx
174176
175177 -- Get current Ada change
176178 let adaChange = getAdaChange utxoIndex balancedTx
@@ -213,17 +215,17 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx' =
213215 Map TxOutRef TxOut ->
214216 Map PubKeyHash DummyPrivKey ->
215217 Tx ->
216- EitherT Text (Eff effs ) Tx
218+ EitherT WAPI. WalletAPIError (Eff effs ) Tx
217219 balanceTxLoop utxoIndex privKeys tx = do
218220 void $ lift $ Files. writeAll @ w pabConf tx
219221
220222 -- Calculate fees by pre-balancing the tx, building it, and running the CLI on result
221223 txWithoutFees <-
222224 newEitherT $ balanceTxStep @ w balanceCfg utxoIndex changeAddr $ tx `withFee` 0
223225
224- exBudget <- newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
226+ exBudget <- firstEitherT WAPI. OtherError $ newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
225227
226- nonBudgettedFees <- newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
228+ nonBudgettedFees <- firstEitherT WAPI. OtherError $ newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
227229
228230 let fees = nonBudgettedFees + getBudgetPrice (getExecutionUnitPrices pabConf) exBudget
229231
@@ -244,7 +246,7 @@ utxosAndCollateralAtAddress ::
244246 BalanceConfig ->
245247 PABConfig ->
246248 Address ->
247- Eff effs (Either Text (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
249+ Eff effs (Either WAPI. WalletAPIError (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
248250utxosAndCollateralAtAddress balanceCfg _pabConf changeAddr =
249251 runEitherT $ do
250252 inMemCollateral <- lift $ getInMemCollateral @ w
@@ -254,16 +256,17 @@ utxosAndCollateralAtAddress balanceCfg _pabConf changeAddr =
254256 (UtxosAtExcluding changeAddr . Set. singleton . collateralTxOutRef)
255257 inMemCollateral
256258
257- utxos <- firstEitherT (Text. pack . show ) $ newEitherT $ queryNode @ w nodeQuery
259+ utxos <- firstEitherT (WAPI. OtherError . Text. pack . show ) $ newEitherT $ queryNode @ w nodeQuery
258260
259261 -- check if `bcHasScripts` is true, if this is the case then we search of
260262 -- collateral UTxO in the environment, if such collateral is not present we throw Error.
261263 if bcHasScripts balanceCfg
262264 then
263265 maybe
264266 ( throwE $
265- " The given transaction uses script, but there's no collateral provided."
266- <> " This usually means that, we failed to create Tx and update our ContractEnvironment."
267+ WAPI. OtherError $
268+ " The given transaction uses script, but there's no collateral provided."
269+ <> " This usually means that, we failed to create Tx and update our ContractEnvironment."
267270 )
268271 (const $ pure (utxos, inMemCollateral))
269272 inMemCollateral
@@ -302,7 +305,7 @@ balanceTxStep ::
302305 Map TxOutRef TxOut ->
303306 Address ->
304307 Tx ->
305- Eff effs (Either Text Tx )
308+ Eff effs (Either WAPI. WalletAPIError Tx )
306309balanceTxStep balanceCfg utxos changeAddr tx =
307310 runEitherT $
308311 (newEitherT . balanceTxIns @ w utxos) tx
@@ -339,7 +342,7 @@ balanceTxIns ::
339342 Member (PABEffect w ) effs =>
340343 Map TxOutRef TxOut ->
341344 Tx ->
342- Eff effs (Either Text Tx )
345+ Eff effs (Either WAPI. WalletAPIError Tx )
343346balanceTxIns utxos tx = do
344347 runEitherT $ do
345348 let txOuts = Tx. txOutputs tx
@@ -377,7 +380,7 @@ handleNonAdaChange ::
377380 Address ->
378381 Map TxOutRef TxOut ->
379382 Tx ->
380- Eff effs (Either Text Tx )
383+ Eff effs (Either WAPI. WalletAPIError Tx )
381384handleNonAdaChange balanceCfg changeAddr utxos tx = runEitherT $ do
382385 let nonAdaChange :: Value
383386 nonAdaChange = getNonAdaChange utxos tx
@@ -403,7 +406,7 @@ handleNonAdaChange balanceCfg changeAddr utxos tx = runEitherT $ do
403406 }
404407
405408 newOutputWithMinAmt <-
406- firstEitherT ( Text. pack . show ) $
409+ firstEitherT WAPI. OtherError $
407410 newEitherT $ minUtxo @ w newOutput
408411
409412 let outputs :: [TxOut ]
@@ -415,7 +418,7 @@ handleNonAdaChange balanceCfg changeAddr utxos tx = runEitherT $ do
415418
416419 if isValueNat nonAdaChange
417420 then return $ if Value. isZero nonAdaChange then tx else tx {txOutputs = outputs}
418- else throwE " Not enough inputs to balance tokens."
421+ else throwE $ WAPI. InsufficientFunds " Not enough inputs to balance tokens."
419422
420423{- | `addAdaChange` checks if `bcSeparateChange` is true,
421424 if it is then we add the ada change to seperate `TxOut` at changeAddr that contains only ada,
@@ -467,22 +470,21 @@ addOutput changeAddr tx =
467470 }
468471
469472 changeTxOutWithMinAmt <-
470- firstEitherT (Text. pack . show ) $
471- newEitherT $
472- minUtxo @ w changeTxOut
473+ newEitherT $
474+ minUtxo @ w changeTxOut
473475
474476 return $ tx {txOutputs = txOutputs tx ++ [changeTxOutWithMinAmt]}
475477
476478{- | Add the required signatories to the transaction. Be aware the the signature itself is invalid,
477479 and will be ignored. Only the pub key hashes are used, mapped to signing key files on disk.
478480-}
479- addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either Text Tx
481+ addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either WAPI. WalletAPIError Tx
480482addSignatories ownPkh privKeys pkhs tx =
481483 foldM
482484 ( \ tx' pkh ->
483485 case Map. lookup pkh privKeys of
484486 Just privKey -> Right $ Tx. addSignature' (unDummyPrivateKey privKey) tx'
485- Nothing -> Left " Signing key not found. "
487+ Nothing -> Left $ WAPI. PaymentPrivateKeyNotFound $ PaymentPubKeyHash pkh
486488 )
487489 tx
488490 (ownPkh : pkhs)
@@ -492,14 +494,14 @@ addValidRange ::
492494 Member (PABEffect w ) effs =>
493495 POSIXTimeRange ->
494496 Either CardanoBuildTx Tx ->
495- Eff effs (Either Text Tx )
496- addValidRange _ (Left _) = pure $ Left " BPI is not using CardanoBuildTx"
497+ Eff effs (Either WAPI. WalletAPIError Tx )
498+ addValidRange _ (Left _) = pure $ Left $ WAPI. OtherError " BPI is not using CardanoBuildTx"
497499addValidRange timeRange (Right tx) =
498500 if validateRange timeRange
499501 then
500- bimap (Text. pack . show ) (setRange tx)
502+ bimap (WAPI. OtherError . Text. pack . show ) (setRange tx)
501503 <$> posixTimeRangeToContainedSlotRange @ w timeRange
502- else pure $ Left " Invalid validity interval. "
504+ else pure $ Left $ WAPI. ToCardanoError InvalidValidityRange
503505 where
504506 setRange tx' range = tx' {txValidRange = range}
505507
0 commit comments