227 lines
9.4 KiB
JavaScript
227 lines
9.4 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Shared application bootstrap (migrations, background jobs, game registration).
|
|
*/
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.bootstrapCore = bootstrapCore;
|
|
const config_1 = require("../config");
|
|
const connection_1 = require("../db/connection");
|
|
const bootstrap_1 = require("../games/bootstrap");
|
|
const engineRunner_1 = require("../games/engineRunner");
|
|
const depositMonitorService_1 = require("../services/depositMonitorService");
|
|
const securityService_1 = require("../services/securityService");
|
|
const fileIntegrityService_1 = require("../services/fileIntegrityService");
|
|
const dbTamperService_1 = require("../services/dbTamperService");
|
|
const sweepBatchService_1 = require("../services/sweepBatchService");
|
|
const sweepService_1 = require("../services/sweepService");
|
|
const countryService_1 = require("../services/countryService");
|
|
const userAdminStatsService_1 = require("../services/userAdminStatsService");
|
|
const adminUserExportService_1 = require("../services/adminUserExportService");
|
|
const referralService_1 = require("../services/referralService");
|
|
const analyticsMaterializedViews_1 = require("../utils/analyticsMaterializedViews");
|
|
const encryptionRotationReminderService_1 = require("../services/encryptionRotationReminderService");
|
|
const networkGuard_1 = require("../safety/networkGuard");
|
|
const productionSecretsGuard_1 = require("../safety/productionSecretsGuard");
|
|
const client_1 = require("../realtime/redis/client");
|
|
const DEPOSIT_POLL_INTERVAL_MS = 60 * 1000;
|
|
const WITHDRAWAL_MONITOR_INTERVAL_MS = 20 * 1000;
|
|
/** Confirms broadcast payouts on-chain (completed only after required confirmations). */
|
|
function startWithdrawalMonitor() {
|
|
const run = async () => {
|
|
try {
|
|
const { runWithdrawalMonitorCycle } = await Promise.resolve().then(() => __importStar(require('../services/withdrawalService')));
|
|
const r = await runWithdrawalMonitorCycle();
|
|
if (r.completed > 0)
|
|
console.log(`[withdrawal] ${r.completed} payout(s) confirmed`);
|
|
}
|
|
catch (e) {
|
|
console.error('Withdrawal monitor error:', e);
|
|
}
|
|
setTimeout(run, WITHDRAWAL_MONITOR_INTERVAL_MS);
|
|
};
|
|
setTimeout(run, 20 * 1000);
|
|
}
|
|
async function startDepositMonitor() {
|
|
const run = async () => {
|
|
try {
|
|
await (0, depositMonitorService_1.processAllDeposits)();
|
|
}
|
|
catch (e) {
|
|
console.error('Deposit monitor error:', e);
|
|
}
|
|
setTimeout(run, DEPOSIT_POLL_INTERVAL_MS);
|
|
};
|
|
setTimeout(run, 10 * 1000);
|
|
}
|
|
function startLedgerIntegrityMonitor() {
|
|
const intervalMs = (0, securityService_1.getIntegrityCheckIntervalMs)();
|
|
const run = async () => {
|
|
try {
|
|
const issues = await (0, securityService_1.runLedgerIntegrityCheck)();
|
|
if (issues.length > 0) {
|
|
console.warn(`[security] Ledger integrity check found ${issues.length} issue(s)`);
|
|
}
|
|
}
|
|
catch (e) {
|
|
console.error('Ledger integrity check error:', e);
|
|
}
|
|
setTimeout(run, intervalMs);
|
|
};
|
|
setTimeout(run, 30000);
|
|
}
|
|
function startFileIntegrityMonitor() {
|
|
const intervalMs = (0, securityService_1.getIntegrityCheckIntervalMs)();
|
|
const run = async () => {
|
|
try {
|
|
const issues = await (0, fileIntegrityService_1.runFileIntegrityCheck)();
|
|
if (issues.length > 0) {
|
|
console.warn(`[security] File integrity check found ${issues.length} issue(s)`);
|
|
}
|
|
}
|
|
catch (e) {
|
|
console.error('File integrity check error:', e);
|
|
}
|
|
setTimeout(run, intervalMs);
|
|
};
|
|
setTimeout(run, 45000);
|
|
}
|
|
function startDbTamperMonitor() {
|
|
const intervalMs = (0, dbTamperService_1.getTamperCheckIntervalMs)();
|
|
const run = async () => {
|
|
try {
|
|
const n = await (0, dbTamperService_1.processDbTamperEvents)();
|
|
if (n > 0) {
|
|
console.warn(`[security] Processed ${n} unauthorized DB change event(s)`);
|
|
}
|
|
}
|
|
catch (e) {
|
|
console.error('DB tamper monitor error:', e);
|
|
}
|
|
setTimeout(run, intervalMs);
|
|
};
|
|
setTimeout(run, 15000);
|
|
}
|
|
function startSweepBatchMonitor() {
|
|
const auto = config_1.config.treasury.sweepEnabled && config_1.config.treasury.sweepAutoExecute;
|
|
console.log(`[sweep] monitor start: SWEEP_ENABLED=${config_1.config.treasury.sweepEnabled} ` +
|
|
`SWEEP_AUTO_EXECUTE=${config_1.config.treasury.sweepAutoExecute} ` +
|
|
`allowlist=${config_1.config.treasury.sweepColdAllowlist.length} addr(s) ` +
|
|
`maxRaw=${config_1.config.treasury.sweepAutoMaxAmountRaw || '(none)'}`);
|
|
const run = async () => {
|
|
try {
|
|
if (config_1.config.treasury.sweepEnabled && config_1.config.treasury.sweepAutoExecute) {
|
|
const nAuto = await (0, sweepService_1.processAutoEligibleSweeps)();
|
|
if (nAuto > 0) {
|
|
console.log(`[sweep] auto-executed ${nAuto} sweep(s)`);
|
|
}
|
|
}
|
|
// Creates Approvals only when needed; also cancels orphan batches already auto-swept.
|
|
const n = await (0, sweepBatchService_1.createSweepBatchApprovalRequests)();
|
|
if (n > 0) {
|
|
console.log(`[sweep-batch] Enqueued ${n} batch approval request(s)`);
|
|
}
|
|
}
|
|
catch (e) {
|
|
console.error('Sweep batch job error:', e);
|
|
}
|
|
const nextMs = auto
|
|
? Math.min(sweepBatchService_1.SWEEP_BATCH_INTERVAL_MS, (0, sweepService_1.getSweepPollIntervalMs)())
|
|
: sweepBatchService_1.SWEEP_BATCH_INTERVAL_MS;
|
|
setTimeout(run, nextMs);
|
|
};
|
|
// First pass quickly so a pending sweep after deploy does not wait a full minute
|
|
setTimeout(run, auto ? 10000 : 60000);
|
|
}
|
|
function startReferralCommissionMonitor() {
|
|
const intervalMs = (0, referralService_1.getReferralCommissionPollIntervalMs)();
|
|
const run = async () => {
|
|
try {
|
|
const n = await (0, referralService_1.processPendingReferralCommissions)();
|
|
if (n > 0 && process.env.NODE_ENV !== 'production') {
|
|
console.log(`[referral] Settled ${n} pending commission(s)`);
|
|
}
|
|
}
|
|
catch (e) {
|
|
console.error('Referral commission monitor error:', e);
|
|
}
|
|
setTimeout(run, intervalMs);
|
|
};
|
|
setTimeout(run, 60000);
|
|
}
|
|
/** Core bootstrap shared by API and engine processes. */
|
|
async function bootstrapCore(opts = {}) {
|
|
const { startEngines = config_1.config.engineEnabled, startPlatformMonitors = true, runDevMigrations = config_1.config.env === 'development', } = opts;
|
|
(0, networkGuard_1.validateTestnetSafetyOrThrow)();
|
|
(0, networkGuard_1.validateProductionMainnetOrThrow)();
|
|
(0, productionSecretsGuard_1.validateProductionSecretsOrThrow)();
|
|
if (runDevMigrations) {
|
|
try {
|
|
await (0, connection_1.runMigrations)();
|
|
console.log('Migrations OK');
|
|
}
|
|
catch (e) {
|
|
console.warn('Migrations warning:', e);
|
|
}
|
|
}
|
|
await (0, countryService_1.ensureCountriesSeeded)();
|
|
if (config_1.config.redis.enabled) {
|
|
try {
|
|
await (0, client_1.connectRedis)();
|
|
console.log('[redis] Connected');
|
|
}
|
|
catch (e) {
|
|
console.warn('[redis] Connection failed (continuing without cache):', e);
|
|
}
|
|
}
|
|
(0, bootstrap_1.registerBuiltinGames)();
|
|
if (startPlatformMonitors) {
|
|
(0, userAdminStatsService_1.scheduleAdminStatsBackfill)();
|
|
(0, adminUserExportService_1.startAdminExportWorker)();
|
|
(0, analyticsMaterializedViews_1.scheduleAnalyticsMaterializedViewRefresh)();
|
|
startDepositMonitor();
|
|
startWithdrawalMonitor();
|
|
startLedgerIntegrityMonitor();
|
|
startFileIntegrityMonitor();
|
|
startDbTamperMonitor();
|
|
startSweepBatchMonitor();
|
|
startReferralCommissionMonitor();
|
|
(0, encryptionRotationReminderService_1.scheduleEncryptionKeyRotationReminder)();
|
|
}
|
|
if (startEngines) {
|
|
(0, engineRunner_1.startRegisteredGameEngines)();
|
|
}
|
|
}
|
|
//# sourceMappingURL=appBootstrap.js.map
|