games_app/prediqo_web/src/features/wallet/utils/withdrawEligibility.ts
2026-08-26 14:24:35 +02:00

53 lines
1.5 KiB
TypeScript

/**
* Shared withdraw eligibility for wallet UI (global kill-switch, rails, fee vs balance).
*/
export type WithdrawBlockReason =
| 'global_off'
| 'no_network'
| 'insufficient_for_fee'
| null;
export type WithdrawEligibility = {
allowed: boolean;
reason: WithdrawBlockReason;
/** Max amount user can receive (available - fee), ledger raw micro-USDT. */
maxReceivableRaw: bigint;
feeRaw: bigint;
availableRaw: bigint;
};
function parseRaw(v: string | null | undefined): bigint {
try {
return BigInt(v ?? '0');
} catch {
return 0n;
}
}
/**
* Withdraw is possible only if global on, ≥1 rail enabled, and available > fee
* (so at least 1 micro-USDT can be received after the ledger fee).
*/
export function computeWithdrawEligibility(input: {
withdrawalsEnabled: boolean;
hasWithdrawNetwork: boolean;
availableRaw: string;
feeRaw: string;
}): WithdrawEligibility {
const availableRaw = parseRaw(input.availableRaw);
const feeRaw = parseRaw(input.feeRaw);
const maxReceivableRaw = availableRaw > feeRaw ? availableRaw - feeRaw : 0n;
if (!input.withdrawalsEnabled) {
return { allowed: false, reason: 'global_off', maxReceivableRaw, feeRaw, availableRaw };
}
if (!input.hasWithdrawNetwork) {
return { allowed: false, reason: 'no_network', maxReceivableRaw, feeRaw, availableRaw };
}
if (maxReceivableRaw <= 0n) {
return { allowed: false, reason: 'insufficient_for_fee', maxReceivableRaw, feeRaw, availableRaw };
}
return { allowed: true, reason: null, maxReceivableRaw, feeRaw, availableRaw };
}