fix: liquidation est. should update on price change (#2649)

* fix: liquidation close out price needs to update when price updates on limit orders

* fix(#2624): add test case on limit sell order
This commit is contained in:
m.ray 2023-01-17 11:31:39 -05:00 committed by GitHub
parent 2640b7d77d
commit 2a9f3c7568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 4 deletions

View File

@ -74,6 +74,26 @@ describe('useOrderCloseOut', () => {
expect(result.current).toEqual('1');
});
it('should return proper sell value on limit order', () => {
mockMarketMargin = '0';
const { result } = renderHook(
() =>
useOrderCloseOut({
order: {
...order,
price: '1000000',
type: 'TYPE_LIMIT',
side: 'SIDE_SELL',
} as OrderSubmissionBody['orderSubmission'],
market: market,
}),
{
wrapper: MockedProvider,
}
);
expect(result.current).toEqual('1000000');
});
it('should return proper empty value', () => {
const { result } = renderHook(
() =>

View File

@ -40,14 +40,17 @@ export const useOrderCloseOut = ({ order, market }: Props): string | null => {
const volume = new BigNumber(
addDecimal(openVolume || '0', market.positionDecimalPlaces)
)[order.side === Schema.Side.SIDE_BUY ? 'plus' : 'minus'](order.size);
const markPrice = new BigNumber(
addDecimal(market.data.markPrice || 0, market.decimalPlaces || 0)
);
const price =
order.type === Schema.OrderType.TYPE_LIMIT && order.price
? new BigNumber(order.price)
: new BigNumber(
addDecimal(market.data.markPrice || 0, market.decimalPlaces || 0)
);
// regarding formula (marginMaintenanceLevel - positionAccountBalance - generalAccountBalance) / volume + markPrice
const marginDifference = marginMaintenanceLevel
.minus(positionAccountBalance)
.minus(generalAccountBalance);
const closeOut = marginDifference.div(volume).plus(markPrice);
const closeOut = marginDifference.div(volume).plus(price);
if (closeOut.isPositive()) {
return closeOut.toString();
}