Plutus: Ledger.Tx.Constraints
Plutus: Ledger.Tx.Constraints
π Table of Contents
π Introduction
π§© Defining Constraints
π³ Must-Pay Constraints for Specific Types of Addresses
π§ Off-Chain Only Constraints
π Queries on Constraints
ποΈ Off-Chain Transaction Generation
π Combining Multiple Typed Scripts in One Transaction
π¦ Lookups
β οΈ Deprecated Functions
π Glossary of Terms
1. π Introduction
The Ledger.Tx.Constraints
module defines transaction constraints (TxConstraints
), which are declarative rules for transactions.
Off-chain (wallet/backend): Used to build transactions (
mkTx
).On-chain (validator): Used to verify that transactions meet rules (
checkScriptContext
).
This helps ensure correctness and reduces boilerplate transaction logic.
2. π§© Defining Constraints
Constraints express conditions like payments, signatures, datums, and validity ranges.
Examples:
mustPayToTheScriptWithDatumHash
mustSpendAtLeast
mustProduceAtLeast
mustIncludeDatumInTx
π These can be combined into a TxConstraints
set.
3. π³ Must-Pay Constraints for Specific Types of Addresses
πΉ Public Key Constraints
mustPayToPubKey
mustPayToPubKeyWithDatumHash
mustPayToPubKeyWithInlineDatum
πΉ Script Constraints
mustPayToOtherScriptWithDatumHash
mustPayToOtherScriptWithInlineDatum
πΉ Reference Script Constraints
mustPayToAddressWithReferenceScript
mustPayToAddressWithReferenceValidator
These ensure correct value locking at addresses.
4. π§ Off-Chain Only Constraints
Used only while constructing transactions:
spendUtxosFromPlutusV1Script
spendUtxosFromTheScript
spendUtxosFromPlutusV2Script
They help collect UTxOs from validators with appropriate redeemers.
5. π Queries on Constraints
Check feasibility of constraints:
isSatisfiable
β is the constraint set achievable?modifiesUtxoSet
β must any satisfying transaction change the UTxO set?
Useful for contract debugging.
6. ποΈ Off-Chain Transaction Generation
πΉ Unbalanced Transactions
Constraints produce an UnbalancedTx
, which must be balanced & signed.
πΉ mkTx Function
mkTx :: Params -> ScriptLookups a
-> TxConstraints (RedeemerType a) (DatumType a)
-> Either MkTxError UnbalancedTx
πΉ Errors
Possible errors:
TxOutRefNotFound
DatumNotFound
DeclaredInputMismatch
7. π Combining Multiple Typed Scripts in One Transaction
SomeLookupsAndConstraints
lets you merge multiple constraint sets.
mkSomeTx
β builds a multi-script transaction.mkTxWithParams
β same asmkTx
with parameters.
π Enables cross-contract workflows.
8. π¦ Lookups
ScriptLookups
provide context:
UTxOs β
unspentOutputs
Scripts β
otherScript
,plutusV2OtherScript
Datums β
otherData
Keys β
paymentPubKeyHash
Lookups link abstract constraints with actual ledger data.
9. β οΈ Deprecated Functions
Old functions (to avoid):
mustPayToTheScript
β usemustPayToTheScriptWithDatumHash
mustPayToAddressWithDatum
β usemustPayToAddressWithDatumHash
mustValidateIn
β usemustValidateInTimeRange
β Always use updated versions.
10. π Glossary of Terms
TxConstraint β A single transaction rule.
TxConstraints β A collection of multiple constraints.
Datum β Data attached to outputs.
Redeemer β Data used when spending outputs.
UTxO β Unspent transaction output.
ScriptLookups β Context for resolving constraints.
UnbalancedTx β Draft transaction before balancing.
Validity Interval β Slot/time range when a tx is valid.
PaymentPubKeyHash β Identifies a wallet.
Minting Policy β Script controlling token mint/burn.
Last updated