@@ -228,31 +228,27 @@ dontAddChangeToDatum = do
228228 <> Constraints. mustSpendPubKeyOutput txOutRef7
229229 eunbalancedTx = Constraints. mkTx @ Void scrLkups txConsts
230230
231- case eunbalancedTx of
232- Left mkTxErr -> assertFailure (" MkTx Error: " <> show mkTxErr)
233- Right unbalancedTx -> do
234- let (eRslt, _finalState) = runPABEffectPure initState (balanceTxIO @ () @ '[PABEffect () ] pabConf pkh3 unbalancedTx)
235- case eRslt of
236- (Left txt) -> assertFailure (" PAB effect error: " <> Text. unpack txt)
237- (Right (Left txt)) -> assertFailure $ " Balancing error: " <> Text. unpack txt
238- (Right (Right trx)) -> do
239- let scrTxOut'' = scrTxOut' & Ledger. ciTxOutValue <>~ Ada. lovelaceValueOf 500
240- scrTxOutExpected = Ledger. toTxOut scrTxOut''
241- isScrUtxo :: TxOut -> Bool
242- isScrUtxo utxo = txOutAddress utxo == txOutAddress scrTxOutExpected
243- (balScrUtxos, balOtherUtxos) = partition isScrUtxo (txOutputs trx)
244- assertBool
245- ( " Expected UTxO not in output Tx."
246- <> " \n Expected UTxO: "
247- <> show scrTxOutExpected
248- <> " \n Balanced Script UTxOs: "
249- <> show balScrUtxos
250- <> " \n Other Balanced UTxOs: "
251- <> show balOtherUtxos
252- <> " \n Unbalanced UTxOs: "
253- <> show (txOutputs (unbalancedTx ^. OffChain. tx))
254- )
255- (scrTxOutExpected `elem` txOutputs trx)
231+ unbalancedTx <- liftAssertFailure eunbalancedTx (\ err -> " MkTx Error: " <> show err)
232+ let (eRslt, _finalState) = runPABEffectPure initState (balanceTxIO @ () @ '[PABEffect () ] pabConf pkh3 unbalancedTx)
233+ eRslt' <- liftAssertFailure eRslt (\ txt -> " PAB effect error: " <> Text. unpack txt)
234+ trx <- liftAssertFailure eRslt' (\ txt -> " Balancing error: " <> Text. unpack txt)
235+ let scrTxOut'' = scrTxOut' & Ledger. ciTxOutValue <>~ Ada. lovelaceValueOf 500
236+ scrTxOutExpected = Ledger. toTxOut scrTxOut''
237+ isScrUtxo :: TxOut -> Bool
238+ isScrUtxo utxo = txOutAddress utxo == txOutAddress scrTxOutExpected
239+ (balScrUtxos, balOtherUtxos) = partition isScrUtxo (txOutputs trx)
240+ assertBool
241+ ( " Expected UTxO not in output Tx."
242+ <> " \n Expected UTxO: "
243+ <> show scrTxOutExpected
244+ <> " \n Balanced Script UTxOs: "
245+ <> show balScrUtxos
246+ <> " \n Other Balanced UTxOs: "
247+ <> show balOtherUtxos
248+ <> " \n Unbalanced UTxOs: "
249+ <> show (txOutputs (unbalancedTx ^. OffChain. tx))
250+ )
251+ (scrTxOutExpected `elem` txOutputs trx)
256252
257253-- Like the first one, but
258254-- only has inputs from the script.
@@ -297,64 +293,65 @@ dontAddChangeToDatum2 = do
297293 <> Constraints. mustSpendScriptOutput txOutRef6 Ledger. unitRedeemer
298294 eunbalancedTx = Constraints. mkTx @ Void scrLkups txConsts
299295
300- case eunbalancedTx of
301- Left mkTxErr -> assertFailure (" MkTx Error: " <> show mkTxErr)
302- Right unbalancedTx -> do
303- let (eRslt, _finalState) = runPABEffectPure initState (balanceTxIO @ () @ '[PABEffect () ] pabConf pkh3 unbalancedTx)
304- case eRslt of
305- (Left txt) -> assertFailure (" PAB effect error: " <> Text. unpack txt)
306- (Right (Left txt)) -> assertFailure $ " Balancing error: " <> Text. unpack txt
307- (Right (Right trx)) -> do
308- let scrTxOut'' = scrTxOut' & Ledger. ciTxOutValue .~ scrValue'
309- scrTxOutExpected = Ledger. toTxOut scrTxOut''
310- isScrUtxo :: TxOut -> Bool
311- isScrUtxo utxo = txOutAddress utxo == txOutAddress scrTxOutExpected
312- (balScrUtxos, balOtherUtxos) = partition isScrUtxo (txOutputs trx)
313- -- Check that the expected script UTxO
314- -- is in the output.
315- assertBool
316- ( " Expected UTxO not in output Tx."
317- <> " \n Expected UTxO: "
318- <> show scrTxOutExpected
319- <> " \n Balanced Script UTxOs: "
320- <> show balScrUtxos
321- <> " \n Other Balanced UTxOs: "
322- <> show balOtherUtxos
323- <> " \n Unbalanced UTxOs: "
324- <> show (txOutputs (unbalancedTx ^. OffChain. tx))
325- )
326- (scrTxOutExpected `elem` txOutputs trx)
327- -- Check that the output has the remaining change
328- let trxFee = txFee trx
329- adaChange' :: Integer
330- adaChange' = ((-) `on` (lovelaceInValue . txOutValue)) scrTxOut scrTxOutExpected
331- adaChange :: Integer
332- adaChange = adaChange' - lovelaceInValue trxFee
333- tokChange :: Integer
334- tokChange = ((-) `on` (acValueOf tokenAsset . txOutValue)) scrTxOut scrTxOutExpected
335- remainingTxOuts :: [TxOut ]
336- remainingTxOuts = delete scrTxOutExpected (txOutputs trx)
337- remainingValue :: Value. Value
338- remainingValue = foldMap txOutValue remainingTxOuts
339- -- Check for ADA change
340- assertBool
341- ( " Other UTxOs do not contain expected ADA change."
342- <> " \n Expected Amount : "
343- <> show adaChange
344- <> " Lovelace"
345- <> " \n Actual Amount : "
346- <> show (lovelaceInValue remainingValue)
347- <> " Lovelace"
348- )
349- (adaChange == lovelaceInValue remainingValue)
350- -- Check for Token change
351- assertBool
352- ( " Other UTxOs do not contain expected Token change."
353- <> " \n Expected Amount : "
354- <> show tokChange
355- <> " tokens"
356- <> " \n Actual Amount : "
357- <> show (acValueOf tokenAsset remainingValue)
358- <> " tokens"
359- )
360- (tokChange == acValueOf tokenAsset remainingValue)
296+ unbalancedTx <- liftAssertFailure eunbalancedTx (\ err -> " MkTx Error: " <> show err)
297+ let (eRslt, _finalState) = runPABEffectPure initState (balanceTxIO @ () @ '[PABEffect () ] pabConf pkh3 unbalancedTx)
298+ eRslt' <- liftAssertFailure eRslt (\ txt -> " PAB effect error: " <> Text. unpack txt)
299+ trx <- liftAssertFailure eRslt' (\ txt -> " Balancing error: " <> Text. unpack txt)
300+ let scrTxOut'' = scrTxOut' & Ledger. ciTxOutValue .~ scrValue'
301+ scrTxOutExpected = Ledger. toTxOut scrTxOut''
302+ isScrUtxo :: TxOut -> Bool
303+ isScrUtxo utxo = txOutAddress utxo == txOutAddress scrTxOutExpected
304+ (balScrUtxos, balOtherUtxos) = partition isScrUtxo (txOutputs trx)
305+ -- Check that the expected script UTxO
306+ -- is in the output.
307+ assertBool
308+ ( " Expected UTxO not in output Tx."
309+ <> " \n Expected UTxO: "
310+ <> show scrTxOutExpected
311+ <> " \n Balanced Script UTxOs: "
312+ <> show balScrUtxos
313+ <> " \n Other Balanced UTxOs: "
314+ <> show balOtherUtxos
315+ <> " \n Unbalanced UTxOs: "
316+ <> show (txOutputs (unbalancedTx ^. OffChain. tx))
317+ )
318+ (scrTxOutExpected `elem` txOutputs trx)
319+ -- Check that the output has the remaining change
320+ let trxFee = txFee trx
321+ adaChange' :: Integer
322+ adaChange' = ((-) `on` (lovelaceInValue . txOutValue)) scrTxOut scrTxOutExpected
323+ adaChange :: Integer
324+ adaChange = adaChange' - lovelaceInValue trxFee
325+ tokChange :: Integer
326+ tokChange = ((-) `on` (acValueOf tokenAsset . txOutValue)) scrTxOut scrTxOutExpected
327+ remainingTxOuts :: [TxOut ]
328+ remainingTxOuts = delete scrTxOutExpected (txOutputs trx)
329+ remainingValue :: Value. Value
330+ remainingValue = foldMap txOutValue remainingTxOuts
331+ -- Check for ADA change
332+ assertBool
333+ ( " Other UTxOs do not contain expected ADA change."
334+ <> " \n Expected Amount : "
335+ <> show adaChange
336+ <> " Lovelace"
337+ <> " \n Actual Amount : "
338+ <> show (lovelaceInValue remainingValue)
339+ <> " Lovelace"
340+ )
341+ (adaChange == lovelaceInValue remainingValue)
342+ -- Check for Token change
343+ assertBool
344+ ( " Other UTxOs do not contain expected Token change."
345+ <> " \n Expected Amount : "
346+ <> show tokChange
347+ <> " tokens"
348+ <> " \n Actual Amount : "
349+ <> show (acValueOf tokenAsset remainingValue)
350+ <> " tokens"
351+ )
352+ (tokChange == acValueOf tokenAsset remainingValue)
353+
354+ -- | Lift an `Either` value into an `assertFailure`.
355+ liftAssertFailure :: Either a b -> (a -> String ) -> IO b
356+ liftAssertFailure (Left err) fstr = assertFailure (fstr err)
357+ liftAssertFailure (Right rslt) _ = return rslt
0 commit comments