Skip to main content

Subscription strategy

When implementing a subscription strategy for your dapp, users will need to pay regularly to access its functionality. This approach helps generate a steady income to sustain and grow your dapp.

To set up the subscription strategy, you'll need to go through several technical steps. First, configure the chromia.yml file to enable subscriptions. Then, users must call the transfer function, pay a predefined fee, and transfer a specific amount of tokens to a non-existent account. This non-existent account represents an empty account that needs to be filled with tokens and is a crucial part of the process.

Afterward, users must invoke the claim function to claim the non-existent account. You can set the desired subscription period by configuring the subscription_period_days in the chromia.yml file. When the subscription period ends, the account becomes inactive and cannot be used further. To reactivate the account, users must renew the subscription by sending a specific amount of tokens to the account.

Code example

For practical guidance on building your dapp from the template, it's crucial that you refer to the code examples and corresponding tests. These resources will provide you with a clear understanding of the implementation process.

To make the subscription available, extend your dapp configuration by copying and pasting the following configuration into your chromia.yml file:

lib.ft4.core.accounts.strategies.transfer:
rules:
- sender_blockchain: x"0000000000000000000000000000000000000000000000000000000000000000"
sender: "*"
recipient: "*"
asset:
- name: "MyTestAsset"
min_amount: 100L
timeout_days: 60
strategy:
- "subscription"
lib.ft4.core.accounts.strategies.transfer.subscription:
asset:
- name: "MyTestAsset" # issued by current blockchain # OR id: x"C633343E4AA3213EA92158648F11BA8DFF606C6CAC80614CFA5F45E57367F823"
amount: 10L
subscription_period_days: 30
free_operations:
- some_free_operation

You can divide the configuration into two parts:

Account registration process: This configuration defines the process for registering an account (lib.ft4.core.accounts.strategies.transfer).

PropertyDescription
sender_blockchainThe sender_blockchain property is set to receive tokens from the blockchain with the specified BRID: x"0000000000000000000000000000000000000000000000000000000000000000". To receive tokens from all chains in the Chromia network, use the asterisk sign "*" as the value.
senderThe sender property defines who can send tokens to the dapp. The value "*" allows everyone to send tokens.
recipientThe recipient property defines who can receive tokens in the dapp. The value "*" indicates that everyone can receive tokens.
nameThe name property represents the name of the asset required for account registration.
min_amountThe min_amount property sets a minimum threshold for the transfer to a non-existent account for registration purposes.
timeout_daysThe timeout_days property specifies the period in days during which a user can claim a non-existing account with a balance.
strategyThe strategy property configures the strategy type used for the registration process.

**Subscription rules: **This configuration specifies the rules for subscriptions (lib.ft4.core.accounts.strategies.transfer.subscription).

PropertyDescription
nameThe name property represents the name of the asset required for subscription payment.
amountThe amount property sets a minimum threshold for subscription payment by a user.
subscription_period_daysThe subscription_period_days property identifies the number of days for which the subscription is active for a user.
free_operationsThe free_operations property specifies the operations that accounts can call with an expired subscription.
note

Please refer to the tests for testing the above configuration.

Example: Subscription dapp business logic

The dapp business logic should allow users to upload their notes based on a monthly subscription, and users must renew their subscription if it expires.

  1. Configure the project to use the provided Subscription strategy and build the dapp from the existing code example.
  2. Create the module User and use the register_account operation for account registration, which must be conducted using the Subscription strategy and the FT4 token standard. Refer to the code example showcasing the registration operation.
  3. Create the module Notes to encapsulate entities, operations, and queries.
  4. Create the entity Note with the property note:text.
  5. Create the operation add_note to allow users to upload notes to the dapp, which is only allowed if the user's subscription is active. The operation expects the note:text argument.
  6. Create the query get_notes to retrieve all notes, which can only be accessed if the account has an active subscription.