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
π§© 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
β οΈ Deprecated Functions
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
π These can be combined into a TxConstraints set .
3. π³ Must-Pay Constraints for Specific Types of Addresses
πΉ Public Key Constraints
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
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
Possible errors:
7. π Combining Multiple Typed Scripts in One Transaction
SomeLookupsAndConstraints lets you merge multiple constraint sets.
mkSomeTx β builds a multi-script transaction .
mkTxWithParams β same as mkTx with parameters.
π Enables cross-contract workflows .
8. π¦ Lookups
ScriptLookups provide context:
Scripts β otherScript, plutusV2OtherScript
Keys β paymentPubKeyHash
Lookups link abstract constraints with actual ledger data.
9. β οΈ Deprecated Functions
Old functions (to avoid):
mustPayToTheScript β use mustPayToTheScriptWithDatumHash
mustPayToAddressWithDatum β use mustPayToAddressWithDatumHash
mustValidateIn β use mustValidateInTimeRange
β
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 5 months ago