Plutus: Ledger.Tx.Constraints


Plutus: Ledger.Tx.Constraints

πŸ“‘ Table of Contents

  1. πŸš€ Introduction

  2. 🧩 Defining Constraints

  3. πŸ’³ Must-Pay Constraints for Specific Types of Addresses

  4. πŸ”§ Off-Chain Only Constraints

  5. πŸ” Queries on Constraints

  6. πŸ—οΈ Off-Chain Transaction Generation

  7. πŸ”— Combining Multiple Typed Scripts in One Transaction

  8. πŸ“¦ Lookups

  9. ⚠️ Deprecated Functions

  10. πŸ“˜ 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 as mkTx 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 β†’ 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