Skip to content

Commit

Permalink
chore: tweaking discount price behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
aorumbayev committed Oct 9, 2023
1 parent bf88a3b commit 04cb8ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
31 changes: 19 additions & 12 deletions src/clients/SubtopiaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand Down
15 changes: 14 additions & 1 deletion tests/subtopia.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -457,7 +471,6 @@ describe("subtopia", () => {
const subscribeResponse = await productClient.createSubscription({
subscriber: subscriberSigner,
duration: Duration.MONTH,
parseWholeUnits: false,
});

expect(subscribeResponse.subscriptionID).toBeGreaterThan(0);
Expand Down

0 comments on commit 04cb8ad

Please sign in to comment.