Module M003
Smart Contracts with Validation Logic
DirectEd x CATS Hackathon
Aiken Development Workshop Series
Duration: 2 hours
Format: 1 hour lecture + 1 hour exercises
Smart Contracts with Validation Logic
DirectEd x CATS Hackathon
Aiken Development Workshop Series
Duration: 2 hours
Format: 1 hour lecture + 1 hour exercises
In M001 and M002, you learned to write validators and test them with mock transactions. Now it's time to make your validators actually DO something!
Validation logic is the set of rules that determines whether a transaction can spend a UTxO or mint tokens.
Your Funds
Locked at Script
Validator
(Checks Rules)
Rules Pass
Transaction Succeeds
Rules Fail
Transaction Rejected
False or raises an error, the entire transaction fails and nothing changes on the blockchain.
Every spending validator receives four parameters:
A redeemer is the argument passed when spending from a validator. Think of it as providing the "reason" or "proof" to unlock.
A datum is custom data stored with a UTxO at a script address. It represents the "state" or "configuration" of the locked funds.
expect Some(datum) = datum to extract, then destructure fields with pattern matching.
Parameters are values set at compile time, making validators reusable with different configurations.
Think of it like a house blueprint:
Same Blueprint
(Validator Code)
Different Parameters
(Owner, Admin, etc.)
Different Addresses
(Unique Instances)
Check WHO signed the transaction using the extra_signatories field.
extra_signatories is a list of public key hashes that signed the transaction.
Time on Cardano is represented as an interval, not a single timestamp.
Check transaction structure: How many? From where? To where?
Real-world validators combine multiple checks for comprehensive security.
&& to combine checks: ALL conditions must pass for transaction to succeed.
Comprehensive tests ensure your validation logic is correct and secure.
Valid redeemers, correct signatures, proper timing, balanced transactions
Invalid redeemers, wrong signatures, bad timing, unbalanced transactions
Boundary values, empty lists, None datums, maximum/minimum amounts
Every redeemer constructor should have passing and failing tests
Check owner signature before allowing actions
Enforce deadlines or unlock times
Ensure minimum values or amounts
Require multiple signatures
Combine these patterns to create sophisticated smart contracts: Time Lock + Access Control, Multi-Sig + Value Checks, etc.
Time to build! 🛠️
Build redeemer-based validation with password checking
Implement datum + signature validation
Combine signature and time validation
Validate transaction structure and values
Combine ALL validation techniques!
Use expect Some(d) = datum first, then destructure
Add signatories to mock tx: tx_extra_signatories([#"aabbcc"])
Set validity range: set_validity_range(after(1000))
You can now:
✅ Validate redeemers with pattern matching
✅ Extract and validate datum fields
✅ Create parameterized validators
✅ Verify transaction signatures
✅ Implement time-based validation
✅ Validate inputs and outputs
✅ Combine multiple validation techniques
Module M003 Complete
You can now build secure validators with real logic!
Practice combining validation techniques 🛡️
See you in M004! 🚀