@@ -36,7 +36,7 @@ import Cardano.Api.Shelley (ProtocolParameters (protocolParamPrices))
3636import Control.Monad (foldM , void , zipWithM )
3737import Control.Monad.Freer (Eff , Member )
3838import Control.Monad.Trans.Class (lift )
39- import Control.Monad.Trans.Either (EitherT , hoistEither , newEitherT , runEitherT )
39+ import Control.Monad.Trans.Either (EitherT , firstEitherT , hoistEither , newEitherT , runEitherT )
4040import Control.Monad.Trans.Except (throwE )
4141import Data.Bifunctor (bimap )
4242import Data.Coerce (coerce )
@@ -52,12 +52,10 @@ import Data.Text qualified as Text
5252import GHC.Real (Ratio ((:%) ))
5353import Ledger qualified
5454import Ledger.Ada qualified as Ada
55- import Ledger.Address (Address (.. ))
55+ import Ledger.Address (Address (.. ), PaymentPubKeyHash ( PaymentPubKeyHash ) )
5656import Ledger.Constraints.OffChain (UnbalancedTx (.. ))
57- import Ledger.Crypto (PubKeyHash )
5857import Ledger.Interval (
5958 Extended (Finite , NegInf , PosInf ),
60- Interval (Interval ),
6159 LowerBound (LowerBound ),
6260 UpperBound (UpperBound ),
6361 )
@@ -71,13 +69,15 @@ import Ledger.Tx (
7169 TxOutRef (.. ),
7270 )
7371import Ledger.Tx qualified as Tx
72+ import Ledger.Tx.CardanoAPI (ToCardanoError (InvalidValidityRange ))
7473import Ledger.Value (Value )
7574import Ledger.Value qualified as Value
7675import Plutus.V1.Ledger.Api (
7776 CurrencySymbol (.. ),
7877 TokenName (.. ),
7978 )
8079import Prettyprinter (pretty , viaShow , (<+>) )
80+ import Wallet.API as WAPI
8181import Prelude
8282
8383-- Config for balancing a `Tx`.
@@ -101,7 +101,7 @@ balanceTxIO ::
101101 PABConfig ->
102102 PubKeyHash ->
103103 UnbalancedTx ->
104- Eff effs (Either Text Tx )
104+ Eff effs (Either WAPI. WalletAPIError Tx )
105105balanceTxIO = balanceTxIO' @ w defaultBalanceConfig
106106
107107-- | `balanceTxIO'` is more flexible version of `balanceTxIO`, this let us specify custom `BalanceConfig`.
@@ -112,12 +112,12 @@ balanceTxIO' ::
112112 PABConfig ->
113113 PubKeyHash ->
114114 UnbalancedTx ->
115- Eff effs (Either Text Tx )
115+ Eff effs (Either WAPI. WalletAPIError Tx )
116116balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
117117 runEitherT $
118118 do
119119 (utxos, mcollateral) <- newEitherT $ utxosAndCollateralAtAddress @ w balanceCfg pabConf changeAddr
120- privKeys <- newEitherT $ Files. readPrivateKeys @ w pabConf
120+ privKeys <- firstEitherT WAPI. OtherError $ newEitherT $ Files. readPrivateKeys @ w pabConf
121121
122122 let utxoIndex :: Map TxOutRef TxOut
123123 utxoIndex = fmap Tx. toTxOut utxos <> unBalancedTxUtxoIndex unbalancedTx
@@ -142,7 +142,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
142142 if bcHasScripts balanceCfg
143143 then
144144 maybe
145- (throwE " Tx uses script but no collateral was provided." )
145+ (throwE $ WAPI. OtherError " Tx uses script but no collateral was provided." )
146146 (hoistEither . addSignatories ownPkh privKeys requiredSigs . flip addTxCollaterals tx)
147147 mcollateral
148148 else hoistEither $ addSignatories ownPkh privKeys requiredSigs tx
@@ -189,12 +189,13 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
189189 Map PubKeyHash DummyPrivKey ->
190190 [(TxOut , Integer )] ->
191191 Tx ->
192- EitherT Text (Eff effs ) (Tx , [(TxOut , Integer )])
192+ EitherT WAPI. WalletAPIError (Eff effs ) (Tx , [(TxOut , Integer )])
193193 balanceTxLoop utxoIndex privKeys prevMinUtxos tx = do
194194 void $ lift $ Files. writeAll @ w pabConf tx
195195 nextMinUtxos <-
196- newEitherT $
197- calculateMinUtxos @ w pabConf (Tx. txData tx) $ Tx. txOutputs tx \\ map fst prevMinUtxos
196+ firstEitherT WAPI. OtherError $
197+ newEitherT $
198+ calculateMinUtxos @ w pabConf (Tx. txData tx) $ Tx. txOutputs tx \\ map fst prevMinUtxos
198199
199200 let minUtxos = prevMinUtxos ++ nextMinUtxos
200201
@@ -204,9 +205,9 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
204205 txWithoutFees <-
205206 newEitherT $ balanceTxStep @ w balanceCfg minUtxos utxoIndex changeAddr $ tx `withFee` 0
206207
207- exBudget <- newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
208+ exBudget <- firstEitherT WAPI. OtherError $ newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
208209
209- nonBudgettedFees <- newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
210+ nonBudgettedFees <- firstEitherT WAPI. OtherError $ newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
210211
211212 let fees = nonBudgettedFees + getBudgetPrice (getExecutionUnitPrices pabConf) exBudget
212213
@@ -227,10 +228,10 @@ utxosAndCollateralAtAddress ::
227228 BalanceConfig ->
228229 PABConfig ->
229230 Address ->
230- Eff effs (Either Text (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
231+ Eff effs (Either WAPI. WalletAPIError (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
231232utxosAndCollateralAtAddress balanceCfg pabConf changeAddr =
232233 runEitherT $ do
233- utxos <- newEitherT $ CardanoCLI. utxosAt @ w pabConf changeAddr
234+ utxos <- firstEitherT WAPI. OtherError $ newEitherT $ CardanoCLI. utxosAt @ w pabConf changeAddr
234235 inMemCollateral <- lift $ getInMemCollateral @ w
235236
236237 -- check if `bcHasScripts` is true, if this is the case then we search of
@@ -239,8 +240,9 @@ utxosAndCollateralAtAddress balanceCfg pabConf changeAddr =
239240 then
240241 maybe
241242 ( throwE $
242- " The given transaction uses script, but there's no collateral provided."
243- <> " This usually means that, we failed to create Tx and update our ContractEnvironment."
243+ WAPI. OtherError $
244+ " The given transaction uses script, but there's no collateral provided."
245+ <> " This usually means that, we failed to create Tx and update our ContractEnvironment."
244246 )
245247 (const $ pure (removeCollateralFromMap inMemCollateral utxos, inMemCollateral))
246248 inMemCollateral
@@ -288,7 +290,7 @@ balanceTxStep ::
288290 Map TxOutRef TxOut ->
289291 Address ->
290292 Tx ->
291- Eff effs (Either Text Tx )
293+ Eff effs (Either WAPI. WalletAPIError Tx )
292294balanceTxStep balanceCfg minUtxos utxos changeAddr tx =
293295 runEitherT $
294296 (newEitherT . balanceTxIns @ w utxos) (addLovelaces minUtxos tx)
@@ -336,7 +338,7 @@ balanceTxIns ::
336338 Member (PABEffect w ) effs =>
337339 Map TxOutRef TxOut ->
338340 Tx ->
339- Eff effs (Either Text Tx )
341+ Eff effs (Either WAPI. WalletAPIError Tx )
340342balanceTxIns utxos tx = do
341343 runEitherT $ do
342344 let txOuts = Tx. txOutputs tx
@@ -346,7 +348,7 @@ balanceTxIns utxos tx = do
346348 [ txFee tx
347349 , nonMintedValue
348350 ]
349- txIns <- newEitherT $ selectTxIns @ w (txInputs tx) utxos minSpending
351+ txIns <- firstEitherT WAPI. OtherError $ newEitherT $ selectTxIns @ w (txInputs tx) utxos minSpending
350352 pure $ tx {txInputs = txIns <> txInputs tx}
351353
352354-- | Set collateral or fail in case it's required but not available
@@ -363,7 +365,7 @@ txUsesScripts Tx {txInputs, txMintScripts} =
363365 (Set. toList txInputs)
364366
365367-- | Ensures all non ada change goes back to user
366- handleNonAdaChange :: BalanceConfig -> Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx
368+ handleNonAdaChange :: BalanceConfig -> Address -> Map TxOutRef TxOut -> Tx -> Either WAPI. WalletAPIError Tx
367369handleNonAdaChange balanceCfg changeAddr utxos tx =
368370 let nonAdaChange = getNonAdaChange utxos tx
369371 predicate =
@@ -387,7 +389,7 @@ handleNonAdaChange balanceCfg changeAddr utxos tx =
387389 (txOutputs tx)
388390 in if isValueNat nonAdaChange
389391 then Right $ if Value. isZero nonAdaChange then tx else tx {txOutputs = outputs}
390- else Left " Not enough inputs to balance tokens."
392+ else Left $ WAPI. InsufficientFunds " Not enough inputs to balance tokens."
391393
392394{- | `addAdaChange` checks if `bcSeparateChange` is true,
393395 if it is then we add the ada change to seperate `TxOut` at changeAddr that contains only ada,
@@ -431,13 +433,13 @@ addOutput changeAddr tx = tx {txOutputs = txOutputs tx ++ [changeTxOut]}
431433{- | Add the required signatories to the transaction. Be aware the the signature itself is invalid,
432434 and will be ignored. Only the pub key hashes are used, mapped to signing key files on disk.
433435-}
434- addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either Text Tx
436+ addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either WAPI. WalletAPIError Tx
435437addSignatories ownPkh privKeys pkhs tx =
436438 foldM
437439 ( \ tx' pkh ->
438440 case Map. lookup pkh privKeys of
439441 Just privKey -> Right $ Tx. addSignature' (unDummyPrivateKey privKey) tx'
440- Nothing -> Left " Signing key not found. "
442+ Nothing -> Left $ WAPI. PaymentPrivateKeyNotFound $ PaymentPubKeyHash pkh
441443 )
442444 tx
443445 (ownPkh : pkhs)
@@ -447,13 +449,13 @@ addValidRange ::
447449 Member (PABEffect w ) effs =>
448450 POSIXTimeRange ->
449451 Tx ->
450- Eff effs (Either Text Tx )
452+ Eff effs (Either WAPI. WalletAPIError Tx )
451453addValidRange timeRange tx =
452454 if validateRange timeRange
453455 then
454- bimap (Text. pack . show ) (setRange tx)
456+ bimap (WAPI. OtherError . Text. pack . show ) (setRange tx)
455457 <$> posixTimeRangeToContainedSlotRange @ w timeRange
456- else pure $ Left " Invalid validity interval. "
458+ else pure $ Left $ WAPI. ToCardanoError InvalidValidityRange
457459 where
458460 setRange tx' range = tx' {txValidRange = range}
459461
0 commit comments