From 04cb8ad14273c8d303c76d9fe27ec89aa481c78f Mon Sep 17 00:00:00 2001 From: Altynbek Orumbayev Date: Tue, 10 Oct 2023 00:46:27 +0200 Subject: [PATCH] chore: tweaking discount price behaviour --- src/clients/SubtopiaClient.ts | 31 +++++++++++++++++++------------ tests/subtopia.test.ts | 15 ++++++++++++++- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/clients/SubtopiaClient.ts b/src/clients/SubtopiaClient.ts index 2bcf014..26d9220 100644 --- a/src/clients/SubtopiaClient.ts +++ b/src/clients/SubtopiaClient.ts @@ -579,11 +579,9 @@ export class SubtopiaClient { public async createSubscription({ subscriber, duration, - parseWholeUnits = false, }: { subscriber: TransactionSignerAccount; duration: Duration; - parseWholeUnits?: boolean; }): Promise<{ txID: string; subscriptionID: number; @@ -597,7 +595,7 @@ export class SubtopiaClient { oracleAdminState.valueRaw ); const platformFeeAmount = await this.getSubscriptionPlatformFee(); - const state = await this.getAppState(parseWholeUnits); + const state = await this.getAppState(); const managerLockerID = await SubtopiaRegistryClient.getLocker({ registryID: TESTNET_SUBTOPIA_REGISTRY_ID, algodClient: this.algodClient, @@ -611,6 +609,21 @@ export class SubtopiaClient { const lockerAddress = getApplicationAddress(managerLockerID); + let subscriptionPrice = this.price; + for (const discount of state.discounts) { + if (discount.duration === duration.valueOf()) { + if (discount.discountType === DiscountType.PERCENTAGE) { + subscriptionPrice = + subscriptionPrice - + (subscriptionPrice * discount.discountValue) / 100; + break; + } else if (discount.discountType === DiscountType.FIXED) { + subscriptionPrice = subscriptionPrice - discount.discountValue; + break; + } + } + } + const createSubscriptionAtc = new AtomicTransactionComposer(); createSubscriptionAtc.addMethodCall({ appID: this.appID, @@ -675,7 +688,7 @@ export class SubtopiaClient { txn: makePaymentTxnWithSuggestedParamsFromObject({ from: subscriber.addr, to: adminAddress, - amount: platformFeeAmount, + amount: this.price > 0 ? platformFeeAmount : 0, suggestedParams: await getParamsWithFeeCount(this.algodClient, 0), }), signer: subscriber.signer, @@ -685,7 +698,7 @@ export class SubtopiaClient { txn: makePaymentTxnWithSuggestedParamsFromObject({ from: subscriber.addr, to: lockerAddress, - amount: this.price, + amount: subscriptionPrice, suggestedParams: await getParamsWithFeeCount( this.algodClient, 0 @@ -697,13 +710,7 @@ export class SubtopiaClient { txn: makeAssetTransferTxnWithSuggestedParamsFromObject({ from: subscriber.addr, to: lockerAddress, - amount: parseWholeUnits - ? normalizePrice( - this.price, - this.coin.decimals, - PriceNormalizationType.RAW - ) - : this.price, + amount: subscriptionPrice, assetIndex: this.coin.index, suggestedParams: await getParamsWithFeeCount( this.algodClient, diff --git a/tests/subtopia.test.ts b/tests/subtopia.test.ts index 42a30de..981dc5b 100644 --- a/tests/subtopia.test.ts +++ b/tests/subtopia.test.ts @@ -274,8 +274,22 @@ describe("subtopia", () => { expect(createDiscountResponse.txID).toBeDefined(); + const purchaseResponse = await productClient.createSubscription({ + subscriber: creatorSignerAccount, + duration: Duration.MONTH, + }); + expect(purchaseResponse.txID).toBeDefined(); + const productState = await productClient.getAppState(); + const deleteSubscriptionResponse = await productClient.deleteSubscription( + { + subscriber: creatorSignerAccount, + subscriptionID: purchaseResponse.subscriptionID, + } + ); + expect(deleteSubscriptionResponse.txID).toBeDefined(); + expect(productState.discounts.length).toBe(1); expect(productState.discounts[0].duration).toBe(Duration.MONTH.valueOf()); @@ -457,7 +471,6 @@ describe("subtopia", () => { const subscribeResponse = await productClient.createSubscription({ subscriber: subscriberSigner, duration: Duration.MONTH, - parseWholeUnits: false, }); expect(subscribeResponse.subscriptionID).toBeGreaterThan(0);