Skip to content

Commit c54e2e9

Browse files
committed
regression tests for outputs adjsutment
- see issue #138
1 parent aab0f48 commit c54e2e9

File tree

2 files changed

+46
-37
lines changed

2 files changed

+46
-37
lines changed

test/Spec/Integration.hs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import Plutus.Contract qualified as Contract
1818
import Spec.TestContract.AdjustTx (runAdjustTest)
1919
import Spec.TestContract.AlwaysFail (lockThenFailToSpend)
2020
import Spec.TestContract.LockSpendMint (lockThenSpend)
21+
import Spec.TestContract.MintAndPay (zeroAdaOutTestContract)
2122
import Spec.TestContract.SimpleContracts (
2223
getUtxos,
2324
getUtxosThrowsErr,
@@ -26,7 +27,6 @@ import Spec.TestContract.SimpleContracts (
2627
ownValueToState,
2728
payTo,
2829
)
29-
import Spec.TestContract.MintAndPay (mintAndPayTokens)
3030
import Spec.TestContract.ValidateTimeRange (failingTimeContract, successTimeContract)
3131
import Test.Plutip.Contract (
3232
TestWallets,
@@ -215,21 +215,20 @@ test =
215215
]
216216
, -- Test `adjustUnbalancedTx`
217217
runAdjustTest
218-
, testMintMintAndPay
218+
, testBugMintAndPay
219219
]
220220
++ testValueAssertionsOrderCorrectness
221221

222-
-- mint bug
223-
testMintMintAndPay :: (TestWallets, IO (ClusterEnv, NonEmpty BpiWallet) -> TestTree)
224-
testMintMintAndPay =
222+
-- https://github.com/mlabs-haskell/plutip/issues/138
223+
testBugMintAndPay :: (TestWallets, IO (ClusterEnv, NonEmpty BpiWallet) -> TestTree)
224+
testBugMintAndPay =
225225
assertExecution
226-
"testMintMintAndPay"
226+
"Adjustment of outputs with 0 Ada does not fail"
227227
(withCollateral $ initAda [1000] <> initAda [1111])
228-
(withContract $ \[p1] -> mintAndPayTokens p1)
228+
(withContract $ \[p1] -> zeroAdaOutTestContract p1)
229229
[ shouldSucceed
230230
]
231231

232-
233232
-- Tests for https://github.com/mlabs-haskell/plutip/issues/84
234233
testValueAssertionsOrderCorrectness ::
235234
[(TestWallets, IO (ClusterEnv, NonEmpty BpiWallet) -> TestTree)]

test/Spec/TestContract/MintAndPay.hs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,67 @@
1-
module Spec.TestContract.MintAndPay (mintAndPayTokens) where
1+
module Spec.TestContract.MintAndPay (zeroAdaOutTestContract) where
22

3-
import Control.Monad (void)
4-
import Data.List.NonEmpty qualified as NonEmpty
5-
import Data.Map qualified as Map
63
import Data.Text (Text)
74
import Ledger (
8-
Address,
9-
CardanoTx,
10-
ChainIndexTxOut,
115
CurrencySymbol,
12-
PaymentPubKeyHash (PaymentPubKeyHash),
13-
ScriptContext (scriptContextTxInfo),
14-
TxId,
15-
TxInfo (txInfoMint),
16-
TxOutRef,
6+
PaymentPubKeyHash,
7+
ScriptContext,
178
getCardanoTxId,
18-
scriptHashAddress,
199
)
20-
import Ledger.Ada (adaValueOf)
2110
import Ledger.Constraints qualified as Constraints
2211
import Ledger.Scripts qualified as Scripts
23-
import Ledger.Typed.Scripts (TypedValidator, Validator, ValidatorTypes, mkUntypedMintingPolicy)
12+
import Ledger.Typed.Scripts (mkUntypedMintingPolicy)
2413
import Ledger.Typed.Scripts qualified as TypedScripts
25-
import Ledger.Value (flattenValue, tokenName)
26-
import Plutus.Contract (Contract, mkTxConstraints, adjustUnbalancedTx, submitTxConfirmed)
27-
-- import Plutus.Contract qualified as Contract
14+
import Ledger.Value (tokenName)
15+
import Plutus.Contract (Contract, adjustUnbalancedTx, awaitTxConfirmed, mkTxConstraints, submitTxConfirmed, submitTxConstraintsWith)
16+
17+
import Data.Void (Void)
2818
import Plutus.PAB.Effects.Contract.Builtin (EmptySchema)
2919
import Plutus.Script.Utils.V1.Scripts qualified as ScriptUtils
3020
import Plutus.V1.Ledger.Value qualified as Value
3121
import PlutusTx qualified
3222
import PlutusTx.Prelude qualified as PP
3323
import Prelude
34-
import Data.Void (Void)
3524

25+
{- This test contract checks that outputs with 0 Ada are hadled properly.
26+
BPI does adjustment of ouptupt even w/o explicit `adjustUnbalancedTx`,
27+
so this test contract checks bot cases - with implicit and explicit adjustment.
28+
-}
29+
zeroAdaOutTestContract :: PaymentPubKeyHash -> Contract () EmptySchema Text ()
30+
zeroAdaOutTestContract pkh =
31+
mintAndPayWithAdjustment 0 pkh
32+
>> mintAndPayNoAdjustment 0 pkh
33+
>> mintAndPayWithAdjustment 7 pkh
34+
>> mintAndPayNoAdjustment 7 pkh
35+
36+
mintAndPayWithAdjustment :: Integer -> PaymentPubKeyHash -> Contract () EmptySchema Text ()
37+
mintAndPayWithAdjustment tokensAmt pkh = do
38+
let token = Value.singleton currencySymbol (tokenName "ff") tokensAmt
39+
txc1 =
40+
Constraints.mustMintValueWithRedeemer Scripts.unitRedeemer token
41+
<> Constraints.mustPayToPubKey pkh token
42+
lookups1 = Constraints.plutusV1MintingPolicy mintingPolicy
43+
44+
utx <- mkTxConstraints @Void lookups1 txc1
45+
tx <- adjustUnbalancedTx utx
46+
submitTxConfirmed tx
3647

37-
mintAndPayTokens :: PaymentPubKeyHash -> Contract () EmptySchema Text ()
38-
mintAndPayTokens pkh = do
39-
let token = Value.singleton currencySymbol (tokenName "ff") 10
40-
txc1 = Constraints.mustMintValueWithRedeemer Scripts.unitRedeemer token
41-
<> Constraints.mustPayToPubKey pkh token
42-
lookups1 = Constraints.plutusV1MintingPolicy mintingPolicy
48+
mintAndPayNoAdjustment :: Integer -> PaymentPubKeyHash -> Contract () EmptySchema Text ()
49+
mintAndPayNoAdjustment tokensAmt pkh = do
50+
let token = Value.singleton currencySymbol (tokenName "ff") tokensAmt
51+
txc1 =
52+
Constraints.mustMintValueWithRedeemer Scripts.unitRedeemer token
53+
<> Constraints.mustPayToPubKey pkh token
54+
lookups1 = Constraints.plutusV1MintingPolicy mintingPolicy
4355

44-
utx <- mkTxConstraints @Void lookups1 txc1
45-
tx <- adjustUnbalancedTx utx
46-
submitTxConfirmed tx
56+
tx <- submitTxConstraintsWith @Void lookups1 txc1
57+
awaitTxConfirmed (getCardanoTxId tx)
4758

4859
-- minting policy
4960
{-# INLINEABLE mkPolicy #-}
5061
mkPolicy :: () -> ScriptContext -> Bool
5162
mkPolicy _ _ =
5263
PP.traceIfFalse "Mint only check" check
5364
where
54-
-- info = scriptContextTxInfo ctx
5565
check = PP.length someWork PP.== 10
5666
someWork = PP.sort [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] :: [Integer]
5767

0 commit comments

Comments
 (0)