Skip to content

Commit

Permalink
refactor: adding ability to pass custom signer
Browse files Browse the repository at this point in the history
  • Loading branch information
aorumbayev committed Mar 17, 2024
1 parent 542db22 commit 3558644
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
66 changes: 44 additions & 22 deletions src/clients/SubtopiaClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
LockerType,
PriceNormalizationType,
ProductDiscountCreationParams,
ProductExtraParams,
ProductInitParams,
ProductLifecycleStateUpdate,
ProductState,
Expand Down Expand Up @@ -253,7 +254,9 @@ export class SubtopiaClient {
* @returns {Promise<{txID: string}>} A promise that resolves to an object containing the transaction ID.
*/
protected async updateLifecycle(
params: ProductLifecycleStateUpdate
params: ProductLifecycleStateUpdate,

extraParams?: ProductExtraParams
): Promise<{
txID: string;
}> {
Expand All @@ -273,8 +276,8 @@ export class SubtopiaClient {
returns: { type: "void" },
}),
methodArgs: [lifecycle],
sender: this.creator.addr,
signer: this.creator.signer,
sender: extraParams?.creator.addr ?? this.creator.addr,
signer: extraParams?.creator.signer ?? this.creator.signer,
suggestedParams: await getParamsWithFeeCount(this.algodClient, 1),
});

Expand All @@ -290,6 +293,15 @@ export class SubtopiaClient {
};
}

/**
* Updates the creator account for the SubtopiaClient instance.
* This method sets a new creator account, which is used for signing transactions.
* @param {TransactionSignerAccount} newCreator - The new creator account to be set.
*/
public updateCreator(newCreator: TransactionSignerAccount): void {
this.creator = newCreator;
}

/**
* This method is used to enable the application (updates the lifecycle).
* The method returns the transaction ID.
Expand Down Expand Up @@ -362,7 +374,9 @@ export class SubtopiaClient {
* It returns the platform fee as a number.
* @returns {Promise<number>} A promise that resolves to the platform fee.
*/
public async getSubscriptionPlatformFee(): Promise<number> {
public async getSubscriptionPlatformFee(
extraParams?: ProductExtraParams
): Promise<number> {
if (this.price === 0) {
return new Promise((resolve) => resolve(0));
}
Expand All @@ -383,8 +397,8 @@ export class SubtopiaClient {
returns: { type: "uint64" },
}),
methodArgs: [priceInCents],
sender: this.creator.addr,
signer: makeEmptyTransactionSigner(),
sender: extraParams?.creator.addr ?? this.creator.addr,
signer: extraParams?.creator.signer ?? this.creator.signer,
suggestedParams: await getParamsWithFeeCount(this.algodClient, 1),
});

Expand Down Expand Up @@ -432,7 +446,9 @@ export class SubtopiaClient {
* It accepts a duration object as an argument and returns a promise that resolves to a DiscountRecord.
* @returns {Promise<DiscountRecord>} A promise that resolves to the discount record.
*/
public async getDiscount(): Promise<DiscountRecord | undefined> {
public async getDiscount(
extraParams?: ProductExtraParams
): Promise<DiscountRecord | undefined> {
const getDiscountAtc = new AtomicTransactionComposer();
getDiscountAtc.addMethodCall({
appID: this.appID,
Expand All @@ -452,8 +468,8 @@ export class SubtopiaClient {
name: ENCODED_DISCOUNT_BOX_KEY,
},
],
sender: this.creator.addr,
signer: makeEmptyTransactionSigner(),
sender: extraParams?.creator.addr ?? this.creator.addr,
signer: extraParams?.creator.signer ?? this.creator.signer,
suggestedParams: await getParamsWithFeeCount(this.algodClient, 1),
});

Expand Down Expand Up @@ -503,7 +519,10 @@ export class SubtopiaClient {
* @param {ProductDiscountCreationParams} params - The parameters for creating a discount.
* @returns {Promise<{txID: string}>} A promise that resolves to an object containing the transaction ID.
*/
public async createDiscount(params: ProductDiscountCreationParams): Promise<{
public async createDiscount(
params: ProductDiscountCreationParams,
extraParams?: ProductExtraParams
): Promise<{
txID: string;
}> {
const {
Expand Down Expand Up @@ -553,12 +572,12 @@ export class SubtopiaClient {
expiresIn,
{
txn: makePaymentTxnWithSuggestedParamsFromObject({
from: this.creator.addr,
from: extraParams?.creator.addr ?? this.creator.addr,
to: this.appAddress,
amount: calculateProductDiscountBoxCreateMbr(),
suggestedParams: await getParamsWithFeeCount(this.algodClient, 0),
}),
signer: this.creator.signer,
signer: extraParams?.creator.signer ?? this.creator.signer,
},
],
boxes: [
Expand All @@ -567,8 +586,8 @@ export class SubtopiaClient {
name: ENCODED_DISCOUNT_BOX_KEY,
},
],
sender: this.creator.addr,
signer: this.creator.signer,
sender: extraParams?.creator.addr ?? this.creator.addr,
signer: extraParams?.creator.signer ?? this.creator.signer,
suggestedParams: await getParamsWithFeeCount(this.algodClient, 2),
});

Expand All @@ -589,7 +608,7 @@ export class SubtopiaClient {
* Removes active discount from a product contract if exists.
* @returns {Promise<{txID: string}>} A promise that resolves to an object containing the transaction ID.
*/
public async deleteDiscount(): Promise<{
public async deleteDiscount(extraParams?: ProductExtraParams): Promise<{
txID: string;
}> {
const deleteDiscountAtc = new AtomicTransactionComposer();
Expand All @@ -607,8 +626,8 @@ export class SubtopiaClient {
name: ENCODED_DISCOUNT_BOX_KEY,
},
],
sender: this.creator.addr,
signer: this.creator.signer,
sender: extraParams?.creator.addr ?? this.creator.addr,
signer: extraParams?.creator.signer ?? this.creator.signer,
suggestedParams: await getParamsWithFeeCount(this.algodClient, 2),
});

Expand Down Expand Up @@ -1002,7 +1021,9 @@ export class SubtopiaClient {
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating whether the address is a subscriber.
*/
public async isSubscriber(
params: ProductSubscriberCheckParams
params: ProductSubscriberCheckParams,

extraParams?: ProductExtraParams
): Promise<boolean> {
const { subscriberAddress } = params;
const isSubscriberAtc = new AtomicTransactionComposer();
Expand All @@ -1020,8 +1041,8 @@ export class SubtopiaClient {
returns: { type: "uint64" },
}),
methodArgs: [subscriberAddress],
sender: this.creator.addr,
signer: makeEmptyTransactionSigner(),
sender: extraParams?.creator.addr ?? this.creator.addr,
signer: extraParams?.creator.signer ?? makeEmptyTransactionSigner(),
boxes: [
{
appIndex: this.appID,
Expand Down Expand Up @@ -1060,7 +1081,8 @@ export class SubtopiaClient {
* @returns {Promise<SubscriptionRecord>} A promise that resolves to a SubscriptionRecord.
*/
public async getSubscription(
params: ProductSubscriptionRetrievalParams
params: ProductSubscriptionRetrievalParams,
extraParams?: ProductExtraParams
): Promise<SubscriptionRecord> {
const { algodClient, subscriberAddress } = params;
const getSubscriptionAtc = new AtomicTransactionComposer();
Expand All @@ -1078,7 +1100,7 @@ export class SubtopiaClient {
returns: { type: "(uint64,uint64,uint64,uint64,uint64)" },
}),
methodArgs: [subscriberAddress],
sender: this.creator.addr,
sender: extraParams?.creator.addr ?? this.creator.addr,
signer: makeEmptyTransactionSigner(),
boxes: [
{
Expand Down
4 changes: 4 additions & 0 deletions src/types/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ export interface ProductInitParams {
registryID?: number;
timeout?: number;
}
export interface ProductExtraParams {
creator: TransactionSignerAccount;
[key: string]: string | number | object;
}

0 comments on commit 3558644

Please sign in to comment.