Skip to content
This repository was archived by the owner on Dec 2, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ write-ghc-environment-files: never
tests: true
benchmarks: true

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

allow-newer:
size-based:template-haskell
Expand Down
95 changes: 50 additions & 45 deletions plutus-contract/test/Spec/TxConstraints/MustProduceAtLeast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Test.Tasty (TestTree, testGroup)
import Data.Function ((&))
import Ledger qualified
import Ledger.Ada qualified as Ada
import Ledger.CardanoWallet (paymentPrivateKey)
import Ledger.Constraints.OffChain qualified as Constraints (MkTxError (OwnPubKeyMissing), ownPaymentPubKeyHash,
plutusV1TypedValidatorLookups, unspentOutputs)
import Ledger.Constraints.OnChain.V1 qualified as Constraints (checkScriptContext)
Expand All @@ -22,18 +23,21 @@ import Ledger.Constraints.TxConstraints qualified as Constraints (collectFromThe
import Ledger.Generators (someTokenValue)
import Ledger.Tx qualified as Tx
import Ledger.Typed.Scripts qualified as Scripts
import Plutus.Contract as Con
import Plutus.Contract as Con (Contract, ContractError (ConstraintResolutionContractError, WalletContractError), Empty,
awaitTxConfirmed, submitTxConstraintsWith, utxosAt)
import Plutus.Contract.Test (assertContractError, assertFailedTransaction, assertValidatedTransactionCount,
changeInitialWalletValue, checkPredicateOptions, defaultCheckOptions,
mockWalletPaymentPubKeyHash, w1, (.&&.))
import Plutus.Trace qualified as Trace
mockWalletPaymentPubKeyHash, w1, w6, (.&&.))
import Plutus.Trace.Emulator qualified as Trace (EmulatorTrace, activateContractWallet, setSigningProcess, waitNSlots,
walletInstanceTag)
import Plutus.V1.Ledger.Api (Datum (Datum), ScriptContext, ValidatorHash)
import Plutus.V1.Ledger.Scripts (ScriptError (EvaluationError))
import Plutus.V1.Ledger.Value qualified as Value
import PlutusTx qualified
import PlutusTx.Prelude qualified as P
import Prelude hiding (not)
import Wallet.API (WalletAPIError (InsufficientFunds))
import Wallet.Emulator.Error (WalletAPIError (InsufficientFunds))
import Wallet.Emulator.Wallet (signPrivateKeys, walletToMockWallet')

tests :: TestTree
tests =
Expand All @@ -42,7 +46,7 @@ tests =
, spendLessThanScriptBalance
, spendTokenBalanceFromScript
, spendMoreThanScriptBalanceWithOwnWalletAsOwnPubkeyLookup
--, spendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup -- FAILING
, contractErrorWhenSpendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup
, contractErrorWhenUnableToSpendMoreThanScriptTokenBalance
, contractErrorWhenOwnPaymentPubKeyHashLookupIsMissing
, phase2FailureWhenProducedAdaAmountIsNotSatisfied
Expand Down Expand Up @@ -91,22 +95,22 @@ trace contract = do
spendAtLeastTheScriptBalance :: TestTree
spendAtLeastTheScriptBalance =
let contract = mustProduceAtLeastContract baseAdaValueLockedByScript baseAdaValueLockedByScript baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w1
in checkPredicateOptions
defaultCheckOptions
"Successful use of mustProduceAtLeast at script's exact balance"
(assertValidatedTransactionCount 2)
(void $ trace contract)
in checkPredicateOptions
defaultCheckOptions
"Successful use of mustProduceAtLeast at script's exact balance"
(assertValidatedTransactionCount 2)
(void $ trace contract)

-- | Uses onchain and offchain constraint mustProduceAtLeast to spend less ada than is locked by the script
spendLessThanScriptBalance :: TestTree
spendLessThanScriptBalance =
let amt = Ada.lovelaceValueOf (baseLovelaceLockedByScript - 500)
contract = mustProduceAtLeastContract amt amt baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w1
in checkPredicateOptions
defaultCheckOptions
"Successful use of mustProduceAtLeast below script's balance"
(assertValidatedTransactionCount 2)
(void $ trace contract )
in checkPredicateOptions
defaultCheckOptions
"Successful use of mustProduceAtLeast below script's balance"
(assertValidatedTransactionCount 2)
(void $ trace contract )

-- | Uses onchain and offchain constraint mustProduceAtLeast to spend entire token balance locked by the script
spendTokenBalanceFromScript :: TestTree
Expand All @@ -115,27 +119,28 @@ spendTokenBalanceFromScript =
contract = mustProduceAtLeastContract (baseAdaValueLockedByScript <> token) (baseAdaValueLockedByScript <> token) baseAdaAndTokenValueLockedByScript $ mockWalletPaymentPubKeyHash w1
options = defaultCheckOptions
& changeInitialWalletValue w1 (token <>)
in checkPredicateOptions
options
"Successful use of mustProduceAtLeast sending tokens"
(assertValidatedTransactionCount 2)
(void $ trace contract)
in checkPredicateOptions
options
"Successful use of mustProduceAtLeast sending tokens"
(assertValidatedTransactionCount 2)
(void $ trace contract)

-- | Uses onchain and offchain constraint mustProduceAtLeast to spend more than the ada balance locked by the script, excess is paid by own wallet.
spendMoreThanScriptBalanceWithOwnWalletAsOwnPubkeyLookup :: TestTree
spendMoreThanScriptBalanceWithOwnWalletAsOwnPubkeyLookup =
let amt = Ada.lovelaceValueOf (baseLovelaceLockedByScript + 5_000_000)
contract = mustProduceAtLeastContract amt amt baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w1
in checkPredicateOptions
defaultCheckOptions
"Validation pass when mustProduceAtLeast is greater than script's balance"
(assertValidatedTransactionCount 2)
(void $ trace contract)

{- 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
-- | 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.
spendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup :: TestTree
spendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup =
in checkPredicateOptions
defaultCheckOptions
"Validation pass when mustProduceAtLeast is greater than script's balance"
(assertValidatedTransactionCount 2)
(void $ trace contract)

-- | Uses onchain and offchain constraint mustProduceAtLeast to spend more than the ada balance
-- locked by the script and own wallet. Using setSigningProcess doesn't mean that other wallet can
-- be used to pay excess.
contractErrorWhenSpendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup :: TestTree
contractErrorWhenSpendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup =
let amt = Ada.lovelaceValueOf (baseLovelaceLockedByScript + 5_000_000)
contract = mustProduceAtLeastContract amt amt baseAdaValueLockedByScript $ mockWalletPaymentPubKeyHash w6
options = defaultCheckOptions
Expand All @@ -144,12 +149,12 @@ spendMoreThanScriptBalanceWithOtherWalletAsOwnPubkeyLookup =
Trace.setSigningProcess w1 (Just $ signPrivateKeys [paymentPrivateKey $ walletToMockWallet' w1, paymentPrivateKey $ walletToMockWallet' w6])
void $ Trace.activateContractWallet w1 contract
void $ Trace.waitNSlots 1
in checkPredicateOptions
options
"Validation pass when mustProduceAtLeast is greater than script's balance and other wallet's paymentPubKeyHash is used"
(assertValidatedTransactionCount 2)
(void traceWithW6Signing)
-}
in checkPredicateOptions
options
"Fail validation when there are not enough ada in own wallet to satisfy mustProduceAtLeast constraint. Other wallet is not used."
(assertContractError contract (Trace.walletInstanceTag w1) (\case { WalletContractError (InsufficientFunds _)-> True; _ -> False }) "failed to throw error"
.&&. assertValidatedTransactionCount 1)
(void traceWithW6Signing)

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

amt = baseAdaValueLockedByScript
contract = withoutOwnPubkeyHashLookupContract amt amt
in checkPredicateOptions
defaultCheckOptions
"Fail validation when mustProduceAtLeast is greater than script's balance and wallet's pubkey is not in the lookup"
(assertContractError contract (Trace.walletInstanceTag w1) (\case { ConstraintResolutionContractError Constraints.OwnPubKeyMissing -> True; _ -> False }) "failed to throw error"
.&&. assertValidatedTransactionCount 1)
(void $ trace contract)
in checkPredicateOptions
defaultCheckOptions
"Fail validation when mustProduceAtLeast is greater than script's balance and wallet's pubkey is not in the lookup"
(assertContractError contract (Trace.walletInstanceTag w1) (\case { ConstraintResolutionContractError Constraints.OwnPubKeyMissing -> True; _ -> False }) "failed to throw error"
.&&. assertValidatedTransactionCount 1)
(void $ trace contract)

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