fix(execution): include unpaid fees in funding checks

This commit is contained in:
Boki 2026-10-22 14:17:25 -04:00
parent 74ba3b97f5
commit a21a1b8cb8
3 changed files with 248 additions and 19 deletions

View file

@ -5,6 +5,7 @@ import {
amount,
applyFill,
decimal,
fillPrice,
fingerprint,
riskViolations,
terminal,
@ -101,10 +102,17 @@ export class SimulationExecution {
quote.timestamp > timestamp
? 'Missing, stale, or future market quote'
: undefined;
const price = quote && !invalid ? this.fillPrice(order, quote) : 0;
const price = quote && !invalid ? fillPrice(order, quote) : 0;
const violations = invalid
? [invalid]
: riskViolations(tx.account, request, await tx.openOrders(), price, timestamp);
: riskViolations(
tx.account,
request,
await tx.openOrders(),
price,
timestamp,
order.policy.commission
);
if (violations.length) {
order.report.status = 'rejected';
order.report.rejectionReason = violations.join('; ');
@ -119,19 +127,6 @@ export class SimulationExecution {
await this.journalChange(tx, change);
return change;
}
private fillPrice(order: StoredOrder, quote: Quote) {
const raw =
order.request.side === 'buy'
? quote.ask * (1 + order.policy.slippage)
: quote.bid * (1 - order.policy.slippage);
return amount(
order.request.orderType === 'limit'
? order.request.side === 'buy'
? Math.min(raw, defined(order.request.limitPrice))
: Math.max(raw, defined(order.request.limitPrice))
: raw
);
}
async fill(
tx: ExecutionTransaction,
order: StoredOrder,
@ -166,13 +161,14 @@ export class SimulationExecution {
}
return [];
}
const price = this.fillPrice(order, quote);
const price = fillPrice(order, quote);
const violations = riskViolations(
tx.account,
{ ...request, quantity: remaining },
reservations.filter(o => o.report.orderId !== order.report.orderId),
price,
this.clock.now(quote)
this.clock.now(quote),
order.policy.commission
);
if (violations.length) {
order.report.status = 'cancelled';