53 lines
1.7 KiB
TypeScript
53 lines
1.7 KiB
TypeScript
import { describe, test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
|
|
// Mirror of prediqo_web withdrawEligibility (kept in sync for backend-facing docs/tests).
|
|
function computeWithdrawEligibility(input: {
|
|
withdrawalsEnabled: boolean;
|
|
hasWithdrawNetwork: boolean;
|
|
availableRaw: string;
|
|
feeRaw: string;
|
|
}) {
|
|
const availableRaw = BigInt(input.availableRaw || '0');
|
|
const feeRaw = BigInt(input.feeRaw || '0');
|
|
const maxReceivableRaw = availableRaw > feeRaw ? availableRaw - feeRaw : 0n;
|
|
if (!input.withdrawalsEnabled) return { allowed: false, reason: 'global_off' as const };
|
|
if (!input.hasWithdrawNetwork) return { allowed: false, reason: 'no_network' as const };
|
|
if (maxReceivableRaw <= 0n) return { allowed: false, reason: 'insufficient_for_fee' as const };
|
|
return { allowed: true, reason: null };
|
|
}
|
|
|
|
describe('withdraw eligibility rules', () => {
|
|
test('blocks when global kill-switch off', () => {
|
|
const r = computeWithdrawEligibility({
|
|
withdrawalsEnabled: false,
|
|
hasWithdrawNetwork: true,
|
|
availableRaw: '5000000',
|
|
feeRaw: '500000',
|
|
});
|
|
assert.equal(r.allowed, false);
|
|
assert.equal(r.reason, 'global_off');
|
|
});
|
|
|
|
test('blocks when available equals fee only', () => {
|
|
const r = computeWithdrawEligibility({
|
|
withdrawalsEnabled: true,
|
|
hasWithdrawNetwork: true,
|
|
availableRaw: '500000',
|
|
feeRaw: '500000',
|
|
});
|
|
assert.equal(r.allowed, false);
|
|
assert.equal(r.reason, 'insufficient_for_fee');
|
|
});
|
|
|
|
test('allows when available covers fee + dust', () => {
|
|
const r = computeWithdrawEligibility({
|
|
withdrawalsEnabled: true,
|
|
hasWithdrawNetwork: true,
|
|
availableRaw: '500001',
|
|
feeRaw: '500000',
|
|
});
|
|
assert.equal(r.allowed, true);
|
|
});
|
|
});
|