@@ -55,7 +55,7 @@ import Data.Text qualified as Text
55
55
import GHC.Real (Ratio ((:%) ))
56
56
import Ledger qualified
57
57
import Ledger.Ada qualified as Ada
58
- import Ledger.Address (Address (.. ))
58
+ import Ledger.Address (Address (.. ), PaymentPubKeyHash ( PaymentPubKeyHash ) )
59
59
import Ledger.Constraints.OffChain (UnbalancedTx (.. ))
60
60
import Ledger.Crypto (PubKeyHash )
61
61
import Ledger.Interval (
@@ -66,6 +66,7 @@ import Ledger.Interval (
66
66
)
67
67
import Ledger.Time (POSIXTimeRange )
68
68
import Ledger.Tx (
69
+ ToCardanoError (InvalidValidityRange ),
69
70
Tx (.. ),
70
71
TxIn (.. ),
71
72
TxInType (.. ),
@@ -83,6 +84,7 @@ import Plutus.V1.Ledger.Api (
83
84
84
85
import Ledger.Constraints.OffChain qualified as Constraints
85
86
import Prettyprinter (pretty , viaShow , (<+>) )
87
+ import Wallet.API qualified as WAPI
86
88
import Prelude
87
89
88
90
-- Config for balancing a `Tx`.
@@ -106,7 +108,7 @@ balanceTxIO ::
106
108
PABConfig ->
107
109
PubKeyHash ->
108
110
UnbalancedTx ->
109
- Eff effs (Either Text Tx )
111
+ Eff effs (Either WAPI. WalletAPIError Tx )
110
112
balanceTxIO = balanceTxIO' @ w defaultBalanceConfig
111
113
112
114
-- | `balanceTxIO'` is more flexible version of `balanceTxIO`, this lets us specify custom `BalanceConfig`.
@@ -117,12 +119,12 @@ balanceTxIO' ::
117
119
PABConfig ->
118
120
PubKeyHash ->
119
121
UnbalancedTx ->
120
- Eff effs (Either Text Tx )
122
+ Eff effs (Either WAPI. WalletAPIError Tx )
121
123
balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx' =
122
124
runEitherT $
123
125
do
124
126
updatedOuts <-
125
- firstEitherT ( Text. pack . show ) $
127
+ firstEitherT WAPI. OtherError $
126
128
newEitherT $
127
129
sequence <$> traverse (minUtxo @ w ) (unbalancedTx' ^. Constraints. tx . Tx. outputs)
128
130
@@ -136,7 +138,7 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx' =
136
138
pabConf
137
139
changeAddr
138
140
139
- privKeys <- newEitherT $ Files. readPrivateKeys @ w pabConf
141
+ privKeys <- firstEitherT WAPI. OtherError $ newEitherT $ Files. readPrivateKeys @ w pabConf
140
142
141
143
let utxoIndex :: Map TxOutRef TxOut
142
144
utxoIndex = fmap Tx. toTxOut utxos <> unBalancedTxUtxoIndex unbalancedTx
@@ -163,14 +165,14 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx' =
163
165
if bcHasScripts balanceCfg
164
166
then
165
167
maybe
166
- (throwE " Tx uses script but no collateral was provided." )
168
+ (throwE $ WAPI. OtherError " Tx uses script but no collateral was provided." )
167
169
(hoistEither . addSignatories ownPkh privKeys requiredSigs . flip addTxCollaterals tx)
168
170
mcollateral
169
171
else hoistEither $ addSignatories ownPkh privKeys requiredSigs tx
170
172
171
173
-- Balance the tx
172
174
balancedTx <- balanceTxLoop utxoIndex privKeys preBalancedTx
173
- changeTxOutWithMinAmt <- newEitherT $ addOutput @ w changeAddr balancedTx
175
+ changeTxOutWithMinAmt <- firstEitherT WAPI. OtherError $ newEitherT $ addOutput @ w changeAddr balancedTx
174
176
175
177
-- Get current Ada change
176
178
let adaChange = getAdaChange utxoIndex balancedTx
@@ -213,17 +215,17 @@ balanceTxIO' balanceCfg pabConf ownPkh unbalancedTx' =
213
215
Map TxOutRef TxOut ->
214
216
Map PubKeyHash DummyPrivKey ->
215
217
Tx ->
216
- EitherT Text (Eff effs ) Tx
218
+ EitherT WAPI. WalletAPIError (Eff effs ) Tx
217
219
balanceTxLoop utxoIndex privKeys tx = do
218
220
void $ lift $ Files. writeAll @ w pabConf tx
219
221
220
222
-- Calculate fees by pre-balancing the tx, building it, and running the CLI on result
221
223
txWithoutFees <-
222
224
newEitherT $ balanceTxStep @ w balanceCfg utxoIndex changeAddr $ tx `withFee` 0
223
225
224
- exBudget <- newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
226
+ exBudget <- firstEitherT WAPI. OtherError $ newEitherT $ BodyBuilder. buildAndEstimateBudget @ w pabConf privKeys txWithoutFees
225
227
226
- nonBudgettedFees <- newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
228
+ nonBudgettedFees <- firstEitherT WAPI. OtherError $ newEitherT $ CardanoCLI. calculateMinFee @ w pabConf txWithoutFees
227
229
228
230
let fees = nonBudgettedFees + getBudgetPrice (getExecutionUnitPrices pabConf) exBudget
229
231
@@ -244,7 +246,7 @@ utxosAndCollateralAtAddress ::
244
246
BalanceConfig ->
245
247
PABConfig ->
246
248
Address ->
247
- Eff effs (Either Text (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
249
+ Eff effs (Either WAPI. WalletAPIError (Map TxOutRef Tx. ChainIndexTxOut , Maybe CollateralUtxo ))
248
250
utxosAndCollateralAtAddress balanceCfg _pabConf changeAddr =
249
251
runEitherT $ do
250
252
inMemCollateral <- lift $ getInMemCollateral @ w
@@ -254,16 +256,17 @@ utxosAndCollateralAtAddress balanceCfg _pabConf changeAddr =
254
256
(UtxosAtExcluding changeAddr . Set. singleton . collateralTxOutRef)
255
257
inMemCollateral
256
258
257
- utxos <- firstEitherT (Text. pack . show ) $ newEitherT $ queryNode @ w nodeQuery
259
+ utxos <- firstEitherT (WAPI. OtherError . Text. pack . show ) $ newEitherT $ queryNode @ w nodeQuery
258
260
259
261
-- check if `bcHasScripts` is true, if this is the case then we search of
260
262
-- collateral UTxO in the environment, if such collateral is not present we throw Error.
261
263
if bcHasScripts balanceCfg
262
264
then
263
265
maybe
264
266
( 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."
267
270
)
268
271
(const $ pure (utxos, inMemCollateral))
269
272
inMemCollateral
@@ -302,7 +305,7 @@ balanceTxStep ::
302
305
Map TxOutRef TxOut ->
303
306
Address ->
304
307
Tx ->
305
- Eff effs (Either Text Tx )
308
+ Eff effs (Either WAPI. WalletAPIError Tx )
306
309
balanceTxStep balanceCfg utxos changeAddr tx =
307
310
runEitherT $
308
311
(newEitherT . balanceTxIns @ w utxos) tx
@@ -339,7 +342,7 @@ balanceTxIns ::
339
342
Member (PABEffect w ) effs =>
340
343
Map TxOutRef TxOut ->
341
344
Tx ->
342
- Eff effs (Either Text Tx )
345
+ Eff effs (Either WAPI. WalletAPIError Tx )
343
346
balanceTxIns utxos tx = do
344
347
runEitherT $ do
345
348
let txOuts = Tx. txOutputs tx
@@ -377,7 +380,7 @@ handleNonAdaChange ::
377
380
Address ->
378
381
Map TxOutRef TxOut ->
379
382
Tx ->
380
- Eff effs (Either Text Tx )
383
+ Eff effs (Either WAPI. WalletAPIError Tx )
381
384
handleNonAdaChange balanceCfg changeAddr utxos tx = runEitherT $ do
382
385
let nonAdaChange :: Value
383
386
nonAdaChange = getNonAdaChange utxos tx
@@ -403,7 +406,7 @@ handleNonAdaChange balanceCfg changeAddr utxos tx = runEitherT $ do
403
406
}
404
407
405
408
newOutputWithMinAmt <-
406
- firstEitherT ( Text. pack . show ) $
409
+ firstEitherT WAPI. OtherError $
407
410
newEitherT $ minUtxo @ w newOutput
408
411
409
412
let outputs :: [TxOut ]
@@ -415,7 +418,7 @@ handleNonAdaChange balanceCfg changeAddr utxos tx = runEitherT $ do
415
418
416
419
if isValueNat nonAdaChange
417
420
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."
419
422
420
423
{- | `addAdaChange` checks if `bcSeparateChange` is true,
421
424
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 =
467
470
}
468
471
469
472
changeTxOutWithMinAmt <-
470
- firstEitherT (Text. pack . show ) $
471
- newEitherT $
472
- minUtxo @ w changeTxOut
473
+ newEitherT $
474
+ minUtxo @ w changeTxOut
473
475
474
476
return $ tx {txOutputs = txOutputs tx ++ [changeTxOutWithMinAmt]}
475
477
476
478
{- | Add the required signatories to the transaction. Be aware the the signature itself is invalid,
477
479
and will be ignored. Only the pub key hashes are used, mapped to signing key files on disk.
478
480
-}
479
- addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either Text Tx
481
+ addSignatories :: PubKeyHash -> Map PubKeyHash DummyPrivKey -> [PubKeyHash ] -> Tx -> Either WAPI. WalletAPIError Tx
480
482
addSignatories ownPkh privKeys pkhs tx =
481
483
foldM
482
484
( \ tx' pkh ->
483
485
case Map. lookup pkh privKeys of
484
486
Just privKey -> Right $ Tx. addSignature' (unDummyPrivateKey privKey) tx'
485
- Nothing -> Left " Signing key not found. "
487
+ Nothing -> Left $ WAPI. PaymentPrivateKeyNotFound $ PaymentPubKeyHash pkh
486
488
)
487
489
tx
488
490
(ownPkh : pkhs)
@@ -492,14 +494,14 @@ addValidRange ::
492
494
Member (PABEffect w ) effs =>
493
495
POSIXTimeRange ->
494
496
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"
497
499
addValidRange timeRange (Right tx) =
498
500
if validateRange timeRange
499
501
then
500
- bimap (Text. pack . show ) (setRange tx)
502
+ bimap (WAPI. OtherError . Text. pack . show ) (setRange tx)
501
503
<$> posixTimeRangeToContainedSlotRange @ w timeRange
502
- else pure $ Left " Invalid validity interval. "
504
+ else pure $ Left $ WAPI. ToCardanoError InvalidValidityRange
503
505
where
504
506
setRange tx' range = tx' {txValidRange = range}
505
507
0 commit comments