148 lines
6.0 KiB
JavaScript
148 lines
6.0 KiB
JavaScript
"use strict";
|
||
/**
|
||
* Daily-revenue outcome strategy for candle-predict.
|
||
*
|
||
* Pipeline:
|
||
* 1. Optional shared pool neutrality (min/max tolerance, equal pools) — same as other modes.
|
||
* 2. Random directional candle (caller may apply the consecutive same-direction cap
|
||
* via resolveAutoNoBetOutcome before passing randomWinner).
|
||
* 3. Financial gate if the random pick is the majority side.
|
||
* 4. Overflow strategy when the daily revenue floor would be breached.
|
||
*/
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.pickRandomDirectionalOutcome = pickRandomDirectionalOutcome;
|
||
exports.resolveDailyRevenueOutcome = resolveDailyRevenueOutcome;
|
||
exports.projectedMajorityRoundPnl = projectedMajorityRoundPnl;
|
||
exports.projectMajorityWinSettlement = projectMajorityWinSettlement;
|
||
exports.usdtDisplayToRaw = usdtDisplayToRaw;
|
||
exports.usdtRawToDisplay = usdtRawToDisplay;
|
||
exports.utcRevenueDate = utcRevenueDate;
|
||
const candleGenerationService_1 = require("./candleGenerationService");
|
||
function directional(winner) {
|
||
return winner === 'short'
|
||
? { type: 'short_win', winner: 'short', neutralColor: null }
|
||
: { type: 'long_win', winner: 'long', neutralColor: null };
|
||
}
|
||
function neutralFromPools(shortPool, longPool) {
|
||
let neutralColor = 'green';
|
||
if (shortPool < longPool)
|
||
neutralColor = 'red';
|
||
else if (longPool < shortPool)
|
||
neutralColor = 'green';
|
||
return { type: 'neutral', winner: null, neutralColor };
|
||
}
|
||
function majoritySide(shortPool, longPool) {
|
||
if (shortPool === longPool)
|
||
return null;
|
||
return shortPool > longPool ? 'short' : 'long';
|
||
}
|
||
function minoritySide(shortPool, longPool) {
|
||
if (shortPool === longPool)
|
||
return null;
|
||
return shortPool < longPool ? 'short' : 'long';
|
||
}
|
||
/** 50/50 random directional candle outcome. */
|
||
function pickRandomDirectionalOutcome(random = Math.random) {
|
||
return random() < 0.5 ? directional('short') : directional('long');
|
||
}
|
||
/**
|
||
* Resolve round outcome for daily-revenue mode.
|
||
*
|
||
* 1. Optional neutrality pre-filter (same rules as majority/minority).
|
||
* 2. Start from `randomWinner`.
|
||
* 3. If that side is not the majority pool, keep it.
|
||
* 4. If it is the majority, keep only when
|
||
* `dailyNetRaw + projectedMajorityRoundPnlRaw >= targetRaw`.
|
||
* 5. Otherwise apply overflow: force minority win or neutral.
|
||
*/
|
||
function resolveDailyRevenueOutcome(input) {
|
||
const { shortPool, longPool, randomWinner, projectedMajorityRoundPnlRaw, dailyNetRaw, targetRaw, overflowStrategy, neutralityRules, } = input;
|
||
if (neutralityRules) {
|
||
const neutral = (0, candleGenerationService_1.resolvePoolNeutrality)(shortPool, longPool, neutralityRules);
|
||
if (neutral)
|
||
return neutral;
|
||
}
|
||
if (shortPool + longPool <= 0) {
|
||
return { type: 'neutral', winner: null, neutralColor: 'green' };
|
||
}
|
||
const majority = majoritySide(shortPool, longPool);
|
||
if (majority == null || randomWinner !== majority) {
|
||
return directional(randomWinner);
|
||
}
|
||
const netAfter = dailyNetRaw + projectedMajorityRoundPnlRaw;
|
||
if (netAfter >= targetRaw) {
|
||
return directional(randomWinner);
|
||
}
|
||
if (overflowStrategy === 'neutral') {
|
||
return neutralFromPools(shortPool, longPool);
|
||
}
|
||
const minority = minoritySide(shortPool, longPool);
|
||
// Solo pool (empty minority): forcing minority still yields that empty side as winner.
|
||
return directional(minority ?? (majority === 'short' ? 'long' : 'short'));
|
||
}
|
||
/** House PnL if majority wins: totalSettlingStakeRaw − majorityPayoutRaw. */
|
||
function projectedMajorityRoundPnl(totalSettlingStakeRaw, majorityPayoutRaw) {
|
||
return totalSettlingStakeRaw - majorityPayoutRaw;
|
||
}
|
||
const STAKE_DECIMALS = 6;
|
||
function isRealMoney(kind) {
|
||
return kind === 'main' || kind === 'bonus' || kind == null;
|
||
}
|
||
/**
|
||
* Project house PnL if `majority` wins this maturity.
|
||
* Counts only real-money bets that would reach a terminal status (won/lost/refunded).
|
||
*/
|
||
function projectMajorityWinSettlement(input) {
|
||
let totalStakeRaw = 0n;
|
||
let majorityPayoutRaw = 0n;
|
||
const { majority } = input;
|
||
for (const bet of input.singles) {
|
||
if (!isRealMoney(bet.stake_account_kind))
|
||
continue;
|
||
const stake = BigInt(bet.stake_raw);
|
||
totalStakeRaw += stake;
|
||
if (bet.direction === majority) {
|
||
const mult = input.singlePayoutMultiplier(bet.interval_minutes);
|
||
majorityPayoutRaw += BigInt(Math.round(Number(stake) * mult));
|
||
}
|
||
}
|
||
for (const bet of input.multis) {
|
||
if (!isRealMoney(bet.stake_account_kind))
|
||
continue;
|
||
const sequence = bet.sequence_directions ?? [];
|
||
const level = bet.multi_level ?? sequence.length;
|
||
const step = bet.sequence_step ?? 0;
|
||
const expected = sequence[step];
|
||
const stake = BigInt(bet.stake_raw);
|
||
const isFinal = step >= level - 1;
|
||
if (expected === majority) {
|
||
if (isFinal) {
|
||
totalStakeRaw += stake;
|
||
const mult = input.multiPayoutMultiplier(level);
|
||
const displayPayout = Number(bet.stake_display) * mult;
|
||
majorityPayoutRaw += BigInt(Math.round(displayPayout * 10 ** STAKE_DECIMALS));
|
||
}
|
||
// Intermediate correct step: bet continues — no terminal PnL yet.
|
||
continue;
|
||
}
|
||
// Wrong direction (or missing expected): bet is lost at this candle.
|
||
totalStakeRaw += stake;
|
||
}
|
||
return {
|
||
totalStakeRaw,
|
||
majorityPayoutRaw,
|
||
roundPnlRaw: projectedMajorityRoundPnl(totalStakeRaw, majorityPayoutRaw),
|
||
};
|
||
}
|
||
/** Convert USDT display amount to ledger raw (6 decimals). */
|
||
function usdtDisplayToRaw(display) {
|
||
return BigInt(Math.round(display * 10 ** STAKE_DECIMALS));
|
||
}
|
||
function usdtRawToDisplay(raw) {
|
||
return Number(raw) / 10 ** STAKE_DECIMALS;
|
||
}
|
||
/** UTC calendar date `YYYY-MM-DD` for daily revenue bucketing. */
|
||
function utcRevenueDate(now = new Date()) {
|
||
return now.toISOString().slice(0, 10);
|
||
}
|
||
//# sourceMappingURL=dailyRevenueOutcomeService.js.map
|