@@ -370,6 +370,75 @@ pub fn increase_liquidity_quote_b(
370370 )
371371}
372372
373+ /// Calculate the estimated token amounts for a given liquidity delta and price range
374+ ///
375+ /// # Parameters
376+ /// - `liquidity_delta` - The amount of liquidity to get token estimates for
377+ /// - `current_sqrt_price` - The current sqrt price of the pool
378+ /// - `tick_lower_index` - The lower tick index of the range
379+ /// - `tick_upper_index` - The upper tick index of the range
380+ /// - `round_up` - Whether to round the token amounts up
381+ ///
382+ /// # Returns
383+ /// - A tuple containing the estimated amounts of token A and token B
384+ pub fn try_get_token_estimates_from_liquidity (
385+ liquidity_delta : u128 ,
386+ current_sqrt_price : u128 ,
387+ tick_lower_index : i32 ,
388+ tick_upper_index : i32 ,
389+ round_up : bool ,
390+ ) -> Result < ( u64 , u64 ) , CoreError > {
391+ if liquidity_delta == 0 {
392+ return Ok ( ( 0 , 0 ) ) ;
393+ }
394+
395+ let sqrt_price_lower = tick_index_to_sqrt_price ( tick_lower_index) . into ( ) ;
396+ let sqrt_price_upper = tick_index_to_sqrt_price ( tick_upper_index) . into ( ) ;
397+
398+ let position_status = position_status (
399+ current_sqrt_price. into ( ) ,
400+ tick_lower_index,
401+ tick_upper_index,
402+ ) ;
403+
404+ match position_status {
405+ PositionStatus :: PriceBelowRange => {
406+ let token_a = try_get_token_a_from_liquidity (
407+ liquidity_delta,
408+ sqrt_price_lower,
409+ sqrt_price_upper,
410+ round_up,
411+ ) ?;
412+ Ok ( ( token_a, 0 ) )
413+ }
414+ PositionStatus :: PriceInRange => {
415+ let token_a = try_get_token_a_from_liquidity (
416+ liquidity_delta,
417+ current_sqrt_price,
418+ sqrt_price_upper,
419+ round_up,
420+ ) ?;
421+ let token_b = try_get_token_b_from_liquidity (
422+ liquidity_delta,
423+ sqrt_price_lower,
424+ current_sqrt_price,
425+ round_up,
426+ ) ?;
427+ Ok ( ( token_a, token_b) )
428+ }
429+ PositionStatus :: PriceAboveRange => {
430+ let token_b = try_get_token_b_from_liquidity (
431+ liquidity_delta,
432+ sqrt_price_lower,
433+ sqrt_price_upper,
434+ round_up,
435+ ) ?;
436+ Ok ( ( 0 , token_b) )
437+ }
438+ PositionStatus :: Invalid => Ok ( ( 0 , 0 ) ) ,
439+ }
440+ }
441+
373442// Private functions
374443
375444fn try_get_liquidity_from_a (
@@ -444,64 +513,6 @@ fn try_get_token_b_from_liquidity(
444513 }
445514}
446515
447- fn try_get_token_estimates_from_liquidity (
448- liquidity_delta : u128 ,
449- current_sqrt_price : u128 ,
450- tick_lower_index : i32 ,
451- tick_upper_index : i32 ,
452- round_up : bool ,
453- ) -> Result < ( u64 , u64 ) , CoreError > {
454- if liquidity_delta == 0 {
455- return Ok ( ( 0 , 0 ) ) ;
456- }
457-
458- let sqrt_price_lower = tick_index_to_sqrt_price ( tick_lower_index) . into ( ) ;
459- let sqrt_price_upper = tick_index_to_sqrt_price ( tick_upper_index) . into ( ) ;
460-
461- let position_status = position_status (
462- current_sqrt_price. into ( ) ,
463- tick_lower_index,
464- tick_upper_index,
465- ) ;
466-
467- match position_status {
468- PositionStatus :: PriceBelowRange => {
469- let token_a = try_get_token_a_from_liquidity (
470- liquidity_delta,
471- sqrt_price_lower,
472- sqrt_price_upper,
473- round_up,
474- ) ?;
475- Ok ( ( token_a, 0 ) )
476- }
477- PositionStatus :: PriceInRange => {
478- let token_a = try_get_token_a_from_liquidity (
479- liquidity_delta,
480- current_sqrt_price,
481- sqrt_price_upper,
482- round_up,
483- ) ?;
484- let token_b = try_get_token_b_from_liquidity (
485- liquidity_delta,
486- sqrt_price_lower,
487- current_sqrt_price,
488- round_up,
489- ) ?;
490- Ok ( ( token_a, token_b) )
491- }
492- PositionStatus :: PriceAboveRange => {
493- let token_b = try_get_token_b_from_liquidity (
494- liquidity_delta,
495- sqrt_price_lower,
496- sqrt_price_upper,
497- round_up,
498- ) ?;
499- Ok ( ( 0 , token_b) )
500- }
501- PositionStatus :: Invalid => Ok ( ( 0 , 0 ) ) ,
502- }
503- }
504-
505516#[ cfg( all( test, not( feature = "wasm" ) ) ) ]
506517mod tests {
507518 use super :: * ;
0 commit comments