Introducing BlockSubs: A Blockchain-Powered Subscription Platform

Introducing BlockSubs: A Blockchain-Powered Subscription Platform

Subscription services have become extremely popular in recent years across many online platforms and apps. However, managing subscriptions can be challenging, especially when dealing with cryptocurrency payments. In this post, I will walk through my experience building a decentralized subscription management system called BlockSubs using Ethereum smart contracts.

Overview

The core idea behind BlockSubs is to allow users to register and subscribe to different subscription packages or “roles” using ERC20 tokens on Ethereum. The smart contract keeps track of each user’s subscription status and handles the payments and refunds automatically based on certain conditions.

Some key features of the BlockSubs contract include:

  • User registration and identity management

  • Defining subscription roles with different prices

  • Subscribing and unsubscribing from roles

  • Checking subscription status

  • Token payments for subscriptions

  • Partial refunds on cancellation

  • Ownership and access control logic

By leveraging the transparency, security and automation of smart contracts, BlockSubs aims to make subscription management on the blockchain seamless and trustless.

Implementation

I decided to use Solidity and the Truffle framework for implementing the core logic of BlockSubs. The contract is split into multiple files for each feature set:

  • BlockSubs.sol: Main smart contract code containing user struct, events, ownership logic etc.

  • Roles.sol: Subscription roles and pricing logic.

  • Subscriptions.sol: Subscription status management and refund calculations.

  • Token.sol: ERC20 token implementation for payments.

Some notable Solidity concepts and libraries used:

  • Structs to store subscription data for each user

  • Mapping to store user profiles and subscription statuses

  • Events for off-chain notifications

  • Inheritance to take advantage of OpenZeppelin contracts

  • SafeMath library to prevent overflows

To handle ownership and access control, I used the Ownable contract from OpenZeppelin. The contract owner can update subscription prices and carry out admin functions.

The contract uses ERC20 tokens called BlockSubs (symbol: BSB) for subscription payments. I implemented a basic ERC20 token contract with minting and burning functions.

Testing the Contract

I wrote unit tests using the Truffle test framework to validate all the core functions like registration, subscriptions, cancellations, refunds, transfers etc. Here is a snippet from the subscription test:

it("should subscribe and unsubscribe user", async function() {

  // Subscribe user to Gold role
  await blockSubs.addSubscription(GOLD_ROLE, {from: user1});

  // Check active subscription
  let subs = await blockSubs.getUser(user1);
  assert.equal(subs.active, true);

  // Unsubscribe 
  await blockSubs.cancelSubscription({from: user1});

  // Check canceled subscription  
  subs = await blockSubs.getUser(user1);
  assert.equal(subs.active, false);  
});

This ensures that the contract logic runs as expected before deploying to the network.

Deployment and Usage

After testing locally, I deployed the BlockSubs contract on the Ropsten testnet using Truffle’s migration scripts. The contract is also verified on Etherscan for transparency.

For the frontend, I built a simple UI using React that interacts with the contract. Users can register, subscribe to plans, mint tokens and manage their subscriptions.

The source code for the project is available open-source on GitHub. Feel free to experiment with the demo app and audit the contract code.

Closing Thoughts

Building BlockSubs was a great learning experience in designing real-world smart contracts. I gained hands-on practice with concepts like access control, payments, account balances, time-based logic etc.

There is still room for improvement in areas like security, optimization and feature expansion. Overall, I’m satisfied with the end result as a basic decentralized subscription management system powered by Ethereum.

Let me know if you have any other ideas for building subscription smart contracts! I’m open to feedback and collaboration.

Did you find this article valuable?

Support Jay A. Keraliya by becoming a sponsor. Any amount is appreciated!