Skip to content

Commit

Permalink
add smart constructors for inline datum
Browse files Browse the repository at this point in the history
  • Loading branch information
berewt committed Sep 23, 2022
1 parent 2a670c8 commit d2bda14
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 71 deletions.
7 changes: 2 additions & 5 deletions plutus-contract/test/Spec/Balancing.hs
Expand Up @@ -11,10 +11,10 @@ import Data.Map qualified as Map
import Data.Void (Void)
import Test.Tasty (TestTree, testGroup)

import Ledger (unitDatum, unitRedeemer)
import Ledger qualified
import Ledger.Ada qualified as Ada
import Ledger.Constraints qualified as L.Constraints
import Ledger.Scripts (unitRedeemer)
import Ledger.Test
import Ledger.Tx.Constraints qualified as Tx.Constraints
import Ledger.Value qualified as Value
Expand All @@ -30,9 +30,6 @@ import PlutusTx qualified
import Prelude hiding (not)
import Wallet.Emulator qualified as EM

unitDatum :: L.Constraints.OutDatum
unitDatum = L.Constraints.Hashed Ledger.unitDatum

tests :: TestTree
tests =
testGroup "balancing"
Expand Down Expand Up @@ -101,7 +98,7 @@ balanceTxnMinAda2 =
<> L.Constraints.plutusV1MintingPolicy mps
constraints = L.Constraints.mustSpendScriptOutput txOutRef unitRedeemer -- spend utxo1
<> L.Constraints.mustPayToOtherScript vHash unitDatum (vB 1) -- 2 ada and 1 B to script
<> L.Constraints.mustPayToOtherScript vHash (L.Constraints.Hashed $ Datum $ PlutusTx.toBuiltinData (0 :: Integer)) (vB 1) -- 2 ada and 1 B to script (different datum)
<> L.Constraints.mustPayToOtherScript vHash (Datum $ PlutusTx.toBuiltinData (0 :: Integer)) (vB 1) -- 2 ada and 1 B to script (different datum)
<> L.Constraints.mustMintValue (vL 1) -- 1 L and 2 ada to wallet2
submitTxConfirmed =<< mkTx lookups constraints

Expand Down
6 changes: 3 additions & 3 deletions plutus-contract/test/Spec/Contract.hs
Expand Up @@ -212,7 +212,7 @@ tests =

, let c :: Contract [Maybe DatumHash] Schema ContractError () = do
let w2PubKeyHash = mockWalletPaymentPubKeyHash w2
let payment = Constraints.mustPayWithDatumToPubKey w2PubKeyHash (Constraints.Hashed datum) (Ada.adaValueOf 10)
let payment = Constraints.mustPayWithDatumToPubKey w2PubKeyHash datum (Ada.adaValueOf 10)
tx <- submitTx payment
let txOuts = fmap fst $ Ledger.getCardanoTxOutRefs tx
-- tell the tx out' datum hash that was specified by 'mustPayWithDatumToPubKey'
Expand All @@ -232,11 +232,11 @@ tests =
-- in case of two transactions with 'mustPayWithDatumToPubKey'
, let c1 :: Contract [Maybe DatumHash] Schema ContractError () = do
let w2PubKeyHash = mockWalletPaymentPubKeyHash w2
let payment = Constraints.mustPayWithDatumToPubKey w2PubKeyHash (Constraints.Hashed datum1) (Ada.adaValueOf 10)
let payment = Constraints.mustPayWithDatumToPubKey w2PubKeyHash datum1 (Ada.adaValueOf 10)
void $ submitTx payment
c2 :: Contract [Maybe DatumHash] Schema ContractError () = do
let w3PubKeyHash = mockWalletPaymentPubKeyHash w3
let payment = Constraints.mustPayWithDatumToPubKey w3PubKeyHash (Constraints.Hashed datum2) (Ada.adaValueOf 50)
let payment = Constraints.mustPayWithDatumToPubKey w3PubKeyHash datum2 (Ada.adaValueOf 50)
void $ submitTx payment

datum1 = Datum $ PlutusTx.toBuiltinData (23 :: Integer)
Expand Down
8 changes: 4 additions & 4 deletions plutus-contract/test/Spec/Contract/TxConstraints.hs
Expand Up @@ -128,7 +128,7 @@ mustReferenceOutputV1ConTest = do
let ((utxoRef, utxo), (utxoRefForBalance1, _), (utxoRefForBalance2, _)) = get3 $ Map.toList utxos
vh = fromJust $ Addr.toValidatorHash mustReferenceOutputV1ValidatorAddress
lookups = TC.unspentOutputs utxos
tx = TC.mustPayToOtherScript vh (TC.Hashed $ Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
tx = TC.mustPayToOtherScript vh (Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
<> TC.mustSpendPubKeyOutput utxoRefForBalance1
mkTxConstraints @Void lookups tx >>= submitTxConfirmed

Expand All @@ -152,7 +152,7 @@ mustReferenceOutputTxV1ConTest = do
let ((utxoRef, utxo), (utxoRefForBalance1, _), (utxoRefForBalance2, _)) = get3 $ Map.toList utxos
vh = fromJust $ Addr.toValidatorHash mustReferenceOutputV1ValidatorAddress
lookups = Tx.Constraints.unspentOutputs utxos
tx = Tx.Constraints.mustPayToOtherScript vh (TC.Hashed $ Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
tx = Tx.Constraints.mustPayToOtherScript vh (Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
<> Tx.Constraints.mustSpendPubKeyOutput utxoRefForBalance1
<> Tx.Constraints.mustUseOutputAsCollateral utxoRefForBalance1
submitTxConfirmed $ mkTx lookups tx
Expand Down Expand Up @@ -197,7 +197,7 @@ mustReferenceOutputV2ConTest = do
let ((utxoRef, utxo), (utxoRefForBalance1, _), (utxoRefForBalance2, _)) = get3 $ Map.toList utxos
vh = fromJust $ Addr.toValidatorHash mustReferenceOutputV2ValidatorAddress
lookups = TC.unspentOutputs utxos
tx = TC.mustPayToOtherScript vh (TC.Hashed $ Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
tx = TC.mustPayToOtherScript vh (Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
<> TC.mustSpendPubKeyOutput utxoRefForBalance1
mkTxConstraints @Void lookups tx >>= submitTxConfirmed

Expand All @@ -221,7 +221,7 @@ mustReferenceOutputTxV2ConTest = do
let ((utxoRef, utxo), (utxoRefForBalance1, _), (utxoRefForBalance2, _)) = get3 $ Map.toList utxos
vh = fromJust $ Addr.toValidatorHash mustReferenceOutputV2ValidatorAddress
lookups = Tx.Constraints.unspentOutputs utxos
tx = Tx.Constraints.mustPayToOtherScript vh (TC.Hashed $ Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
tx = Tx.Constraints.mustPayToOtherScript vh (Datum $ PlutusTx.toBuiltinData utxoRef) (Ada.adaValueOf 5)
<> Tx.Constraints.mustSpendPubKeyOutput utxoRefForBalance1
<> Tx.Constraints.mustUseOutputAsCollateral utxoRefForBalance1
submitTxConfirmed $ mkTx lookups tx
Expand Down
4 changes: 2 additions & 2 deletions plutus-contract/test/Spec/ErrorChecking.hs
Expand Up @@ -19,7 +19,7 @@ import Data.Row
import Test.Tasty

import Ledger.Ada qualified as Ada
import Ledger.Constraints (OutDatum (Inline), collectFromTheScript, mustPayToOtherScript)
import Ledger.Constraints (collectFromTheScript, mustPayToOtherScript)
import Ledger.Tx (getCardanoTxId)
import Ledger.Typed.Scripts qualified as Scripts hiding (validatorHash)
import Plutus.Contract as Contract
Expand Down Expand Up @@ -141,7 +141,7 @@ contract = selectList [failFalseC, failHeadNilC, divZeroC, divZeroTraceC, succes
run validator = void $ do
let addr = mkValidatorAddress (validatorScript validator)
hash = validatorHash (validatorScript validator)
tx = mustPayToOtherScript hash (Inline $ Datum $ toBuiltinData ()) (Ada.adaValueOf 10)
tx = mustPayToOtherScript hash (Datum $ toBuiltinData ()) (Ada.adaValueOf 10)
r <- submitTx tx
awaitTxConfirmed (getCardanoTxId r)
utxos <- utxosAt addr
Expand Down
13 changes: 6 additions & 7 deletions plutus-contract/test/Spec/TxConstraints/MustIncludeDatum.hs
Expand Up @@ -13,10 +13,9 @@ import Test.Tasty (TestTree, testGroup)

import Ledger qualified
import Ledger.Ada qualified as Ada
import Ledger.Constraints qualified as Constraints (OutDatum (..), collectFromTheScript, mustIncludeDatum,
mustMintValueWithRedeemer, mustPayToOtherScript, mustPayToTheScript,
mustPayWithDatumToPubKey, plutusV1MintingPolicy,
typedValidatorLookups, unspentOutputs)
import Ledger.Constraints qualified as Constraints (collectFromTheScript, mustIncludeDatum, mustMintValueWithRedeemer,
mustPayToOtherScript, mustPayToTheScript, mustPayWithDatumToPubKey,
plutusV1MintingPolicy, typedValidatorLookups, unspentOutputs)
import Ledger.Constraints.OnChain.V1 qualified as Constraints (checkScriptContext)
import Ledger.Tx qualified as Tx
import Ledger.Typed.Scripts qualified as Scripts
Expand Down Expand Up @@ -74,8 +73,8 @@ mustIncludeDatumWhenPayingToScriptContract offChainDatums onChainDatums = do
where
mustPayToTheScriptAndIncludeDatumsIfUsingOffChainConstraint =
if null offChainDatums
then Constraints.mustPayToOtherScript valHash (Constraints.Hashed validatorDatum) (Ada.lovelaceValueOf 2_000_000)
else mconcat $ fmap (\datum -> Constraints.mustPayToOtherScript valHash (Constraints.Hashed validatorDatum) (Ada.lovelaceValueOf 2_000_000) <> Constraints.mustIncludeDatum datum) offChainDatums
then Constraints.mustPayToOtherScript valHash validatorDatum (Ada.lovelaceValueOf 2_000_000)
else mconcat $ fmap (\datum -> Constraints.mustPayToOtherScript valHash validatorDatum (Ada.lovelaceValueOf 2_000_000) <> Constraints.mustIncludeDatum datum) offChainDatums

trace :: Contract () Empty ContractError () -> Trace.EmulatorTrace ()
trace contract = do
Expand Down Expand Up @@ -164,7 +163,7 @@ mustIncludeDatumToPubKeyAddress =
let onChainConstraintDatumsAsRedeemer = Redeemer $ PlutusTx.dataToBuiltinData $ PlutusTx.toData ([validatorDatum] :: [Datum])
contract = do
let lookups1 = Constraints.plutusV1MintingPolicy mustIncludeDatumPolicy
tx1 = Constraints.mustPayWithDatumToPubKey (mockWalletPaymentPubKeyHash w1) (Constraints.Hashed validatorDatum) (Ada.lovelaceValueOf 25_000_000)
tx1 = Constraints.mustPayWithDatumToPubKey (mockWalletPaymentPubKeyHash w1) validatorDatum (Ada.lovelaceValueOf 25_000_000)
<> Constraints.mustIncludeDatum validatorDatum
<> Constraints.mustMintValueWithRedeemer onChainConstraintDatumsAsRedeemer tknValue
ledgerTx1 <- submitTxConstraintsWith @UnitTest lookups1 tx1
Expand Down
23 changes: 12 additions & 11 deletions plutus-contract/test/Spec/TxConstraints/MustPayToOtherScript.hs
Expand Up @@ -14,8 +14,9 @@ import Test.Tasty (TestTree, testGroup)
import Control.Lens ((&))
import Ledger qualified
import Ledger.Ada qualified as Ada
import Ledger.Constraints qualified as Constraints (OutDatum (Hashed, Inline), mustMintValueWithRedeemer,
mustPayToOtherScript, mustPayToOtherScriptAddress,
import Ledger.Constraints qualified as Constraints (mustMintValueWithRedeemer, mustPayToOtherScript,
mustPayToOtherScriptAddress, mustPayToOtherScriptAddressInlineDatum,
mustPayToOtherScriptInlineDatum,
mustSpendScriptOutputWithMatchingDatumAndValue,
plutusV1MintingPolicy, plutusV1OtherScript, plutusV2MintingPolicy,
unspentOutputs)
Expand Down Expand Up @@ -100,7 +101,7 @@ trace contract = do
mustPayToOtherScriptContract :: Value.Value -> Redeemer -> Contract () Empty ContractError ()
mustPayToOtherScriptContract offChainValue onChainConstraint = do
let lookups1 = Constraints.plutusV1MintingPolicy mustPayToOtherScriptPolicy
tx1 = Constraints.mustPayToOtherScript someValidatorHash (Constraints.Hashed someDatum) offChainValue
tx1 = Constraints.mustPayToOtherScript someValidatorHash someDatum offChainValue
<> Constraints.mustMintValueWithRedeemer onChainConstraint tknValue
ledgerTx1 <- submitTxConstraintsWith @UnitTest lookups1 tx1
awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1
Expand All @@ -121,7 +122,7 @@ successfulUseOfMustPayToOtherScriptWithMintedToken =
mustPayToOtherScriptInlineContractV2 :: Value.Value -> Redeemer -> Contract () Empty ContractError ()
mustPayToOtherScriptInlineContractV2 offChainValue onChainConstraint = do
let lookups1 = Constraints.plutusV2MintingPolicy mustPayToOtherScriptPolicyV2
tx1 = Constraints.mustPayToOtherScript someValidatorHash (Constraints.Inline someDatum) offChainValue
tx1 = Constraints.mustPayToOtherScriptInlineDatum someValidatorHash someDatum offChainValue
<> Constraints.mustMintValueWithRedeemer onChainConstraint tknValueV2
ledgerTx1 <- submitTxConstraintsWith @UnitTest lookups1 tx1
awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1
Expand Down Expand Up @@ -170,15 +171,15 @@ successfulUseOfMustPayToOtherScriptWithScriptsExactTokenBalance =
options = defaultCheckOptions & changeInitialWalletValue w1 (otherTokenValue <>)
contract = do
let lookups1 = Constraints.plutusV1OtherScript someValidator
tx1 = Constraints.mustPayToOtherScript someValidatorHash (Constraints.Hashed someDatum) adaAndOtherTokenValue
tx1 = Constraints.mustPayToOtherScript someValidatorHash someDatum adaAndOtherTokenValue
ledgerTx1 <- submitTxConstraintsWith @UnitTest lookups1 tx1
awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1

scriptUtxos <- utxosAt $ Ledger.scriptHashAddress someValidatorHash
let lookups2 = Constraints.plutusV1OtherScript someValidator
<> Constraints.unspentOutputs scriptUtxos
<> Constraints.plutusV1MintingPolicy mustPayToOtherScriptPolicy
tx2 = Constraints.mustPayToOtherScript otherValidatorHash (Constraints.Hashed someDatum) adaAndOtherTokenValue
tx2 = Constraints.mustPayToOtherScript otherValidatorHash someDatum adaAndOtherTokenValue
<> Constraints.mustSpendScriptOutputWithMatchingDatumAndValue someValidatorHash (\d -> d == someDatum) (\v -> v == adaAndOtherTokenValue) (asRedeemer ())
<> Constraints.mustMintValueWithRedeemer onChainConstraint tknValue
ledgerTx2 <- submitTxConstraintsWith @UnitTest lookups2 tx2
Expand All @@ -204,7 +205,7 @@ successfulUseOfMustPayToOtherScriptWhenOnchainExpectsLowerAdaValue =
mustPayToOtherScriptInlineContract :: Value.Value -> Redeemer -> Contract () Empty ContractError ()
mustPayToOtherScriptInlineContract offChainValue onChainConstraint = do
let lookups1 = Constraints.plutusV1MintingPolicy mustPayToOtherScriptPolicy
tx1 = Constraints.mustPayToOtherScript someValidatorHash (Constraints.Inline someDatum) offChainValue
tx1 = Constraints.mustPayToOtherScriptInlineDatum someValidatorHash someDatum offChainValue
<> Constraints.mustMintValueWithRedeemer onChainConstraint tknValue
ledgerTx1 <- submitTxConstraintsWith @UnitTest lookups1 tx1
awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1
Expand Down Expand Up @@ -262,14 +263,14 @@ instance Scripts.ValidatorTypes UnitTest
{-# INLINEABLE mkMustPayToOtherScriptPolicy #-}
mkMustPayToOtherScriptPolicy :: ConstraintParams -> Ledger.ScriptContext -> Bool
mkMustPayToOtherScriptPolicy t = case t of
MustPayToOtherScript vh d v -> Constraints.checkScriptContext @() @() (Constraints.mustPayToOtherScript vh (Constraints.Hashed d) v)
MustPayToOtherScriptAddress vh svh d v -> Constraints.checkScriptContext @() @() (Constraints.mustPayToOtherScriptAddress vh svh (Constraints.Hashed d) v)
MustPayToOtherScript vh d v -> Constraints.checkScriptContext @() @() (Constraints.mustPayToOtherScript vh d v)
MustPayToOtherScriptAddress vh svh d v -> Constraints.checkScriptContext @() @() (Constraints.mustPayToOtherScriptAddress vh svh d v)

{-# INLINEABLE mkMustPayToOtherScriptPolicyV2 #-}
mkMustPayToOtherScriptPolicyV2 :: ConstraintParams -> V2.Scripts.ScriptContext -> Bool
mkMustPayToOtherScriptPolicyV2 t = case t of
MustPayToOtherScript vh d v -> V2.Constraints.checkScriptContext @() @() (Constraints.mustPayToOtherScript vh (Constraints.Inline d) v)
MustPayToOtherScriptAddress vh svh d v -> V2.Constraints.checkScriptContext @() @() (Constraints.mustPayToOtherScriptAddress vh svh (Constraints.Inline d) v)
MustPayToOtherScript vh d v -> V2.Constraints.checkScriptContext @() @() (Constraints.mustPayToOtherScriptInlineDatum vh d v)
MustPayToOtherScriptAddress vh svh d v -> V2.Constraints.checkScriptContext @() @() (Constraints.mustPayToOtherScriptAddressInlineDatum vh svh d v)

mustPayToOtherScriptPolicy :: Scripts.MintingPolicy
mustPayToOtherScriptPolicy = Ledger.mkMintingPolicyScript $$(PlutusTx.compile [||wrap||])
Expand Down

0 comments on commit d2bda14

Please sign in to comment.