Plutus.V1.Ledger.Tx

  • 1. 📜 Overview

  • 2. 🔧 LANGUAGE PRAGMAS AND IMPORTS

  • 3. 🗄️ Core Types

    • 3.1 🆔 TxId

    • 3.2 🏷️ ScriptTag & RedeemerPtr

    • 3.3 💾 Redeemers

    • 3.4 📮 TxOutRef

    • 3.5 🎟️ TxInType & TxIn

    • 3.6 📤 TxOut

  • 4. ⚙️ Utility Functions

    • 4.1 🔗 inRef / inType

    • 4.2 📜 inScripts

    • 4.3 👤 pubKeyTxIn / scriptTxIn

    • 4.4 🔍 pubKeyTxIns / scriptTxIns

    • 4.5 🏷️ txOutDatum / txOutPubKey / txOutValidatorHash

    • 4.6 🏠 outAddress / outValue

    • 4.7 ✅ isPubKeyOut / isPayToScriptOut

    • 4.8 📦 pubKeyHashTxOut

  • 5. 🏭 On-Chain Derivations

  • 6. 📚 Glossary


1 📜 Overview

The Plutus.V1.Ledger.Tx module defines core transaction types and utilities:

  • Transaction IDs and pointers (TxId, TxOutRef).

  • Inputs and outputs representations (TxIn, TxOut).

  • Redeemer pointers and script tags.

  • Convenience functions to inspect, filter, and construct TxIn/TxOut values.

All types derive on-chain equality and serialization, and lenses simplify field access.


2 🔧 LANGUAGE PRAGMAS AND IMPORTS

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

module Plutus.V1.Ledger.Tx where

import Prelude qualified as Haskell
import Control.DeepSeq (NFData)
import Control.Lens    (Lens', lens, Folding, folding)
import Data.Map        (Map)
import Data.Maybe      (isJust)
import Data.Set        qualified as Set
import Data.String     (IsString)
import GHC.Generics    (Generic)
import Prettyprinter   (Pretty(pretty), hang, vsep, (<+>), comma)

import PlutusTx          qualified
import PlutusTx.Bool     qualified as PlutusTx
import PlutusTx.Builtins qualified as PlutusTx
import PlutusTx.Eq       qualified as PlutusTx
import PlutusTx.Ord      qualified as PlutusTx

import Plutus.V1.Ledger.Address
import Plutus.V1.Ledger.Bytes
import Plutus.V1.Ledger.Crypto
import Plutus.V1.Ledger.Scripts
import Plutus.V1.Ledger.Value

Key imports include Control.Lens for field lenses, Data.Map/Set for redeemer maps, and core ledger modules.


3 🗄️ Core Types

3.1 🆔 TxId

newtype TxId = TxId { getTxId :: PlutusTx.BuiltinByteString }
  deriving stock   (Eq, Ord, Generic)
  deriving anyclass (NFData)
  deriving newtype (PlutusTx.Eq, PlutusTx.Ord)
  deriving (Show, Pretty, IsString) via LedgerBytes
  • Represents a SHA256 transaction hash.

  • Derives on-chain equality and convenient Pretty and IsString via LedgerBytes.

3.2 🏷️ ScriptTag & RedeemerPtr

data ScriptTag = Spend | Mint | Cert | Reward

data RedeemerPtr = RedeemerPtr ScriptTag Integer
  • ScriptTag classifies script types in a transaction.

  • RedeemerPtr points to the nth redeemer of a given tag.

3.3 💾 Redeemers

type Redeemers = Map RedeemerPtr Redeemer
  • Maps pointers to their associated Redeemer values.

3.4 📮 TxOutRef

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

  • Pretty instance shows as txId!index.

3.5 🎟️ TxInType & TxIn

data TxInType =
    ConsumeScriptAddress Validator Redeemer Datum
  | ConsumePublicKeyAddress
  | ConsumeSimpleScriptAddress

data TxIn = TxIn { txInRef :: TxOutRef, txInType :: Maybe TxInType }
  • TxInType describes spending mode: script vs pubkey.

  • TxIn pairs a reference with optional script data.

3.6 📤 TxOut

data TxOut = TxOut { txOutAddress :: Address, txOutValue :: Value, txOutDatumHash :: Maybe DatumHash }
  • Represents an output locked at an address with a value and optional datum hash.


4 ⚙️ Utility Functions

4.1 🔗 inRef / inType

inRef :: Lens' TxIn TxOutRef
i nType :: Lens' TxIn (Maybe TxInType)
  • Lenses to get or set txInRef and txInType fields.

4.2 📜 inScripts

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

4.3 👤 pubKeyTxIn / scriptTxIn

pubKeyTxIn :: TxOutRef -> TxIn
scriptTxIn :: TxOutRef -> Validator -> Redeemer -> Datum -> TxIn
  • Constructors for standard public key or script inputs.

4.4 🔍 pubKeyTxIns / scriptTxIns

pubKeyTxIns :: Fold (Set.Set TxIn) TxIn
scriptTxIns :: Fold (Set.Set TxIn) TxIn
  • Filters a Set of inputs by type.

4.5 🏷️ txOutDatum / txOutPubKey / txOutValidatorHash

txOutDatum :: TxOut -> Maybe DatumHash
txOutPubKey :: TxOut -> Maybe PubKeyHash
txOutValidatorHash :: TxOut -> Maybe ValidatorHash
  • Extracts optional datum, pubkey, or script hash from TxOutAddress.

4.6 🏠 outAddress / outValue

outAddress :: Lens' TxOut Address
outValue   :: Lens' TxOut Value
  • Lenses for modifying output fields.

4.7 ✅ isPubKeyOut / isPayToScriptOut

isPubKeyOut :: TxOut -> Bool
isPayToScriptOut :: TxOut -> Bool
  • Detects whether an output is locked by a public key or script.

4.8 📦 pubKeyHashTxOut

pubKeyHashTxOut :: Value -> PubKeyHash -> TxOut
  • Builds a standard public-key locked TxOut with no datum.


5 🏭 On-Chain Derivations

PlutusTx.makeLift ''TxId
PlutusTx.makeIsDataIndexed ''TxId [('TxId,0)]
PlutusTx.makeIsDataIndexed ''TxOut [('TxOut,0)]
PlutusTx.makeLift ''TxOut
PlutusTx.makeIsDataIndexed ''TxOutRef [('TxOutRef,0)]
PlutusTx.makeLift ''TxOutRef
  • makeIsDataIndexed and makeLift for on-chain serialization of key types.


6 📚 Glossary

  • TxId: transaction identifier (SHA256).

  • TxOutRef: pointer to a specific output of a transaction.

  • TxInType: describes how an input spends an output (script vs key).

  • RedeemerPtr: index into redeemer scripts by type.

  • DatumHash: optional data attached to outputs.

  • Fold / Lens: optics from Control.Lens for querying and updating.

  • IsDataIndexed / makeLift: Template Haskell for on-chain data serialization and embedding.

Last updated