Rate Conversion

Rate conversion functions of Azoth SDK for working with time periods

AzothPay stores subscription rates as per-second values (raw), but in applications, you'll typically want to display these as more human-readable periods like per month or per year.

Rate Periods

The SDK exports a RatePeriod enum to represent different time periods:

export enum RatePeriod {
  SECOND = 'second',
  HOUR = 'hour',
  DAY = 'day',
  WEEK = 'week',
  MONTH = 'month',
  YEAR = 'year'
}

These period values correspond to the following conversion factors (in seconds):

Period
Seconds

SECOND

1

HOUR

3600

DAY

86400

WEEK

604800

MONTH

2628000

YEAR

31536000

convertRatePerSecond()

Converts an amount for a specific period (e.g., 10 USDT per month) to a per-second rate.

Parameters:

  • amount: The rate amount as a string

  • period: The time period for the rate (e.g., RatePeriod.MONTH)

Returns: The equivalent per-second rate as a number.

Example:

convertRateToPeriod()

Converts a per-second rate to a rate for a specified period.

Parameters:

  • ratePerSecond: The per-second rate as a number

  • period: The target time period (e.g., RatePeriod.MONTH)

Returns: The rate for the target period as a number.

Example:

Practical Example

Last updated