Skip to content

Commit

Permalink
chore: adding typed discounts into product state
Browse files Browse the repository at this point in the history
  • Loading branch information
aorumbayev committed Sep 3, 2023
1 parent 411d724 commit ad6f344
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
41 changes: 34 additions & 7 deletions src/clients/SubtopiaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
DurationType,
DiscountType,
Duration,
SubscriptionType,
} from "../enums";

import {
Expand All @@ -49,7 +50,7 @@ import {
ApplicationSpec,
AssetMetadata,
DiscountRecord,
ProductGlobalState,
ProductState,
SubscriptionRecord,
} from "interfaces";

Expand Down Expand Up @@ -175,16 +176,39 @@ export class SubtopiaClient {
});
}

public async getGlobalState(
public async getAppState(
withPriceNormalization = true
): Promise<ProductGlobalState> {
): Promise<ProductState> {
const globalState = await getAppGlobalState(
this.appID,
this.algodClient
).catch((error) => {
throw new Error(error);
});

const durations = [];
if (Number(globalState.sub_type.value) === SubscriptionType.UNLIMITED) {
durations.push(Duration.UNLIMITED);
} else {
durations.push(
Duration.MONTH,
Duration.QUARTER,
Duration.SEMI_ANNUAL,
Duration.ANNUAL
);
}

const discounts = [];
for (const duration of durations) {
try {
const discount = await this.getDiscount({ duration });
discounts.push(discount);
} catch (error) {
// Ignore
continue;
}
}

return {
productName: String(globalState.product_name.value),
subscriptionName: String(globalState.subscription_name.value),
Expand All @@ -209,6 +233,7 @@ export class SubtopiaClient {
oracleID: Number(globalState.oracle_id.value),
unitName: String(globalState.unit_name.value),
imageURL: String(globalState.image_url.value),
discounts: discounts,
};
}

Expand Down Expand Up @@ -260,7 +285,7 @@ export class SubtopiaClient {
public async getDiscount({
duration,
}: {
duration: DurationType;
duration: Duration;
}): Promise<DiscountRecord> {
const getDiscountAtc = new AtomicTransactionComposer();
getDiscountAtc.addMethodCall({
Expand Down Expand Up @@ -289,9 +314,11 @@ export class SubtopiaClient {
});

const response = await getDiscountAtc.simulate(this.algodClient);
const boxContent: Array<number> = (
response.methodResults[0].returnValue?.valueOf() as Array<number>
).map((value) => Number(value));
const rawContent = response.methodResults[0].returnValue?.valueOf();

const boxContent: Array<number> = Array.isArray(rawContent)
? rawContent.map((value) => Number(value))
: [];

if (boxContent.length !== 6) {
throw new Error("Invalid subscription record");
Expand Down
6 changes: 5 additions & 1 deletion src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface DiscountMetadata extends BaseDiscountRecord {
expiresIn?: number;
}

export interface ProductGlobalState {
interface ProductGlobalState {
productName: string;
subscriptionName: string;
manager: string;
Expand All @@ -64,3 +64,7 @@ export interface ProductGlobalState {
unitName: string;
imageURL: string;
}

export interface ProductState extends ProductGlobalState {
discounts: DiscountRecord[];
}
7 changes: 6 additions & 1 deletion tests/subtopia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe("subtopia", () => {
duration: DurationType.UNLIMITED,
});

const productClientGlobalState = await productClient.getGlobalState();
const productClientGlobalState = await productClient.getAppState();
expect(productClientGlobalState.totalSubs).toBe(1);

expect(subscribeResponse.subscriptionID).toBeGreaterThan(0);
Expand Down Expand Up @@ -273,6 +273,11 @@ describe("subtopia", () => {

expect(createDiscountResponse.txID).toBeDefined();

const productState = await productClient.getAppState();

expect(productState.discounts.length).toBe(1);
expect(productState.discounts[0].duration).toBe(Duration.MONTH.valueOf());

const getDiscountResponse = await productClient.getDiscount({
duration: Duration.MONTH.valueOf(),
});
Expand Down

0 comments on commit ad6f344

Please sign in to comment.