Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions apps/api/src/modules/Payout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ export class PayoutModule {
private readonly platformListHelper: ListHelper<PayoutType>;
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) {
this.db = db;
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<PayoutType>(db, {
collection: 'Payouts',
Expand Down Expand Up @@ -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(
Expand All @@ -158,9 +161,8 @@ export class PayoutModule {
}
} else {
// Get the account's default external wallet
const wallets = await externalWalletModule.GetExternalWalletsByAccount(
account
);
const wallets =
await this.externalWalletModule.GetExternalWalletsByAccount(account);

if (wallets.length === 0) {
throw new AppError(
Expand Down Expand Up @@ -200,9 +202,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,
Expand All @@ -218,7 +217,7 @@ export class PayoutModule {

const timestamp = Now();
const balanceTransaction =
balanceTransactionModule.BalanceTransactionObject({
this.balanceTransactionModule.BalanceTransactionObject({
amount: -amount,
currency: currency ?? 'usdc',
account: account,
Expand All @@ -234,7 +233,10 @@ 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,
Expand Down Expand Up @@ -265,7 +267,7 @@ export class PayoutModule {
);

// Deduct from available balance
const updatedBalance = balanceModule.UpdateBalance(
const updatedBalance = this.balanceModule.UpdateBalance(
balanceData,
-amount,
currency ?? 'usdc',
Expand Down Expand Up @@ -564,7 +566,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);
Expand Down Expand Up @@ -613,7 +614,7 @@ export class PayoutModule {
[];

for (const payout of payouts) {
const wallet = await externalWalletModule.GetExternalWallet(
const wallet = await this.externalWalletModule.GetExternalWallet(
payout.destination
);

Expand Down Expand Up @@ -901,11 +902,13 @@ export class PayoutModule {
payout: PayoutType,
session?: ClientSession
): Promise<void> {
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,
Expand Down
Loading