Plutus.V1.Ledger.Contexts
1. 📜 Overview
2. 🔧 LANGUAGE EXTENSIONS AND IMPORTS
3. 🗄️ Data Types
3.1 📥 TxInInfo
3.2 🎯 ScriptPurpose
3.3 📋 TxInfo
3.4 📜 ScriptContext
4. ⚙️ Utility Functions
4.1 🔍 findOwnInput
4.2 🔎 findDatum
4.3 🔖 findDatumHash
4.4 🔗 findTxInByTxOutRef
4.5 🔄 findContinuingOutputs
4.6 📥 getContinuingOutputs
4.7 ✔️ txSignedBy
4.8 🔑 pubKeyOutput
4.9 🧩 ownHashes
4.10 🏷️ ownHash
4.11 🔃 fromSymbol
4.12 🖋️ scriptOutputsAt
4.13 💰 valueLockedBy
4.14 💳 pubKeyOutputsAt
4.15 🏦 valuePaidTo
4.16 🔥 valueSpent
4.17 🏭 valueProduced
4.18 💱 ownCurrencySymbol
4.19 ⚔️ spendsOutput
5. 🤝 Typeclass Instances
6. 📚 Glossary
1 📜 Overview
The Plutus.V1.Ledger.Contexts module defines the core types and helper functions for accessing transaction context within Plutus validators:
Data types:
TxInInfo,ScriptPurpose,TxInfo,ScriptContextcapture inputs, outputs, and purpose of pending transactions.Utility functions to inspect and query components of the pending transaction (
findDatum,valuePaidTo, etc.).Typeclass instances for equality and pretty-printing, all marked
INLINABLEor with compiler pragmas for on-chain use.
This tutorial explains the language pragmas/imports, each data type (fields, constructors), utility function (inputs, processing, outputs), and concludes with a glossary.
2 🔧 LANGUAGE EXTENSIONS AND IMPORTS
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-simplifiable-class-constraints #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
module Plutus.V1.Ledger.Contexts where
import GHC.Generics (Generic)
import PlutusTx
import PlutusTx.Prelude
import Prettyprinter (Pretty(..), nest, viaShow, vsep, (<+>))
import Plutus.V1.Ledger.Address (Address(..), toPubKeyHash)
import Plutus.V1.Ledger.Credential (Credential(..), StakingCredential)
import Plutus.V1.Ledger.Crypto (PubKeyHash(..))
import Plutus.V1.Ledger.DCert (DCert(..))
import Plutus.V1.Ledger.Scripts
import Plutus.V1.Ledger.Time (POSIXTimeRange)
import Plutus.V1.Ledger.Tx (TxId(..), TxOut(..), TxOutRef(..))
import Plutus.V1.Ledger.Value (CurrencySymbol(..), Value)
import Prelude qualified as HaskellMany
LANGUAGEpragmas to support generics, deriving strategies, and on-chain requirements.OPTIONS_GHC to control inlining and interface pragmas.
Imports from
PlutusTxand other ledger modules (Address,Crypto,Tx,Value).
3 🗄️ Data Types
3.1 📥 TxInInfo
data TxInInfo = TxInInfo
{ txInInfoOutRef :: TxOutRef
, txInInfoResolved :: TxOut
}
deriving stock (Generic, Haskell.Show, Haskell.Eq)Fields:
txInInfoOutRef: reference to the spent outputtxInInfoResolved: fullTxOutbeing consumed
Instances:
Custom
EqandPrettydefined below.
3.2 🎯 ScriptPurpose
data ScriptPurpose
= Minting CurrencySymbol
| Spending TxOutRef
| Rewarding StakingCredential
| Certifying DCert
deriving stock (Generic, Haskell.Show, Haskell.Eq)Constructors: indicates why the script is running:
Minting: forging tokensSpending: consuming an outputRewarding: distributing staking rewardsCertifying: handling certificates
Custom
Eqensures on-chain pattern-matching equality.
3.3 📋 TxInfo
data TxInfo = TxInfo
{ txInfoInputs :: [TxInInfo]
, txInfoOutputs :: [TxOut]
, txInfoFee :: Value
, txInfoMint :: Value
, txInfoDCert :: [DCert]
, txInfoWdrl :: [(StakingCredential, Integer)]
, txInfoValidRange :: POSIXTimeRange
, txInfoSignatories :: [PubKeyHash]
, txInfoData :: [(DatumHash, Datum)]
, txInfoId :: TxId
}
deriving stock (Generic, Haskell.Show, Haskell.Eq)Aggregates all relevant parts of the pending transaction as seen by a validator.
Custom
EqandPrettydefined below.
3.4 📜 ScriptContext
data ScriptContext = ScriptContext
{ scriptContextTxInfo :: TxInfo
, scriptContextPurpose :: ScriptPurpose
}
deriving stock (Generic, Haskell.Eq, Haskell.Show)Wraps the full context passed to a validator: transaction info + its purpose.
Custom
EqandPrettybelow.
4 ⚙️ Utility Functions
Each function is {-# INLINABLE #-} for on-chain compilation.
4.1 🔍 findOwnInput
findOwnInput :: ScriptContext -> Maybe TxInInfoFinds the
TxInInfocorresponding to the output being validated whenpurpose = Spending.Example: returns
Justwhen matching input, elseNothing.
4.2 🔎 findDatum
findDatum :: DatumHash -> TxInfo -> Maybe DatumSearches
txInfoDataby hash, returns the associatedDatum.
4.3 🔖 findDatumHash
findDatumHash :: Datum -> TxInfo -> Maybe DatumHashInverse of
findDatum: finds the hash for a givenDatumin the transaction.
4.4 🔗 findTxInByTxOutRef
findTxInByTxOutRef :: TxOutRef -> TxInfo -> Maybe TxInInfoLookup an input by its
TxOutRefkey intxInfoInputs.
4.5 🔄 findContinuingOutputs
findContinuingOutputs :: ScriptContext -> [Integer]Returns indices of outputs paying back to the same script address being validated.
Error (
traceError) if no matching input found.
4.6 📥 getContinuingOutputs
getContinuingOutputs :: ScriptContext -> [TxOut]Filters
txInfoOutputsto those with the same script address as the input.
4.7 ✔️ txSignedBy
txSignedBy :: TxInfo -> PubKeyHash -> BoolChecks if a given public key signed the transaction.
4.8 🔑 pubKeyOutput
pubKeyOutput :: TxOut -> Maybe PubKeyHashExtracts the
PubKeyHashlocking aTxOut, if present.
4.9 🧩 ownHashes
ownHashes :: ScriptContext -> (ValidatorHash, DatumHash)Gets both the validator script hash and datum hash for the input being validated.
4.10 🏷️ ownHash
ownHash :: ScriptContext -> ValidatorHashExtracts only the validator hash via
fst . ownHashes.
4.11 🔃 fromSymbol
fromSymbol :: CurrencySymbol -> ValidatorHashConverts a
CurrencySymbolinto aValidatorHash.
4.12 🖋️ scriptOutputsAt
scriptOutputsAt :: ValidatorHash -> TxInfo -> [(DatumHash, Value)]Collects outputs paying to a given script address.
4.13 💰 valueLockedBy
valueLockedBy :: TxInfo -> ValidatorHash -> ValueSums the
Valueof all script outputs for a given hash.
4.14 💳 pubKeyOutputsAt
pubKeyOutputsAt :: PubKeyHash -> TxInfo -> [Value]Lists values sent to a public key address.
4.15 🏦 valuePaidTo
valuePaidTo :: TxInfo -> PubKeyHash -> ValueAggregates all outputs to a public key into one total
Value.
4.16 🔥 valueSpent
valueSpent :: TxInfo -> ValueSums the
Valueof inputs consumed by the transaction.
4.17 🏭 valueProduced
valueProduced :: TxInfo -> ValueSums the
Valueof outputs created by the transaction.
4.18 💱 ownCurrencySymbol
ownCurrencySymbol :: ScriptContext -> CurrencySymbolReturns the minting currency symbol when
purpose = Minting.
4.19 ⚔️ spendsOutput
spendsOutput :: TxInfo -> TxId -> Integer -> BoolChecks if the transaction spends a specific output by ID and index.
5 🤝 Typeclass Instances
instance Eq TxInInfo where
TxInInfo r1 o1 == TxInInfo r2 o2 = r1 == r2 && o1 == o2instance Pretty TxInInfo where
pretty TxInInfo{txInInfoOutRef, txInInfoResolved} = pretty txInInfoOutRef <+> "->" <+> pretty txInInfoResolvedinstance Eq ScriptPurpose where
{-# INLINABLE (==) #-}
... -- structural equality for each constructorinstance Pretty ScriptPurpose where
pretty = viaShowinstance Eq TxInfo where
{-# INLINABLE (==) #-}
... -- equality across all fieldsinstance Pretty TxInfo where
pretty TxInfo{...} = vsep [ ... ]instance Eq ScriptContext where
{-# INLINABLE (==) #-}
ScriptContext i p == ScriptContext i' p' = i == i' && p == p'instance Pretty ScriptContext where
pretty ScriptContext{scriptContextTxInfo, scriptContextPurpose} = vsep ["Purpose:" <+> pretty scriptContextPurpose, nest 2 $ vsep ["TxInfo:", pretty scriptContextTxInfo]]makeLift ''TxInInfo
makeIsDataIndexed ''TxInInfo [('TxInInfo,0)]
makeLift ''TxInfo
makeIsDataIndexed ''TxInfo [('TxInfo,0)]
makeLift ''ScriptPurpose
makeIsDataIndexed ''ScriptPurpose [('Minting,0),('Spending,1),('Rewarding,2),('Certifying,3)]
makeLift ''ScriptContext
makeIsDataIndexed ''ScriptContext [('ScriptContext,0)]6 📚 Glossary
TxInInfo: Input of a pending transaction (reference + resolved output).
ScriptPurpose: Why a validator is run (minting, spending, rewarding, certifying).
TxInfo: Summary of transaction inputs, outputs, fees, minting, certificates, and more.
ScriptContext: Full context passed to a validator:
TxInfo+ScriptPurpose.TxOutRef: Pointer to a transaction output (TxId + index).
Datum / DatumHash: Off-chain data attached to outputs.
CurrencySymbol: Identifier for a minted token.
INLINABLE / INLINE: Pragmas to control on-chain compilation and specialization.
makeLift / makeIsDataIndexed: Template Haskell for on-chain data serialization.
Last updated