From 027f682849d1cf7df7da0d7b96a7fdd46b51e8b5 Mon Sep 17 00:00:00 2001 From: Sjoerd Visscher Date: Tue, 16 Aug 2022 16:08:42 +0200 Subject: [PATCH 1/3] Move common code to Plutus.Script.Utils.Typed --- .../test/Spec/Contract/TxConstraints.hs | 2 +- plutus-ledger/src/Ledger/Typed/Scripts.hs | 4 +- plutus-ledger/src/Ledger/Typed/TypeUtils.hs | 6 +- plutus-script-utils/plutus-script-utils.cabal | 2 +- .../src/Plutus/Script/Utils/Typed.hs | 99 +++++++++++++++++++ .../Plutus/Script/Utils/V1/Typed/Scripts.hs | 18 ---- .../Utils/V1/Typed/Scripts/Validators.hs | 79 ++------------- .../Plutus/Script/Utils/V1/Typed/TypeUtils.hs | 35 ------- .../Utils/V2/Typed/Scripts/Validators.hs | 58 ++--------- 9 files changed, 124 insertions(+), 179 deletions(-) create mode 100644 plutus-script-utils/src/Plutus/Script/Utils/Typed.hs delete mode 100644 plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/TypeUtils.hs diff --git a/plutus-contract/test/Spec/Contract/TxConstraints.hs b/plutus-contract/test/Spec/Contract/TxConstraints.hs index d75d15f343..900826af9f 100644 --- a/plutus-contract/test/Spec/Contract/TxConstraints.hs +++ b/plutus-contract/test/Spec/Contract/TxConstraints.hs @@ -38,9 +38,9 @@ import Plutus.Contract as Con import Plutus.Contract.Test (TracePredicate, assertValidatedTransactionCount, assertValidatedTransactionCountOfTotal, checkPredicate, checkPredicateOptions, defaultCheckOptions, minLogLevel, valueAtAddress, w1, walletFundsChange, (.&&.)) +import Plutus.Script.Utils.Typed (Any) import Plutus.Script.Utils.V1.Address qualified as PV1 import Plutus.Script.Utils.V1.Typed.Scripts qualified as PV1 -import Plutus.Script.Utils.V1.Typed.TypeUtils (Any) import Plutus.Script.Utils.V2.Address qualified as PV2 import Plutus.Script.Utils.V2.Typed.Scripts qualified as PV2 import Plutus.Trace.Emulator (ContractInstanceTag, EmulatorTrace, activateContract) diff --git a/plutus-ledger/src/Ledger/Typed/Scripts.hs b/plutus-ledger/src/Ledger/Typed/Scripts.hs index a2b5dcde13..d072ec9b26 100644 --- a/plutus-ledger/src/Ledger/Typed/Scripts.hs +++ b/plutus-ledger/src/Ledger/Typed/Scripts.hs @@ -13,10 +13,10 @@ module Ledger.Typed.Scripts , makeTypedScriptTxIn ) where -import Ledger.Tx.Internal (Language, TxIn (TxIn), TxInType (ConsumeScriptAddress)) +import Ledger.Tx.Internal (TxIn (TxIn), TxInType (ConsumeScriptAddress)) import Ledger.Typed.Scripts.Orphans as Export () +import Plutus.Script.Utils.Typed as Export import Plutus.Script.Utils.V1.Typed.Scripts as Export -import Plutus.Script.Utils.V1.Typed.TypeUtils as Export import Plutus.V1.Ledger.Api (Datum (Datum), Redeemer (Redeemer), ToData (..)) -- | A 'TxIn' tagged by two phantom types: a list of the types of the data scripts in the transaction; and the connection type of the input. diff --git a/plutus-ledger/src/Ledger/Typed/TypeUtils.hs b/plutus-ledger/src/Ledger/Typed/TypeUtils.hs index 887acd9213..4ce2c631b8 100644 --- a/plutus-ledger/src/Ledger/Typed/TypeUtils.hs +++ b/plutus-ledger/src/Ledger/Typed/TypeUtils.hs @@ -1,7 +1,7 @@ module Ledger.Typed.TypeUtils - {-# DEPRECATED "Use Plutus.Script.Utils.V1.Typed.TypeUtils instead" #-} - ( module Plutus.Script.Utils.V1.Typed.TypeUtils, + {-# DEPRECATED "Use Plutus.Script.Utils.Typed instead" #-} + ( module Plutus.Script.Utils.Typed, ) where -import Plutus.Script.Utils.V1.Typed.TypeUtils +import Plutus.Script.Utils.Typed diff --git a/plutus-script-utils/plutus-script-utils.cabal b/plutus-script-utils/plutus-script-utils.cabal index 629a97e26c..cca374a0f9 100644 --- a/plutus-script-utils/plutus-script-utils.cabal +++ b/plutus-script-utils/plutus-script-utils.cabal @@ -58,6 +58,7 @@ library default-language: Haskell2010 exposed-modules: Plutus.Script.Utils.Scripts + Plutus.Script.Utils.Typed Plutus.Script.Utils.V1.Address Plutus.Script.Utils.V1.Generators Plutus.Script.Utils.V1.Scripts @@ -66,7 +67,6 @@ library Plutus.Script.Utils.V1.Typed.Scripts.MonetaryPolicies Plutus.Script.Utils.V1.Typed.Scripts.StakeValidators Plutus.Script.Utils.V1.Typed.Scripts.Validators - Plutus.Script.Utils.V1.Typed.TypeUtils Plutus.Script.Utils.V2.Address Plutus.Script.Utils.V2.Contexts Plutus.Script.Utils.V2.Generators diff --git a/plutus-script-utils/src/Plutus/Script/Utils/Typed.hs b/plutus-script-utils/src/Plutus/Script/Utils/Typed.hs new file mode 100644 index 0000000000..c6a26bacc4 --- /dev/null +++ b/plutus-script-utils/src/Plutus/Script/Utils/Typed.hs @@ -0,0 +1,99 @@ +{-# LANGUAGE DeriveAnyClass #-} +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE EmptyDataDeriving #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE TypeFamilies #-} +module Plutus.Script.Utils.Typed ( + UntypedValidator + --- + , ValidatorTypes (..) + , TypedValidator (..) + , validatorHash + , validatorAddress + , validatorScript + , forwardingMintingPolicy + , forwardingMintingPolicyHash + , generalise + --- + , Any + , Language (PlutusV1, PlutusV2) +) where + +import Cardano.Ledger.Alonzo.Language (Language (PlutusV1, PlutusV2)) +import Data.Aeson (ToJSON) +import Data.Kind (Type) +import Data.Void (Void) +import GHC.Generics (Generic) +import Plutus.V1.Ledger.Address qualified as PV1 +import Plutus.V1.Ledger.Api qualified as PV1 +import PlutusTx.Builtins (BuiltinData) + +type UntypedValidator = BuiltinData -> BuiltinData -> BuiltinData -> () + +data Any + deriving stock (Eq, Show, Generic) + deriving anyclass (ToJSON) + +-- | A class that associates a type standing for a connection type with two types, the type of the +-- redeemer and the data script for that connection type. +class ValidatorTypes (a :: Type) where + -- | The type of the redeemers of this connection type. + type RedeemerType a :: Type + + -- | The type of the data of this connection type. + type DatumType a :: Type + + -- Defaults + type RedeemerType a = () + type DatumType a = () + +instance ValidatorTypes Void where + type RedeemerType Void = Void + type DatumType Void = Void + +instance ValidatorTypes Any where + type RedeemerType Any = BuiltinData + type DatumType Any = BuiltinData + +-- | A typed validator script with its 'ValidatorScript' and 'Address'. +data TypedValidator (a :: Type) = TypedValidator + { tvValidator :: PV1.Validator + , tvValidatorHash :: PV1.ValidatorHash + , tvForwardingMPS :: PV1.MintingPolicy + -- | The hash of the minting policy that checks whether the validator + -- is run in this transaction + , tvForwardingMPSHash :: PV1.MintingPolicyHash + , tvLanguage :: Language + } + deriving stock (Show, Eq, Generic) + +-- | The hash of the validator. +validatorHash :: TypedValidator a -> PV1.ValidatorHash +validatorHash = tvValidatorHash + +-- | The address of the validator. +validatorAddress :: TypedValidator a -> PV1.Address +validatorAddress = PV1.scriptHashAddress . tvValidatorHash + +-- | The validator script itself. +validatorScript :: TypedValidator a -> PV1.Validator +validatorScript = tvValidator + +-- | Generalise the typed validator to one that works with the 'Data' type. +generalise :: forall a. TypedValidator a -> TypedValidator Any +generalise TypedValidator {tvValidator, tvValidatorHash, tvForwardingMPS, tvForwardingMPSHash, tvLanguage} = + -- we can do this safely because the on-chain validators are untyped, so they always + -- take 'BuiltinData' arguments. The validator script stays the same, so the conversion + -- from 'BuiltinData' to 'a' still takes place, even if it's not reflected in the type + -- signature anymore. + TypedValidator {tvValidator, tvValidatorHash, tvForwardingMPS, tvForwardingMPSHash, tvLanguage} + +-- | The minting policy that forwards all checks to the instance's +-- validator +forwardingMintingPolicy :: TypedValidator a -> PV1.MintingPolicy +forwardingMintingPolicy = tvForwardingMPS + +-- | Hash of the minting policy that forwards all checks to the instance's +-- validator +forwardingMintingPolicyHash :: TypedValidator a -> PV1.MintingPolicyHash +forwardingMintingPolicyHash = tvForwardingMPSHash diff --git a/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/Scripts.hs b/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/Scripts.hs index 60640215ea..4614cf4cd8 100644 --- a/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/Scripts.hs +++ b/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/Scripts.hs @@ -15,7 +15,6 @@ module Plutus.Script.Utils.V1.Typed.Scripts StakeValidator, TypedScriptTxOut (tyTxOutData, tyTxOutTxOut), TypedScriptTxOutRef (tyTxOutRefOut, tyTxOutRefRef), - typePubKeyTxOut, makeTypedScriptTxOut, typeScriptTxOut, typeScriptTxOutRef, @@ -23,7 +22,6 @@ module Plutus.Script.Utils.V1.Typed.Scripts where import Control.Monad.Except (MonadError (throwError)) -import GHC.Generics (Generic) import Plutus.Script.Utils.Scripts (datumHash) import Plutus.Script.Utils.V1.Typed.Scripts.MonetaryPolicies hiding (forwardToValidator) import Plutus.Script.Utils.V1.Typed.Scripts.StakeValidators hiding (forwardToValidator) @@ -87,22 +85,6 @@ instance Eq (DatumType a) => Eq (TypedScriptTxOutRef a) where tyTxOutRefRef l == tyTxOutRefRef r && tyTxOutRefOut l == tyTxOutRefOut r --- | A public-key 'TxOut'. We need this to be sure that it is not a script output. -newtype PubKeyTxOut = PubKeyTxOut {unPubKeyTxOut :: TxOut} - deriving stock (Eq, Generic) - -- deriving newtype (FromJSON, ToJSON) - --- | Create a 'PubKeyTxOut' from an existing 'TxOut' by checking that it has the right payment type. -typePubKeyTxOut :: - forall m. - (MonadError ConnectionError m) => - TxOut -> - m PubKeyTxOut -typePubKeyTxOut txOut = - case txOutDatumHash txOut of - Nothing -> pure $ PubKeyTxOut txOut - Just _ -> throwError $ WrongOutType ExpectedPubkeyGotScript - -- | Create a 'TypedScriptTxOut' from an existing 'TxOut' by checking the types of its parts. typeScriptTxOut :: forall out m. diff --git a/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/Scripts/Validators.hs b/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/Scripts/Validators.hs index 23610b47b0..6136c41301 100644 --- a/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/Scripts/Validators.hs +++ b/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/Scripts/Validators.hs @@ -2,7 +2,6 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveAnyClass #-} {-# LANGUAGE DerivingStrategies #-} -{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeFamilies #-} {-# OPTIONS_GHC -Wno-orphans #-} @@ -39,12 +38,15 @@ import Control.Monad (unless) import Control.Monad.Except (MonadError (throwError)) import Data.Aeson (FromJSON, ToJSON) import Data.Kind (Type) -import Data.Void (Void) import GHC.Generics (Generic) import Plutus.Script.Utils.Scripts qualified as Scripts +import Plutus.Script.Utils.Typed (Any, DatumType, Language (PlutusV1), RedeemerType, + TypedValidator (TypedValidator, tvForwardingMPS, tvForwardingMPSHash, tvLanguage, tvValidator, tvValidatorHash), + UntypedValidator, ValidatorTypes, forwardingMintingPolicy, + forwardingMintingPolicyHash, generalise, validatorAddress, validatorHash, + validatorScript) import Plutus.Script.Utils.V1.Scripts qualified as Scripts import Plutus.Script.Utils.V1.Typed.Scripts.MonetaryPolicies qualified as MPS -import Plutus.Script.Utils.V1.Typed.TypeUtils (Any) import Plutus.V1.Ledger.Address qualified as PV1 import Plutus.V1.Ledger.Api qualified as PV1 import PlutusCore.Default (DefaultUni) @@ -52,8 +54,6 @@ import PlutusTx (CompiledCode, Lift, applyCode, liftCode) import PlutusTx.Prelude (check) import Prettyprinter (Pretty (pretty), viaShow, (<+>)) -type UntypedValidator = PV1.BuiltinData -> PV1.BuiltinData -> PV1.BuiltinData -> () - {-# INLINEABLE mkUntypedValidator #-} -- | Converts a custom datum and redeemer from a validator function to an @@ -111,50 +111,9 @@ mkUntypedValidator :: mkUntypedValidator f d r p = check $ f (PV1.unsafeFromBuiltinData d) (PV1.unsafeFromBuiltinData r) (PV1.unsafeFromBuiltinData p) --- | A class that associates a type standing for a connection type with two types, the type of the --- redeemer and the data script for that connection type. -class ValidatorTypes (a :: Type) where - -- | The type of the redeemers of this connection type. - type RedeemerType a :: Type - - -- | The type of the data of this connection type. - type DatumType a :: Type - - -- Defaults - type RedeemerType a = () - type DatumType a = () - -- | The type of validators for the given connection type. type ValidatorType (a :: Type) = DatumType a -> RedeemerType a -> PV1.ScriptContext -> Bool -instance ValidatorTypes Void where - type RedeemerType Void = Void - type DatumType Void = Void - -instance ValidatorTypes Any where - type RedeemerType Any = PV1.BuiltinData - type DatumType Any = PV1.BuiltinData - --- | A typed validator script with its 'ValidatorScript' and 'Address'. -data TypedValidator (a :: Type) = TypedValidator - { tvValidator :: PV1.Validator, - tvValidatorHash :: PV1.ValidatorHash, - tvForwardingMPS :: PV1.MintingPolicy, - -- | The hash of the minting policy that checks whether the validator - -- is run in this transaction - tvForwardingMPSHash :: PV1.MintingPolicyHash - } - deriving stock (Show, Eq, Generic) - --- | Generalise the typed validator to one that works with the 'Data' type. -generalise :: forall a. TypedValidator a -> TypedValidator Any -generalise TypedValidator {tvValidator, tvValidatorHash, tvForwardingMPS, tvForwardingMPSHash} = - -- we can do this safely because the on-chain validators are untyped, so they always - -- take 'BuiltinData' arguments. The validator script stays the same, so the conversion - -- from 'BuiltinData' to 'a' still takes place, even if it's not reflected in the type - -- signature anymore. - TypedValidator {tvValidator, tvValidatorHash, tvForwardingMPS, tvForwardingMPSHash} - -- | Make a 'TypedValidator' from the 'CompiledCode' of a validator script and its wrapper. mkTypedValidator :: -- | Validator script (compiled) @@ -167,7 +126,8 @@ mkTypedValidator vc wrapper = { tvValidator = val, tvValidatorHash = hsh, tvForwardingMPS = mps, - tvForwardingMPSHash = Scripts.mintingPolicyHash mps + tvForwardingMPSHash = Scripts.mintingPolicyHash mps, + tvLanguage = PlutusV1 } where val = PV1.mkValidatorScript $ wrapper `applyCode` vc @@ -188,18 +148,6 @@ mkTypedValidatorParam :: mkTypedValidatorParam vc wrapper param = mkTypedValidator (vc `applyCode` liftCode param) wrapper --- | The hash of the validator. -validatorHash :: TypedValidator a -> PV1.ValidatorHash -validatorHash = tvValidatorHash - --- | The address of the validator. -validatorAddress :: TypedValidator a -> PV1.Address -validatorAddress = PV1.scriptHashAddress . tvValidatorHash - --- | The validator script itself. -validatorScript :: TypedValidator a -> PV1.Validator -validatorScript = tvValidator - -- | Make a 'TypedValidator' (with no type constraints) from an untyped 'Validator' script. unsafeMkTypedValidator :: PV1.Validator -> TypedValidator Any unsafeMkTypedValidator vl = @@ -207,22 +155,13 @@ unsafeMkTypedValidator vl = { tvValidator = vl, tvValidatorHash = vh, tvForwardingMPS = mps, - tvForwardingMPSHash = Scripts.mintingPolicyHash mps + tvForwardingMPSHash = Scripts.mintingPolicyHash mps, + tvLanguage = PlutusV1 } where vh = Scripts.validatorHash vl mps = MPS.mkForwardingMintingPolicy vh --- | The minting policy that forwards all checks to the instance's --- validator -forwardingMintingPolicy :: TypedValidator a -> PV1.MintingPolicy -forwardingMintingPolicy = tvForwardingMPS - --- | Hash of the minting policy that forwards all checks to the instance's --- validator -forwardingMintingPolicyHash :: TypedValidator a -> PV1.MintingPolicyHash -forwardingMintingPolicyHash = tvForwardingMPSHash - data WrongOutTypeError = ExpectedScriptGotPubkey | ExpectedPubkeyGotScript diff --git a/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/TypeUtils.hs b/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/TypeUtils.hs deleted file mode 100644 index c5af65d420..0000000000 --- a/plutus-script-utils/src/Plutus/Script/Utils/V1/Typed/TypeUtils.hs +++ /dev/null @@ -1,35 +0,0 @@ -{-# LANGUAGE ConstraintKinds #-} -{-# LANGUAGE DataKinds #-} -{-# LANGUAGE DeriveAnyClass #-} -{-# LANGUAGE DerivingStrategies #-} -{-# LANGUAGE EmptyDataDeriving #-} -{-# LANGUAGE GADTs #-} -{-# LANGUAGE MultiParamTypeClasses #-} -{-# LANGUAGE PolyKinds #-} -{-# LANGUAGE Rank2Types #-} -{-# LANGUAGE ScopedTypeVariables #-} -{-# LANGUAGE TypeFamilies #-} -{-# LANGUAGE TypeOperators #-} -module Plutus.Script.Utils.V1.Typed.TypeUtils - ( Any - , HListF(..) - , hfOut - ) where - -import Data.Aeson (ToJSON) -import Data.Kind (Type) -import GHC.Generics (Generic) - -data Any - deriving stock (Eq, Show, Generic) - deriving anyclass (ToJSON) - --- | A heterogeneous list where every element is wrapped with the given functor. -data HListF (f :: Type -> Type) (l :: [Type]) where - HNilF :: HListF f '[] - HConsF :: f e -> HListF f l -> HListF f (e ': l) - --- | Turn a 'HListF' into a homogeneous list. Requires a very polymorphic function, likely something like 'coerce'. -hfOut :: forall o f (ts :: [Type]) . (forall a . f a -> o) -> HListF f ts -> [o] -hfOut _ HNilF = [] -hfOut f (HConsF e es) = f e : hfOut f es diff --git a/plutus-script-utils/src/Plutus/Script/Utils/V2/Typed/Scripts/Validators.hs b/plutus-script-utils/src/Plutus/Script/Utils/V2/Typed/Scripts/Validators.hs index 6de5d514a0..6186c92aa4 100644 --- a/plutus-script-utils/src/Plutus/Script/Utils/V2/Typed/Scripts/Validators.hs +++ b/plutus-script-utils/src/Plutus/Script/Utils/V2/Typed/Scripts/Validators.hs @@ -1,7 +1,6 @@ {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DerivingStrategies #-} -{-# LANGUAGE NamedFieldPuns #-} {-# LANGUAGE TypeFamilies #-} {-# OPTIONS_GHC -fno-specialise #-} @@ -28,12 +27,13 @@ module Plutus.Script.Utils.V2.Typed.Scripts.Validators where import Data.Kind (Type) -import GHC.Generics (Generic) -import Plutus.Script.Utils.V1.Typed.Scripts.Validators (DatumType, RedeemerType, UntypedValidator, ValidatorTypes) -import Plutus.Script.Utils.V1.Typed.TypeUtils (Any) +import Plutus.Script.Utils.Typed (Any, DatumType, Language (PlutusV2), RedeemerType, + TypedValidator (TypedValidator, tvForwardingMPS, tvForwardingMPSHash, tvLanguage, tvValidator, tvValidatorHash), + UntypedValidator, ValidatorTypes, forwardingMintingPolicy, + forwardingMintingPolicyHash, generalise, validatorAddress, validatorHash, + validatorScript) import Plutus.Script.Utils.V2.Scripts qualified as Scripts import Plutus.Script.Utils.V2.Typed.Scripts.MonetaryPolicies qualified as MPS -import Plutus.V1.Ledger.Address qualified as PV1 import Plutus.V2.Ledger.Api qualified as PV2 import PlutusCore.Default (DefaultUni) import PlutusTx (CompiledCode, Lift, UnsafeFromData (unsafeFromBuiltinData), applyCode, liftCode) @@ -98,26 +98,6 @@ mkUntypedValidator f d r p = -- | The type of validators for the given connection type. type ValidatorType (a :: Type) = DatumType a -> RedeemerType a -> PV2.ScriptContext -> Bool --- | A typed validator script with its 'ValidatorScript' and 'Address'. -data TypedValidator (a :: Type) = TypedValidator - { tvValidator :: PV2.Validator, - tvValidatorHash :: PV2.ValidatorHash, - tvForwardingMPS :: PV2.MintingPolicy, - -- | The hash of the minting policy that checks whether the validator - -- is run in this transaction - tvForwardingMPSHash :: PV2.MintingPolicyHash - } - deriving stock (Show, Eq, Generic) - --- | Generalise the typed validator to one that works with the 'Data' type. -generalise :: forall a. TypedValidator a -> TypedValidator Any -generalise TypedValidator {tvValidator, tvValidatorHash, tvForwardingMPS, tvForwardingMPSHash} = - -- we can do this safely because the on-chain validators are untyped, so they always - -- take 'BuiltinData' arguments. The validator script stays the same, so the conversion - -- from 'BuiltinData' to 'a' still takes place, even if it's not reflected in the type - -- signature anymore. - TypedValidator {tvValidator, tvValidatorHash, tvForwardingMPS, tvForwardingMPSHash} - -- | Make a 'TypedValidator' from the 'CompiledCode' of a validator script and its wrapper. mkTypedValidator :: -- | Validator script (compiled) @@ -130,7 +110,8 @@ mkTypedValidator vc wrapper = { tvValidator = val, tvValidatorHash = hsh, tvForwardingMPS = mps, - tvForwardingMPSHash = Scripts.mintingPolicyHash mps + tvForwardingMPSHash = Scripts.mintingPolicyHash mps, + tvLanguage = PlutusV2 } where val = PV2.mkValidatorScript $ wrapper `applyCode` vc @@ -151,18 +132,6 @@ mkTypedValidatorParam :: mkTypedValidatorParam vc wrapper param = mkTypedValidator (vc `applyCode` liftCode param) wrapper --- | The hash of the validator. -validatorHash :: TypedValidator a -> PV2.ValidatorHash -validatorHash = tvValidatorHash - --- | The address of the validator. -validatorAddress :: TypedValidator a -> PV2.Address -validatorAddress = PV1.scriptHashAddress . tvValidatorHash - --- | The validator script itself. -validatorScript :: TypedValidator a -> PV2.Validator -validatorScript = tvValidator - -- | Make a 'TypedValidator' (with no type constraints) from an untyped 'Validator' script. unsafeMkTypedValidator :: PV2.Validator -> TypedValidator Any unsafeMkTypedValidator vl = @@ -170,18 +139,9 @@ unsafeMkTypedValidator vl = { tvValidator = vl, tvValidatorHash = vh, tvForwardingMPS = mps, - tvForwardingMPSHash = Scripts.mintingPolicyHash mps + tvForwardingMPSHash = Scripts.mintingPolicyHash mps, + tvLanguage = PlutusV2 } where vh = Scripts.validatorHash vl mps = MPS.mkForwardingMintingPolicy vh - --- | The minting policy that forwards all checks to the instance's --- validator -forwardingMintingPolicy :: TypedValidator a -> PV2.MintingPolicy -forwardingMintingPolicy = tvForwardingMPS - --- | Hash of the minting policy that forwards all checks to the instance's --- validator -forwardingMintingPolicyHash :: TypedValidator a -> PV2.MintingPolicyHash -forwardingMintingPolicyHash = tvForwardingMPSHash From 653d611d4201f88ff8f2f9b2a68ae82c569c7687 Mon Sep 17 00:00:00 2001 From: Sjoerd Visscher Date: Wed, 17 Aug 2022 12:25:52 +0200 Subject: [PATCH 2/3] Enable V2 TypedValidators --- doc/plutus/tutorials/EscrowImpl.hs | 6 +- .../src/Plutus/Contract/Request.hs | 4 +- .../src/Plutus/Contract/StateMachine.hs | 4 +- .../Spec/TxConstraints/MustSpendAtLeast.hs | 6 +- .../test/Spec/TxConstraints/RequiredSigner.hs | 11 ++-- .../test/Spec/TxConstraints/TimeValidity.hs | 2 +- .../src/Ledger/Constraints.hs | 2 +- .../src/Ledger/Constraints/OffChain.hs | 55 +++++++++---------- .../src/Ledger/Constraints/TxConstraints.hs | 2 +- plutus-ledger/src/Ledger/Typed/Scripts.hs | 7 +-- .../usecases/Crowdfunding.hs | 4 +- plutus-playground-server/usecases/Vesting.hs | 4 +- .../src/Ledger/Tx/Constraints.hs | 2 +- .../src/Ledger/Tx/Constraints/OffChain.hs | 2 +- .../src/Plutus/Contracts/Crowdfunding.hs | 6 +- .../src/Plutus/Contracts/Escrow.hs | 6 +- plutus-use-cases/src/Plutus/Contracts/Game.hs | 4 +- .../src/Plutus/Contracts/MultiSig.hs | 4 +- .../src/Plutus/Contracts/PubKey.hs | 2 +- .../src/Plutus/Contracts/SimpleEscrow.hs | 6 +- .../src/Plutus/Contracts/TokenAccount.hs | 4 +- .../src/Plutus/Contracts/Tutorial/Escrow.hs | 6 +- .../Plutus/Contracts/Tutorial/EscrowStrict.hs | 6 +- .../src/Plutus/Contracts/Uniswap/OffChain.hs | 12 ++-- .../src/Plutus/Contracts/Vesting.hs | 4 +- .../test/Spec/Escrow/Endpoints.hs | 2 +- .../test/Spec/Uniswap/Endpoints.hs | 2 +- 27 files changed, 84 insertions(+), 91 deletions(-) diff --git a/doc/plutus/tutorials/EscrowImpl.hs b/doc/plutus/tutorials/EscrowImpl.hs index 2357889a5b..1bf5afdac2 100644 --- a/doc/plutus/tutorials/EscrowImpl.hs +++ b/doc/plutus/tutorials/EscrowImpl.hs @@ -270,7 +270,7 @@ pay inst escrow vl = do pk <- ownFirstPaymentPubKeyHash let tx = Constraints.mustPayToTheScript pk vl <> Constraints.mustValidateIn (Ledger.interval 1 (escrowDeadline escrow)) - utx <- mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) tx >>= adjustUnbalancedTx + utx <- mkTxConstraints (Constraints.typedValidatorLookups inst) tx >>= adjustUnbalancedTx getCardanoTxId <$> submitUnbalancedTx utx newtype RedeemSuccess = RedeemSuccess TxId @@ -311,7 +311,7 @@ redeem inst escrow = mapError (review _EscrowError) $ do else if foldMap (view Tx.ciTxOutValue) unspentOutputs `lt` targetTotal escrow then throwing _RedeemFailed NotEnoughFundsAtAddress else do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx >>= adjustUnbalancedTx RedeemSuccess . getCardanoTxId <$> submitUnbalancedTx utx @@ -343,7 +343,7 @@ refund inst escrow = do <> Constraints.mustValidateIn (from (Haskell.succ $ escrowDeadline escrow)) if Constraints.modifiesUtxoSet tx' then do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx' >>= adjustUnbalancedTx RefundSuccess . getCardanoTxId <$> submitUnbalancedTx utx diff --git a/plutus-contract/src/Plutus/Contract/Request.hs b/plutus-contract/src/Plutus/Contract/Request.hs index 5ad2ff90f2..902a9d3026 100644 --- a/plutus-contract/src/Plutus/Contract/Request.hs +++ b/plutus-contract/src/Plutus/Contract/Request.hs @@ -884,7 +884,7 @@ submitTxConstraints => TypedValidator a -> TxConstraints (RedeemerType a) (DatumType a) -> Contract w s e CardanoTx -submitTxConstraints inst = submitTxConstraintsWith (Constraints.plutusV1TypedValidatorLookups inst) +submitTxConstraints inst = submitTxConstraintsWith (Constraints.typedValidatorLookups inst) -- | Build a transaction that satisfies the constraints using the UTXO map -- to resolve any input constraints (see 'Ledger.Constraints.TxConstraints.InputConstraint') @@ -900,7 +900,7 @@ submitTxConstraintsSpending -> TxConstraints (RedeemerType a) (DatumType a) -> Contract w s e CardanoTx submitTxConstraintsSpending inst utxo = - let lookups = Constraints.plutusV1TypedValidatorLookups inst <> Constraints.unspentOutputs utxo + let lookups = Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs utxo in submitTxConstraintsWith lookups {-| A variant of 'mkTx' that runs in the 'Contract' monad, throwing errors and diff --git a/plutus-contract/src/Plutus/Contract/StateMachine.hs b/plutus-contract/src/Plutus/Contract/StateMachine.hs index 116292dc8d..5d5b46d3f2 100644 --- a/plutus-contract/src/Plutus/Contract/StateMachine.hs +++ b/plutus-contract/src/Plutus/Contract/StateMachine.hs @@ -440,7 +440,7 @@ runInitialiseWith customLookups customConstraints StateMachineClient{scInstance} ttConstraints ThreadToken{ttOutRef} = mustMintValueWithRedeemer red (SM.threadTokenValueOrZero scInstance) <> mustSpendPubKeyOutput ttOutRef - lookups = Constraints.plutusV1TypedValidatorLookups typedValidator + lookups = Constraints.typedValidatorLookups typedValidator <> foldMap (plutusV1MintingPolicy . curPolicy . ttOutRef) (smThreadToken stateMachine) <> Constraints.unspentOutputs utxo <> customLookups @@ -538,7 +538,7 @@ mkStep client@StateMachineClient{scInstance} input = do Just (newConstraints, newState) -> let isFinal = smFinal stateMachine (stateData newState) lookups = - Constraints.plutusV1TypedValidatorLookups typedValidator + Constraints.typedValidatorLookups typedValidator <> Constraints.unspentOutputs utxo <> if isFinal then foldMap (plutusV1MintingPolicy . curPolicy . ttOutRef) (smThreadToken stateMachine) else mempty red = Ledger.Redeemer (PlutusTx.toBuiltinData (Scripts.validatorHash typedValidator, Burn)) diff --git a/plutus-contract/test/Spec/TxConstraints/MustSpendAtLeast.hs b/plutus-contract/test/Spec/TxConstraints/MustSpendAtLeast.hs index 61f946872e..7b03106403 100644 --- a/plutus-contract/test/Spec/TxConstraints/MustSpendAtLeast.hs +++ b/plutus-contract/test/Spec/TxConstraints/MustSpendAtLeast.hs @@ -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 (ownPaymentPubKeyHash, plutusV1TypedValidatorLookups, +import Ledger.Constraints.OffChain qualified as Constraints (ownPaymentPubKeyHash, typedValidatorLookups, unspentOutputs) import Ledger.Constraints.OnChain.V1 qualified as Constraints (checkScriptContext) import Ledger.Constraints.TxConstraints qualified as Constraints (collectFromTheScript, mustIncludeDatum, @@ -45,13 +45,13 @@ scriptBalance = 25_000_000 mustSpendAtLeastContract :: Integer -> Integer -> Ledger.PaymentPubKeyHash-> Contract () Empty ContractError () mustSpendAtLeastContract offAmt onAmt pkh = do - let lookups1 = Constraints.plutusV1TypedValidatorLookups typedValidator + let lookups1 = Constraints.typedValidatorLookups typedValidator tx1 = Constraints.mustPayToTheScript onAmt (Ada.lovelaceValueOf scriptBalance) ledgerTx1 <- submitTxConstraintsWith lookups1 tx1 awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1 utxos <- utxosAt scrAddress - let lookups2 = Constraints.plutusV1TypedValidatorLookups typedValidator + let lookups2 = Constraints.typedValidatorLookups typedValidator <> Constraints.unspentOutputs utxos <> Constraints.ownPaymentPubKeyHash pkh tx2 = diff --git a/plutus-contract/test/Spec/TxConstraints/RequiredSigner.hs b/plutus-contract/test/Spec/TxConstraints/RequiredSigner.hs index 40fd390628..288cf60923 100644 --- a/plutus-contract/test/Spec/TxConstraints/RequiredSigner.hs +++ b/plutus-contract/test/Spec/TxConstraints/RequiredSigner.hs @@ -17,8 +17,7 @@ import Data.String (fromString) import Ledger qualified import Ledger.Ada qualified as Ada import Ledger.CardanoWallet as CW -import Ledger.Constraints.OffChain qualified as Constraints (paymentPubKey, plutusV1TypedValidatorLookups, - unspentOutputs) +import Ledger.Constraints.OffChain qualified as Constraints (paymentPubKey, typedValidatorLookups, unspentOutputs) import Ledger.Constraints.OnChain.V1 qualified as Constraints import Ledger.Constraints.TxConstraints qualified as Constraints (collectFromTheScript, mustBeSignedBy, mustIncludeDatum, mustPayToTheScript, @@ -47,14 +46,14 @@ tests = mustBeSignedByContract :: Ledger.PaymentPubKey -> Ledger.PaymentPubKeyHash -> Contract () Empty ContractError () mustBeSignedByContract pk pkh = do - let lookups1 = Constraints.plutusV1TypedValidatorLookups mustBeSignedByTypedValidator + let lookups1 = Constraints.typedValidatorLookups mustBeSignedByTypedValidator tx1 = Constraints.mustPayToTheScript () (Ada.lovelaceValueOf 25_000_000) ledgerTx1 <- submitTxConstraintsWith lookups1 tx1 awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1 utxos <- utxosAt (Ledger.scriptHashAddress $ Scripts.validatorHash mustBeSignedByTypedValidator) let lookups2 = - Constraints.plutusV1TypedValidatorLookups mustBeSignedByTypedValidator + Constraints.typedValidatorLookups mustBeSignedByTypedValidator <> Constraints.unspentOutputs utxos <> Constraints.paymentPubKey pk tx2 = @@ -67,14 +66,14 @@ mustBeSignedByContract pk pkh = do withoutOffChainMustBeSignedByContract :: Ledger.PaymentPubKey -> Ledger.PaymentPubKeyHash -> Contract () Empty ContractError () withoutOffChainMustBeSignedByContract pk pkh = do - let lookups1 = Constraints.plutusV1TypedValidatorLookups mustBeSignedByTypedValidator + let lookups1 = Constraints.typedValidatorLookups mustBeSignedByTypedValidator tx1 = Constraints.mustPayToTheScript () (Ada.lovelaceValueOf 25_000_000) ledgerTx1 <- submitTxConstraintsWith lookups1 tx1 awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1 utxos <- utxosAt (Ledger.scriptHashAddress $ Scripts.validatorHash mustBeSignedByTypedValidator) let lookups2 = - Constraints.plutusV1TypedValidatorLookups mustBeSignedByTypedValidator + Constraints.typedValidatorLookups mustBeSignedByTypedValidator <> Constraints.unspentOutputs utxos <> Constraints.paymentPubKey pk tx2 = diff --git a/plutus-contract/test/Spec/TxConstraints/TimeValidity.hs b/plutus-contract/test/Spec/TxConstraints/TimeValidity.hs index 629e3fbf42..a772296300 100644 --- a/plutus-contract/test/Spec/TxConstraints/TimeValidity.hs +++ b/plutus-contract/test/Spec/TxConstraints/TimeValidity.hs @@ -44,7 +44,7 @@ contract :: Contract () Empty ContractError () contract = do now <- Con.currentTime logInfo @String $ "now: " ++ show now - let lookups1 = Constraints.plutusV1TypedValidatorLookups $ typedValidator deadline + let lookups1 = Constraints.typedValidatorLookups $ typedValidator deadline tx1 = Constraints.mustPayToTheScript () (Ada.lovelaceValueOf 25000000) ledgerTx1 <- submitTxConstraintsWith lookups1 tx1 awaitTxConfirmed $ Tx.getCardanoTxId ledgerTx1 diff --git a/plutus-ledger-constraints/src/Ledger/Constraints.hs b/plutus-ledger-constraints/src/Ledger/Constraints.hs index ab862efe2a..6ca3cec2bf 100644 --- a/plutus-ledger-constraints/src/Ledger/Constraints.hs +++ b/plutus-ledger-constraints/src/Ledger/Constraints.hs @@ -50,7 +50,7 @@ module Ledger.Constraints( , OC.mkSomeTx -- ** Lookups , OC.ScriptLookups(..) - , OC.plutusV1TypedValidatorLookups + , OC.typedValidatorLookups , OC.unspentOutputs , OC.plutusV1MintingPolicy , OC.plutusV2MintingPolicy diff --git a/plutus-ledger-constraints/src/Ledger/Constraints/OffChain.hs b/plutus-ledger-constraints/src/Ledger/Constraints/OffChain.hs index 928e99c38a..1d42ba3a7d 100644 --- a/plutus-ledger-constraints/src/Ledger/Constraints/OffChain.hs +++ b/plutus-ledger-constraints/src/Ledger/Constraints/OffChain.hs @@ -19,7 +19,7 @@ module Ledger.Constraints.OffChain( -- * Lookups ScriptLookups(..) - , plutusV1TypedValidatorLookups + , typedValidatorLookups , generalise , unspentOutputs , plutusV1MintingPolicy @@ -104,7 +104,6 @@ import Ledger.Tx qualified as Tx import Ledger.Tx.CardanoAPI qualified as C import Ledger.Typed.Scripts (Any, ConnectionError (UnknownRef), TypedValidator, ValidatorTypes (DatumType, RedeemerType)) -import Ledger.Typed.Scripts qualified as Scripts import Ledger.Typed.Scripts qualified as Typed import Ledger.Validation (evaluateMinLovelaceOutput, fromPlutusTxOutUnsafe) import Plutus.Script.Utils.Scripts qualified as P @@ -120,29 +119,29 @@ import PlutusTx.Numeric qualified as N data ScriptLookups a = ScriptLookups - { slMPS :: Map MintingPolicyHash MintingPolicy + { slMPS :: Map MintingPolicyHash MintingPolicy -- ^ Minting policies that the script interacts with - , slTxOutputs :: Map TxOutRef ChainIndexTxOut + , slTxOutputs :: Map TxOutRef ChainIndexTxOut -- ^ Unspent outputs that the script may want to spend - , slOtherScripts :: Map ValidatorHash (Validator, Language) + , slOtherScripts :: Map ValidatorHash (Validator, Language) -- ^ Validators of scripts other than "our script" - , slOtherData :: Map DatumHash Datum + , slOtherData :: Map DatumHash Datum -- ^ Datums that we might need - , slPaymentPubKeyHashes :: Set PaymentPubKeyHash + , slPaymentPubKeyHashes :: Set PaymentPubKeyHash -- ^ Public keys that we might need - , slTypedPlutusV1Validator :: Maybe (TypedValidator a) + , slTypedValidator :: Maybe (TypedValidator a) -- ^ The script instance with the typed validator hash & actual compiled program - , slOwnPaymentPubKeyHash :: Maybe PaymentPubKeyHash + , slOwnPaymentPubKeyHash :: Maybe PaymentPubKeyHash -- ^ The contract's payment public key hash, used for depositing tokens etc. - , slOwnStakePubKeyHash :: Maybe StakePubKeyHash + , slOwnStakePubKeyHash :: Maybe StakePubKeyHash -- ^ The contract's stake public key hash (optional) } deriving stock (Show, Generic) deriving anyclass (ToJSON, FromJSON) generalise :: ScriptLookups a -> ScriptLookups Any generalise sl = - let validator = fmap Scripts.generalise (slTypedPlutusV1Validator sl) - in sl{slTypedPlutusV1Validator = validator} + let validator = fmap Typed.generalise (slTypedValidator sl) + in sl{slTypedValidator = validator} instance Semigroup (ScriptLookups a) where l <> r = @@ -153,7 +152,7 @@ instance Semigroup (ScriptLookups a) where , slOtherData = slOtherData l <> slOtherData r , slPaymentPubKeyHashes = slPaymentPubKeyHashes l <> slPaymentPubKeyHashes r -- 'First' to match the semigroup instance of Map (left-biased) - , slTypedPlutusV1Validator = fmap getFirst $ (First <$> slTypedPlutusV1Validator l) <> (First <$> slTypedPlutusV1Validator r) + , slTypedValidator = fmap getFirst $ (First <$> slTypedValidator l) <> (First <$> slTypedValidator r) , slOwnPaymentPubKeyHash = fmap getFirst $ (First <$> slOwnPaymentPubKeyHash l) <> (First <$> slOwnPaymentPubKeyHash r) @@ -173,14 +172,14 @@ instance Monoid (ScriptLookups a) where -- If called multiple times, only the first typed validator is kept: -- -- @ --- plutusV1TypedValidatorLookups tv1 <> plutusV1TypedValidatorLookups tv2 <> ... --- == plutusV1TypedValidatorLookups tv1 +-- typedValidatorLookups tv1 <> typedValidatorLookups tv2 <> ... +-- == typedValidatorLookups tv1 -- @ -plutusV1TypedValidatorLookups :: TypedValidator a -> ScriptLookups a -plutusV1TypedValidatorLookups inst = +typedValidatorLookups :: TypedValidator a -> ScriptLookups a +typedValidatorLookups inst = mempty - { slMPS = Map.singleton (Scripts.forwardingMintingPolicyHash inst) (Scripts.forwardingMintingPolicy inst) - , slTypedPlutusV1Validator = Just inst + { slMPS = Map.singleton (Typed.forwardingMintingPolicyHash inst) (Typed.forwardingMintingPolicy inst) + , slTypedValidator = Just inst } -- | A script lookups value that uses the map of unspent outputs to resolve @@ -518,8 +517,8 @@ addOwnInput => ScriptInputConstraint (RedeemerType a) -> m () addOwnInput ScriptInputConstraint{icRedeemer, icTxOutRef} = do - ScriptLookups{slTxOutputs, slTypedPlutusV1Validator} <- ask - inst <- maybe (throwError TypedValidatorMissing) pure slTypedPlutusV1Validator + ScriptLookups{slTxOutputs, slTypedValidator} <- ask + inst <- maybe (throwError TypedValidatorMissing) pure slTypedValidator typedOutRef <- either (throwError . TypeCheckFailed) pure $ runExcept @Typed.ConnectionError @@ -529,8 +528,7 @@ addOwnInput ScriptInputConstraint{icRedeemer, icTxOutRef} = do datum <- ciTxOut ^? Tx.ciTxOutScriptDatum . _2 . _Just pure (Tx.toTxOut ciTxOut, datum) Typed.typeScriptTxOutRef inst icTxOutRef txOut datum - -- TODO Needs to work with PlutusV1 AND PlutusV2. - let txIn = Scripts.makeTypedScriptTxIn PlutusV1 inst icRedeemer typedOutRef + let txIn = Typed.makeTypedScriptTxIn inst icRedeemer typedOutRef vl = Tx.txOutValue $ Typed.tyTxOutTxOut $ Typed.tyTxOutRefOut typedOutRef unbalancedTx . tx . Tx.inputs %= (Typed.tyTxInTxIn txIn :) valueSpentInputs <>= provided vl @@ -546,8 +544,8 @@ addOwnOutput => ScriptOutputConstraint (DatumType a) -> m () addOwnOutput ScriptOutputConstraint{ocDatum, ocValue} = do - ScriptLookups{slTypedPlutusV1Validator} <- ask - inst <- maybe (throwError TypedValidatorMissing) pure slTypedPlutusV1Validator + ScriptLookups{slTypedValidator} <- ask + inst <- maybe (throwError TypedValidatorMissing) pure slTypedValidator let txOut = Typed.makeTypedScriptTxOut inst ocDatum ocValue dsV = Datum (toBuiltinData ocDatum) unbalancedTx . tx . Tx.outputs %= (Typed.tyTxOutTxOut txOut :) @@ -752,13 +750,12 @@ resolveScriptTxOut => ChainIndexTxOut -> m (Maybe ((ValidatorHash, Validator, Language), (DatumHash, Datum), Value)) resolveScriptTxOut Tx.ScriptChainIndexTxOut - { Tx._ciTxOutValidator = (vh, v) + { Tx._ciTxOutValidator = (vh, _) , Tx._ciTxOutScriptDatum = (dh, d) , Tx._ciTxOutValue } = do - -- first check in the 'ChainIndexTx' for the validator, then - -- look for it in the 'slOtherScripts map. - (validator, pv) <- maybe (lookupValidator vh) (pure . (, PlutusV1)) v + -- Look for the validator in the 'slOtherScripts map so we also get the language. + (validator, pv) <- lookupValidator vh -- first check in the 'ChainIndexTxOut' for the datum, then -- look for it in the 'slOtherData' map. diff --git a/plutus-ledger-constraints/src/Ledger/Constraints/TxConstraints.hs b/plutus-ledger-constraints/src/Ledger/Constraints/TxConstraints.hs index 8c198f7f56..06ca59a415 100644 --- a/plutus-ledger-constraints/src/Ledger/Constraints/TxConstraints.hs +++ b/plutus-ledger-constraints/src/Ledger/Constraints/TxConstraints.hs @@ -300,7 +300,7 @@ mustIncludeDatum = singleton . MustIncludeDatum -- output with @d@ and @v@ and adds @d@ in the transaction's datum witness set. -- The script address is derived from the typed validator that is provided in -- the 'Ledger.Constraints.OffChain.ScriptLookups' with --- 'Ledger.Constraints.OffChain.plutusV1TypedValidatorLookups'. +-- 'Ledger.Constraints.OffChain.typedValidatorLookups'. -- -- If used in 'Ledger.Constraints.OnChain', this constraint verifies that @d@ is -- part of the datum witness set and that the new script transaction output with diff --git a/plutus-ledger/src/Ledger/Typed/Scripts.hs b/plutus-ledger/src/Ledger/Typed/Scripts.hs index d072ec9b26..cc30462f7a 100644 --- a/plutus-ledger/src/Ledger/Typed/Scripts.hs +++ b/plutus-ledger/src/Ledger/Typed/Scripts.hs @@ -1,7 +1,5 @@ {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE GADTs #-} -{-# LANGUAGE NamedFieldPuns #-} -{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE UndecidableInstances #-} @@ -34,15 +32,14 @@ instance Eq (DatumType a) => Eq (TypedScriptTxIn a) where makeTypedScriptTxIn :: forall inn. (ToData (RedeemerType inn), ToData (DatumType inn)) => - Language -> TypedValidator inn -> RedeemerType inn -> TypedScriptTxOutRef inn -> TypedScriptTxIn inn -makeTypedScriptTxIn lang si r tyRef = +makeTypedScriptTxIn si r tyRef = let d = Export.tyTxOutData (Export.tyTxOutRefOut tyRef) vs = validatorScript si rs = Redeemer (toBuiltinData r) ds = Datum (toBuiltinData d) - txInT = ConsumeScriptAddress lang vs rs ds + txInT = ConsumeScriptAddress (Export.tvLanguage si) vs rs ds in TypedScriptTxIn @inn (TxIn (Export.tyTxOutRefRef tyRef) (Just txInT)) tyRef diff --git a/plutus-playground-server/usecases/Crowdfunding.hs b/plutus-playground-server/usecases/Crowdfunding.hs index 5460bd5623..5b4bec916e 100644 --- a/plutus-playground-server/usecases/Crowdfunding.hs +++ b/plutus-playground-server/usecases/Crowdfunding.hs @@ -166,7 +166,7 @@ contribute cmp = endpoint @"contribute" $ \Contribution{contribValue} -> do let inst = typedValidator cmp tx = Constraints.mustPayToTheScript contributor contribValue <> Constraints.mustValidateIn (Interval.to (campaignDeadline cmp)) - txid <- fmap getCardanoTxId $ mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) tx + txid <- fmap getCardanoTxId $ mkTxConstraints (Constraints.typedValidatorLookups inst) tx >>= adjustUnbalancedTx >>= submitUnbalancedTx utxo <- watchAddressUntilTime (Scripts.validatorAddress inst) (campaignCollectionDeadline cmp) @@ -182,7 +182,7 @@ contribute cmp = endpoint @"contribute" $ \Contribution{contribValue} -> do if Constraints.modifiesUtxoSet tx' then do logInfo @Text "Claiming refund" - void $ mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst + void $ mkTxConstraints (Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs utxo) tx' >>= adjustUnbalancedTx >>= submitUnbalancedTx else pure () diff --git a/plutus-playground-server/usecases/Vesting.hs b/plutus-playground-server/usecases/Vesting.hs index 53ef0a2825..56ffab5c39 100644 --- a/plutus-playground-server/usecases/Vesting.hs +++ b/plutus-playground-server/usecases/Vesting.hs @@ -164,7 +164,7 @@ vestFundsC -> Contract () s T.Text () vestFundsC vesting = do let txn = payIntoContract (totalAmount vesting) - mkTxConstraints (Constraints.plutusV1TypedValidatorLookups $ typedValidator vesting) txn + mkTxConstraints (Constraints.typedValidatorLookups $ typedValidator vesting) txn >>= adjustUnbalancedTx >>= void . submitUnbalancedTx data Liveness = Alive | Dead @@ -207,7 +207,7 @@ retrieveFundsC vesting payment = do -- we don't need to add a pubkey output for 'vestingOwner' here -- because this will be done by the wallet when it balances the -- transaction. - mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst + mkTxConstraints (Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs) txn >>= adjustUnbalancedTx >>= void . submitUnbalancedTx return liveness diff --git a/plutus-tx-constraints/src/Ledger/Tx/Constraints.hs b/plutus-tx-constraints/src/Ledger/Tx/Constraints.hs index 8f1c1f4736..fe02f2bc84 100644 --- a/plutus-tx-constraints/src/Ledger/Tx/Constraints.hs +++ b/plutus-tx-constraints/src/Ledger/Tx/Constraints.hs @@ -50,7 +50,7 @@ module Ledger.Tx.Constraints( , OC.mkSomeTx -- ** Lookups , OC.ScriptLookups(..) - , OC.plutusV1TypedValidatorLookups + , OC.typedValidatorLookups , OC.unspentOutputs , OC.plutusV1MintingPolicy , OC.plutusV2MintingPolicy diff --git a/plutus-tx-constraints/src/Ledger/Tx/Constraints/OffChain.hs b/plutus-tx-constraints/src/Ledger/Tx/Constraints/OffChain.hs index 280679bf50..3f174a1779 100644 --- a/plutus-tx-constraints/src/Ledger/Tx/Constraints/OffChain.hs +++ b/plutus-tx-constraints/src/Ledger/Tx/Constraints/OffChain.hs @@ -21,7 +21,7 @@ module Ledger.Tx.Constraints.OffChain( -- * Lookups P.ScriptLookups(..) - , P.plutusV1TypedValidatorLookups + , P.typedValidatorLookups , P.generalise , P.unspentOutputs , P.plutusV1MintingPolicy diff --git a/plutus-use-cases/src/Plutus/Contracts/Crowdfunding.hs b/plutus-use-cases/src/Plutus/Contracts/Crowdfunding.hs index 3642365552..49e64493c0 100644 --- a/plutus-use-cases/src/Plutus/Contracts/Crowdfunding.hs +++ b/plutus-use-cases/src/Plutus/Contracts/Crowdfunding.hs @@ -207,7 +207,7 @@ contribute cmp = endpoint @"contribute" $ \Contribution{contribValue} -> do let inst = typedValidator cmp tx = Constraints.mustPayToTheScript contributor contribValue <> Constraints.mustValidateIn (Interval.to (campaignDeadline cmp)) - txid <- fmap getCardanoTxId $ mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) tx + txid <- fmap getCardanoTxId $ mkTxConstraints (Constraints.typedValidatorLookups inst) tx >>= adjustUnbalancedTx >>= submitUnbalancedTx utxo <- watchAddressUntilTime (Scripts.validatorAddress inst) $ campaignCollectionDeadline cmp @@ -223,7 +223,7 @@ contribute cmp = endpoint @"contribute" $ \Contribution{contribValue} -> do if Constraints.modifiesUtxoSet tx' then do logInfo @Text "Claiming refund" - void $ mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst + void $ mkTxConstraints (Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs utxo) tx' >>= adjustUnbalancedTx >>= submitUnbalancedTx else pure () @@ -247,7 +247,7 @@ scheduleCollection cmp = endpoint @"schedule collection" $ \() -> do <> Constraints.mustValidateIn (collectionRange cmp) logInfo @Text "Collecting funds" - void $ mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst + void $ mkTxConstraints (Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs) tx >>= adjustUnbalancedTx >>= submitUnbalancedTx diff --git a/plutus-use-cases/src/Plutus/Contracts/Escrow.hs b/plutus-use-cases/src/Plutus/Contracts/Escrow.hs index 243aa1687a..5680e1aa67 100644 --- a/plutus-use-cases/src/Plutus/Contracts/Escrow.hs +++ b/plutus-use-cases/src/Plutus/Contracts/Escrow.hs @@ -260,7 +260,7 @@ pay inst escrow vl = do pk <- ownFirstPaymentPubKeyHash let tx = Constraints.mustPayToTheScript pk vl <> Constraints.mustValidateIn (Ledger.interval 1 (escrowDeadline escrow)) - mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) tx + mkTxConstraints (Constraints.typedValidatorLookups inst) tx >>= adjustUnbalancedTx >>= submitUnbalancedTx >>= return . getCardanoTxId @@ -303,7 +303,7 @@ redeem inst escrow = mapError (review _EscrowError) $ do else if foldMap (view Tx.ciTxOutValue) unspentOutputs `lt` targetTotal escrow then throwing _RedeemFailed NotEnoughFundsAtAddress else do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx adjusted <- adjustUnbalancedTx utx @@ -336,7 +336,7 @@ refund inst escrow = do <> Constraints.mustValidateIn (from (Haskell.succ $ escrowDeadline escrow)) if Constraints.modifiesUtxoSet tx' then do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx' adjusted <- adjustUnbalancedTx utx diff --git a/plutus-use-cases/src/Plutus/Contracts/Game.hs b/plutus-use-cases/src/Plutus/Contracts/Game.hs index 5fb7fee8a5..37ecffc949 100644 --- a/plutus-use-cases/src/Plutus/Contracts/Game.hs +++ b/plutus-use-cases/src/Plutus/Contracts/Game.hs @@ -159,7 +159,7 @@ data GuessArgs = lock :: AsContractError e => Promise () GameSchema e () lock = endpoint @"lock" $ \LockArgs { lockArgsGameParam, lockArgsSecret, lockArgsValue } -> do logInfo @Haskell.String $ "Pay " <> Haskell.show lockArgsValue <> " to the script" - let lookups = Constraints.plutusV1TypedValidatorLookups (gameInstance lockArgsGameParam) + let lookups = Constraints.typedValidatorLookups (gameInstance lockArgsGameParam) tx = Constraints.mustPayToTheScript (hashString lockArgsSecret) lockArgsValue mkTxConstraints lookups tx >>= adjustUnbalancedTx >>= yieldUnbalancedTx @@ -170,7 +170,7 @@ guess = endpoint @"guess" $ \GuessArgs { guessArgsGameParam, guessArgsSecret } - logInfo @Haskell.String "Waiting for script to have a UTxO of at least 1 lovelace" utxos <- fundsAtAddressGeq (gameAddress guessArgsGameParam) (Ada.lovelaceValueOf 1) - let lookups = Constraints.plutusV1TypedValidatorLookups (gameInstance guessArgsGameParam) + let lookups = Constraints.typedValidatorLookups (gameInstance guessArgsGameParam) Haskell.<> Constraints.unspentOutputs utxos redeemer = clearString guessArgsSecret tx = Constraints.collectFromTheScript utxos redeemer diff --git a/plutus-use-cases/src/Plutus/Contracts/MultiSig.hs b/plutus-use-cases/src/Plutus/Contracts/MultiSig.hs index 87045ecdd6..906cd8ac21 100644 --- a/plutus-use-cases/src/Plutus/Contracts/MultiSig.hs +++ b/plutus-use-cases/src/Plutus/Contracts/MultiSig.hs @@ -78,7 +78,7 @@ lock :: AsContractError e => Promise () MultiSigSchema e () lock = endpoint @"lock" $ \(ms, vl) -> do let inst = typedValidator ms let tx = Constraints.mustPayToTheScript () vl - lookups = Constraints.plutusV1TypedValidatorLookups inst + lookups = Constraints.typedValidatorLookups inst mkTxConstraints lookups tx >>= adjustUnbalancedTx >>= void . submitUnbalancedTx @@ -90,7 +90,7 @@ unlock = endpoint @"unlock" $ \(ms, pks) -> do utx <- utxosAt (Scripts.validatorAddress inst) let tx = Constraints.collectFromTheScript utx () <> foldMap Constraints.mustBeSignedBy pks - lookups = Constraints.plutusV1TypedValidatorLookups inst + lookups = Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs utx mkTxConstraints lookups tx >>= adjustUnbalancedTx >>= void . submitUnbalancedTx diff --git a/plutus-use-cases/src/Plutus/Contracts/PubKey.hs b/plutus-use-cases/src/Plutus/Contracts/PubKey.hs index 3bd7127c5d..03b9ba19fd 100644 --- a/plutus-use-cases/src/Plutus/Contracts/PubKey.hs +++ b/plutus-use-cases/src/Plutus/Contracts/PubKey.hs @@ -75,7 +75,7 @@ pubKeyContract pk vl = mapError (review _PubKeyError ) $ do address = Scripts.validatorAddress inst tx = Constraints.mustPayToTheScript () vl - ledgerTx <- mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) tx + ledgerTx <- mkTxConstraints (Constraints.typedValidatorLookups inst) tx >>= adjustUnbalancedTx >>= submitUnbalancedTx _ <- awaitTxConfirmed (getCardanoTxId ledgerTx) diff --git a/plutus-use-cases/src/Plutus/Contracts/SimpleEscrow.hs b/plutus-use-cases/src/Plutus/Contracts/SimpleEscrow.hs index f0074b8c2c..a9a78b80a7 100644 --- a/plutus-use-cases/src/Plutus/Contracts/SimpleEscrow.hs +++ b/plutus-use-cases/src/Plutus/Contracts/SimpleEscrow.hs @@ -128,7 +128,7 @@ lockEp = endpoint @"lock" $ \params -> do let valRange = Interval.to (Haskell.pred $ deadline params) tx = Constraints.mustPayToTheScript params (paying params) <> Constraints.mustValidateIn valRange - void $ mkTxConstraints (Constraints.plutusV1TypedValidatorLookups escrowInstance) tx + void $ mkTxConstraints (Constraints.typedValidatorLookups escrowInstance) tx >>= adjustUnbalancedTx >>= submitUnbalancedTx -- | Attempts to redeem the 'Value' locked into this script by paying in from @@ -152,7 +152,7 @@ redeemEp = endpoint @"redeem" redeem if time >= deadline params then throwing _RedeemFailed DeadlinePassed else do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups escrowInstance + utx <- mkTxConstraints ( Constraints.typedValidatorLookups escrowInstance <> Constraints.unspentOutputs unspentOutputs ) tx >>= adjustUnbalancedTx RedeemSuccess . getCardanoTxId <$> submitUnbalancedTx utx @@ -169,7 +169,7 @@ refundEp = endpoint @"refund" refund if Constraints.modifiesUtxoSet tx then do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups escrowInstance + utx <- mkTxConstraints ( Constraints.typedValidatorLookups escrowInstance <> Constraints.unspentOutputs unspentOutputs ) tx >>= adjustUnbalancedTx RefundSuccess . getCardanoTxId <$> submitUnbalancedTx utx diff --git a/plutus-use-cases/src/Plutus/Contracts/TokenAccount.hs b/plutus-use-cases/src/Plutus/Contracts/TokenAccount.hs index 3af77842bb..c4bed56d9b 100644 --- a/plutus-use-cases/src/Plutus/Contracts/TokenAccount.hs +++ b/plutus-use-cases/src/Plutus/Contracts/TokenAccount.hs @@ -162,7 +162,7 @@ pay account vl = do <> " into " <> show account mapError (review _TAContractError) $ - mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) (payTx vl) + mkTxConstraints (Constraints.typedValidatorLookups inst) (payTx vl) >>= adjustUnbalancedTx >>= submitUnbalancedTx -- | Create a transaction that spends all outputs belonging to the 'Account'. @@ -184,7 +184,7 @@ redeemTx account pk = mapError (review _TAContractError) $ do <> show totalVal let constraints = Constraints.collectFromTheScript utxos () <> Constraints.mustPayToPubKey pk (accountToken account) - lookups = Constraints.plutusV1TypedValidatorLookups inst + lookups = Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs utxos -- TODO. Replace 'PubKey' with a more general 'Address' type of output? -- Or perhaps add a field 'requiredTokens' to 'LedgerTxConstraints' and let the diff --git a/plutus-use-cases/src/Plutus/Contracts/Tutorial/Escrow.hs b/plutus-use-cases/src/Plutus/Contracts/Tutorial/Escrow.hs index b7f160faf1..4fd2015075 100644 --- a/plutus-use-cases/src/Plutus/Contracts/Tutorial/Escrow.hs +++ b/plutus-use-cases/src/Plutus/Contracts/Tutorial/Escrow.hs @@ -239,7 +239,7 @@ pay :: pay inst _escrow vl = do pk <- ownFirstPaymentPubKeyHash let tx = Constraints.mustPayToTheScript pk vl - utx <- mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) tx >>= adjustUnbalancedTx + utx <- mkTxConstraints (Constraints.typedValidatorLookups inst) tx >>= adjustUnbalancedTx getCardanoTxId <$> submitUnbalancedTx utx newtype RedeemSuccess = RedeemSuccess TxId @@ -275,7 +275,7 @@ redeem inst escrow = mapError (review _EscrowError) $ do if foldMap (view Tx.ciTxOutValue) unspentOutputs `lt` targetTotal escrow then throwing _RedeemFailed NotEnoughFundsAtAddress else do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx >>= adjustUnbalancedTx RedeemSuccess . getCardanoTxId <$> submitUnbalancedTx utx @@ -306,7 +306,7 @@ refund inst _escrow = do tx' = Constraints.collectFromTheScriptFilter flt unspentOutputs Refund if Constraints.modifiesUtxoSet tx' then do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx' >>= adjustUnbalancedTx RefundSuccess . getCardanoTxId <$> submitUnbalancedTx utx diff --git a/plutus-use-cases/src/Plutus/Contracts/Tutorial/EscrowStrict.hs b/plutus-use-cases/src/Plutus/Contracts/Tutorial/EscrowStrict.hs index 4b43c2a694..7072ed671f 100644 --- a/plutus-use-cases/src/Plutus/Contracts/Tutorial/EscrowStrict.hs +++ b/plutus-use-cases/src/Plutus/Contracts/Tutorial/EscrowStrict.hs @@ -246,7 +246,7 @@ pay :: pay inst _escrow vl = do pk <- ownFirstPaymentPubKeyHash let tx = Constraints.mustPayToTheScript pk vl - utx <- mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) tx >>= adjustUnbalancedTx + utx <- mkTxConstraints (Constraints.typedValidatorLookups inst) tx >>= adjustUnbalancedTx getCardanoTxId <$> submitUnbalancedTx utx newtype RedeemSuccess = RedeemSuccess TxId @@ -282,7 +282,7 @@ redeem inst escrow = mapError (review _EscrowError) $ do if foldMap (view Tx.ciTxOutValue) unspentOutputs `lt` targetTotal escrow then throwing _RedeemFailed NotEnoughFundsAtAddress else do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx >>= adjustUnbalancedTx RedeemSuccess . getCardanoTxId <$> submitUnbalancedTx utx @@ -313,7 +313,7 @@ refund inst _escrow = do tx' = Constraints.collectFromTheScriptFilter flt unspentOutputs Refund if Constraints.modifiesUtxoSet tx' then do - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx' >>= adjustUnbalancedTx RefundSuccess . getCardanoTxId <$> submitUnbalancedTx utx diff --git a/plutus-use-cases/src/Plutus/Contracts/Uniswap/OffChain.hs b/plutus-use-cases/src/Plutus/Contracts/Uniswap/OffChain.hs index 1c9d11e4ea..cced71a1c3 100644 --- a/plutus-use-cases/src/Plutus/Contracts/Uniswap/OffChain.hs +++ b/plutus-use-cases/src/Plutus/Contracts/Uniswap/OffChain.hs @@ -208,7 +208,7 @@ start = do inst = uniswapInstance us tx = mustPayToTheScript (Factory []) $ unitValue c - mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst) tx + mkTxConstraints (Constraints.typedValidatorLookups inst) tx >>= adjustUnbalancedTx >>= submitTxConfirmed void $ waitNSlots 1 @@ -232,7 +232,7 @@ create us CreateParams{..} = do usVal = unitValue $ usCoin us lpVal = valueOf cpCoinA cpAmountA <> valueOf cpCoinB cpAmountB <> unitValue psC - lookups = Constraints.plutusV1TypedValidatorLookups usInst <> + lookups = Constraints.typedValidatorLookups usInst <> Constraints.plutusV1OtherScript usScript <> Constraints.plutusV1MintingPolicy (liquidityPolicy us) <> Constraints.unspentOutputs (Map.singleton oref o) @@ -262,7 +262,7 @@ close us CloseParams{..} = do lVal = valueOf lC liquidity redeemer = Redeemer $ PlutusTx.toBuiltinData Close - lookups = Constraints.plutusV1TypedValidatorLookups usInst <> + lookups = Constraints.typedValidatorLookups usInst <> Constraints.plutusV1OtherScript usScript <> Constraints.plutusV1MintingPolicy (liquidityPolicy us) <> Constraints.ownPaymentPubKeyHash pkh <> @@ -298,7 +298,7 @@ remove us RemoveParams{..} = do val = psVal <> valueOf rpCoinA outA <> valueOf rpCoinB outB redeemer = Redeemer $ PlutusTx.toBuiltinData Remove - lookups = Constraints.plutusV1TypedValidatorLookups usInst <> + lookups = Constraints.typedValidatorLookups usInst <> Constraints.plutusV1OtherScript usScript <> Constraints.plutusV1MintingPolicy (liquidityPolicy us) <> Constraints.unspentOutputs (Map.singleton oref o) <> @@ -338,7 +338,7 @@ add us AddParams{..} = do val = psVal <> valueOf apCoinA newA <> valueOf apCoinB newB redeemer = Redeemer $ PlutusTx.toBuiltinData Add - lookups = Constraints.plutusV1TypedValidatorLookups usInst <> + lookups = Constraints.typedValidatorLookups usInst <> Constraints.plutusV1OtherScript usScript <> Constraints.plutusV1MintingPolicy (liquidityPolicy us) <> Constraints.ownPaymentPubKeyHash pkh <> @@ -379,7 +379,7 @@ swap us SwapParams{..} = do let inst = uniswapInstance us val = valueOf spCoinA newA <> valueOf spCoinB newB <> unitValue (poolStateCoin us) - lookups = Constraints.plutusV1TypedValidatorLookups inst <> + lookups = Constraints.typedValidatorLookups inst <> Constraints.plutusV1OtherScript (Scripts.validatorScript inst) <> Constraints.unspentOutputs (Map.singleton oref o) <> Constraints.ownPaymentPubKeyHash pkh diff --git a/plutus-use-cases/src/Plutus/Contracts/Vesting.hs b/plutus-use-cases/src/Plutus/Contracts/Vesting.hs index 3ce8bdae0d..65643637b1 100644 --- a/plutus-use-cases/src/Plutus/Contracts/Vesting.hs +++ b/plutus-use-cases/src/Plutus/Contracts/Vesting.hs @@ -187,7 +187,7 @@ vestFundsC -> Contract w s e () vestFundsC vesting = mapError (review _VestingError) $ do let tx = payIntoContract (totalAmount vesting) - mkTxConstraints (Constraints.plutusV1TypedValidatorLookups $ typedValidator vesting) tx + mkTxConstraints (Constraints.typedValidatorLookups $ typedValidator vesting) tx >>= adjustUnbalancedTx >>= void . submitUnbalancedTx data Liveness = Alive | Dead @@ -224,7 +224,7 @@ retrieveFundsC vesting payment = mapError (review _VestingError) $ do -- we don't need to add a pubkey output for 'vestingOwner' here -- because this will be done by the wallet when it balances the -- transaction. - mkTxConstraints (Constraints.plutusV1TypedValidatorLookups inst + mkTxConstraints (Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs) tx >>= adjustUnbalancedTx >>= void . submitUnbalancedTx return liveness diff --git a/plutus-use-cases/test/Spec/Escrow/Endpoints.hs b/plutus-use-cases/test/Spec/Escrow/Endpoints.hs index b5c17e1109..28f6389a99 100644 --- a/plutus-use-cases/test/Spec/Escrow/Endpoints.hs +++ b/plutus-use-cases/test/Spec/Escrow/Endpoints.hs @@ -51,7 +51,7 @@ badRefund inst pk = do let flt _ ciTxOut = fst (Tx._ciTxOutScriptDatum ciTxOut) == Ledger.datumHash (Datum (PlutusTx.toBuiltinData pk)) tx' = Constraints.collectFromTheScriptFilter flt unspentOutputs Refund <> Constraints.mustValidateIn (from (current - 1)) - utx <- mkTxConstraints ( Constraints.plutusV1TypedValidatorLookups inst + utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst <> Constraints.unspentOutputs unspentOutputs ) tx' handleError (\err -> logError $ "Caught error: " ++ unpack err) $ diff --git a/plutus-use-cases/test/Spec/Uniswap/Endpoints.hs b/plutus-use-cases/test/Spec/Uniswap/Endpoints.hs index 772af39ee2..f3a1686a56 100644 --- a/plutus-use-cases/test/Spec/Uniswap/Endpoints.hs +++ b/plutus-use-cases/test/Spec/Uniswap/Endpoints.hs @@ -67,7 +67,7 @@ badRemove us BadRemoveParams{..} = do val = psVal <> valueOf brpCoinA brpOutA <> valueOf brpCoinB brpOutB redeemer = Redeemer $ PlutusTx.toBuiltinData Remove - lookups = Constraints.plutusV1TypedValidatorLookups usInst <> + lookups = Constraints.typedValidatorLookups usInst <> Constraints.plutusV1OtherScript usScript <> Constraints.plutusV1MintingPolicy (liquidityPolicy us) <> Constraints.unspentOutputs (Map.singleton oref o) <> From 070a5f990a99febf3b51373bf05e2a8321719ca3 Mon Sep 17 00:00:00 2001 From: Sjoerd Visscher Date: Wed, 17 Aug 2022 12:26:25 +0200 Subject: [PATCH 3/3] Enable and fix reference output tests --- plutus-contract/test/Spec/Contract/TxConstraints.hs | 8 +++----- plutus-ledger/src/Ledger/Index.hs | 7 ++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/plutus-contract/test/Spec/Contract/TxConstraints.hs b/plutus-contract/test/Spec/Contract/TxConstraints.hs index 900826af9f..b5e78a5441 100644 --- a/plutus-contract/test/Spec/Contract/TxConstraints.hs +++ b/plutus-contract/test/Spec/Contract/TxConstraints.hs @@ -62,10 +62,8 @@ tag :: ContractInstanceTag tag = "instance 1" tests :: TestTree -tests = testGroup "contract tx constraints" [] +tests = testGroup "contract tx constraints" -disabledTests :: TestTree -disabledTests = testGroup "contract tx constraints" -- Testing package plutus-ledger-constraints [ checkPredicate "mustReferenceOutput returns False on-chain when used for unlocking funds in a PlutusV1 script" @@ -86,10 +84,10 @@ disabledTests = testGroup "contract tx constraints" -- Testing package plutus-tx-constraints - , checkPredicate "Tx.Constraints.mustReferenceOutput returns False on-chain when used for unlocking funds in a PlutusV1 script" + , checkPredicate "Tx.Constraints.mustReferenceOutput fails when trying to unlock funds in a PlutusV1 script" (walletFundsChange w1 (Ada.adaValueOf (-5)) .&&. valueAtAddress mustReferenceOutputV1ValidatorAddress (== Ada.adaValueOf 5) - .&&. assertValidatedTransactionCountOfTotal 1 2 + .&&. assertValidatedTransactionCountOfTotal 1 1 ) $ do void $ activateContract w1 mustReferenceOutputTxV1ConTest tag void $ Trace.waitNSlots 2 diff --git a/plutus-ledger/src/Ledger/Index.hs b/plutus-ledger/src/Ledger/Index.hs index 73394eadfc..66692b282e 100644 --- a/plutus-ledger/src/Ledger/Index.hs +++ b/plutus-ledger/src/Ledger/Index.hs @@ -455,10 +455,11 @@ mkPV1TxInInfo TxIn{txInRef} = do mkPV2TxInfo :: ValidationMonad m => Tx -> m PV2.TxInfo mkPV2TxInfo tx = do slotCfg <- pSlotConfig . vctxParams <$> ask - txins <- traverse mkPV2TxInInfo $ view inputs tx + txIns <- traverse mkPV2TxInInfo $ view inputs tx + txRefIns <- traverse mkPV2TxInInfo $ view referenceInputs tx let ptx = PV2.TxInfo - { PV2.txInfoInputs = txins - , PV2.txInfoReferenceInputs = [] + { PV2.txInfoInputs = txIns + , PV2.txInfoReferenceInputs = txRefIns , PV2.txInfoOutputs = txOutV1ToTxOutV2 <$> txOutputs tx -- See note [Mint and Fee fields must have ada symbol] , PV2.txInfoMint = Ada.lovelaceValueOf 0 <> txMint tx