@@ -197,14 +197,16 @@ export function calculateUpdatedAMMSpreadReserves(
197197 amm : AMM ,
198198 direction : PositionDirection ,
199199 mmOraclePriceData : MMOraclePriceData ,
200- isPrediction = false
200+ isPrediction = false ,
201+ latestSlot ?: BN
201202) : { baseAssetReserve : BN ; quoteAssetReserve : BN ; sqrtK : BN ; newPeg : BN } {
202203 const newAmm = calculateUpdatedAMM ( amm , mmOraclePriceData ) ;
203204 const [ shortReserves , longReserves ] = calculateSpreadReserves (
204205 newAmm ,
205206 mmOraclePriceData ,
206207 undefined ,
207- isPrediction
208+ isPrediction ,
209+ latestSlot
208210 ) ;
209211
210212 const dirReserves = isVariant ( direction , 'long' )
@@ -225,7 +227,8 @@ export function calculateBidAskPrice(
225227 amm : AMM ,
226228 mmOraclePriceData : MMOraclePriceData ,
227229 withUpdate = true ,
228- isPrediction = false
230+ isPrediction = false ,
231+ latestSlot ?: BN
229232) : [ BN , BN ] {
230233 let newAmm : AMM ;
231234 if ( withUpdate ) {
@@ -238,7 +241,8 @@ export function calculateBidAskPrice(
238241 newAmm ,
239242 mmOraclePriceData ,
240243 undefined ,
241- isPrediction
244+ isPrediction ,
245+ latestSlot
242246 ) ;
243247
244248 const askPrice = calculatePrice (
@@ -939,7 +943,8 @@ export function calculateSpreadReserves(
939943 amm : AMM ,
940944 mmOraclePriceData : MMOraclePriceData ,
941945 now ?: BN ,
942- isPrediction = false
946+ isPrediction = false ,
947+ latestSlot ?: BN
943948) {
944949 function calculateSpreadReserve (
945950 spread : number ,
@@ -1041,28 +1046,28 @@ export function calculateSpreadReserves(
10411046 amm . curveUpdateIntensity > 100 ;
10421047
10431048 if ( doReferencePricOffsetSmooth ) {
1044- if ( mmOraclePriceData . slot !== amm . lastUpdateSlot ) {
1045- const slotsPassed =
1046- mmOraclePriceData . slot . toNumber ( ) - amm . lastUpdateSlot . toNumber ( ) ;
1047- const fullOffsetDelta = referencePriceOffset - amm . referencePriceOffset ;
1048- const raw = Math . trunc (
1049- Math . min ( Math . abs ( fullOffsetDelta ) , slotsPassed * 1000 ) / 10
1050- ) ;
1051- const maxAllowed =
1052- Math . abs ( amm . referencePriceOffset ) || Math . abs ( referencePriceOffset ) ;
1049+ const slotsPassed =
1050+ latestSlot != null
1051+ ? BN . max ( latestSlot . sub ( amm . lastUpdateSlot ) , ZERO ) . toNumber ( )
1052+ : 0 ;
1053+ const fullOffsetDelta = referencePriceOffset - amm . referencePriceOffset ;
1054+ const raw = Math . trunc (
1055+ Math . min ( Math . abs ( fullOffsetDelta ) , slotsPassed * 1000 ) / 10
1056+ ) ;
1057+ const maxAllowed =
1058+ Math . abs ( amm . referencePriceOffset ) || Math . abs ( referencePriceOffset ) ;
10531059
1054- const magnitude = Math . min ( Math . max ( raw , 10 ) , maxAllowed ) ;
1055- const referencePriceDelta = Math . sign ( fullOffsetDelta ) * magnitude ;
1060+ const magnitude = Math . min ( Math . max ( raw , 10 ) , maxAllowed ) ;
1061+ const referencePriceDelta = Math . sign ( fullOffsetDelta ) * magnitude ;
10561062
1057- referencePriceOffset = amm . referencePriceOffset + referencePriceDelta ;
1063+ referencePriceOffset = amm . referencePriceOffset + referencePriceDelta ;
10581064
1059- if ( referencePriceDelta < 0 ) {
1060- longSpread += Math . abs ( referencePriceDelta ) ;
1061- shortSpread += Math . abs ( referencePriceOffset ) ;
1062- } else {
1063- shortSpread += Math . abs ( referencePriceDelta ) ;
1064- longSpread += Math . abs ( referencePriceOffset ) ;
1065- }
1065+ if ( referencePriceDelta < 0 ) {
1066+ longSpread += Math . abs ( referencePriceDelta ) ;
1067+ shortSpread += Math . abs ( referencePriceOffset ) ;
1068+ } else {
1069+ shortSpread += Math . abs ( referencePriceDelta ) ;
1070+ longSpread += Math . abs ( referencePriceOffset ) ;
10661071 }
10671072 }
10681073
0 commit comments