1+ {-# LANGUAGE QuasiQuotes #-}
2+ {-# LANGUAGE TemplateHaskell #-}
3+
14module Spec.BotPlutusInterface.Balance (tests ) where
25
3- import BotPlutusInterface.Balance (defaultBalanceConfig , withFee )
6+ import BotPlutusInterface.Balance (balanceTxIO , defaultBalanceConfig , withFee )
47import BotPlutusInterface.Balance qualified as Balance
58import BotPlutusInterface.Effects (PABEffect )
69import Data.Default (Default (def ))
@@ -13,11 +16,14 @@ import Ledger.Address (Address, PaymentPubKeyHash (PaymentPubKeyHash))
1316import Ledger.Address qualified as Address
1417import Ledger.CardanoWallet qualified as Wallet
1518import Ledger.Crypto (PubKeyHash )
19+ import Ledger.Scripts qualified as Scripts
1620import Ledger.Tx (Tx (.. ), TxIn (.. ), TxInType (.. ), TxOut (.. ), TxOutRef (.. ))
1721import Ledger.Value qualified as Value
22+ import Plutus.V1.Ledger.Api qualified as Api
23+ import PlutusTx qualified
1824import Spec.MockContract (runPABEffectPure )
1925import Test.Tasty (TestTree , testGroup )
20- import Test.Tasty.HUnit (Assertion , assertFailure , testCase , (@?=) )
26+ import Test.Tasty.HUnit (Assertion , assertBool , assertFailure , testCase , (@?=) )
2127import Prelude
2228
2329{- | Tests for 'cardano-cli query utxo' result parsers
@@ -30,33 +36,50 @@ tests =
3036 [ testCase " Add utxos to cover fees" addUtxosForFees
3137 , testCase " Add utxos to cover native tokens" addUtxosForNativeTokens
3238 , testCase " Add utxos to cover change min utxo" addUtxosForChange
39+ , testCase " Don't add change to UTxOs with datums" dontAddChangeToDatum
3340 ]
3441
42+ validator :: Scripts. Validator
43+ validator =
44+ Scripts. mkValidatorScript
45+ $$ (PlutusTx. compile [|| (\ _ _ _ -> () )|| ])
46+
47+ valHash :: Ledger. ValidatorHash
48+ (Just valHash) = Ledger. toValidatorHash addr3
49+
3550pkh1 , pkh2 :: PubKeyHash
3651pkh1 = Address. unPaymentPubKeyHash . Wallet. paymentPubKeyHash $ Wallet. knownMockWallet 1
3752pkh2 = Address. unPaymentPubKeyHash . Wallet. paymentPubKeyHash $ Wallet. knownMockWallet 2
3853
39- addr1 , addr2 :: Address
54+ addr1 , addr2 , addr3 :: Address
4055addr1 = Ledger. pubKeyHashAddress (PaymentPubKeyHash pkh1) Nothing
4156addr2 = Ledger. pubKeyHashAddress (PaymentPubKeyHash pkh2) Nothing
57+ addr3 = Ledger. scriptAddress validator
4258
43- txOutRef1 , txOutRef2 , txOutRef3 , txOutRef4 :: TxOutRef
59+ txOutRef1 , txOutRef2 , txOutRef3 , txOutRef4 , txOutRef5 , txOutRef6 , txOutRef7 :: TxOutRef
4460txOutRef1 = TxOutRef " 384de3f29396fdf687551e3f9e05bd400adcd277720c71f1d2b61f17f5183e51" 0
4561txOutRef2 = TxOutRef " 52a003b3f4956433429631afe4002f82a924a5a7a891db7ae1f6434797a57dff" 1
4662txOutRef3 = TxOutRef " d8a5630a9d7e913f9d186c95e5138a239a4e79ece3414ac894dbf37280944de3" 0
4763txOutRef4 = TxOutRef " d8a5630a9d7e913f9d186c95e5138a239a4e79ece3414ac894dbf37280944de3" 2
64+ txOutRef5 = TxOutRef " 52a003b3f4956433429631afe4002f82a924a5a7a891db7ae1f6434797a57dff" 0
65+ txOutRef6 = TxOutRef " 52a003b3f4956433429631afe4002f82a924a5a7a891db7ae1f6434797a57dff" 3
66+ txOutRef7 = TxOutRef " 384de3f29396fdf687551e3f9e05bd400adcd277720c71f1d2b61f17f5183e51" 1
4867
49- txIn1 , txIn2 , txIn3 , txIn4 :: TxIn
68+ txIn1 , txIn2 , txIn3 , txIn4 , txIn5 :: TxIn
5069txIn1 = TxIn txOutRef1 (Just ConsumePublicKeyAddress )
5170txIn2 = TxIn txOutRef2 (Just ConsumePublicKeyAddress )
5271txIn3 = TxIn txOutRef3 (Just ConsumePublicKeyAddress )
5372txIn4 = TxIn txOutRef4 (Just ConsumePublicKeyAddress )
73+ txIn5 = TxIn txOutRef5 (Just ConsumeSimpleScriptAddress )
5474
55- utxo1 , utxo2 , utxo3 , utxo4 :: (TxOutRef , TxOut )
75+ utxo1 , utxo2 , utxo3 , utxo4 , utxo5 , utxo6 , utxo7 :: (TxOutRef , TxOut )
5676utxo1 = (txOutRef1, TxOut addr1 (Ada. lovelaceValueOf 1_100_000 ) Nothing )
5777utxo2 = (txOutRef2, TxOut addr1 (Ada. lovelaceValueOf 1_000_000 ) Nothing )
5878utxo3 = (txOutRef3, TxOut addr1 (Ada. lovelaceValueOf 900_000 ) Nothing )
5979utxo4 = (txOutRef4, TxOut addr1 (Ada. lovelaceValueOf 800_000 <> Value. singleton " 11223344" " Token" 200 ) Nothing )
80+ utxo5 = (txOutRef5, TxOut addr3 (Ada. lovelaceValueOf 900_000 ) (Just $ Ledger. DatumHash " " ))
81+ utxo6 = (txOutRef6, TxOut addr3 (Value. singleton " 11223344" " Token" 200 ) Nothing )
82+ utxo7 = (txOutRef2, TxOut addr1 (Ada. lovelaceValueOf 5_000_000 ) Nothing )
6083
6184addUtxosForFees :: Assertion
6285addUtxosForFees = do
@@ -105,3 +128,34 @@ addUtxosForChange = do
105128 case ebalancedTx of
106129 Left e -> assertFailure (Text. unpack e)
107130 Right balancedTx -> txInputs <$> balancedTx @?= Right (Set. fromList [txIn1, txIn2])
131+
132+ dontAddChangeToDatum :: Assertion
133+ dontAddChangeToDatum = do
134+ let txout = TxOut addr2 (Ada. lovelaceValueOf 1_300_000 ) Nothing
135+ txout2 = snd utxo5
136+ txout3 = snd utxo6
137+ tx = mempty {txOutputs = [txout, txout2, txout3]} `withFee` 500_000
138+ minUtxo = [(txout, 1_000_000 ), (txout3, 1_100_000 )] -- add change to these utxos
139+ utxoIndex = Map. fromList [utxo5, utxo3, utxo1, utxo4, utxo7]
140+ ownAddr = addr1
141+ ebalancedTx =
142+ fst $
143+ runPABEffectPure def $
144+ Balance. balanceTxStep @ () @ '[PABEffect () ] defaultBalanceConfig minUtxo utxoIndex ownAddr tx
145+
146+ case ebalancedTx of
147+ Left e -> assertFailure (Text. unpack e)
148+ Right (Left e) -> assertFailure (Text. unpack e)
149+ Right (Right balancedTx) ->
150+ assertBool
151+ ( " Original UTxO not in output;\n "
152+ <> " TxOuts: "
153+ <> show (txOutputs balancedTx)
154+ <> " \n "
155+ <> " TxIns : "
156+ <> show (txInputs balancedTx)
157+ <> " \n "
158+ )
159+ $ snd utxo5 `elem` txOutputs balancedTx
160+
161+ -- txIn5 `Set.member` (txInputs balancedTx)
0 commit comments