Plutus.V2.Ledger.Tx

  • 1. 📜 Overview

  • 2. 🔧 LANGUAGE PRAGMAS & IMPORTS

  • 3. 🗄️ Core Types

    • 3.1 🆔 TxId

    • 3.2 🏷️ ScriptTag & RedeemerPtr

    • 3.3 💾 Redeemers

    • 3.4 📮 TxOutRef

    • 3.5 🎯 OutputDatum

    • 3.6 📤 TxOut

    • 3.7 🎟️ TxInType & TxIn

  • 4. ⚙️ Utility Functions

    • 4.1 🔗 inRef / inType

    • 4.2 📜 inScripts

    • 4.3 👤 pubKeyTxIn / scriptTxIn

    • 4.4 🔍 pubKeyTxIns / scriptTxIns

    • 4.5 🏷️ txOutPubKey / txOutValidatorHash

    • 4.6 🏠 outAddress / outValue / outDatum / outReferenceScript

    • 4.7 ✅ isPubKeyOut / isPayToScriptOut

    • 4.8 📦 pubKeyHashTxOut

  • 5. 🤝 Typeclass Instances

  • 6. 🏭 On-Chain Derivations

  • 7. 📚 Glossary


1 📜 Overview

Plutus.V2.Ledger.Tx extends transaction types for Plutus V2:

  • Adds inline datums and reference scripts on outputs.

  • Reuses V1 types and introduces OutputDatum.

  • Provides constructors, lenses, and helpers for inputs and outputs.


2 🔧 LANGUAGE PRAGMAS & IMPORTS

{-# LANGUAGE DeriveAnyClass      #-}
{-# LANGUAGE DeriveGeneric       #-}
{-# LANGUAGE DerivingVia         #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE FlexibleInstances   #-}
{-# LANGUAGE LambdaCase         #-}
{-# LANGUAGE NamedFieldPuns     #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE RecordWildCards    #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# OPTIONS_GHC -fno-specialise     #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}

module Plutus.V2.Ledger.Tx where

import Control.DeepSeq (NFData)
import Control.Lens
import Data.Maybe (isJust)
import GHC.Generics (Generic)
import Prettyprinter
import PlutusTx qualified
import PlutusTx.Bool qualified as PlutusTx
import PlutusTx.Eq qualified as PlutusTx
import Plutus.V1.Ledger.Address
import Plutus.V1.Ledger.Crypto
import Plutus.V1.Ledger.Scripts
import Plutus.V1.Ledger.Tx hiding (TxOut, isPayToScriptOut, isPubKeyOut, outAddress, outValue, pubKeyHashTxOut, txOutDatum, txOutPubKey)
import Plutus.V1.Ledger.Value

3 🗄️ Core Types

3.1 🆔 TxId

Re-exported from V1

newtype TxId = TxId { getTxId :: BuiltinByteString }
  • Transaction hash; derives Eq, Ord, NFData, on-chain Eq, Pretty, and IsString.

3.2 🏷️ ScriptTag & RedeemerPtr

Re-exported from V1

data ScriptTag = Spend | Mint | Cert | Reward
data RedeemerPtr = RedeemerPtr ScriptTag Integer
type Redeemers = Map RedeemerPtr Redeemer
  • Tags and pointers for redeemer maps.

3.3 💾 Redeemers

type Redeemers = Map RedeemerPtr Redeemer
  • Maps each script purpose and index to its Redeemer.

3.4 📮 TxOutRef

Re-exported from V1

data TxOutRef = TxOutRef { txOutRefId :: TxId, txOutRefIdx :: Integer }
  • Pointer to a specific output of a prior transaction.

3.5 🎯 OutputDatum

data OutputDatum
  = NoOutputDatum
  | OutputDatumHash DatumHash
  | OutputDatum Datum
  • Represents no datum, a hashed datum, or an inline datum.

  • Implements on-chain Eq and Pretty.

3.6 📤 TxOut

data TxOut = TxOut
  { txOutAddress         :: Address
  , txOutValue           :: Value
  , txOutDatum           :: OutputDatum
  , txOutReferenceScript :: Maybe ScriptHash
  }
  • Extends V1 TxOut with inline datum and optional reference script hash.

3.7 🎟️ TxInType & TxIn

Re-exported from V1

data TxInType = ConsumeScriptAddress Validator Redeemer Datum | ...
data TxIn = TxIn { txInRef :: TxOutRef, txInType :: Maybe TxInType }
  • Unchanged input types.


4 ⚙️ Utility Functions

4.1 🔗 inRef / inType

inRef  :: Lens' TxIn TxOutRef
inType :: Lens' TxIn (Maybe TxInType)
  • Lenses to focus on input reference and type.

4.2 📜 inScripts

inScripts :: TxIn -> Maybe (Validator, Redeemer, Datum)
  • Extracts script witnesses if ConsumeScriptAddress.

4.3 👤 pubKeyTxIn / scriptTxIn

pubKeyTxIn  :: TxOutRef -> TxIn
scriptTxIn  :: TxOutRef -> Validator -> Redeemer -> Datum -> TxIn
  • Constructors for standard pubkey and script inputs.

4.4 🔍 pubKeyTxIns / scriptTxIns

pubKeyTxIns  :: Fold (Set.Set TxIn) TxIn
scriptTxIns  :: Fold (Set.Set TxIn) TxIn
  • Optics to filter sets of inputs by type.

4.5 🏷️ txOutPubKey / txOutValidatorHash

txOutPubKey      :: TxOut -> Maybe PubKeyHash
txOutValidatorHash :: TxOut -> Maybe ValidatorHash
  • Extracts optional key/script credentials from txOutAddress.

4.6 🏠 outAddress / outValue / outDatum / outReferenceScript

outAddress          :: Lens' TxOut Address
outValue            :: Lens' TxOut Value
outDatum            :: Lens' TxOut OutputDatum
outReferenceScript  :: Lens' TxOut (Maybe ScriptHash)
  • Lenses for modifying output fields.

4.7 ✅ isPubKeyOut / isPayToScriptOut

isPubKeyOut     :: TxOut -> Bool
isPayToScriptOut :: TxOut -> Bool
  • Checks if an output locks to a pubkey or a script.

4.8 📦 pubKeyHashTxOut

pubKeyHashTxOut :: Value -> PubKeyHash -> TxOut
  • Creates a pubkey-locked output with no datum or reference script.


5 🤝 Typeclass Instances

  • Eq OutputDatum: INLINABLE on-chain equality.

  • Pretty OutputDatum: human-readable datum forms.

  • Eq TxOut: compares all fields including inline datum and reference script.


6 🏭 On-Chain Derivations

PlutusTx.makeIsDataIndexed ''OutputDatum
  [ ('NoOutputDatum,0), ('OutputDatumHash,1), ('OutputDatum,2) ]
PlutusTx.makeLift ''OutputDatum
PlutusTx.makeIsDataIndexed ''TxOut [('TxOut,0)]
PlutusTx.makeLift ''TxOut
  • Provides IsData and Lift for serialization of new V2 types.


7 📚 Glossary

  • OutputDatum: datum state on outputs (none, hash-only, or inline full data).

  • ReferenceScript: optional on-chain script attached to an output for reuse.

  • Inline Datum: stores the actual datum in the UTXO rather than referencing by hash.

  • Lens / Fold: optics from Control.Lens for field access and filtering.

  • RedeemerPtr / Redeemers: pointers and map for redeemer data by script purpose.

  • ConsumeScriptAddress: input type that includes validator, redeemer, and datum.

Last updated