Skip to main content

Design Patterns

This section covers common design patterns and data structures for building efficient and secure Cardano smart contracts, all with Aiken implementations and code examples.

Design Patterns Library

The patterns below come from the Anastasia Labs aiken-design-patterns library (v1.5.0). This is a ready-to-use Aiken library that provides production-grade implementations of common on-chain patterns, so developers can import and use them directly without the overhead of reimplementing the base logic themselves.

PatternDescription
Stake ValidatorDelegate computations to staking scripts using the "withdraw zero trick" for optimized validation
UTxO IndexersEfficient one-to-one and one-to-many mappings between inputs and outputs with O(1) lookups
Transaction Level Minting PolicyCouple spend and mint endpoints for single-execution validation logic
Validity Range NormalizationStandardize validity range handling to eliminate redundancies
Merkelized ValidatorDelegate logic to external withdrawal scripts to stay within size limits
Parameter ValidationVerify script instances are derived from specific parameterized scripts
Linked ListOn-chain linked list for storing arbitrarily large collections across UTxOs

Data Structures

The data structures below are standalone Aiken implementations from separate repositories. They are not part of the aiken-design-patterns library but serve as reference implementations that demonstrate how to use these structures on-chain.

Data StructureDescription
Merkle TreeMerkle tree for efficient data verification and proof of membership
TrieDistributed trie for scalable on-chain key-value storage across UTxOs