Skip to content

Commit dc5c944

Browse files
authored
Merge pull request #137 from mlabs-haskell/bug-adjust-unbalanced
Bugfix: adjusting unbalanced transaction
2 parents 1da1d04 + 7c0556d commit dc5c944

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

flake.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
flake = false;
1212
};
1313
bot-plutus-interface.url =
14-
"github:mlabs-haskell/bot-plutus-interface?ref=857ec745d50f7f0ebd5cd934110403fae301ef6f";
14+
"github:mlabs-haskell/bot-plutus-interface?ref=d6cf1e3686bc31bb2571c6feefbe28e3a2c8bb06";
1515
};
1616

1717
outputs =

plutip.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ test-suite plutip-tests
188188
Spec.TestContract.AdjustTx
189189
Spec.TestContract.AlwaysFail
190190
Spec.TestContract.LockSpendMint
191+
Spec.TestContract.MintAndPay
191192
Spec.TestContract.SimpleContracts
192193
Spec.TestContract.ValidateTimeRange
193194

test/Spec/Integration.hs

Lines changed: 12 additions & 0 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,
@@ -214,9 +215,20 @@ test =
214215
]
215216
, -- Test `adjustUnbalancedTx`
216217
runAdjustTest
218+
, testBugMintAndPay
217219
]
218220
++ testValueAssertionsOrderCorrectness
219221

222+
-- https://github.com/mlabs-haskell/plutip/issues/138
223+
testBugMintAndPay :: (TestWallets, IO (ClusterEnv, NonEmpty BpiWallet) -> TestTree)
224+
testBugMintAndPay =
225+
assertExecution
226+
"Adjustment of outputs with 0 Ada does not fail"
227+
(withCollateral $ initAda [1000] <> initAda [1111])
228+
(withContract $ \[p1] -> zeroAdaOutTestContract p1)
229+
[ shouldSucceed
230+
]
231+
220232
-- Tests for https://github.com/mlabs-haskell/plutip/issues/84
221233
testValueAssertionsOrderCorrectness ::
222234
[(TestWallets, IO (ClusterEnv, NonEmpty BpiWallet) -> TestTree)]

test/Spec/TestContract/MintAndPay.hs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
module Spec.TestContract.MintAndPay (zeroAdaOutTestContract) where
2+
3+
import Data.Text (Text)
4+
import Ledger (
5+
CurrencySymbol,
6+
PaymentPubKeyHash,
7+
ScriptContext,
8+
getCardanoTxId,
9+
)
10+
import Ledger.Constraints qualified as Constraints
11+
import Ledger.Scripts qualified as Scripts
12+
import Ledger.Typed.Scripts (mkUntypedMintingPolicy)
13+
import Ledger.Typed.Scripts qualified as TypedScripts
14+
import Ledger.Value (tokenName)
15+
import Plutus.Contract (Contract, adjustUnbalancedTx, awaitTxConfirmed, mkTxConstraints, submitTxConfirmed, submitTxConstraintsWith)
16+
17+
import Data.Void (Void)
18+
import Plutus.PAB.Effects.Contract.Builtin (EmptySchema)
19+
import Plutus.Script.Utils.V1.Scripts qualified as ScriptUtils
20+
import Plutus.V1.Ledger.Value qualified as Value
21+
import PlutusTx qualified
22+
import PlutusTx.Prelude qualified as PP
23+
import Prelude
24+
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
47+
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
55+
56+
tx <- submitTxConstraintsWith @Void lookups1 txc1
57+
awaitTxConfirmed (getCardanoTxId tx)
58+
59+
-- minting policy
60+
{-# INLINEABLE mkPolicy #-}
61+
mkPolicy :: () -> ScriptContext -> Bool
62+
mkPolicy _ _ =
63+
PP.traceIfFalse "Mint only check" check
64+
where
65+
check = PP.length someWork PP.== 10
66+
someWork = PP.sort [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] :: [Integer]
67+
68+
mintingPolicy :: TypedScripts.MintingPolicy
69+
mintingPolicy =
70+
Scripts.mkMintingPolicyScript
71+
$$(PlutusTx.compile [||mkUntypedMintingPolicy mkPolicy||])
72+
73+
currencySymbol :: CurrencySymbol
74+
currencySymbol = ScriptUtils.scriptCurrencySymbol mintingPolicy

0 commit comments

Comments
 (0)