@@ -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 , firstEitherT , hoistEither , newEitherT , runEitherT )
39+ import Control.Monad.Trans.Either (EitherT , hoistEither , newEitherT , runEitherT )
4040import Control.Monad.Trans.Except (throwE )
4141import Data.Bifunctor (bimap )
4242import Data.Coerce (coerce )
@@ -52,10 +52,12 @@ import Data.Text qualified as Text
5252import GHC.Real (Ratio ((:%) ))
5353import Ledger qualified
5454import Ledger.Ada qualified as Ada
55- import Ledger.Address (Address (.. ), PaymentPubKeyHash ( PaymentPubKeyHash ) )
55+ import Ledger.Address (Address (.. ))
5656import Ledger.Constraints.OffChain (UnbalancedTx (.. ))
57+ import Ledger.Crypto (PubKeyHash )
5758import Ledger.Interval (
5859 Extended (Finite , NegInf , PosInf ),
60+ Interval (Interval ),
5961 LowerBound (LowerBound ),
6062 UpperBound (UpperBound ),
6163 )
@@ -69,15 +71,13 @@ import Ledger.Tx (
6971 TxOutRef (.. ),
7072 )
7173import Ledger.Tx qualified as Tx
72- import Ledger.Tx.CardanoAPI (ToCardanoError (InvalidValidityRange ))
7374import Ledger.Value (Value )
7475import Ledger.Value qualified as Value
7576import Plutus.V1.Ledger.Api (
7677 CurrencySymbol (.. ),
7778 TokenName (.. ),
7879 )
7980import 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 WAPI. WalletAPIError Tx )
104+ Eff effs (Either Text 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 WAPI. WalletAPIError Tx )
115+ Eff effs (Either Text Tx )
116116balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
117117 runEitherT $
118118 do
119119 (utxos, mcollateral) <- newEitherT $ utxosAndCollateralAtAddress @ w balanceCfg pabConf changeAddr
120- privKeys <- firstEitherT WAPI. OtherError $ newEitherT $ Files. readPrivateKeys @ w pabConf
120+ privKeys <- 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 $ WAPI. OtherError " Tx uses script but no collateral was provided." )
145+ (throwE " 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,13 +189,12 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
189189 Map PubKeyHash DummyPrivKey ->
190190 [(TxOut , Integer )] ->
191191 Tx ->
192- EitherT WAPI. WalletAPIError (Eff effs ) (Tx , [(TxOut , Integer )])
192+ EitherT Text (Eff effs ) (Tx , [(TxOut , Integer )])
193193 balanceTxLoop utxoIndex privKeys prevMinUtxos tx = do
194194 void $ lift $ Files. writeAll @ w pabConf tx
195195 nextMinUtxos <-
196- firstEitherT WAPI. OtherError $
197- newEitherT $
198- calculateMinUtxos @ w pabConf (Tx. txData tx) $ Tx. txOutputs tx \\ map fst prevMinUtxos
196+ newEitherT $
197+ calculateMinUtxos @ w pabConf (Tx. txData tx) $ Tx. txOutputs tx \\ map fst prevMinUtxos
199198
200199 let minUtxos = prevMinUtxos ++ nextMinUtxos
201200
@@ -205,9 +204,9 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
205204 txWithoutFees <-
206205 newEitherT $ balanceTxStep @ w balanceCfg minUtxos utxoIndex changeAddr $ tx `withFee` 0
207206
208- exBudget <- firstEitherT WAPI. OtherError $ newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
207+ exBudget <- newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
209208
210- nonBudgettedFees <- firstEitherT WAPI. OtherError $ newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
209+ nonBudgettedFees <- newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
211210
212211 let fees = nonBudgettedFees + getBudgetPrice (getExecutionUnitPrices pabConf) exBudget
213212
@@ -228,10 +227,10 @@ utxosAndCollateralAtAddress ::
228227 BalanceConfig ->
229228 PABConfig ->
230229 Address ->
231- Eff effs (Either WAPI. WalletAPIError (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
230+ Eff effs (Either Text (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
232231utxosAndCollateralAtAddress balanceCfg pabConf changeAddr =
233232 runEitherT $ do
234- utxos <- firstEitherT WAPI. OtherError $ newEitherT $ CardanoCLI. utxosAt @ w pabConf changeAddr
233+ utxos <- newEitherT $ CardanoCLI. utxosAt @ w pabConf changeAddr
235234 inMemCollateral <- lift $ getInMemCollateral @ w
236235
237236 -- check if `bcHasScripts` is true, if this is the case then we search of
@@ -240,9 +239,8 @@ utxosAndCollateralAtAddress balanceCfg pabConf changeAddr =
240239 then
241240 maybe
242241 ( throwE $
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."
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."
246244 )
247245 (const $ pure (removeCollateralFromMap inMemCollateral utxos, inMemCollateral))
248246 inMemCollateral
@@ -290,7 +288,7 @@ balanceTxStep ::
290288 Map TxOutRef TxOut ->
291289 Address ->
292290 Tx ->
293- Eff effs (Either WAPI. WalletAPIError Tx )
291+ Eff effs (Either Text Tx )
294292balanceTxStep balanceCfg minUtxos utxos changeAddr tx =
295293 runEitherT $
296294 (newEitherT . balanceTxIns @ w utxos) (addLovelaces minUtxos tx)
@@ -338,7 +336,7 @@ balanceTxIns ::
338336 Member (PABEffect w ) effs =>
339337 Map TxOutRef TxOut ->
340338 Tx ->
341- Eff effs (Either WAPI. WalletAPIError Tx )
339+ Eff effs (Either Text Tx )
342340balanceTxIns utxos tx = do
343341 runEitherT $ do
344342 let txOuts = Tx. txOutputs tx
@@ -348,7 +346,7 @@ balanceTxIns utxos tx = do
348346 [ txFee tx
349347 , nonMintedValue
350348 ]
351- txIns <- firstEitherT WAPI. OtherError $ newEitherT $ selectTxIns @ w (txInputs tx) utxos minSpending
349+ txIns <- newEitherT $ selectTxIns @ w (txInputs tx) utxos minSpending
352350 pure $ tx {txInputs = txIns <> txInputs tx}
353351
354352-- | Set collateral or fail in case it's required but not available
@@ -365,7 +363,7 @@ txUsesScripts Tx {txInputs, txMintScripts} =
365363 (Set. toList txInputs)
366364
367365-- | Ensures all non ada change goes back to user
368- handleNonAdaChange :: BalanceConfig -> Address -> Map TxOutRef TxOut -> Tx -> Either WAPI. WalletAPIError Tx
366+ handleNonAdaChange :: BalanceConfig -> Address -> Map TxOutRef TxOut -> Tx -> Either Text Tx
369367handleNonAdaChange balanceCfg changeAddr utxos tx =
370368 let nonAdaChange = getNonAdaChange utxos tx
371369 predicate =
@@ -389,7 +387,7 @@ handleNonAdaChange balanceCfg changeAddr utxos tx =
389387 (txOutputs tx)
390388 in if isValueNat nonAdaChange
391389 then Right $ if Value. isZero nonAdaChange then tx else tx {txOutputs = outputs}
392- else Left $ WAPI. InsufficientFunds " Not enough inputs to balance tokens."
390+ else Left " Not enough inputs to balance tokens."
393391
394392{- | `addAdaChange` checks if `bcSeparateChange` is true,
395393 if it is then we add the ada change to seperate `TxOut` at changeAddr that contains only ada,
@@ -433,13 +431,13 @@ addOutput changeAddr tx = tx {txOutputs = txOutputs tx ++ [changeTxOut]}
433431{- | Add the required signatories to the transaction. Be aware the the signature itself is invalid,
434432 and will be ignored. Only the pub key hashes are used, mapped to signing key files on disk.
435433-}
436- addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either WAPI. WalletAPIError Tx
434+ addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either Text Tx
437435addSignatories ownPkh privKeys pkhs tx =
438436 foldM
439437 ( \ tx' pkh ->
440438 case Map. lookup pkh privKeys of
441439 Just privKey -> Right $ Tx. addSignature' (unDummyPrivateKey privKey) tx'
442- Nothing -> Left $ WAPI. PaymentPrivateKeyNotFound $ PaymentPubKeyHash pkh
440+ Nothing -> Left " Signing key not found. "
443441 )
444442 tx
445443 (ownPkh : pkhs)
@@ -449,13 +447,13 @@ addValidRange ::
449447 Member (PABEffect w ) effs =>
450448 POSIXTimeRange ->
451449 Tx ->
452- Eff effs (Either WAPI. WalletAPIError Tx )
450+ Eff effs (Either Text Tx )
453451addValidRange timeRange tx =
454452 if validateRange timeRange
455453 then
456- bimap (WAPI. OtherError . Text. pack . show ) (setRange tx)
454+ bimap (Text. pack . show ) (setRange tx)
457455 <$> posixTimeRangeToContainedSlotRange @ w timeRange
458- else pure $ Left $ WAPI. ToCardanoError InvalidValidityRange
456+ else pure $ Left " Invalid validity interval. "
459457 where
460458 setRange tx' range = tx' {txValidRange = range}
461459
0 commit comments