Subscription Methods

Subscription methods of Azoth SDK for managing subscriptions to creators, platforms, or services.

subscribe()

Creates a new subscription.

async subscribe(
  author: string, 
  amount: number | bigint,
  period: RatePeriod = RatePeriod.MONTH,
  projectId: number
): Promise<ethers.TransactionResponse>

Parameters:

  • author: The address of the creator or platform to subscribe to

  • amount: The amount of tokens for the subscription period

  • period (optional): Subscription period (default: RatePeriod.MONTH)

  • projectId: The project ID associated with the subscription

Returns:

  • An ethers.js TransactionResponse object

Example:

const creatorAddress = '0x...';
const tx = await azoth.subscribe(creatorAddress, 10, RatePeriod.MONTH, 0);
await tx.wait();

subscribeBySig()

Creates a subscription transaction that can be signed off-chain and executed by anyone.

async subscribeBySig(
  author: string, 
  amount: number | bigint,
  period: RatePeriod = RatePeriod.MONTH,
  projectId: number,
  deadline: number
): Promise<ethers.TransactionResponse>

Parameters:

  • author: The address of the creator or platform

  • amount: The amount of tokens for the subscription period

  • period (optional): Subscription period (default: RatePeriod.MONTH)

  • projectId: The project ID associated with the subscription

  • deadline: Timestamp after which the transaction cannot be executed

Returns:

  • An ethers.js TransactionResponse object

Example:

const creatorAddress = '0x...';
const deadline = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
const tx = await azoth.subscribeBySig(creatorAddress, 10, RatePeriod.MONTH, 0, deadline);
await tx.wait();

unsubscribe()

Cancel subscription.

async unsubscribe(author: string): Promise<ethers.TransactionResponse>

Parameters:

  • author: The address of the creator or platform to unsubscribe from

Returns:

  • An ethers.js TransactionResponse object

Example:

const creatorAddress = '0x...';
const tx = await azoth.unsubscribe(creatorAddress);
await tx.wait();

unsubscribeBySig()

Creates an unsubscribe transaction that can be signed off-chain and executed by anyone.

async unsubscribeBySig(author: string, deadline: number): Promise<ethers.TransactionResponse>

Parameters:

  • author: The address of the creator or platform

  • deadline: Timestamp after which the transaction cannot be executed

Returns:

  • An ethers.js TransactionResponse object

Example:

const creatorAddress = '0x...';
const deadline = Math.floor(Date.now() / 1000) + 3600; // 1 hour from now
const tx = await azoth.unsubscribeBySig(creatorAddress, deadline);
await tx.wait();

getSubscriptions()

Gets all subscriptions for an account.

async getSubscriptions(account?: string): Promise<Subscription[]>

Parameters:

  • account (optional): The address to get subscriptions for. If not provided, uses the connected signer’s address.

Returns:

  • Array of Subscription objects, each containing:

    • recipient: The address receiving the subscription

    • incomeRate: Rate at which the recipient is receiving tokens (raw format)

    • outgoingRate: Rate at which the subscriber is paying tokens (raw format)

    • projectId: The project ID associated with the subscription

Note: incomeRate and outgoingRate are per-second raw blockchain values. UseutiUtilitiesto convert to human-readable values (e.g., monthly).

// Get raw subscriptions
const subscriptions = await azoth.getSubscriptions();

// Format subscription rates to human-readable monthly values
const formattedSubscriptions = subscriptions.map(sub => ({
  recipient: sub.recipient,
  incomeRate: convertRateToPeriod(formatOutput(sub.incomeRate, 18), RatePeriod.MONTH),
  outgoingRate: convertRateToPeriod(formatOutput(sub.outgoingRate, 18), RatePeriod.MONTH),
  projectId: sub.projectId
}));

console.log(formattedSubscriptions);

Last updated