139 lines
4.7 KiB
TypeScript
139 lines
4.7 KiB
TypeScript
import { query, getClient, beginAuthorizedTransaction } from '../db/connection';
|
|
|
|
/** Unified spendable USDT ledger code — sole referral commission / bonus pool wallet. */
|
|
const COMMISSION_LEDGER_CODE = 'USDT_TRC20';
|
|
|
|
export async function getPoolBalance(walletTypeId: number): Promise<string> {
|
|
const rows = await query<{ available_balance: string }[]>(
|
|
'SELECT available_balance::text FROM platform_bonus_pools WHERE wallet_type_id = $1',
|
|
[walletTypeId]
|
|
);
|
|
return rows[0]?.available_balance ?? '0';
|
|
}
|
|
|
|
export async function listPools(): Promise<Array<{ wallet_type_id: number; available_balance: string }>> {
|
|
return query(
|
|
'SELECT wallet_type_id, available_balance::text FROM platform_bonus_pools ORDER BY wallet_type_id'
|
|
);
|
|
}
|
|
|
|
/** Single fundable pool = unified USDT ledger (USDT_TRC20). */
|
|
export async function listCommissionPools(): Promise<
|
|
Array<{ wallet_type_id: number; available_balance: string }>
|
|
> {
|
|
return query(
|
|
`SELECT p.wallet_type_id, p.available_balance::text
|
|
FROM platform_bonus_pools p
|
|
INNER JOIN wallet_types wt ON wt.id = p.wallet_type_id
|
|
WHERE wt.code = $1
|
|
ORDER BY p.wallet_type_id`,
|
|
[COMMISSION_LEDGER_CODE]
|
|
);
|
|
}
|
|
|
|
export async function hasPool(walletTypeId: number): Promise<boolean> {
|
|
const rows = await query<{ wallet_type_id: number }[]>(
|
|
'SELECT wallet_type_id FROM platform_bonus_pools WHERE wallet_type_id = $1',
|
|
[walletTypeId]
|
|
);
|
|
return Boolean(rows[0]);
|
|
}
|
|
|
|
/** Ensure a pool row exists (e.g. after changing commission wallet type). */
|
|
export async function ensurePool(walletTypeId: number): Promise<void> {
|
|
await query(
|
|
`INSERT INTO platform_bonus_pools (wallet_type_id, available_balance)
|
|
VALUES ($1, 0)
|
|
ON CONFLICT (wallet_type_id) DO NOTHING`,
|
|
[walletTypeId]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Resolve the fixed commission ledger wallet (USDT_TRC20), ensure its pool exists,
|
|
* and drop stray pools for custody rails (e.g. USDT_BEP20) so only one pool remains.
|
|
*/
|
|
export async function lockCommissionLedgerPool(): Promise<{ walletTypeId: number }> {
|
|
const rows = await query<{ id: number }[]>(
|
|
'SELECT id FROM wallet_types WHERE code = $1 LIMIT 1',
|
|
[COMMISSION_LEDGER_CODE]
|
|
);
|
|
const walletTypeId = rows[0]?.id;
|
|
if (!walletTypeId) {
|
|
throw new Error('USDT_TRC20 wallet type not configured — cannot lock referral commission pool');
|
|
}
|
|
await ensurePool(walletTypeId);
|
|
await query(
|
|
`DELETE FROM platform_bonus_pools p
|
|
USING wallet_types wt
|
|
WHERE p.wallet_type_id = wt.id AND wt.code <> $1`,
|
|
[COMMISSION_LEDGER_CODE]
|
|
);
|
|
return { walletTypeId };
|
|
}
|
|
|
|
/** Admin funds the platform bonus pool (off-chain treasury allocation). */
|
|
export async function fundPool(walletTypeId: number, amount: bigint): Promise<void> {
|
|
const client = await getClient();
|
|
try {
|
|
await beginAuthorizedTransaction(client);
|
|
const row = await client.query(
|
|
'SELECT available_balance FROM platform_bonus_pools WHERE wallet_type_id = $1 FOR UPDATE',
|
|
[walletTypeId]
|
|
);
|
|
if (!row.rows[0]) {
|
|
throw new Error(
|
|
'Bonus pool not found for wallet type. Fund only the commission wallet type (USDT_TRC20 ledger), not a custody rail like USDT_BEP20.'
|
|
);
|
|
}
|
|
const newBalance = BigInt(row.rows[0].available_balance) + amount;
|
|
await client.query(
|
|
'UPDATE platform_bonus_pools SET available_balance = $1, updated_at = NOW() WHERE wallet_type_id = $2',
|
|
[newBalance.toString(), walletTypeId]
|
|
);
|
|
await client.query('COMMIT');
|
|
} catch (e) {
|
|
await client.query('ROLLBACK');
|
|
throw e;
|
|
} finally {
|
|
client.release();
|
|
}
|
|
}
|
|
|
|
export async function debitPool(walletTypeId: number, amount: bigint): Promise<boolean> {
|
|
const client = await getClient();
|
|
try {
|
|
await beginAuthorizedTransaction(client);
|
|
const row = await client.query(
|
|
'SELECT available_balance FROM platform_bonus_pools WHERE wallet_type_id = $1 FOR UPDATE',
|
|
[walletTypeId]
|
|
);
|
|
if (!row.rows[0]) {
|
|
await client.query('ROLLBACK');
|
|
return false;
|
|
}
|
|
const available = BigInt(row.rows[0].available_balance);
|
|
if (available < amount) {
|
|
await client.query('ROLLBACK');
|
|
return false;
|
|
}
|
|
await client.query(
|
|
'UPDATE platform_bonus_pools SET available_balance = $1, updated_at = NOW() WHERE wallet_type_id = $2',
|
|
[(available - amount).toString(), walletTypeId]
|
|
);
|
|
await client.query('COMMIT');
|
|
return true;
|
|
} catch (e) {
|
|
await client.query('ROLLBACK');
|
|
throw e;
|
|
} finally {
|
|
client.release();
|
|
}
|
|
}
|
|
|
|
/** Credit pool when reversing a rejected/expired pending referral commission. */
|
|
export async function creditPool(walletTypeId: number, amount: bigint): Promise<void> {
|
|
if (amount <= 0n) return;
|
|
await fundPool(walletTypeId, amount);
|
|
}
|