@@ -36,7 +36,7 @@ import Cardano.Api.Shelley (ProtocolParameters (protocolParamPrices))
36
36
import Control.Monad (foldM , void , zipWithM )
37
37
import Control.Monad.Freer (Eff , Member )
38
38
import 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 )
40
40
import Control.Monad.Trans.Except (throwE )
41
41
import Data.Bifunctor (bimap )
42
42
import Data.Coerce (coerce )
@@ -52,12 +52,10 @@ import Data.Text qualified as Text
52
52
import GHC.Real (Ratio ((:%) ))
53
53
import Ledger qualified
54
54
import Ledger.Ada qualified as Ada
55
- import Ledger.Address (Address (.. ))
55
+ import Ledger.Address (Address (.. ), PaymentPubKeyHash ( PaymentPubKeyHash ) )
56
56
import Ledger.Constraints.OffChain (UnbalancedTx (.. ))
57
- import Ledger.Crypto (PubKeyHash )
58
57
import Ledger.Interval (
59
58
Extended (Finite , NegInf , PosInf ),
60
- Interval (Interval ),
61
59
LowerBound (LowerBound ),
62
60
UpperBound (UpperBound ),
63
61
)
@@ -71,13 +69,15 @@ import Ledger.Tx (
71
69
TxOutRef (.. ),
72
70
)
73
71
import Ledger.Tx qualified as Tx
72
+ import Ledger.Tx.CardanoAPI (ToCardanoError (InvalidValidityRange ))
74
73
import Ledger.Value (Value )
75
74
import Ledger.Value qualified as Value
76
75
import Plutus.V1.Ledger.Api (
77
76
CurrencySymbol (.. ),
78
77
TokenName (.. ),
79
78
)
80
79
import Prettyprinter (pretty , viaShow , (<+>) )
80
+ import Wallet.API as WAPI
81
81
import Prelude
82
82
83
83
-- Config for balancing a `Tx`.
@@ -101,7 +101,7 @@ balanceTxIO ::
101
101
PABConfig ->
102
102
PubKeyHash ->
103
103
UnbalancedTx ->
104
- Eff effs (Either Text Tx )
104
+ Eff effs (Either WAPI. WalletAPIError Tx )
105
105
balanceTxIO = balanceTxIO' @ w defaultBalanceConfig
106
106
107
107
-- | `balanceTxIO'` is more flexible version of `balanceTxIO`, this let us specify custom `BalanceConfig`.
@@ -112,12 +112,12 @@ balanceTxIO' ::
112
112
PABConfig ->
113
113
PubKeyHash ->
114
114
UnbalancedTx ->
115
- Eff effs (Either Text Tx )
115
+ Eff effs (Either WAPI. WalletAPIError Tx )
116
116
balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
117
117
runEitherT $
118
118
do
119
119
(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
121
121
122
122
let utxoIndex :: Map TxOutRef TxOut
123
123
utxoIndex = fmap Tx. toTxOut utxos <> unBalancedTxUtxoIndex unbalancedTx
@@ -142,7 +142,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
142
142
if bcHasScripts balanceCfg
143
143
then
144
144
maybe
145
- (throwE " Tx uses script but no collateral was provided." )
145
+ (throwE $ WAPI. OtherError " Tx uses script but no collateral was provided." )
146
146
(hoistEither . addSignatories ownPkh privKeys requiredSigs . flip addTxCollaterals tx)
147
147
mcollateral
148
148
else hoistEither $ addSignatories ownPkh privKeys requiredSigs tx
@@ -189,12 +189,13 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
189
189
Map PubKeyHash DummyPrivKey ->
190
190
[(TxOut , Integer )] ->
191
191
Tx ->
192
- EitherT Text (Eff effs ) (Tx , [(TxOut , Integer )])
192
+ EitherT WAPI. WalletAPIError (Eff effs ) (Tx , [(TxOut , Integer )])
193
193
balanceTxLoop utxoIndex privKeys prevMinUtxos tx = do
194
194
void $ lift $ Files. writeAll @ w pabConf tx
195
195
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
198
199
199
200
let minUtxos = prevMinUtxos ++ nextMinUtxos
200
201
@@ -204,9 +205,9 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx =
204
205
txWithoutFees <-
205
206
newEitherT $ balanceTxStep @ w balanceCfg minUtxos utxoIndex changeAddr $ tx `withFee` 0
206
207
207
- exBudget <- newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
208
+ exBudget <- firstEitherT WAPI. OtherError $ newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
208
209
209
- nonBudgettedFees <- newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
210
+ nonBudgettedFees <- firstEitherT WAPI. OtherError $ newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
210
211
211
212
let fees = nonBudgettedFees + getBudgetPrice (getExecutionUnitPrices pabConf) exBudget
212
213
@@ -227,10 +228,10 @@ utxosAndCollateralAtAddress ::
227
228
BalanceConfig ->
228
229
PABConfig ->
229
230
Address ->
230
- Eff effs (Either Text (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
231
+ Eff effs (Either WAPI. WalletAPIError (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
231
232
utxosAndCollateralAtAddress balanceCfg pabConf changeAddr =
232
233
runEitherT $ do
233
- utxos <- newEitherT $ CardanoCLI. utxosAt @ w pabConf changeAddr
234
+ utxos <- firstEitherT WAPI. OtherError $ newEitherT $ CardanoCLI. utxosAt @ w pabConf changeAddr
234
235
inMemCollateral <- lift $ getInMemCollateral @ w
235
236
236
237
-- check if `bcHasScripts` is true, if this is the case then we search of
@@ -239,8 +240,9 @@ utxosAndCollateralAtAddress balanceCfg pabConf changeAddr =
239
240
then
240
241
maybe
241
242
( 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."
244
246
)
245
247
(const $ pure (removeCollateralFromMap inMemCollateral utxos, inMemCollateral))
246
248
inMemCollateral
@@ -288,7 +290,7 @@ balanceTxStep ::
288
290
Map TxOutRef TxOut ->
289
291
Address ->
290
292
Tx ->
291
- Eff effs (Either Text Tx )
293
+ Eff effs (Either WAPI. WalletAPIError Tx )
292
294
balanceTxStep balanceCfg minUtxos utxos changeAddr tx =
293
295
runEitherT $
294
296
(newEitherT . balanceTxIns @ w utxos) (addLovelaces minUtxos tx)
@@ -336,7 +338,7 @@ balanceTxIns ::
336
338
Member (PABEffect w ) effs =>
337
339
Map TxOutRef TxOut ->
338
340
Tx ->
339
- Eff effs (Either Text Tx )
341
+ Eff effs (Either WAPI. WalletAPIError Tx )
340
342
balanceTxIns utxos tx = do
341
343
runEitherT $ do
342
344
let txOuts = Tx. txOutputs tx
@@ -346,7 +348,7 @@ balanceTxIns utxos tx = do
346
348
[ txFee tx
347
349
, nonMintedValue
348
350
]
349
- txIns <- newEitherT $ selectTxIns @ w (txInputs tx) utxos minSpending
351
+ txIns <- firstEitherT WAPI. OtherError $ newEitherT $ selectTxIns @ w (txInputs tx) utxos minSpending
350
352
pure $ tx {txInputs = txIns <> txInputs tx}
351
353
352
354
-- | Set collateral or fail in case it's required but not available
@@ -363,7 +365,7 @@ txUsesScripts Tx {txInputs, txMintScripts} =
363
365
(Set. toList txInputs)
364
366
365
367
-- | 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
367
369
handleNonAdaChange balanceCfg changeAddr utxos tx =
368
370
let nonAdaChange = getNonAdaChange utxos tx
369
371
predicate =
@@ -387,7 +389,7 @@ handleNonAdaChange balanceCfg changeAddr utxos tx =
387
389
(txOutputs tx)
388
390
in if isValueNat nonAdaChange
389
391
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."
391
393
392
394
{- | `addAdaChange` checks if `bcSeparateChange` is true,
393
395
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]}
431
433
{- | Add the required signatories to the transaction. Be aware the the signature itself is invalid,
432
434
and will be ignored. Only the pub key hashes are used, mapped to signing key files on disk.
433
435
-}
434
- addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either Text Tx
436
+ addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either WAPI. WalletAPIError Tx
435
437
addSignatories ownPkh privKeys pkhs tx =
436
438
foldM
437
439
( \ tx' pkh ->
438
440
case Map. lookup pkh privKeys of
439
441
Just privKey -> Right $ Tx. addSignature' (unDummyPrivateKey privKey) tx'
440
- Nothing -> Left " Signing key not found. "
442
+ Nothing -> Left $ WAPI. PaymentPrivateKeyNotFound $ PaymentPubKeyHash pkh
441
443
)
442
444
tx
443
445
(ownPkh : pkhs)
@@ -447,13 +449,13 @@ addValidRange ::
447
449
Member (PABEffect w ) effs =>
448
450
POSIXTimeRange ->
449
451
Tx ->
450
- Eff effs (Either Text Tx )
452
+ Eff effs (Either WAPI. WalletAPIError Tx )
451
453
addValidRange timeRange tx =
452
454
if validateRange timeRange
453
455
then
454
- bimap (Text. pack . show ) (setRange tx)
456
+ bimap (WAPI. OtherError . Text. pack . show ) (setRange tx)
455
457
<$> posixTimeRangeToContainedSlotRange @ w timeRange
456
- else pure $ Left " Invalid validity interval. "
458
+ else pure $ Left $ WAPI. ToCardanoError InvalidValidityRange
457
459
where
458
460
setRange tx' range = tx' {txValidRange = range}
459
461
0 commit comments