From e1bfc12545aee852d19bb2a5f5ae5b37f1c7eda6 Mon Sep 17 00:00:00 2001 From: Jeevan Sidhu Date: Thu, 9 Apr 2026 15:55:22 -0400 Subject: [PATCH 1/2] refactor(api): reuse module instances instead of re-instantiating in Payout methods --- apps/api/src/modules/Payout.ts | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/apps/api/src/modules/Payout.ts b/apps/api/src/modules/Payout.ts index f12b377..7844b9b 100644 --- a/apps/api/src/modules/Payout.ts +++ b/apps/api/src/modules/Payout.ts @@ -68,6 +68,8 @@ export class PayoutModule { private readonly platformListHelper: ListHelper; private readonly accountModule: AccountModule; private readonly externalWalletModule: ExternalWalletModule; + private readonly balanceModule: BalanceModule; + private readonly balanceTransactionModule: BalanceTransactionModule; private readonly solana: Solana; constructor(db: Database, eventService?: EventService) { @@ -75,6 +77,8 @@ export class PayoutModule { this.eventService = eventService || null; this.accountModule = new AccountModule(db); this.externalWalletModule = new ExternalWalletModule(db); + this.balanceModule = new BalanceModule(db); + this.balanceTransactionModule = new BalanceTransactionModule(db); this.solana = new Solana(); this.listHelper = new ListHelper(db, { collection: 'Payouts', @@ -126,12 +130,11 @@ export class PayoutModule { } // Get the destination wallet - either specified or the account's default - const externalWalletModule = new ExternalWalletModule(this.db); let wallet; if (destination) { // Use specified destination wallet - wallet = await externalWalletModule.GetExternalWallet(destination); + wallet = await this.externalWalletModule.GetExternalWallet(destination); if (!wallet) { throw new AppError( @@ -158,7 +161,7 @@ export class PayoutModule { } } else { // Get the account's default external wallet - const wallets = await externalWalletModule.GetExternalWalletsByAccount( + const wallets = await this.externalWalletModule.GetExternalWalletsByAccount( account ); @@ -200,9 +203,6 @@ export class PayoutModule { const payout = await this.db.RunTransaction( async (session: ClientSession) => { - const balanceTransactionModule = new BalanceTransactionModule(this.db); - const balanceModule = new BalanceModule(this.db); - // Store the wallet ID as destination (not the wallet address) const newPayout = this.PayoutObject({ account, @@ -218,7 +218,7 @@ export class PayoutModule { const timestamp = Now(); const balanceTransaction = - balanceTransactionModule.BalanceTransactionObject({ + this.balanceTransactionModule.BalanceTransactionObject({ amount: -amount, currency: currency ?? 'usdc', account: account, @@ -234,7 +234,7 @@ export class PayoutModule { newPayout.balance_transaction = balanceTransaction.id; // Verify account has sufficient funds - const balanceData = await balanceModule.GetBalance(account, session); + const balanceData = await this.balanceModule.GetBalance(account, session); if (!balanceData) { throw new AppError( ERRORS.BALANCE_NOT_FOUND.message, @@ -265,7 +265,7 @@ export class PayoutModule { ); // Deduct from available balance - const updatedBalance = balanceModule.UpdateBalance( + const updatedBalance = this.balanceModule.UpdateBalance( balanceData, -amount, currency ?? 'usdc', @@ -564,7 +564,6 @@ export class PayoutModule { // Fetch all payouts and validate they exist and are pending const payouts: PayoutType[] = []; - const externalWalletModule = new ExternalWalletModule(this.db); for (const payoutId of payoutIds) { const payout = await this.GetPayout(payoutId); @@ -613,7 +612,7 @@ export class PayoutModule { []; for (const payout of payouts) { - const wallet = await externalWalletModule.GetExternalWallet( + const wallet = await this.externalWalletModule.GetExternalWallet( payout.destination ); @@ -901,11 +900,10 @@ export class PayoutModule { payout: PayoutType, session?: ClientSession ): Promise { - const balanceModule = new BalanceModule(this.db); - const balanceData = await balanceModule.GetBalance(payout.account, session); + const balanceData = await this.balanceModule.GetBalance(payout.account, session); if (balanceData) { - const updatedBalance = balanceModule.UpdateBalance( + const updatedBalance = this.balanceModule.UpdateBalance( balanceData, payout.amount, payout.currency, From c8400e9f41fbbca8ad284ca445e95ffaa8b379da Mon Sep 17 00:00:00 2001 From: Jeevan Sidhu Date: Sat, 11 Apr 2026 13:28:24 -0400 Subject: [PATCH 2/2] refactor(api): gofmt --- apps/api/src/modules/Payout.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/apps/api/src/modules/Payout.ts b/apps/api/src/modules/Payout.ts index 7844b9b..db4f061 100644 --- a/apps/api/src/modules/Payout.ts +++ b/apps/api/src/modules/Payout.ts @@ -161,9 +161,8 @@ export class PayoutModule { } } else { // Get the account's default external wallet - const wallets = await this.externalWalletModule.GetExternalWalletsByAccount( - account - ); + const wallets = + await this.externalWalletModule.GetExternalWalletsByAccount(account); if (wallets.length === 0) { throw new AppError( @@ -234,7 +233,10 @@ export class PayoutModule { newPayout.balance_transaction = balanceTransaction.id; // Verify account has sufficient funds - const balanceData = await this.balanceModule.GetBalance(account, session); + const balanceData = await this.balanceModule.GetBalance( + account, + session + ); if (!balanceData) { throw new AppError( ERRORS.BALANCE_NOT_FOUND.message, @@ -900,7 +902,10 @@ export class PayoutModule { payout: PayoutType, session?: ClientSession ): Promise { - const balanceData = await this.balanceModule.GetBalance(payout.account, session); + const balanceData = await this.balanceModule.GetBalance( + payout.account, + session + ); if (balanceData) { const updatedBalance = this.balanceModule.UpdateBalance(