Skip to content

Commit 3b7e7e8

Browse files
authored
Merge pull request #126 from mlabs-haskell/david/adjust-tx-test-2
Add Test that `adjustUnbalancedTx` works (updated)
2 parents 9974aa3 + 5cd3722 commit 3b7e7e8

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

plutip.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ test-suite plutip-tests
185185
other-modules:
186186
Spec.Integration
187187
Spec.Test.Plutip.BotPlutusInterface
188+
Spec.TestContract.AdjustTx
188189
Spec.TestContract.AlwaysFail
189190
Spec.TestContract.LockSpendMint
190191
Spec.TestContract.SimpleContracts

test/Spec/Integration.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Plutus.Contract (
1515
waitNSlots,
1616
)
1717
import Plutus.Contract qualified as Contract
18+
import Spec.TestContract.AdjustTx (runAdjustTest)
1819
import Spec.TestContract.AlwaysFail (lockThenFailToSpend)
1920
import Spec.TestContract.LockSpendMint (lockThenSpend)
2021
import Spec.TestContract.SimpleContracts (
@@ -206,6 +207,8 @@ test =
206207
[ shouldFail
207208
, errorSatisfies "Fail validation with 'I always fail'" errCheck
208209
]
210+
, -- Test `adjustUnbalancedTx`
211+
runAdjustTest
209212
]
210213
++ testValueAssertionsOrderCorrectness
211214

test/Spec/TestContract/AdjustTx.hs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
module Spec.TestContract.AdjustTx (
2+
runAdjustTest,
3+
) where
4+
5+
import Control.Lens.Operators ((^.))
6+
import Data.List.NonEmpty qualified as NonEmpty
7+
import Data.Text (Text)
8+
import Data.Void (Void)
9+
import Ledger (
10+
PaymentPubKeyHash,
11+
Tx (..),
12+
TxOut (..),
13+
getCardanoTxId,
14+
)
15+
import Ledger qualified
16+
import Ledger.Ada qualified as Ada
17+
import Ledger.Constraints qualified as Constraints
18+
import Ledger.Constraints.OffChain qualified as OffChain
19+
import Ledger.Value (Value)
20+
import Plutus.Contract (
21+
Contract,
22+
adjustUnbalancedTx,
23+
awaitTxConfirmed,
24+
mkTxConstraints,
25+
)
26+
import Plutus.Contract qualified as Contract
27+
import Plutus.PAB.Effects.Contract.Builtin (EmptySchema)
28+
import Test.Plutip.Contract (
29+
TestWallets,
30+
assertExecution,
31+
initAda,
32+
withContract,
33+
)
34+
import Test.Plutip.Internal.BotPlutusInterface.Wallet (BpiWallet)
35+
import Test.Plutip.Internal.Types (ClusterEnv)
36+
import Test.Plutip.Predicate (
37+
shouldSucceed,
38+
yieldSatisfies,
39+
)
40+
import Test.Tasty (TestTree)
41+
import Prelude
42+
43+
adjustTx :: PaymentPubKeyHash -> Contract () EmptySchema Text [Value]
44+
adjustTx toPkh = do
45+
ownPkh <- Contract.ownFirstPaymentPubKeyHash
46+
let ownAddr = Ledger.pubKeyHashAddress ownPkh Nothing
47+
utxos <- Contract.utxosAt ownAddr
48+
let consts =
49+
Constraints.mustPayToPubKey toPkh (Ada.lovelaceValueOf 50)
50+
lkups =
51+
Constraints.ownPaymentPubKeyHash ownPkh
52+
<> Constraints.unspentOutputs utxos
53+
unbalancedTx <- mkTxConstraints @Void lkups consts
54+
-- Adjust the Tx so that all UTxOs have the minimum ADA.
55+
adjustedTx <- adjustUnbalancedTx unbalancedTx
56+
let rawTx = adjustedTx ^. OffChain.tx
57+
vals = map txOutValue $ txOutputs rawTx
58+
balTx <- Contract.balanceTx adjustedTx
59+
crdTx <- Contract.submitBalancedTx balTx
60+
_ <- awaitTxConfirmed (getCardanoTxId crdTx)
61+
pure vals
62+
63+
adjustTx' :: [PaymentPubKeyHash] -> Contract () EmptySchema Text [Value]
64+
adjustTx' [] = do
65+
pkh <- Contract.ownFirstPaymentPubKeyHash
66+
adjustTx pkh
67+
adjustTx' (pkh : _) = adjustTx pkh
68+
69+
-- | A type for the output of `assertExecution`.
70+
type PlutipTest = (TestWallets, IO (ClusterEnv, NonEmpty.NonEmpty BpiWallet) -> TestTree)
71+
72+
-- | Tests whether `adjustUnbalancedTx` actually tops up the
73+
-- UTxO to get to the minimum required ADA.
74+
runAdjustTest :: PlutipTest
75+
runAdjustTest =
76+
assertExecution
77+
"Adjust Unbalanced Tx Contract"
78+
(initAda [1000] <> initAda [1000])
79+
(withContract adjustTx')
80+
[ shouldSucceed
81+
, yieldSatisfies
82+
"All UTxOs have minimum(?) ADA."
83+
(all (\val -> 500_000 <= Ada.getLovelace (Ada.fromValue val)))
84+
]

0 commit comments

Comments
 (0)