HbarSuite Docs
  • Welcome to HbarSuite
  • HbarSuite Developer Documentation
    • HbarSuite Smart Engine Applications
      • @hsuite/cross-chain-exchange
      • @hsuite/dao
      • @hsuite/exchange
      • @hsuite/launchpad
      • @hsuite/multisig
      • @hsuite/nft-exchange
    • HSuite Libraries
      • @hsuite/api-key
      • @hsuite/auth-types
      • @hsuite/auth
      • @hsuite/client-types
      • @hsuite/client
      • @hsuite/dkg-types
      • @hsuite/hashgraph-types
      • @hsuite/health
      • @hsuite/helpers
      • @hsuite/ipfs
      • @hsuite/smart-config
      • @hsuite/smart-network-types
      • @hsuite/smart-transaction-types
      • @hsuite/smartnode-sdk
      • @hsuite/snapshots
      • @hsuite/subscriptions-types
      • @hsuite/subscriptions
      • @hsuite/throttler-types
      • @hsuite/throttler
      • @hsuite/users-types
      • @hsuite/users
      • @hsuite/validators-types
  • General Documentation
    • Tokenomics
      • Tokenomics v1
      • Tokenomics V2
    • Smart Apps and Interaction
      • Subscription-Based Model
      • Token-Gate Model
    • The Smart Node Network
      • security-layer
        • Security Layer Integration
        • Setting Up Secure Clusters
        • Generating and Sharing Keys Collaboratively
        • Protecting Secrets with Shamir's Method
        • Managing Cluster Membership
        • Handling Node Expulsion and Replacement
        • Automating Responses to Network Changes & Key Rotation
        • Ensuring Continuous Operation and Recovery
      • Understanding Validators in Our System
        • Type of Validators Explained
    • What is a Smart Node?
  • Restful APIs Documentation
Powered by GitBook
On this page
  • Features
  • Installation
  • Dependencies
  • Core Dependencies
  • HSuite Dependencies
  • Module Structure
  • Usage
  • Basic Module Configuration
  • Advanced Configuration
  • Using the Subscription Service
  • API Routes
  • Development
  • Documentation Generation
  • License
  1. HbarSuite Developer Documentation
  2. HSuite Libraries

@hsuite/subscriptions

A comprehensive NestJS module for managing Web3-based subscriptions, providing token gating, subscription issuance, and activity logging capabilities.

Features

  • Web3 Integration

    • Token-based subscription issuance

    • Token gating access control

    • Blockchain activity logging

    • Smart contract integration

  • Subscription Management

    • Subscription lifecycle handling

    • Access rights validation

    • Billing and payment processing

    • Analytics and reporting

  • Background Processing

    • Redis-based job queue

    • Configurable job retry policies

    • Asynchronous task handling

Installation

npm install @hsuite/subscriptions

Dependencies

Core Dependencies

  • @nestjs/common: ^10.4.2

  • @nestjs/core: ^10.4.2

HSuite Dependencies

  • @hsuite/subscriptions-types: 2.0.0

  • @hsuite/nestjs-swagger: 2.0.0

  • @hsuite/smart-config: 2.0.0

  • @hsuite/hashgraph-types: 2.0.0

  • @hsuite/smart-transaction-types: 2.0.0

  • @hsuite/helpers: 2.0.0

  • @hsuite/auth-types: 2.0.0

  • @hsuite/client: ^2.0.20

Module Structure

src/
├── entities/         # Database entities
├── interfaces/       # TypeScript interfaces
├── models/          # Data models
│   ├── analytics/   # Analytics models
│   ├── payments/    # Payment processing models
│   └── subscription/# Subscription models
├── web3/            # Web3 integration
│   ├── issuer/      # Subscription issuance
│   ├── logger/      # Activity logging
│   └── token-gate/  # Access control
└── index.ts         # Public API

Usage

Basic Module Configuration

import { Module } from '@nestjs/common';
import { SubscriptionsModule } from '@hsuite/subscriptions';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    SubscriptionsModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (config: ConfigService) => ({
        subscription: {
          issuer: {
            enabled: true,
            options: {
              token: {
                id: config.get('TOKEN_ID')
              }
            }
          }
        },
        bull: {
          redis: {
            url: config.get('REDIS_URL')
          }
        }
      }),
      inject: [ConfigService],
      enableIssuer: true,
      enableTokenGate: true
    })
  ]
})
export class AppModule {}

Advanced Configuration

SubscriptionsModule.forRootAsync({
  imports: [ConfigModule, Web3Module],
  useFactory: async (config: ConfigService, web3: Web3Service) => ({
    subscription: {
      issuer: {
        enabled: true,
        options: {
          token: {
            id: await web3.resolveAddress('MyNFT'),
            network: 'ethereum'
          }
        },
        utilities: {
          refreshInterval: 3600
        }
      }
    },
    bull: {
      redis: {
        host: config.get('REDIS_HOST'),
        port: config.get('REDIS_PORT'),
        password: config.get('REDIS_PASSWORD'),
        username: config.get('REDIS_USERNAME'),
        database: config.get('REDIS_DATABASE')
      },
      defaultJobOptions: {
        attempts: 3,
        backoff: { 
          type: 'exponential', 
          delay: 1000 
        }
      }
    }
  }),
  inject: [ConfigService, Web3Service],
  enableIssuer: true,
  enableTokenGate: true
})

Using the Subscription Service

import { Injectable } from '@nestjs/common';
import { SubscriptionsService } from '@hsuite/subscriptions';

@Injectable()
export class MyService {
  constructor(private subscriptionsService: SubscriptionsService) {}
  
  async checkAccess(userId: string): Promise<boolean> {
    return this.subscriptionsService.validateSubscription(userId);
  }
}

API Routes

The module exposes the following route hierarchy:

/subscriptions
└── /web3
    └── /logger

Development

Documentation Generation

Generate documentation using Compodoc:

npm run compodoc

Generate documentation with coverage report:

npm run compodoc:coverage

License

This package is part of the HSuite Enterprise ecosystem.

Built with ❤️ by the HbarSuite Team Copyright © 2024 HbarSuite. All rights reserved.

Previous@hsuite/subscriptions-typesNext@hsuite/throttler-types

Last updated 3 months ago