Skip to content

Commit

Permalink
Spilt 'higherThanScriptBalance' test into two and introduced ownPayme…
Browse files Browse the repository at this point in the history
…ntPubKeyHash script lookup (2 tests failing)
  • Loading branch information
James Browning committed Aug 8, 2022
1 parent c102e71 commit 164c269
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions plutus-contract/test/Spec/MustSpendAtLeast.hs
Expand Up @@ -13,7 +13,7 @@ import Test.Tasty (TestTree, testGroup)

import Ledger qualified
import Ledger.Ada qualified as Ada
import Ledger.Constraints.OffChain qualified as Constraints (MkTxError (OwnPubKeyMissing), plutusV1OtherScript,
import Ledger.Constraints.OffChain qualified as Constraints (MkTxError (OwnPubKeyMissing), ownPaymentPubKeyHash,
plutusV1TypedValidatorLookups, unspentOutputs)
import Ledger.Constraints.OnChain.V1 qualified as Constraints (checkScriptContext)
import Ledger.Constraints.TxConstraints qualified as Constraints (collectFromTheScript, mustIncludeDatum,
Expand All @@ -22,9 +22,9 @@ import Ledger.Tx qualified as Tx
import Ledger.Typed.Scripts qualified as Scripts
import Plutus.Contract as Con
import Plutus.Contract.Test (assertContractError, assertFailedTransaction, assertValidatedTransactionCount,
checkPredicateOptions, defaultCheckOptions, w1, (.&&.))
checkPredicateOptions, defaultCheckOptions, mockWalletPaymentPubKeyHash, w1, w6, (.&&.))
import Plutus.Trace qualified as Trace
import Plutus.V1.Ledger.Api (Datum (Datum), ScriptContext, Validator, ValidatorHash)
import Plutus.V1.Ledger.Api (BuiltinByteString, Datum (Datum), ScriptContext, Validator, ValidatorHash)
import Plutus.V1.Ledger.Scripts (ScriptError (EvaluationError))
import PlutusTx qualified
import PlutusTx.Prelude qualified as P
Expand All @@ -35,71 +35,90 @@ tests =
testGroup "MustSpendAtLeast"
[ entireScriptBalance
, lessThanScriptBalance
, higherThanScriptBalance
, higherThanScriptBalanceWithoutWalletPubkeyLookup
, higherThanScriptBalanceWithWalletPubkeyLookup
, phase2Failure
]

scriptBalance :: Integer
scriptBalance = 25_000_000

contract :: Integer -> Integer -> Contract () Empty ContractError ()
contract offAmt onAmt = do
mustSpendAtLeastContract :: Integer -> Integer -> Ledger.PaymentPubKeyHash-> Contract () Empty ContractError ()
mustSpendAtLeastContract offAmt onAmt pkh = do
let lookups1 = Constraints.plutusV1TypedValidatorLookups typedValidator
tx1 = Constraints.mustPayToTheScript onAmt (Ada.lovelaceValueOf scriptBalance)
ledgerTx1 <- submitTxConstraintsWith lookups1 tx1
awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1

utxos <- utxosAt scrAddress
let lookups2 = Constraints.plutusV1TypedValidatorLookups typedValidator
<> Constraints.plutusV1OtherScript validatorScript
<> Constraints.unspentOutputs utxos
<> Constraints.ownPaymentPubKeyHash pkh
tx2 =
Constraints.collectFromTheScript utxos ()
<> Constraints.mustIncludeDatum (Datum $ PlutusTx.toBuiltinData onAmt)
<> Constraints.mustSpendAtLeast (Ada.lovelaceValueOf offAmt)
ledgerTx2 <- submitTxConstraintsWith @UnitTest lookups2 tx2
awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx2

trace :: Integer -> Integer -> Trace.EmulatorTrace ()
trace offAmt onAmt = do
void $ Trace.activateContractWallet w1 $ contract offAmt onAmt
trace :: Contract () Empty ContractError () -> Trace.EmulatorTrace ()
trace contract = do
void $ Trace.activateContractWallet w1 contract
void $ Trace.waitNSlots 1

emptyPubKey :: Ledger.PaymentPubKeyHash
emptyPubKey = PlutusTx.unsafeFromBuiltinData $ PlutusTx.toBuiltinData ("" :: BuiltinByteString)

entireScriptBalance :: TestTree
entireScriptBalance = checkPredicateOptions
defaultCheckOptions
"Successful use of mustSpendAtLeast at script's exact balance"
(assertValidatedTransactionCount 2)
(void $ trace scriptBalance scriptBalance)
entireScriptBalance =
let contract = mustSpendAtLeastContract scriptBalance scriptBalance emptyPubKey
in checkPredicateOptions
defaultCheckOptions
"Successful use of mustSpendAtLeast at script's exact balance"
(assertValidatedTransactionCount 2)
(void $ trace contract)

lessThanScriptBalance :: TestTree
lessThanScriptBalance =
let amt = scriptBalance - 1
contract = mustSpendAtLeastContract amt amt emptyPubKey
in checkPredicateOptions
defaultCheckOptions
"Successful use of mustSpendAtLeast below script's balance"
(assertValidatedTransactionCount 2)
(void $ trace amt amt )
(void $ trace contract )

higherThanScriptBalanceWithWalletPubkeyLookup :: TestTree
higherThanScriptBalanceWithWalletPubkeyLookup =
let amt = scriptBalance + 5_000_000
contract = mustSpendAtLeastContract amt amt $ mockWalletPaymentPubKeyHash w1
in checkPredicateOptions
defaultCheckOptions
"Validation pass when mustSpendAtLeast is greater than script's balance and wallet's pubkey is included in the lookup"
(assertValidatedTransactionCount 2)
(void $ trace contract)

higherThanScriptBalance :: TestTree
higherThanScriptBalance =
let amt = scriptBalance + 1
higherThanScriptBalanceWithoutWalletPubkeyLookup :: TestTree
higherThanScriptBalanceWithoutWalletPubkeyLookup =
let amt = scriptBalance + 5_000_000
contract = mustSpendAtLeastContract amt amt $ mockWalletPaymentPubKeyHash w6
in checkPredicateOptions
defaultCheckOptions
"Fail validation when mustSpendAtLeast is greater than script's balance"
(assertContractError (contract amt amt) (Trace.walletInstanceTag w1) (\case { ConstraintResolutionContractError Constraints.OwnPubKeyMissing -> True; _ -> False }) "failed to throw error"
"Fail validation when mustSpendAtLeast 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 amt amt)
(void $ trace contract)

phase2Failure :: TestTree
phase2Failure =
let offAmt = scriptBalance
onAmt = scriptBalance + 1
contract = mustSpendAtLeastContract offAmt onAmt emptyPubKey
in checkPredicateOptions
defaultCheckOptions
"Fail phase-2 validation when on-chain mustSpendAtLeast is greater than script's balance"
(assertFailedTransaction (\_ err _ -> case err of {Ledger.ScriptFailure (EvaluationError ("L5":_) _) -> True; _ -> False }))
(void $ trace offAmt onAmt)
(void $ trace contract)

{-# INLINEABLE mkValidator #-}
mkValidator :: Integer -> () -> ScriptContext -> Bool
Expand Down

0 comments on commit 164c269

Please sign in to comment.