sub2
  • sub2 protocol
  • Concepts
    • Subscription
    • Fees
    • Fee auction
    • Sponsored subscriptions
    • Processors
  • Deployments
  • SDK
    • Query subscriptions
    • Listen for incoming subscriptions
    • Listen for canceled subscriptions
    • Listen for incoming payments
    • Sponsor subscriptions
    • Check if user is a payed subscriber
    • Cancel subscription
    • Process payments
    • Query active subscriptions to recipient
    • Query active subscriptions from sender
  • Popup integration
  • Become a payment processor
  • Technical Reference
  • DAO
Powered by GitBook
On this page
  1. SDK

Query subscriptions

PreviousSDKNextListen for incoming subscriptions

Last updated 11 months ago

Here is an example of how to query subscriptions given their respective index using sub2-sdk. As mentioned , subscriptions are stored in one public array called subscriptions and are uniquely identified by their index in the array. Since the array is public, we can query all the information of a subscription using their index. Assuming sub2-sdk is installed and initialised like shown , we can query the subscription as follows:

import { Subscription } from 'sub2-sdk';

const subscriptions: Subscription[] = await sub2SDK.getSubscriptions(indices);

Here, indices has type bigint[].The getSubscription function returns a promise with a Subscription[] array. Each Subscription object contains both the on-chain data and auxiliary information:

interface Subscription {
  id: bigint;
  sender: `0x${string}`,
  recipient: `0x${string}`,
  sponsor: `0x${string}`,
  amount: bigint,
  token: `0x${string}`,
  maxProcessingFee: bigint,
  processingFeeToken: `0x${string}`,
  cooldown: number,
  auctionDuration: number,
  paymentCounter: number,
  status: 'canceled' | 'expired' | 'auction' |  'waiting';
  lastPayment: Date,
  nextPaymentAvailable: Date;
  nextPaymentDue: Date;
}

Now we have obtained all information about the subscriptions!

here
here