Skip to content

Commit

Permalink
chore: tweaking time based calcs
Browse files Browse the repository at this point in the history
  • Loading branch information
aorumbayev committed Oct 9, 2023
1 parent 570291c commit 704cde6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/clients/SubtopiaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
calculateProductSubscriptionBoxCreateMbr,
optInAsset,
asyncWithTimeout,
durationToMonths,
} from "../utils";
import { getAssetByID } from "../utils";
import {
Expand Down Expand Up @@ -609,7 +610,7 @@ export class SubtopiaClient {

const lockerAddress = getApplicationAddress(managerLockerID);

let subscriptionPrice = this.price;
let subscriptionPrice = this.price * durationToMonths(duration);
for (const discount of state.discounts) {
if (discount.duration === duration.valueOf()) {
if (discount.discountType === DiscountType.PERCENTAGE) {
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
// Copyright (C) 2023 Altynbek Orumbayev
// =============================================================================

import { normalizePrice, optInAsset, optOutAsset } from "./utils";
import {
normalizePrice,
optInAsset,
optOutAsset,
durationToMonths,
} from "./utils";
import { SubtopiaClient, SubtopiaRegistryClient } from "./clients";

export {
Expand All @@ -12,6 +17,7 @@ export {
normalizePrice,
optInAsset,
optOutAsset,
durationToMonths,
};

export * from "./interfaces";
Expand Down
18 changes: 17 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
LOCKER_GLOBAL_NUM_BYTE_SLICES,
DEFAULT_TXN_SIGN_TIMEOUT_SECONDS,
} from "../constants";
import { LockerType, PriceNormalizationType } from "../enums";
import { Duration, LockerType, PriceNormalizationType } from "../enums";
import { APP_PAGE_MAX_SIZE } from "@algorandfoundation/algokit-utils/types/app";
import { TransactionSignerAccount } from "@algorandfoundation/algokit-utils/types/account";
import AlgodClient from "algosdk/dist/types/client/v2/algod/algod";
Expand Down Expand Up @@ -357,3 +357,19 @@ export async function asyncWithTimeout<T, A extends any[]>(
});
});
}

export function durationToMonths(duration: Duration | undefined): number {
if (duration === Duration.UNLIMITED) {
return 1;
} else if (duration === Duration.MONTH) {
return 1;
} else if (duration === Duration.QUARTER) {
return 3;
} else if (duration === Duration.SEMI_ANNUAL) {
return 6;
} else if (duration === Duration.ANNUAL) {
return 12;
} else {
throw new Error("Invalid expiration type");
}
}
3 changes: 3 additions & 0 deletions tests/subtopia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ async function setupSubtopiaRegistryClient(

describe("subtopia", () => {
beforeAll(async () => {
await refundTestnetAlgos(creatorAccount);
await refundTestnetAlgos(bobTestAccount);

const dispenserInfo = await algodClient
.accountInformation(dispenserAccount.addr)
.do();
Expand Down

0 comments on commit 704cde6

Please sign in to comment.