Plutus.V2.Ledger.Contexts

  • 1. πŸ“œ Overview

  • 2. πŸ”§ LANGUAGE PRAGMAS & IMPORTS

  • 3. πŸ—„οΈ Data Structures

    • 3.1 πŸͺ TxInInfo

    • 3.2 πŸ“„ TxInfo

    • 3.3 πŸ–‹οΈ ScriptContext

    • 3.4 🎯 ScriptPurpose

  • 4. βš™οΈ Core Functions

    • 4.1 πŸ”Ž findOwnInput

    • 4.2 πŸ“‘ findDatum / findDatumHash

    • 4.3 πŸ”— findTxInByTxOutRef

    • 4.4 πŸ”„ findContinuingOutputs / getContinuingOutputs

    • 4.5 βœ… txSignedBy

    • 4.6 πŸ” ownHashes / ownHash

    • 4.7 πŸ” scriptOutputsAt

    • 4.8 πŸ’° valueLockedBy / valuePaidTo

    • 4.9 πŸ’Έ valueSpent / valueProduced

    • 4.10 🏷️ spendsOutput

  • 5. 🀝 Typeclass Instances

  • 6. 🏭 On-Chain Derivations

  • 7. πŸ“š Glossary


1 πŸ“œ Overview

The Plutus.V2.Ledger.Contexts module defines the on-chain view of pending transactions for Plutus V2 scripts. It provides:

  • Data structures: TxInInfo, TxInfo, and ScriptContext capturing inputs, outputs, redeemers, and transaction metadata.

  • ScriptPurpose: tags indicating why a script is run (e.g. spending, minting).

  • Functions: to locate the current input, retrieve datums, compute continuing outputs, and summarize values.

  • Instances: on-chain equality, Pretty, and serialization via makeIsDataIndexed and makeLift.


2 πŸ”§ LANGUAGE PRAGMAS & IMPORTS

  • NoImplicitPrelude: uses PlutusTx.Prelude on-chain.

  • DerivingVia and TemplateHaskell for instances.

  • Imports core V1 modules for reuse of types and functions.


3 πŸ—„οΈ Data Structures

3.1 πŸͺ TxInInfo

  • Usage: Each input pairs a pointer with the resolved TxOut data.

3.2 πŸ“„ TxInfo

  • Enhancements in V2: adds txInfoReferenceInputs and txInfoRedeemers fields.

3.3 πŸ–‹οΈ ScriptContext

  • Encapsulates the entire transaction view plus why the script runs.

3.4 🎯 ScriptPurpose

Imported from V1:

  • Indicates the role (minting, spending, etc.) for lookup of the own input.


4 βš™οΈ Core Functions

4.1 πŸ”Ž findOwnInput

  • Purpose: locates the TxInInfo corresponding to the current spending reference.

  • Logic: matches Spending txOutRef against txInfoInputs.

4.2 πŸ“‘ findDatum / findDatumHash

  • findDatum: looks up a datum by its hash in txInfoData.

  • findDatumHash: reverse-lookup of a datum value to its hash.

4.3 πŸ”— findTxInByTxOutRef

  • Purpose: get the input information given its reference.

4.4 πŸ”„ findContinuingOutputs / getContinuingOutputs

  • findContinuingOutputs: indices of outputs at the same script address.

  • getContinuingOutputs: full TxOut values at that address.

  • Error: calls traceError if no own input found.

4.5 βœ… txSignedBy

  • Checks if a public key is among txInfoSignatories.

4.6 πŸ” ownHashes / ownHash

  • Extracts the validator hash and attached datum for the current input.

  • ownHash: convenience returning only the validator hash.

4.7 πŸ” scriptOutputsAt

  • Filters txInfoOutputs for those paying to a given validator.

4.8 πŸ’° valueLockedBy / valuePaidTo

  • valueLockedBy: sums values at script address.

  • valuePaidTo: sums values to a public key address.

4.9 πŸ’Έ valueSpent / valueProduced

  • valueSpent: total of all inputs’ resolved txOutValue.

  • valueProduced: sum of all output values.

4.10 🏷️ spendsOutput

  • Checks if any input matches a given (TxId, index) pair.


5 🀝 Typeclass Instances

  • Eq TxInInfo, Eq TxInfo, Eq ScriptContext:

    • V2 includes custom INLINABLE (==) on TxInfo to compare all fields, including maps.

  • Pretty instances for human-friendly debugging.


6 🏭 On-Chain Derivations

  • makeLift and makeIsDataIndexed provide on-chain Lift and IsData instances for serialization.


7 πŸ“š Glossary

  • TxInInfo: input record pairing a reference and resolved output.

  • TxInfo: full transaction view available to validators, including redeemers and reference inputs.

  • ScriptContext: wraps TxInfo plus ScriptPurpose for the current script.

  • ScriptPurpose: reason code (e.g. Spending txOutRef, Minting cs).

  • OutputDatum: datum attached to a TxOut (could be inline or by hash).

  • Continuing outputs: new UTXOs at the same script address as the one being spent.

  • traceError: abort execution with an error message when invariants fail.

  • makeLift / makeIsDataIndexed: Template Haskell for on-chain data conversion.

Last updated