109 lines
4.8 KiB
JavaScript
109 lines
4.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getPoolBalance = getPoolBalance;
|
|
exports.listPools = listPools;
|
|
exports.listCommissionPools = listCommissionPools;
|
|
exports.hasPool = hasPool;
|
|
exports.ensurePool = ensurePool;
|
|
exports.lockCommissionLedgerPool = lockCommissionLedgerPool;
|
|
exports.fundPool = fundPool;
|
|
exports.debitPool = debitPool;
|
|
exports.creditPool = creditPool;
|
|
const connection_1 = require("../db/connection");
|
|
/** Unified spendable USDT ledger code — sole referral commission / bonus pool wallet. */
|
|
const COMMISSION_LEDGER_CODE = 'USDT_TRC20';
|
|
async function getPoolBalance(walletTypeId) {
|
|
const rows = await (0, connection_1.query)('SELECT available_balance::text FROM platform_bonus_pools WHERE wallet_type_id = $1', [walletTypeId]);
|
|
return rows[0]?.available_balance ?? '0';
|
|
}
|
|
async function listPools() {
|
|
return (0, connection_1.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). */
|
|
async function listCommissionPools() {
|
|
return (0, connection_1.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]);
|
|
}
|
|
async function hasPool(walletTypeId) {
|
|
const rows = await (0, connection_1.query)('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). */
|
|
async function ensurePool(walletTypeId) {
|
|
await (0, connection_1.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.
|
|
*/
|
|
async function lockCommissionLedgerPool() {
|
|
const rows = await (0, connection_1.query)('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 (0, connection_1.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). */
|
|
async function fundPool(walletTypeId, amount) {
|
|
const client = await (0, connection_1.getClient)();
|
|
try {
|
|
await (0, connection_1.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();
|
|
}
|
|
}
|
|
async function debitPool(walletTypeId, amount) {
|
|
const client = await (0, connection_1.getClient)();
|
|
try {
|
|
await (0, connection_1.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. */
|
|
async function creditPool(walletTypeId, amount) {
|
|
if (amount <= 0n)
|
|
return;
|
|
await fundPool(walletTypeId, amount);
|
|
}
|
|
//# sourceMappingURL=bonusPoolRepository.js.map
|