Skip to content

Commit f7be4a2

Browse files
Added basic "Don't add change" test.
Unsure whether it accurately tests whether change isn't added to utxos with datums.
1 parent e50e57a commit f7be4a2

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import Data.List ((\\))
4545
import Data.List qualified as List
4646
import Data.Map (Map)
4747
import Data.Map qualified as Map
48-
import Data.Maybe (fromMaybe, isJust, isNothing, mapMaybe)
48+
import Data.Maybe (fromMaybe, isJust, mapMaybe)
4949
import Data.Set qualified as Set
5050
import Data.Text (Text)
5151
import Data.Text qualified as Text
@@ -104,7 +104,7 @@ balanceTxIO ::
104104
Eff effs (Either Text Tx)
105105
balanceTxIO = balanceTxIO' @w defaultBalanceConfig
106106

107-
-- | `balanceTxIO'` is more flexible version of `balanceTxIO`, this let us specify custom `BalanceConfig`.
107+
-- | `balanceTxIO'` is more flexible version of `balanceTxIO`, this lets us specify custom `BalanceConfig`.
108108
balanceTxIO' ::
109109
forall (w :: Type) (effs :: [Type -> Type]).
110110
(Member (PABEffect w) effs) =>
@@ -322,7 +322,7 @@ hasDatum :: TxOut -> Bool
322322
hasDatum = isJust . txOutDatumHash
323323

324324
hasNoDatum :: TxOut -> Bool
325-
hasNoDatum = isNothing . txOutDatumHash
325+
hasNoDatum = not . hasDatum
326326

327327
-- | Add min lovelaces to each tx output
328328
addLovelaces :: [(TxOut, Integer)] -> Tx -> Tx
@@ -382,8 +382,9 @@ handleNonAdaChange balanceCfg changeAddr utxos tx =
382382
( \txout ->
383383
Tx.txOutAddress txout == changeAddr
384384
&& not (justLovelace $ Tx.txOutValue txout)
385+
&& hasNoDatum txout
385386
)
386-
else (\txout -> Tx.txOutAddress txout == changeAddr)
387+
else (\txout -> Tx.txOutAddress txout == changeAddr && hasNoDatum txout)
387388
newOutput =
388389
TxOut
389390
{ txOutAddress = changeAddr

test/Spec/BotPlutusInterface/Balance.hs

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
{-# LANGUAGE QuasiQuotes #-}
2+
{-# LANGUAGE TemplateHaskell #-}
3+
14
module Spec.BotPlutusInterface.Balance (tests) where
25

3-
import BotPlutusInterface.Balance (defaultBalanceConfig, withFee)
6+
import BotPlutusInterface.Balance (balanceTxIO, defaultBalanceConfig, withFee)
47
import BotPlutusInterface.Balance qualified as Balance
58
import BotPlutusInterface.Effects (PABEffect)
69
import Data.Default (Default (def))
@@ -13,11 +16,14 @@ import Ledger.Address (Address, PaymentPubKeyHash (PaymentPubKeyHash))
1316
import Ledger.Address qualified as Address
1417
import Ledger.CardanoWallet qualified as Wallet
1518
import Ledger.Crypto (PubKeyHash)
19+
import Ledger.Scripts qualified as Scripts
1620
import Ledger.Tx (Tx (..), TxIn (..), TxInType (..), TxOut (..), TxOutRef (..))
1721
import Ledger.Value qualified as Value
22+
import Plutus.V1.Ledger.Api qualified as Api
23+
import PlutusTx qualified
1824
import Spec.MockContract (runPABEffectPure)
1925
import Test.Tasty (TestTree, testGroup)
20-
import Test.Tasty.HUnit (Assertion, assertFailure, testCase, (@?=))
26+
import Test.Tasty.HUnit (Assertion, assertBool, assertFailure, testCase, (@?=))
2127
import 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+
3550
pkh1, pkh2 :: PubKeyHash
3651
pkh1 = Address.unPaymentPubKeyHash . Wallet.paymentPubKeyHash $ Wallet.knownMockWallet 1
3752
pkh2 = Address.unPaymentPubKeyHash . Wallet.paymentPubKeyHash $ Wallet.knownMockWallet 2
3853

39-
addr1, addr2 :: Address
54+
addr1, addr2, addr3 :: Address
4055
addr1 = Ledger.pubKeyHashAddress (PaymentPubKeyHash pkh1) Nothing
4156
addr2 = 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
4460
txOutRef1 = TxOutRef "384de3f29396fdf687551e3f9e05bd400adcd277720c71f1d2b61f17f5183e51" 0
4561
txOutRef2 = TxOutRef "52a003b3f4956433429631afe4002f82a924a5a7a891db7ae1f6434797a57dff" 1
4662
txOutRef3 = TxOutRef "d8a5630a9d7e913f9d186c95e5138a239a4e79ece3414ac894dbf37280944de3" 0
4763
txOutRef4 = 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
5069
txIn1 = TxIn txOutRef1 (Just ConsumePublicKeyAddress)
5170
txIn2 = TxIn txOutRef2 (Just ConsumePublicKeyAddress)
5271
txIn3 = TxIn txOutRef3 (Just ConsumePublicKeyAddress)
5372
txIn4 = 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)
5676
utxo1 = (txOutRef1, TxOut addr1 (Ada.lovelaceValueOf 1_100_000) Nothing)
5777
utxo2 = (txOutRef2, TxOut addr1 (Ada.lovelaceValueOf 1_000_000) Nothing)
5878
utxo3 = (txOutRef3, TxOut addr1 (Ada.lovelaceValueOf 900_000) Nothing)
5979
utxo4 = (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

6184
addUtxosForFees :: Assertion
6285
addUtxosForFees = 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

Comments
 (0)