19
19
module Ledger.Constraints.OffChain (
20
20
-- * Lookups
21
21
ScriptLookups (.. )
22
- , plutusV1TypedValidatorLookups
22
+ , typedValidatorLookups
23
23
, generalise
24
24
, unspentOutputs
25
25
, plutusV1MintingPolicy
@@ -104,7 +104,6 @@ import Ledger.Tx qualified as Tx
104
104
import Ledger.Tx.CardanoAPI qualified as C
105
105
import Ledger.Typed.Scripts (Any , ConnectionError (UnknownRef ), TypedValidator ,
106
106
ValidatorTypes (DatumType , RedeemerType ))
107
- import Ledger.Typed.Scripts qualified as Scripts
108
107
import Ledger.Typed.Scripts qualified as Typed
109
108
import Ledger.Validation (evaluateMinLovelaceOutput , fromPlutusTxOutUnsafe )
110
109
import Plutus.Script.Utils.Scripts qualified as P
@@ -120,29 +119,29 @@ import PlutusTx.Numeric qualified as N
120
119
121
120
data ScriptLookups a =
122
121
ScriptLookups
123
- { slMPS :: Map MintingPolicyHash MintingPolicy
122
+ { slMPS :: Map MintingPolicyHash MintingPolicy
124
123
-- ^ Minting policies that the script interacts with
125
- , slTxOutputs :: Map TxOutRef ChainIndexTxOut
124
+ , slTxOutputs :: Map TxOutRef ChainIndexTxOut
126
125
-- ^ Unspent outputs that the script may want to spend
127
- , slOtherScripts :: Map ValidatorHash (Validator , Language )
126
+ , slOtherScripts :: Map ValidatorHash (Validator , Language )
128
127
-- ^ Validators of scripts other than "our script"
129
- , slOtherData :: Map DatumHash Datum
128
+ , slOtherData :: Map DatumHash Datum
130
129
-- ^ Datums that we might need
131
- , slPaymentPubKeyHashes :: Set PaymentPubKeyHash
130
+ , slPaymentPubKeyHashes :: Set PaymentPubKeyHash
132
131
-- ^ Public keys that we might need
133
- , slTypedPlutusV1Validator :: Maybe (TypedValidator a )
132
+ , slTypedValidator :: Maybe (TypedValidator a )
134
133
-- ^ The script instance with the typed validator hash & actual compiled program
135
- , slOwnPaymentPubKeyHash :: Maybe PaymentPubKeyHash
134
+ , slOwnPaymentPubKeyHash :: Maybe PaymentPubKeyHash
136
135
-- ^ The contract's payment public key hash, used for depositing tokens etc.
137
- , slOwnStakePubKeyHash :: Maybe StakePubKeyHash
136
+ , slOwnStakePubKeyHash :: Maybe StakePubKeyHash
138
137
-- ^ The contract's stake public key hash (optional)
139
138
} deriving stock (Show , Generic )
140
139
deriving anyclass (ToJSON , FromJSON )
141
140
142
141
generalise :: ScriptLookups a -> ScriptLookups Any
143
142
generalise sl =
144
- let validator = fmap Scripts . generalise (slTypedPlutusV1Validator sl)
145
- in sl{slTypedPlutusV1Validator = validator}
143
+ let validator = fmap Typed . generalise (slTypedValidator sl)
144
+ in sl{slTypedValidator = validator}
146
145
147
146
instance Semigroup (ScriptLookups a ) where
148
147
l <> r =
@@ -153,7 +152,7 @@ instance Semigroup (ScriptLookups a) where
153
152
, slOtherData = slOtherData l <> slOtherData r
154
153
, slPaymentPubKeyHashes = slPaymentPubKeyHashes l <> slPaymentPubKeyHashes r
155
154
-- 'First' to match the semigroup instance of Map (left-biased)
156
- , slTypedPlutusV1Validator = fmap getFirst $ (First <$> slTypedPlutusV1Validator l) <> (First <$> slTypedPlutusV1Validator r)
155
+ , slTypedValidator = fmap getFirst $ (First <$> slTypedValidator l) <> (First <$> slTypedValidator r)
157
156
, slOwnPaymentPubKeyHash =
158
157
fmap getFirst $ (First <$> slOwnPaymentPubKeyHash l)
159
158
<> (First <$> slOwnPaymentPubKeyHash r)
@@ -173,14 +172,14 @@ instance Monoid (ScriptLookups a) where
173
172
-- If called multiple times, only the first typed validator is kept:
174
173
--
175
174
-- @
176
- -- plutusV1TypedValidatorLookups tv1 <> plutusV1TypedValidatorLookups tv2 <> ...
177
- -- == plutusV1TypedValidatorLookups tv1
175
+ -- typedValidatorLookups tv1 <> typedValidatorLookups tv2 <> ...
176
+ -- == typedValidatorLookups tv1
178
177
-- @
179
- plutusV1TypedValidatorLookups :: TypedValidator a -> ScriptLookups a
180
- plutusV1TypedValidatorLookups inst =
178
+ typedValidatorLookups :: TypedValidator a -> ScriptLookups a
179
+ typedValidatorLookups inst =
181
180
mempty
182
- { slMPS = Map. singleton (Scripts . forwardingMintingPolicyHash inst) (Scripts . forwardingMintingPolicy inst)
183
- , slTypedPlutusV1Validator = Just inst
181
+ { slMPS = Map. singleton (Typed . forwardingMintingPolicyHash inst) (Typed . forwardingMintingPolicy inst)
182
+ , slTypedValidator = Just inst
184
183
}
185
184
186
185
-- | A script lookups value that uses the map of unspent outputs to resolve
@@ -518,8 +517,8 @@ addOwnInput
518
517
=> ScriptInputConstraint (RedeemerType a )
519
518
-> m ()
520
519
addOwnInput ScriptInputConstraint {icRedeemer, icTxOutRef} = do
521
- ScriptLookups {slTxOutputs, slTypedPlutusV1Validator } <- ask
522
- inst <- maybe (throwError TypedValidatorMissing ) pure slTypedPlutusV1Validator
520
+ ScriptLookups {slTxOutputs, slTypedValidator } <- ask
521
+ inst <- maybe (throwError TypedValidatorMissing ) pure slTypedValidator
523
522
typedOutRef <-
524
523
either (throwError . TypeCheckFailed ) pure
525
524
$ runExcept @ Typed. ConnectionError
@@ -529,8 +528,7 @@ addOwnInput ScriptInputConstraint{icRedeemer, icTxOutRef} = do
529
528
datum <- ciTxOut ^? Tx. ciTxOutScriptDatum . _2 . _Just
530
529
pure (Tx. toTxOut ciTxOut, datum)
531
530
Typed. typeScriptTxOutRef inst icTxOutRef txOut datum
532
- -- TODO Needs to work with PlutusV1 AND PlutusV2.
533
- let txIn = Scripts. makeTypedScriptTxIn PlutusV1 inst icRedeemer typedOutRef
531
+ let txIn = Typed. makeTypedScriptTxIn inst icRedeemer typedOutRef
534
532
vl = Tx. txOutValue $ Typed. tyTxOutTxOut $ Typed. tyTxOutRefOut typedOutRef
535
533
unbalancedTx . tx . Tx. inputs %= (Typed. tyTxInTxIn txIn : )
536
534
valueSpentInputs <>= provided vl
@@ -546,8 +544,8 @@ addOwnOutput
546
544
=> ScriptOutputConstraint (DatumType a )
547
545
-> m ()
548
546
addOwnOutput ScriptOutputConstraint {ocDatum, ocValue} = do
549
- ScriptLookups {slTypedPlutusV1Validator } <- ask
550
- inst <- maybe (throwError TypedValidatorMissing ) pure slTypedPlutusV1Validator
547
+ ScriptLookups {slTypedValidator } <- ask
548
+ inst <- maybe (throwError TypedValidatorMissing ) pure slTypedValidator
551
549
let txOut = Typed. makeTypedScriptTxOut inst ocDatum ocValue
552
550
dsV = Datum (toBuiltinData ocDatum)
553
551
unbalancedTx . tx . Tx. outputs %= (Typed. tyTxOutTxOut txOut : )
@@ -752,13 +750,12 @@ resolveScriptTxOut
752
750
=> ChainIndexTxOut -> m (Maybe ((ValidatorHash , Validator , Language ), (DatumHash , Datum ), Value ))
753
751
resolveScriptTxOut
754
752
Tx. ScriptChainIndexTxOut
755
- { Tx. _ciTxOutValidator = (vh, v )
753
+ { Tx. _ciTxOutValidator = (vh, _ )
756
754
, Tx. _ciTxOutScriptDatum = (dh, d)
757
755
, Tx. _ciTxOutValue
758
756
} = do
759
- -- first check in the 'ChainIndexTx' for the validator, then
760
- -- look for it in the 'slOtherScripts map.
761
- (validator, pv) <- maybe (lookupValidator vh) (pure . (, PlutusV1 )) v
757
+ -- Look for the validator in the 'slOtherScripts map so we also get the language.
758
+ (validator, pv) <- lookupValidator vh
762
759
763
760
-- first check in the 'ChainIndexTxOut' for the datum, then
764
761
-- look for it in the 'slOtherData' map.
0 commit comments