diff --git a/backend/src/controllers/referralController.ts b/backend/src/controllers/referralController.ts index 564eb3a5..90d87d17 100644 --- a/backend/src/controllers/referralController.ts +++ b/backend/src/controllers/referralController.ts @@ -128,6 +128,9 @@ export async function updateAdminReferralSettings(req: AuthRequest, res: Respons minFirstDepositRaw: body.minFirstDepositRaw, commissionWalletTypeId: body.commissionWalletTypeId, }); + if (body.commissionWalletTypeId != null) { + await bonusPoolRepository.ensurePool(body.commissionWalletTypeId); + } const config = await referralService.getProgramSettingsForAdmin(); res.json(config); } catch (e) { @@ -202,6 +205,20 @@ export async function fundBonusPool(req: AuthRequest, res: Response): Promise { + 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 { + await query( + `INSERT INTO platform_bonus_pools (wallet_type_id, available_balance) + VALUES ($1, 0) + ON CONFLICT (wallet_type_id) DO NOTHING`, + [walletTypeId] + ); +} + /** Admin funds the platform bonus pool (off-chain treasury allocation). */ export async function fundPool(walletTypeId: number, amount: bigint): Promise { const client = await getClient(); @@ -23,7 +41,11 @@ export async function fundPool(walletTypeId: number, amount: bigint): Promise { if (!configQ.data) return; setFundForm((f) => { - if (f.walletTypeId) return f; const cfg = configQ.data; + const allowedIds = new Set( + (cfg.pools.length ? cfg.pools.map((p) => p.walletTypeId) : [cfg.settings.commissionWalletTypeId]).filter( + Boolean + ) + ); + const currentOk = f.walletTypeId && allowedIds.has(Number(f.walletTypeId)); + if (currentOk) return f; const preferred = cfg.settings.commissionWalletTypeId || cfg.pools[0]?.walletTypeId || - cfg.walletTypes[0]?.id; + [...allowedIds][0]; return preferred ? { ...f, walletTypeId: String(preferred) } : f; }); }, [configQ.data]); @@ -930,7 +936,16 @@ export function ReferralAdminPage() { value={fundForm.walletTypeId} onChange={(e) => setFundForm((f) => ({ ...f, walletTypeId: String(e.target.value) }))} > - {walletTypeOptions.map((wt) => ( + {/* Only pool rows (commission ledger) — not custody rails like BEP20 */} + {(config?.pools?.length + ? config.pools.map((p) => ({ + id: p.walletTypeId, + label: p.label, + })) + : walletTypeOptions + .filter((wt) => wt.id === config?.settings.commissionWalletTypeId) + .map((wt) => ({ id: wt.id, label: wt.label })) + ).map((wt) => ( {wt.label}