-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test for the index lookup function
- Loading branch information
Showing
5 changed files
with
85 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,56 @@ | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# OPTIONS_GHC -fobject-code -fno-ignore-interface-pragmas -fno-omit-interface-pragmas -fplugin-opt PlutusTx.Plugin:target-version=1.0.0 #-} -- 1.1.0.0 will be enabled in conway | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
-- | Scripts used for testing | ||
module Scripts( | ||
v2SpendingScriptSerialised, | ||
v2SpendingScript | ||
v2SpendingScript, | ||
|
||
-- * Matching index scripts | ||
matchingIndexScript, | ||
spendMatchingIndex | ||
) where | ||
|
||
import Cardano.Api (TxIn) | ||
import qualified Cardano.Api.Shelley as C | ||
import Convex.BuildTx (MonadBuildTx) | ||
import qualified Convex.BuildTx as BuildTx | ||
import Convex.Scripts (compiledCodeToScript, | ||
toHashableScriptData) | ||
import PlutusLedgerApi.Common (SerialisedScript) | ||
import PlutusLedgerApi.Test.Examples (alwaysSucceedingNAryFunction) | ||
import PlutusTx (BuiltinData, CompiledCode) | ||
import qualified PlutusTx | ||
import qualified Scripts.MatchingIndex as MatchingIndex | ||
|
||
v2SpendingScript :: C.PlutusScript C.PlutusScriptV2 | ||
v2SpendingScript = C.PlutusScriptSerialised $ alwaysSucceedingNAryFunction 3 | ||
|
||
v2SpendingScriptSerialised :: SerialisedScript | ||
v2SpendingScriptSerialised = alwaysSucceedingNAryFunction 3 | ||
|
||
matchingIndexCompiled :: CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ()) | ||
matchingIndexCompiled = $$(PlutusTx.compile [|| \d r c -> MatchingIndex.validator d r c ||]) | ||
|
||
{-| Script that passes if the input's index (in the list of transaction inputs) | ||
matches the number passed as the redeemer | ||
-} | ||
matchingIndexScript :: C.PlutusScript C.PlutusScriptV2 | ||
matchingIndexScript = compiledCodeToScript matchingIndexCompiled | ||
|
||
{-| Spend an output locked by 'matchingIndexScript', setting | ||
the redeemer to the index of the input in the final transaction | ||
-} | ||
spendMatchingIndex :: MonadBuildTx m => TxIn -> m () | ||
spendMatchingIndex txi = | ||
let witness lkp = | ||
C.ScriptWitness C.ScriptWitnessForSpending | ||
$ C.PlutusScriptWitness | ||
C.PlutusScriptV2InBabbage | ||
C.PlutusScriptV2 | ||
(C.PScript matchingIndexScript) | ||
(C.ScriptDatumForTxIn $ toHashableScriptData ()) | ||
(toHashableScriptData $ fromIntegral @Int @Integer $ BuildTx.findIndex txi lkp) | ||
(C.ExecutionUnits 0 0) | ||
in BuildTx.setScriptsValid >> BuildTx.addInput txi witness |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE PatternSynonyms #-} | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE ViewPatterns #-} | ||
-- A plutus validator that only succeeds if the redeemer is identical to the script's input index | ||
module Scripts.MatchingIndex( | ||
validator | ||
) where | ||
|
||
import PlutusLedgerApi.V2.Contexts (ScriptContext (..), | ||
ScriptPurpose (..), TxInInfo (..), | ||
TxInfo (..)) | ||
import PlutusTx.IsData.Class (UnsafeFromData (unsafeFromBuiltinData)) | ||
import PlutusTx.Prelude (BuiltinData) | ||
import qualified PlutusTx.Prelude as P | ||
|
||
{-# INLINABLE validator #-} | ||
validator :: BuiltinData -> BuiltinData -> BuiltinData -> () | ||
validator _datum (unsafeFromBuiltinData -> idx :: P.Integer) (unsafeFromBuiltinData -> ScriptContext{scriptContextPurpose=Spending txOutRef, scriptContextTxInfo=TxInfo{txInfoInputs}}) = | ||
let isOwnIndex TxInInfo{txInInfoOutRef} = txInInfoOutRef P.== txOutRef | ||
ownIndex = P.findIndex isOwnIndex txInfoInputs | ||
in if ownIndex P.== (P.Just idx) then () else P.traceError "Different indices" | ||
validator _ _ _ = P.error () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters