Plutus : Plutus.V1.Ledger Essentials
📌 Table of Contents
📁 What Is
Plutus.V1.Ledger
?🧱 Core Modules Breakdown
📬 Working with Addresses
🔐 Working with Signatures & Keys
💰 Working with Values
🧾 Working with Transactions
🧪 Script Contexts & Purpose
🛠 Utilities (Time, Interval, etc.)
🧠 Glossary of Terms
🧪 Validator Script Examples
🔧 Typed Validator Wrappers
🧪 Emulator Trace Integration
1️⃣ 📁 What Is Plutus.V1.Ledger
?
Plutus.V1.Ledger
?Plutus.V1.Ledger
is a collection of types and utilities from the Plutus smart contract platform used to build secure, type-safe smart contracts for Cardano. It includes definitions for:
Addresses
Transactions
Values
Script contexts
Signatures and more
2️⃣ 🧱 Core Modules Breakdown
Address
Work with Cardano addresses
Value
Represent and manipulate tokens
Tx
, TxInfo
Transaction and transaction context
Scripts
, ScriptContext
For writing validators
Contexts
Access to signatures, redeemers, datums
Interval
, Slot
, Time
Handling time and validity ranges
3️⃣ 📬 Working with Addresses
import Plutus.V1.Ledger.Address
Address
is either a public key hash or a script hash.Use
scriptHashAddress
to create an address from a validator script hash.
scriptAddr :: ValidatorHash -> Address
scriptAddr = scriptHashAddress
4️⃣ 🔐 Working with Signatures & Keys
import Plutus.V1.Ledger.Crypto
PubKeyHash
: Identifies wallets.txSignedBy :: TxInfo -> PubKeyHash -> Bool
checks if a transaction is signed by a given key.
5️⃣ 💰 Working with Values
import Plutus.V1.Ledger.Value
Value
is a map ofCurrencySymbol -> TokenName -> Amount
.Use
singleton
to construct a token bundle:
ada :: Value
ada = lovelaceValueOf 5000000
Combine values using
+
,-
, orvalueOf
.
6️⃣ 🧾 Working with Transactions
import Plutus.V1.Ledger.Tx
TxInfo
gives all information about the transaction.Key fields:
txInfoInputs
,txInfoOutputs
txInfoSignatories
txInfoValidRange
Use it in validators:
{-# INLINABLE mkValidator #-}
mkValidator :: () -> () -> ScriptContext -> Bool
mkValidator _ _ ctx = txSignedBy info somePkh
where info = scriptContextTxInfo ctx
7️⃣ 🧪 Script Contexts & Purpose
import Plutus.V1.Ledger.Contexts
ScriptContext
: Execution context of a Plutus script.ScriptPurpose
: Why the script is running (e.g., Spending, Minting).
data ScriptPurpose = Spending TxOutRef | Minting CurrencySymbol | ...
Access redeemer with
findOwnInput
,findDatum
, etc.
8️⃣ 🛠 Utilities (Time, Interval, etc.)
import Plutus.V1.Ledger.Interval
import Plutus.V1.Ledger.Time
Use
POSIXTime
andPOSIXTimeRange
for timelocks.Example:
validBeforeDeadline :: POSIXTime -> ScriptContext -> Bool
validBeforeDeadline dl ctx = to dl `contains` txInfoValidRange info
where info = scriptContextTxInfo ctx
9️⃣ 🧠 Glossary of Terms
Address
Script or wallet hash for locking funds
Value
Multi-asset bundle of tokens
TxInfo
Transaction information (inputs, signers, outputs)
ScriptContext
Runtime context passed to validators
PubKeyHash
Hash of wallet public key
CurrencySymbol
Identifier for a token type
TokenName
Token label (e.g., "MyCoin")
POSIXTime
UTC time representation
Validator
Script that determines if a transaction is allowed
🔟 🧪 Validator Script Examples
🔐 Signature Check Validator
{-# INLINABLE mkSigValidator #-}
mkSigValidator :: PubKeyHash -> () -> () -> ScriptContext -> Bool
mkSigValidator pkh _ _ ctx = txSignedBy (scriptContextTxInfo ctx) pkh
⏰ Timelock Validator
{-# INLINABLE mkTimeValidator #-}
mkTimeValidator :: POSIXTime -> () -> () -> ScriptContext -> Bool
mkTimeValidator deadline _ _ ctx = from deadline `contains` txInfoValidRange (scriptContextTxInfo ctx)
💸 Minimum ADA Sent to Address
{-# INLINABLE mkPaymentValidator #-}
mkPaymentValidator :: Address -> () -> () -> ScriptContext -> Bool
mkPaymentValidator addr _ _ ctx =
any (\o -> txOutAddress o == addr && lovelaceValueOf (txOutValue o) >= 1000000) $ txInfoOutputs (scriptContextTxInfo ctx)
1️⃣1️⃣ 🔧 Typed Validator Wrappers
data Basic
instance Scripts.ValidatorTypes Basic where
type instance DatumType Basic = ()
type instance RedeemerType Basic = ()
typedSigValidator :: PubKeyHash -> Scripts.TypedValidator Basic
typedSigValidator pkh = Scripts.mkTypedValidator @Basic
($$(PlutusTx.compile [|| mkSigValidator ||]) `PlutusTx.applyCode` PlutusTx.liftCode pkh)
$$(PlutusTx.compile [|| Scripts.wrapValidator ||])
1️⃣2️⃣ 🧪 Emulator Trace Integration
emulatorTraceExample :: EmulatorTrace ()
emulatorTraceExample = do
let pkh = mockWalletPaymentPubKeyHash $ knownWallet 1
val = Scripts.validatorScript $ typedSigValidator pkh
addr = scriptAddress val
h1 <- activateContractWallet (knownWallet 1) endpoints
callEndpoint @"lock" h1 (addr, 5000000)
void $ Emulator.waitNSlots 1
Use this with endpoints for lock
/spend
via submitTxConstraints
.
✅ Summary
The Plutus.V1.Ledger
module is your toolkit for interacting with the Cardano ledger when writing smart contracts. It provides powerful primitives to:
Read transaction data
Check authorization
Work with tokens and addresses
Enforce timelocks and intervals
Last updated