Module M005
Output Validation & State Machines
DirectEd x CATS Hackathon
Aiken Development Workshop Series
Duration: 2 hours
Format: 1 hour lecture + 1 hour exercises
Output Validation & State Machines
DirectEd x CATS Hackathon
Aiken Development Workshop Series
Duration: 2 hours
Format: 1 hour lecture + 1 hour exercises
In M004, you mastered input validation. Now we focus on OUTPUTS and learn the most powerful pattern: STATE MACHINES!
Outputs are the NEW UTxOs created by a transaction.
Just like inputs, filter outputs to find those going to your validator.
For state machines, we use inline datums in outputs.
A state machine is a system that transitions between defined states based on actions.
State 1
count = 5
Increment
State 2
count = 6
In Cardano's eUTxO model, state transitions happen through the input-output pattern.
INPUT UTxO
datum: State1
value: 10 ADA
Current State
VALIDATOR
Checks:
✓ Valid transition?
✓ Rules followed?
OUTPUT UTxO
datum: State2
value: 10 ADA
New State
State is stored in the datum of a UTxO.
Redeemers define the possible actions that trigger state transitions.
Validate that state changes correctly based on the action.
Most state machines follow a three-phase lifecycle.
1. INITIALIZE
Create first state
Lock funds, set initial values
2. UPDATE
Transition states
Multiple times, changing state
3. CLOSE
Finalize & distribute
Unlock funds, end lifecycle
The core pattern for state machines:
Some fields should NEVER change during state transitions.
Track value changes alongside state changes.
Test all state transitions and edge cases.
Each action with correct state changes
Wrong state changes should fail
Unchanging fields modified should fail
Build your own state machines! 🔄
Extract input/output datums, validate count increments by 1
Implement VoteYes/VoteNo with time constraints
Track accumulated amount, validate value changes match state
Task manager: Created → Assigned → Completed states
Check: expect InlineDatum(data) = output.datum then cast to type
Use trace to debug: trace @"Current", trace current_state
Use single_script_output(self.inputs, self.outputs, input)
Always validate: new_state.owner == current_state.owner
You can now:
✅ Validate transaction outputs
✅ Extract and validate output datums
✅ Understand state machine patterns
✅ Implement state transitions
✅ Compare input and output states
✅ Build Initialize-Update-Close patterns
✅ Create stateful DApps with validators
Module M005 Complete
You can now build stateful validators with state machines!
Master the input-output pattern 🔄
See you in M006! 🚀