Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.

Commit 06623ca

Browse files
author
James Browning
committed
Resolve GTH-697 by adding contractErrorWhenSpendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup test
1 parent 41875be commit 06623ca

File tree

2 files changed

+53
-47
lines changed

2 files changed

+53
-47
lines changed

cabal.project

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ write-ghc-environment-files: never
3030
tests: true
3131
benchmarks: true
3232

33-
-- The only sensible test display option.
34-
test-show-details: streaming
33+
-- The only sensible test display option, since it allows us to have colourized
34+
-- 'tasty' output.
35+
test-show-details: direct
3536

3637
allow-newer:
3738
size-based:template-haskell

plutus-contract/test/Spec/TxConstraints/MustProduceAtLeast.hs

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Test.Tasty (TestTree, testGroup)
1414
import Data.Function ((&))
1515
import Ledger qualified
1616
import Ledger.Ada qualified as Ada
17+
import Ledger.CardanoWallet (paymentPrivateKey)
1718
import Ledger.Constraints.OffChain qualified as Constraints (MkTxError (OwnPubKeyMissing), ownPaymentPubKeyHash,
1819
plutusV1TypedValidatorLookups, unspentOutputs)
1920
import Ledger.Constraints.OnChain.V1 qualified as Constraints (checkScriptContext)
@@ -22,18 +23,21 @@ import Ledger.Constraints.TxConstraints qualified as Constraints (collectFromThe
2223
import Ledger.Generators (someTokenValue)
2324
import Ledger.Tx qualified as Tx
2425
import Ledger.Typed.Scripts qualified as Scripts
25-
import Plutus.Contract as Con
26+
import Plutus.Contract as Con (Contract, ContractError (ConstraintResolutionContractError, WalletContractError), Empty,
27+
awaitTxConfirmed, submitTxConstraintsWith, utxosAt)
2628
import Plutus.Contract.Test (assertContractError, assertFailedTransaction, assertValidatedTransactionCount,
2729
changeInitialWalletValue, checkPredicateOptions, defaultCheckOptions,
28-
mockWalletPaymentPubKeyHash, w1, (.&&.))
29-
import Plutus.Trace qualified as Trace
30+
mockWalletPaymentPubKeyHash, w1, w6, (.&&.))
31+
import Plutus.Trace.Emulator qualified as Trace (EmulatorTrace, activateContractWallet, setSigningProcess, waitNSlots,
32+
walletInstanceTag)
3033
import Plutus.V1.Ledger.Api (Datum (Datum), ScriptContext, ValidatorHash)
3134
import Plutus.V1.Ledger.Scripts (ScriptError (EvaluationError))
3235
import Plutus.V1.Ledger.Value qualified as Value
3336
import PlutusTx qualified
3437
import PlutusTx.Prelude qualified as P
3538
import Prelude hiding (not)
36-
import Wallet.API (WalletAPIError (InsufficientFunds))
39+
import Wallet.Emulator.Error (WalletAPIError (InsufficientFunds))
40+
import Wallet.Emulator.Wallet (signPrivateKeys, walletToMockWallet')
3741

3842
tests :: TestTree
3943
tests =
@@ -42,7 +46,7 @@ tests =
4246
, spendLessThanScriptBalance
4347
, spendTokenBalanceFromScript
4448
, spendMoreThanScriptBalanceWithOwnWalletAsOwnPubkeyLookup
45-
--, spendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup -- FAILING
49+
, contractErrorWhenSpendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup
4650
, contractErrorWhenUnableToSpendMoreThanScriptTokenBalance
4751
, contractErrorWhenOwnPaymentPubKeyHashLookupIsMissing
4852
, phase2FailureWhenProducedAdaAmountIsNotSatisfied
@@ -91,22 +95,22 @@ trace contract = do
9195
spendAtLeastTheScriptBalance :: TestTree
9296
spendAtLeastTheScriptBalance =
9397
let contract = mustProduceAtLeastContract baseAdaValueLockedByScript baseAdaValueLockedByScript baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w1
94-
in checkPredicateOptions
95-
defaultCheckOptions
96-
"Successful use of mustProduceAtLeast at script's exact balance"
97-
(assertValidatedTransactionCount 2)
98-
(void $ trace contract)
98+
in checkPredicateOptions
99+
defaultCheckOptions
100+
"Successful use of mustProduceAtLeast at script's exact balance"
101+
(assertValidatedTransactionCount 2)
102+
(void $ trace contract)
99103

100104
-- | Uses onchain and offchain constraint mustProduceAtLeast to spend less ada than is locked by the script
101105
spendLessThanScriptBalance :: TestTree
102106
spendLessThanScriptBalance =
103107
let amt = Ada.lovelaceValueOf (baseLovelaceLockedByScript - 500)
104108
contract = mustProduceAtLeastContract amt amt baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w1
105-
in checkPredicateOptions
106-
defaultCheckOptions
107-
"Successful use of mustProduceAtLeast below script's balance"
108-
(assertValidatedTransactionCount 2)
109-
(void $ trace contract )
109+
in checkPredicateOptions
110+
defaultCheckOptions
111+
"Successful use of mustProduceAtLeast below script's balance"
112+
(assertValidatedTransactionCount 2)
113+
(void $ trace contract )
110114

111115
-- | Uses onchain and offchain constraint mustProduceAtLeast to spend entire token balance locked by the script
112116
spendTokenBalanceFromScript :: TestTree
@@ -115,27 +119,28 @@ spendTokenBalanceFromScript =
115119
contract = mustProduceAtLeastContract (baseAdaValueLockedByScript <> token) (baseAdaValueLockedByScript <> token) baseAdaAndTokenValueLockedByScript $ mockWalletPaymentPubKeyHash w1
116120
options = defaultCheckOptions
117121
& changeInitialWalletValue w1 (token <>)
118-
in checkPredicateOptions
119-
options
120-
"Successful use of mustProduceAtLeast sending tokens"
121-
(assertValidatedTransactionCount 2)
122-
(void $ trace contract)
122+
in checkPredicateOptions
123+
options
124+
"Successful use of mustProduceAtLeast sending tokens"
125+
(assertValidatedTransactionCount 2)
126+
(void $ trace contract)
123127

124128
-- | Uses onchain and offchain constraint mustProduceAtLeast to spend more than the ada balance locked by the script, excess is paid by own wallet.
125129
spendMoreThanScriptBalanceWithOwnWalletAsOwnPubkeyLookup :: TestTree
126130
spendMoreThanScriptBalanceWithOwnWalletAsOwnPubkeyLookup =
127131
let amt = Ada.lovelaceValueOf (baseLovelaceLockedByScript + 5_000_000)
128132
contract = mustProduceAtLeastContract amt amt baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w1
129-
in checkPredicateOptions
130-
defaultCheckOptions
131-
"Validation pass when mustProduceAtLeast is greater than script's balance"
132-
(assertValidatedTransactionCount 2)
133-
(void $ trace contract)
134-
135-
{- Failing due to https://github.com/input-output-hk/plutus-apps/issues/697: Errors with InsufficientFunds because it doesn't attempt to pay from w6
136-
-- | Uses onchain and offchain constraint mustProduceAtLeast to spend more than the ada balance locked by the script and own wallet, excess is paid by other wallet.
137-
spendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup :: TestTree
138-
spendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup =
133+
in checkPredicateOptions
134+
defaultCheckOptions
135+
"Validation pass when mustProduceAtLeast is greater than script's balance"
136+
(assertValidatedTransactionCount 2)
137+
(void $ trace contract)
138+
139+
-- | Uses onchain and offchain constraint mustProduceAtLeast to spend more than the ada balance
140+
-- locked by the script and own wallet. Other wallet is not used to pay excess because only
141+
-- own wallet can used.
142+
contractErrorWhenSpendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup :: TestTree
143+
contractErrorWhenSpendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup =
139144
let amt = Ada.lovelaceValueOf (baseLovelaceLockedByScript + 5_000_000)
140145
contract = mustProduceAtLeastContract amt amt baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w6
141146
options = defaultCheckOptions
@@ -144,12 +149,12 @@ spendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup =
144149
Trace.setSigningProcess w1 (Just $ signPrivateKeys [paymentPrivateKey $ walletToMockWallet' w1, paymentPrivateKey $ walletToMockWallet' w6])
145150
void $ Trace.activateContractWallet w1 contract
146151
void $ Trace.waitNSlots 1
147-
in checkPredicateOptions
148-
options
149-
"Validation pass when mustProduceAtLeast is greater than script's balance and other wallet's paymentPubKeyHash is used"
150-
(assertValidatedTransactionCount 2)
151-
(void traceWithW6Signing)
152-
-}
152+
in checkPredicateOptions
153+
options
154+
"Fail validation when there are not enough ada in own wallet to satisfy mustProduceAtLeast constraint. Other wallet is not used."
155+
(assertContractError contract (Trace.walletInstanceTag w1) (\case { WalletContractError (InsufficientFunds _)-> True; _ -> False }) "failed to throw error"
156+
.&&. assertValidatedTransactionCount 1)
157+
(void traceWithW6Signing)
153158

154159
-- Contract error is thrown when there are not enough tokens at the script and own wallet to satisfy mostProduceAtLeast constraint
155160
contractErrorWhenUnableToSpendMoreThanScriptTokenBalance :: TestTree
@@ -159,7 +164,7 @@ contractErrorWhenUnableToSpendMoreThanScriptTokenBalance =
159164
contract = mustProduceAtLeastContract offAmt onAmt baseAdaAndTokenValueLockedByScript $ mockWalletPaymentPubKeyHash w1
160165
options = defaultCheckOptions
161166
& changeInitialWalletValue w1 (someTokens 1 <>)
162-
in checkPredicateOptions
167+
in checkPredicateOptions
163168
options
164169
"Fail validation when there are not enough tokens to satisfy mustProduceAtLeast constraint"
165170
(assertContractError contract (Trace.walletInstanceTag w1) (\case { WalletContractError (InsufficientFunds _)-> True; _ -> False }) "failed to throw error"
@@ -188,12 +193,12 @@ contractErrorWhenOwnPaymentPubKeyHashLookupIsMissing =
188193

189194
amt = baseAdaValueLockedByScript
190195
contract = withoutOwnPubkeyHashLookupContract amt amt
191-
in checkPredicateOptions
192-
defaultCheckOptions
193-
"Fail validation when mustProduceAtLeast is greater than script's balance and wallet's pubkey is not in the lookup"
194-
(assertContractError contract (Trace.walletInstanceTag w1) (\case { ConstraintResolutionContractError Constraints.OwnPubKeyMissing -> True; _ -> False }) "failed to throw error"
195-
.&&. assertValidatedTransactionCount 1)
196-
(void $ trace contract)
196+
in checkPredicateOptions
197+
defaultCheckOptions
198+
"Fail validation when mustProduceAtLeast is greater than script's balance and wallet's pubkey is not in the lookup"
199+
(assertContractError contract (Trace.walletInstanceTag w1) (\case { ConstraintResolutionContractError Constraints.OwnPubKeyMissing -> True; _ -> False }) "failed to throw error"
200+
.&&. assertValidatedTransactionCount 1)
201+
(void $ trace contract)
197202

198203
-- Uses onchain and offchain constraint mustProduceAtLeast with a higher expected ada value onchain, asserts script evaluation error.
199204
phase2FailureWhenProducedAdaAmountIsNotSatisfied :: TestTree
@@ -204,7 +209,7 @@ phase2FailureWhenProducedAdaAmountIsNotSatisfied =
204209
contract = mustProduceAtLeastContract offAmt onAmt baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w1
205210
options = defaultCheckOptions
206211
& changeInitialWalletValue w1 (const $ Ada.lovelaceValueOf w1StartingBalance)
207-
in checkPredicateOptions
212+
in checkPredicateOptions
208213
options
209214
"Fail phase-2 validation when on-chain mustProduceAtLeast is greater than script's ada balance"
210215
(assertFailedTransaction (\_ err -> case err of {Ledger.ScriptFailure (EvaluationError ("L6":_) _) -> True; _ -> False }))
@@ -218,7 +223,7 @@ phase2FailureWhenProducedTokenAmountIsNotSatisfied =
218223
contract = mustProduceAtLeastContract offAmt onAmt baseAdaAndTokenValueLockedByScript $ mockWalletPaymentPubKeyHash w1
219224
options = defaultCheckOptions
220225
& changeInitialWalletValue w1 (someTokens 1 <>)
221-
in checkPredicateOptions
226+
in checkPredicateOptions
222227
options
223228
"Fail phase-2 validation when on-chain mustProduceAtLeast is greater than script's token balance"
224229
(assertFailedTransaction (\_ err -> case err of {Ledger.ScriptFailure (EvaluationError ("L6":_) _) -> True; _ -> False }))

0 commit comments

Comments
 (0)