Introduction to the Subscription App
Tutorial Part 1: Introduction to the Subscription App
Welcome to the Modsefa Subscription App tutorial! In the first tutorial, we built a simple, single-validator Data Feed application. Now, we're going to take a significant step forward and build a more advanced, multi-validator subscription service.
This application is designed to showcase how Modsefa excels at managing the complexity that arises when multiple smart contracts need to coordinate with each other.
What We'll Build
We will create a complete subscription service on Cardano with the following features:
- Service Providers can define their service and set pricing tiers.
- Customers can subscribe to a service by paying the required amount.
- A Treasury automatically collects all subscription payments.
- Providers can create and manage batches of Coupons that offer percentage-based discounts.
- Customers can redeem a coupon during subscription, which automatically calculates the discounted price and burns the coupon UTxO.
- Providers can securely withdraw funds from the treasury.
Why This is a Great Example
This application moves beyond a simple, single-contract example and forces us to solve problems that are common in real-world dApps:
- Multiple, Interconnected Validators: Our application will be governed by four distinct validator scripts. Three of these (
ServiceAndPricing,Coupon, andTreasury) will be single-instance within our application, meaning there is only one of each for the entire service. The fourth, theCustomerValidator, is multi-instance; a unique instance of this validator will be created for each customer, parameterized by their public key hash. This demonstrates how Modsefa can manage both singleton and collection-based contract patterns within the same application. - Shared State & Parameter Derivation: The Coupon and Treasury validators need to know the address of the main Service validator to function correctly. Instead of passing this manually, we will use Modsefa's
ParameterDerivationsto automatically compute and inject these parameters at compile time. - Complex, Multi-Step Actions: An action like
SubscribeWithCouponSpecis incredibly powerful. It needs to read the pricing tier, read the coupon details, calculate a new price, create a new subscription state, burn the coupon state, and send the correct payment to the treasury—all in a single, atomic transaction. We'll define this entire flow declaratively. - Batch Operations: We will use the
'Map'operation to create and delete entire batches of coupons in a single action, demonstrating how to handle collections of states efficiently. - Aggregated State vs. Instance-Based State: This application uses both state models, which is a key concept to understand:
- Instance-Based States (like
ServiceConfigStateorCouponState) represent a single, discrete piece of information. Each instance is a unique UTxO containing its own specific datum and is identified by a unique token. WeCreate,Update, orDeletethese states as individual units. - Aggregated State (our
TreasuryAdaState) does not represent a single piece of data but rather a collection of value locked by a validator. Its "state" is the total sum of ADA held in all UTxOs at the treasury address. We don'tCreateorUpdateit in the traditional sense; instead, we use special constraints like'MustAddToAggregateState'and'MustWithdrawFromAggregateState'to securely modify the total value controlled by the validator.
- Instance-Based States (like
By the end of this tutorial, you will have a deep understanding of how to use Modsefa to build sophisticated, multi-validator applications that are safe, robust, and easy to maintain.