@@ -389,6 +389,31 @@ export function calculateInventoryLiquidityRatio(
389389 return inventoryScaleBN ;
390390}
391391
392+ export function calculateInventoryLiquidityRatioForReferencePriceOffset (
393+ baseAssetAmountWithAmm : BN ,
394+ baseAssetReserve : BN ,
395+ minBaseAssetReserve : BN ,
396+ maxBaseAssetReserve : BN
397+ ) : BN {
398+ // inventory skew
399+ const [ openBids , openAsks ] = calculateMarketOpenBidAsk (
400+ baseAssetReserve ,
401+ minBaseAssetReserve ,
402+ maxBaseAssetReserve
403+ ) ;
404+
405+ const minSideLiquidity = openBids . abs ( ) . add ( openAsks . abs ( ) ) . div ( TWO ) ;
406+
407+ const inventoryScaleBN = BN . min (
408+ baseAssetAmountWithAmm
409+ . mul ( PERCENTAGE_PRECISION )
410+ . div ( BN . max ( minSideLiquidity , ONE ) )
411+ . abs ( ) ,
412+ PERCENTAGE_PRECISION
413+ ) ;
414+ return inventoryScaleBN ;
415+ }
416+
392417export function calculateInventoryScale (
393418 baseAssetAmountWithAmm : BN ,
394419 baseAssetReserve : BN ,
@@ -440,7 +465,7 @@ export function calculateReferencePriceOffset(
440465 markTwapSlow : BN ,
441466 maxOffsetPct : number
442467) : BN {
443- if ( last24hAvgFundingRate . eq ( ZERO ) ) {
468+ if ( last24hAvgFundingRate . eq ( ZERO ) || liquidityFraction . eq ( ZERO ) ) {
444469 return ZERO ;
445470 }
446471
@@ -1013,19 +1038,38 @@ export function calculateSpreadReserves(
10131038 ( amm . curveUpdateIntensity - 100 )
10141039 ) ;
10151040
1016- const liquidityFraction = calculateInventoryLiquidityRatio (
1017- amm . baseAssetAmountWithAmm ,
1018- amm . baseAssetReserve ,
1019- amm . minBaseAssetReserve ,
1020- amm . maxBaseAssetReserve
1021- ) ;
1041+ const liquidityFraction =
1042+ calculateInventoryLiquidityRatioForReferencePriceOffset (
1043+ amm . baseAssetAmountWithAmm ,
1044+ amm . baseAssetReserve ,
1045+ amm . minBaseAssetReserve ,
1046+ amm . maxBaseAssetReserve
1047+ ) ;
10221048 const liquidityFractionSigned = liquidityFraction . mul (
10231049 sigNum ( amm . baseAssetAmountWithAmm . add ( amm . baseAssetAmountWithUnsettledLp ) )
10241050 ) ;
1051+
1052+ let liquidityFractionAfterDeadband = liquidityFractionSigned ;
1053+ const deadbandPct = amm . referencePriceOffsetDeadbandPct
1054+ ? PERCENTAGE_PRECISION . mul (
1055+ new BN ( amm . referencePriceOffsetDeadbandPct as number )
1056+ ) . divn ( 100 )
1057+ : ZERO ;
1058+ if ( ! liquidityFractionAfterDeadband . eq ( ZERO ) && deadbandPct . gt ( ZERO ) ) {
1059+ const abs = liquidityFractionAfterDeadband . abs ( ) ;
1060+ if ( abs . lte ( deadbandPct ) ) {
1061+ liquidityFractionAfterDeadband = ZERO ;
1062+ } else {
1063+ liquidityFractionAfterDeadband = liquidityFractionAfterDeadband . sub (
1064+ deadbandPct . mul ( sigNum ( liquidityFractionAfterDeadband ) )
1065+ ) ;
1066+ }
1067+ }
1068+
10251069 referencePriceOffset = calculateReferencePriceOffset (
10261070 reservePrice ,
10271071 amm . last24HAvgFundingRate ,
1028- liquidityFractionSigned ,
1072+ liquidityFractionAfterDeadband ,
10291073 amm . historicalOracleData . lastOraclePriceTwap5Min ,
10301074 amm . lastMarkPriceTwap5Min ,
10311075 amm . historicalOracleData . lastOraclePriceTwap ,
0 commit comments