Plutus: Ledger.Tx.Constraints
Got it β Iβll enhance the tutorial with icons so itβs more visually engaging and easier to skim. Iβll use standard emoji-based icons since they render well in Markdown.
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:
mustPayToTheScriptWithDatumHashmustSpendAtLeastmustProduceAtLeastmustIncludeDatumInTx
π These can be combined into a TxConstraints set.
3. π³ Must-Pay Constraints for Specific Types of Addresses
πΉ Public Key Constraints
mustPayToPubKeymustPayToPubKeyWithDatumHashmustPayToPubKeyWithInlineDatum
πΉ Script Constraints
mustPayToOtherScriptWithDatumHashmustPayToOtherScriptWithInlineDatum
πΉ Reference Script Constraints
mustPayToAddressWithReferenceScriptmustPayToAddressWithReferenceValidator
These ensure correct value locking at addresses.
4. π§ Off-Chain Only Constraints
Used only while constructing transactions:
spendUtxosFromPlutusV1ScriptspendUtxosFromTheScriptspendUtxosFromPlutusV2Script
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
πΉ Errors
Possible errors:
TxOutRefNotFoundDatumNotFoundDeclaredInputMismatch
7. π Combining Multiple Typed Scripts in One Transaction
SomeLookupsAndConstraints lets you merge multiple constraint sets.
mkSomeTxβ builds a multi-script transaction.mkTxWithParamsβ same asmkTxwith parameters.
π Enables cross-contract workflows.
8. π¦ Lookups
ScriptLookups provide context:
UTxOs β
unspentOutputsScripts β
otherScript,plutusV2OtherScriptDatums β
otherDataKeys β
paymentPubKeyHash
Lookups link abstract constraints with actual ledger data.
9. β οΈ Deprecated Functions
Old functions (to avoid):
mustPayToTheScriptβ usemustPayToTheScriptWithDatumHashmustPayToAddressWithDatumβ usemustPayToAddressWithDatumHashmustValidateInβ 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