File tree 3 files changed +41
-0
lines changed
3 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,9 @@ export const MARGIN_PRECISION = TEN_THOUSAND;
84
84
export const BID_ASK_SPREAD_PRECISION = new BN ( 1000000 ) ; // 10^6
85
85
export const LIQUIDATION_PCT_PRECISION = TEN_THOUSAND ;
86
86
export const FUNDING_RATE_OFFSET_DENOMINATOR = new BN ( 5000 ) ;
87
+ export const PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO = PRICE_PRECISION . mul (
88
+ AMM_TO_QUOTE_PRECISION_RATIO
89
+ ) ;
87
90
88
91
export const FIVE_MINUTE = new BN ( 60 * 5 ) ;
89
92
export const ONE_HOUR = new BN ( 60 * 60 ) ;
Original file line number Diff line number Diff line change @@ -58,6 +58,7 @@ export * from './math/amm';
58
58
export * from './math/trade' ;
59
59
export * from './math/orders' ;
60
60
export * from './math/repeg' ;
61
+ export * from './math/liquidation' ;
61
62
export * from './math/margin' ;
62
63
export * from './math/insurance' ;
63
64
export * from './math/superStake' ;
Original file line number Diff line number Diff line change
1
+ import { BN } from '@coral-xyz/anchor' ;
2
+ import {
3
+ PRICE_PRECISION ,
4
+ LIQUIDATION_FEE_PRECISION ,
5
+ MARGIN_PRECISION ,
6
+ PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO ,
7
+ } from '../constants/numericConstants' ;
8
+
9
+ export function calculateBaseAssetAmountToCoverMarginShortage (
10
+ marginShortage : BN ,
11
+ marginRatio : number ,
12
+ liquidationFee : number ,
13
+ ifLiquidationFee : number ,
14
+ oraclePrice : BN ,
15
+ quoteOraclePrice : BN
16
+ ) : BN | undefined {
17
+ const marginRatioBN = new BN ( marginRatio )
18
+ . mul ( LIQUIDATION_FEE_PRECISION )
19
+ . div ( MARGIN_PRECISION ) ;
20
+ const liquidationFeeBN = new BN ( liquidationFee ) ;
21
+
22
+ if ( oraclePrice . eq ( new BN ( 0 ) ) || marginRatioBN . lte ( liquidationFeeBN ) ) {
23
+ // undefined is max
24
+ return undefined ;
25
+ }
26
+
27
+ return marginShortage . mul ( PRICE_TIMES_AMM_TO_QUOTE_PRECISION_RATIO ) . div (
28
+ oraclePrice
29
+ . mul ( quoteOraclePrice )
30
+ . div ( PRICE_PRECISION )
31
+ . mul ( marginRatioBN . sub ( liquidationFeeBN ) )
32
+ . div ( LIQUIDATION_FEE_PRECISION )
33
+ . sub (
34
+ oraclePrice . mul ( new BN ( ifLiquidationFee ) ) . div ( LIQUIDATION_FEE_PRECISION )
35
+ )
36
+ ) ;
37
+ }
You can’t perform that action at this time.
0 commit comments