Repository Walkthrough & Demo
Introduction
This session provides a comprehensive walkthrough of a production-ready smart contract project, demonstrating the complete development lifecycle from architecture design to property-based testing. We'll explore real code, real documentation, and real testing practices used in professional Cardano development.
What you'll learn:
- How to architect a multi-validator smart contract system
- Why design specification documents matter (and save time)
- How to structure Aiken code for maintainability
- Property-based testing with fuzzy tests
- Development best practices from real-world experience
The Payment Subscription Smart Contract
What It Does
The payment subscription smart contract enables blockchain-based subscription services on Cardano. Think Netflix, Spotify, or any recurring payment service, but decentralized and trustless.
Key Features:
- Merchants can create subscription services with custom fees and intervals
- Subscribers pay upfront and funds are locked in the contract
- Merchants withdraw earned fees after each interval period
- Subscribers can extend, cancel, or reclaim unused funds
- Optional penalty system for early cancellations
Real-world use cases:
- SaaS subscriptions
- Content creator memberships
- Insurance premium payments
- Recurring donations
Smart Contract Architecture
The system uses a multi-validator pattern with three specialized validators working together. This separation of concerns makes the code more maintainable and testable.
1. Account Multi-Validator
Purpose: User authentication and identity management
How it works:
- Uses the CIP-68 token standard to mint paired NFTs
- Reference Token lives in the validator (for on-chain tracking)
- User Token lives in the subscriber's wallet (for authentication)
- Both tokens share the same policy ID, creating a verifiable link
Endpoints:
- Mint: Create account, Delete account
- Spend: Update account details, Remove account
Why this matters: The CIP-68 pattern allows the contract to track users on-chain while giving them a wallet token for easy authentication. It's like a blockchain-native login system.
2. Service Multi-Validator
Purpose: Service creation and management by merchants
How it works:
- Merchants mint a service NFT to create a new subscription service
- Service datum stores all the subscription parameters
- Merchants can update fees, intervals, or deactivate services
Service Parameters:
- Service fee: Amount and asset type (ADA or native tokens)
- Interval length: How often payments occur (e.g., 30 days)
- Number of intervals: Tracks payment periods for merchant withdrawals
- Penalty fee: Optional fee for early cancellation
- Active status: Enable/disable the service
Example: A merchant creates "Premium Content Access" with a 10 ADA monthly fee, 30-day intervals, and a 2 ADA early cancellation penalty.
3. Payment Multi-Validator (Treasury)
Purpose: Manages subscription payments using linear vesting
What is linear vesting? It's a time-locked payment system where funds unlock periodically. In this case, the merchant can withdraw earned fees after each interval passes (e.g., every 30 days).
Parameters:
- Service Policy ID (which service this payment is for)
- Subscription Account Policy ID (which subscriber is paying)
Endpoints:
- Mint: Initiate subscription, Terminate subscription
- Spend: Extend subscription, Merchant withdraw, Unsubscribe, Subscriber withdraw
Payment flow example:
- Subscriber pays 120 ADA for 12 months (10 ADA/month)
- After 30 days, merchant can withdraw 10 ADA
- After 60 days, merchant can withdraw another 10 ADA
- Subscriber can extend by adding more funds
- Subscriber can unsubscribe and reclaim unused funds (minus penalties)
Design Specification Documents
Why They're Essential
"After writing the design spec, I felt like I had already built it even though I had not written a single line of code." - Harun
Design specs are the blueprint for your smart contract. They force you to think through:
- All possible user interactions
- Edge cases and failure scenarios
- Validation logic for each endpoint
- How validators interact with each other
Time saved: Writing a design spec might take a few hours, but it saves days of refactoring and debugging later.
What Goes in a Design Spec
1. System Actors
- Who can interact with the contract?
- How do they gain permissions? (e.g., by holding specific NFTs)
2. Validator Specifications For each validator, document:
- Parameters (what data is baked into the validator)
- Minting purposes (what can be minted and when)
- Spending purposes (what can be spent and when)
- Datum structures (what data is stored on-chain)
- Redeemer types (what actions users can take)
3. Validation Logic For each redeemer, list all the checks:
Example: Initiate Subscription
✓ Reference input must provide valid service datum