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
Many
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
Fields:
txInInfoOutRef: reference to the spent outputtxInInfoResolved: fullTxOutbeing consumed
Instances:
Custom
EqandPrettydefined below.
3.2 🎯 ScriptPurpose
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
Aggregates all relevant parts of the pending transaction as seen by a validator.
Custom
EqandPrettydefined below.
3.4 📜 ScriptContext
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
Finds the
TxInInfocorresponding to the output being validated whenpurpose = Spending.Example: returns
Justwhen matching input, elseNothing.
4.2 🔎 findDatum
Searches
txInfoDataby hash, returns the associatedDatum.
4.3 🔖 findDatumHash
Inverse of
findDatum: finds the hash for a givenDatumin the transaction.
4.4 🔗 findTxInByTxOutRef
Lookup an input by its
TxOutRefkey intxInfoInputs.
4.5 🔄 findContinuingOutputs
Returns indices of outputs paying back to the same script address being validated.
Error (
traceError) if no matching input found.
4.6 📥 getContinuingOutputs
Filters
txInfoOutputsto those with the same script address as the input.
4.7 ✔️ txSignedBy
Checks if a given public key signed the transaction.
4.8 🔑 pubKeyOutput
Extracts the
PubKeyHashlocking aTxOut, if present.
4.9 🧩 ownHashes
Gets both the validator script hash and datum hash for the input being validated.
4.10 🏷️ ownHash
Extracts only the validator hash via
fst . ownHashes.
4.11 🔃 fromSymbol
Converts a
CurrencySymbolinto aValidatorHash.
4.12 🖋️ scriptOutputsAt
Collects outputs paying to a given script address.
4.13 💰 valueLockedBy
Sums the
Valueof all script outputs for a given hash.
4.14 💳 pubKeyOutputsAt
Lists values sent to a public key address.
4.15 🏦 valuePaidTo
Aggregates all outputs to a public key into one total
Value.
4.16 🔥 valueSpent
Sums the
Valueof inputs consumed by the transaction.
4.17 🏭 valueProduced
Sums the
Valueof outputs created by the transaction.
4.18 💱 ownCurrencySymbol
Returns the minting currency symbol when
purpose = Minting.
4.19 ⚔️ spendsOutput
Checks if the transaction spends a specific output by ID and index.
5 🤝 Typeclass Instances
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