|
| 1 | +import { USDT_REBALANCE_TARGET_PERCENTS } from '../../../src/binance-orderbook-trade/config.js'; |
| 2 | + |
| 3 | +const SCALE = 8; |
| 4 | +const ACCOUNTS = ['FUNDING', 'MAIN', 'UMFUTURE']; |
| 5 | + |
| 6 | +/** |
| 7 | + * Fixture balances follow the shipped allocation so every scenario keeps its transfer |
| 8 | + * shape for any valid configuration. A 200 USDT total gives each account an integer |
| 9 | + * target of at least 2 USDT, which leaves room for whole-unit perturbations. |
| 10 | + */ |
| 11 | +export const REBALANCE_TOTAL = 200; |
| 12 | + |
| 13 | +function units(value) { |
| 14 | + const text = String(value); |
| 15 | + const negative = text.startsWith('-'); |
| 16 | + const [whole, fraction = ''] = (negative ? text.slice(1) : text).split('.'); |
| 17 | + const magnitude = BigInt(whole) * 10n ** BigInt(SCALE) + BigInt(fraction.padEnd(SCALE, '0')); |
| 18 | + return negative ? -magnitude : magnitude; |
| 19 | +} |
| 20 | + |
| 21 | +function amount(value) { |
| 22 | + const sign = value < 0n ? '-' : ''; |
| 23 | + const absolute = value < 0n ? -value : value; |
| 24 | + const whole = absolute / 10n ** BigInt(SCALE); |
| 25 | + const fraction = (absolute % 10n ** BigInt(SCALE)).toString().padStart(SCALE, '0').replace(/0+$/, ''); |
| 26 | + return sign + whole + (fraction ? '.' + fraction : ''); |
| 27 | +} |
| 28 | + |
| 29 | +export const REBALANCE_TARGET = Object.freeze(Object.fromEntries(ACCOUNTS.map(account => [ |
| 30 | + account, String(USDT_REBALANCE_TARGET_PERCENTS[account] * REBALANCE_TOTAL / 100), |
| 31 | +]))); |
| 32 | + |
| 33 | +export const REBALANCE_PERCENTS = USDT_REBALANCE_TARGET_PERCENTS; |
| 34 | + |
| 35 | +const gcd = (left, right) => (right === 0 ? left : gcd(right, left % right)); |
| 36 | +const RATIO_DIVISOR = ACCOUNTS.reduce((value, account) => gcd(value, USDT_REBALANCE_TARGET_PERCENTS[account]), 0); |
| 37 | +/** Reduced Funding:Spot:Futures label the panel must show, derived without production code. */ |
| 38 | +export const REBALANCE_RATIO = ACCOUNTS.map(account => USDT_REBALANCE_TARGET_PERCENTS[account] / RATIO_DIVISOR).join(':'); |
| 39 | + |
| 40 | +/** An allocation that is still exact after every balance is multiplied by a whole factor. */ |
| 41 | +export function scaledTarget(factor) { |
| 42 | + return Object.fromEntries(ACCOUNTS.map(account => [account, String(Number(REBALANCE_TARGET[account]) * factor)])); |
| 43 | +} |
| 44 | + |
| 45 | +/** Shift the exact targets by known amounts; the shifts must conserve the total. */ |
| 46 | +export function shiftedFromTarget(shifts) { |
| 47 | + const balances = Object.fromEntries(ACCOUNTS.map(account => [ |
| 48 | + account, units(REBALANCE_TARGET[account]) + units(shifts[account] ?? '0'), |
| 49 | + ])); |
| 50 | + if (ACCOUNTS.reduce((sum, account) => sum + balances[account], 0n) !== units(REBALANCE_TOTAL)) { |
| 51 | + throw new Error('Fixture shifts must conserve the rebalance total'); |
| 52 | + } |
| 53 | + if (ACCOUNTS.some(account => balances[account] < 0n)) throw new Error('Fixture balance cannot be negative'); |
| 54 | + return Object.fromEntries(ACCOUNTS.map(account => [account, amount(balances[account])])); |
| 55 | +} |
| 56 | + |
| 57 | +/** All USDT starts in one account, as on a fresh deposit or a full Futures withdrawal. */ |
| 58 | +export function allIn(account) { |
| 59 | + return Object.fromEntries(ACCOUNTS.map(code => [code, code === account ? String(REBALANCE_TOTAL) : '0'])); |
| 60 | +} |
| 61 | + |
| 62 | +export function transferBody(amountValue, kindType) { |
| 63 | + return { asset: 'USDT', amount: amount(units(amountValue)), kindType }; |
| 64 | +} |
0 commit comments