Skip to main content

Decentralized subscription management and auto-renewal

img alt


Many web services (in Web2) work on a subscription basis. One of the main features of subscription management is subscription auto-renewal. So that after the expiration of the subscription period, the money is automatically debited from the wallet and the subscription is automatically renewed without user intervention.

This dApp example represents a video-hosting service VaraTube that enables watching videos with a valid subscription that can be renewed automatically.

Decentralization aspect

In most blockchain networks, each transaction is invoked by a user or another program and must be completed in a single block (transactions are atomic). In general, a smart contract is able to invoke several functions, but only within a single block. In case of an error, the result of execution of all related functions is rolled back (the State does not change).

According to the Actor Model in Gear Protocol, actors exchange messages. One of the Gear-specific entities is the Waitlist - a database where messages (not transactions) reside.

This makes it possible to implement logic when several logically related messages can be spaced apart in time. As a result, extrinsics that are caused by these messages can be executed in different blocks. It is possible for messages to stay in the Waitlist for a long time. This is achieved through Gas reservation for deferred messages. A program can send a delayed message to itself over as many blocks as it defines, provided there is enough reserved gas to be in the Waitlist.

State consistency between smart contracts is achieved using techniques described here.

How to run

⚒️ Build program

🏗️ Upload program

  1. You can deploy a program using
  2. In the network selector choose Staging Testnet or Development in case if you have running a local node
  3. Upload program varatube.opt.wasm from /target/wasm32-unknown-unknown/release/
  4. Upload metadata file meta.txt
  5. Specify init payload and calculate gas!
  6. Repeat steps #3-5 for uploading Fungible Token contract fungible_token.opt.wasm...

🖥️ Run UI

  1. Install packages as described in frontend/
yarn install
  1. Create .env file, .env.example will let you know what variables are expected.
  2. Configure .env file. Specify network address and program ID like in the example below:
  1. Run app
yarn start

Program logic

img alt

When a user purchases a subscription, access to the service is open, and after the subscription expires, access is closed.

The interface provides an option whether to enable or disable subscription auto-renewal. And if it is enabled, the subscription will be renewed automatically by getting funds from the user's.

There is also an option to cancel the active subscription.

The VaraTube consists of two smart contracts:

  • Gear Fungible Token (GFT-20) contract determines user and service balances required to purchase a subscription and approves Subscription contract to get funds from user's balance.
  • VaraTube Subscription contract manages service's subscription - its availability, expiration, auto renewal.

Source code

The source code of the VaraTube smart contract including its testing as well as JS frontend web application example is available on GitHub. They can be used as is or modified to suit your own scenarios.

For more details about testing smart contracts written on Gear, refer to the Program Testing article.