"use strict"; 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; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_test_1 = require("node:test"); const strict_1 = __importDefault(require("node:assert/strict")); const connection_1 = require("../db/connection"); const repo = __importStar(require("../repositories/candlePredictRepository")); const candlePredictMultiService_1 = require("../services/candlePredictMultiService"); const testDb_1 = require("./helpers/testDb"); const dbDescribe = (0, testDb_1.isDbTestEnabled)() ? node_test_1.describe : node_test_1.describe.skip; const TEST_USER_ID = '00000000-0000-4000-8000-0000000000a1'; const TEST_EMAIL_HASH = 'b'.repeat(64); const ANCHOR_OPEN_MS = 1700000000000; const INTERVAL_MINUTES = 1; const FINAL_OPEN_MS = ANCHOR_OPEN_MS + (3 - 1) * INTERVAL_MINUTES * 60000; let roundId; let betId; let walletTypeId; dbDescribe('multi-candle pool weight (database)', () => { (0, node_test_1.before)(async () => { await (0, connection_1.runMigrations)(); await (0, connection_1.query)(`INSERT INTO users (id, email_hash, password_hash, role_id) VALUES ($1, $2, 'test-hash', 1) ON CONFLICT (id) DO NOTHING`, [TEST_USER_ID, TEST_EMAIL_HASH]); const walletRows = await (0, connection_1.query)(`SELECT id FROM wallet_types WHERE code = 'USDT_TRC20' LIMIT 1`); walletTypeId = walletRows[0]?.id ?? 1; await repo.upsertMultiLevel({ level: 3, enabled: true, payoutMultiplier: 6, poolMultiplier: 6, sortOrder: 2, }); const round = await repo.createRound({ candleOpenMs: ANCHOR_OPEN_MS, intervalMinutes: INTERVAL_MINUTES, status: 'betting', }); roundId = round.id; const bet = await repo.createMultiCandleBet({ roundId, userId: TEST_USER_ID, stakeRaw: 100000000n, stakeDisplay: 100, walletTypeId, intervalMinutes: INTERVAL_MINUTES, stakeAccountKind: 'main', level: 3, sequenceDirections: ['long', 'short', 'long'], anchorCandleOpenMs: ANCHOR_OPEN_MS, }); betId = bet.id; await (0, connection_1.query)(`UPDATE candle_predict_bets SET sequence_step = 2 WHERE id = $1`, [betId]); }); (0, node_test_1.after)(async () => { if (betId) { await (0, connection_1.query)('DELETE FROM candle_predict_bets WHERE id = $1', [betId]); } if (roundId) { await (0, connection_1.query)('DELETE FROM candle_predict_rounds WHERE id = $1', [roundId]); } await (0, connection_1.query)('DELETE FROM users WHERE id = $1', [TEST_USER_ID]); }); (0, node_test_1.test)('getMultiCandlePoolContributions uses stake × pool multiplier', async () => { const expectedPoolContribution = (0, candlePredictMultiService_1.computeMultiCandlePoolContribution)(100, 6); strict_1.default.equal(expectedPoolContribution, 600); const pools = await repo.getMultiCandlePoolContributions(FINAL_OPEN_MS); strict_1.default.equal(pools.longPool, 600); strict_1.default.equal(pools.shortPool, 0); strict_1.default.notEqual(pools.longPool, (0, candlePredictMultiService_1.computeMultiCandlePoolContribution)(100, 3)); }); (0, node_test_1.test)('raising admin pool multiplier changes pool contribution', async () => { await repo.upsertMultiLevel({ level: 3, poolMultiplier: 999 }); const pools = await repo.getMultiCandlePoolContributions(FINAL_OPEN_MS); strict_1.default.equal(pools.longPool, 99900); await repo.upsertMultiLevel({ level: 3, poolMultiplier: 6 }); }); (0, node_test_1.test)('changing payout multiplier alone does not change pool contribution', async () => { await repo.upsertMultiLevel({ level: 3, payoutMultiplier: 999, poolMultiplier: 6 }); const pools = await repo.getMultiCandlePoolContributions(FINAL_OPEN_MS); strict_1.default.equal(pools.longPool, 600); await repo.upsertMultiLevel({ level: 3, payoutMultiplier: 6, poolMultiplier: 6 }); }); (0, node_test_1.test)('test-wallet multi bet is excluded from real-money pool totals', async () => { const testAnchor = ANCHOR_OPEN_MS + 3600000; const testInterval = 3; const testFinalOpenMs = testAnchor + (3 - 1) * testInterval * 60000; const testRound = await repo.createRound({ candleOpenMs: testAnchor, intervalMinutes: testInterval, status: 'betting', }); const testBet = await repo.createMultiCandleBet({ roundId: testRound.id, userId: TEST_USER_ID, stakeRaw: 100000000n, stakeDisplay: 100, walletTypeId, intervalMinutes: testInterval, stakeAccountKind: 'test', level: 3, sequenceDirections: ['long', 'short', 'long'], anchorCandleOpenMs: testAnchor, }); await (0, connection_1.query)(`UPDATE candle_predict_bets SET sequence_step = 2 WHERE id = $1`, [testBet.id]); const pools = await repo.getMultiCandlePoolContributions(testFinalOpenMs); strict_1.default.equal(pools.longPool, 0); strict_1.default.equal(pools.shortPool, 0); await (0, connection_1.query)('DELETE FROM candle_predict_bets WHERE id = $1', [testBet.id]); await (0, connection_1.query)('DELETE FROM candle_predict_rounds WHERE id = $1', [testRound.id]); }); }); dbDescribe('multi-candle intermediate steps (database)', () => { const LEVEL = 5; const anchor = 1710000000000; const intervalMin = 1; let level5RoundId; let level5BetId; let walletTypeId = 1; (0, node_test_1.before)(async () => { await (0, connection_1.runMigrations)(); await (0, connection_1.query)(`INSERT INTO users (id, email_hash, password_hash, role_id) VALUES ($1, $2, 'test-hash', 1) ON CONFLICT (id) DO NOTHING`, [TEST_USER_ID, TEST_EMAIL_HASH]); const walletRows = await (0, connection_1.query)(`SELECT id FROM wallet_types WHERE code = 'USDT_TRC20' LIMIT 1`); walletTypeId = walletRows[0]?.id ?? 1; const round = await repo.createRound({ candleOpenMs: anchor, intervalMinutes: intervalMin, status: 'betting', }); level5RoundId = round.id; const bet = await repo.createMultiCandleBet({ roundId: level5RoundId, userId: TEST_USER_ID, stakeRaw: 100000000n, stakeDisplay: 100, walletTypeId, intervalMinutes: intervalMin, stakeAccountKind: 'main', level: LEVEL, sequenceDirections: ['long', 'short', 'long', 'long', 'short'], anchorCandleOpenMs: anchor, }); level5BetId = bet.id; }); (0, node_test_1.after)(async () => { if (level5BetId) { await (0, connection_1.query)('DELETE FROM candle_predict_bets WHERE id = $1', [level5BetId]); } if (level5RoundId) { await (0, connection_1.query)('DELETE FROM candle_predict_rounds WHERE id = $1', [level5RoundId]); } }); function openMsForStep(step) { return anchor + step * intervalMin * 60000; } for (const step of [0, 1, 2, 3]) { (0, node_test_1.test)(`step ${step + 1}/5 does not contribute to real-money pools`, async () => { await (0, connection_1.query)(`UPDATE candle_predict_bets SET sequence_step = $2 WHERE id = $1`, [ level5BetId, step, ]); const pools = await repo.getMultiCandlePoolContributions(openMsForStep(step)); strict_1.default.equal(pools.longPool, 0); strict_1.default.equal(pools.shortPool, 0); strict_1.default.equal((0, candlePredictMultiService_1.resolveMaturityOutcomeMode)({ realMoneySinglePending: false, multiFinalPoolShort: pools.shortPool, multiFinalPoolLong: pools.longPool, }), 'auto'); }); } (0, node_test_1.test)('step 5/5 contributes stake × payout multiplier to pools only at final candle', async () => { const finalStep = LEVEL - 1; await repo.upsertMultiLevel({ level: LEVEL, enabled: true, payoutMultiplier: 15, sortOrder: 4 }); await (0, connection_1.query)(`UPDATE candle_predict_bets SET sequence_step = $2 WHERE id = $1`, [ level5BetId, finalStep, ]); const finalOpenMs = openMsForStep(finalStep); const pools = await repo.getMultiCandlePoolContributions(finalOpenMs); strict_1.default.equal(pools.shortPool, (0, candlePredictMultiService_1.computeMultiCandlePoolContribution)(100, 15)); strict_1.default.equal(pools.longPool, 0); strict_1.default.equal((0, candlePredictMultiService_1.resolveMaturityOutcomeMode)({ realMoneySinglePending: false, multiFinalPoolShort: pools.shortPool, multiFinalPoolLong: pools.longPool, }), 'pool'); }); (0, node_test_1.test)('listDueMultiValidationCandleOpenMs includes in-progress validation candles', async () => { await (0, connection_1.query)(`UPDATE candle_predict_bets SET sequence_step = 1 WHERE id = $1`, [level5BetId]); const due = await repo.listDueMultiValidationCandleOpenMs(Date.now() + 86400000); strict_1.default.ok(due.includes(openMsForStep(1))); }); }); //# sourceMappingURL=candlePredictMultiMultiplier.db.test.js.map