@@ -130,7 +130,7 @@ impl<T: Config> SwapStep<T> {
130130 } else {
131131 // Stop at limit price (refund needed)
132132 self . action = SwapStepAction :: StopIn ;
133- self . delta_in = Pallet :: < T > :: delta_in (
133+ self . delta_in = Self :: delta_in (
134134 self . order_type ,
135135 self . current_liquidity ,
136136 self . current_price ,
@@ -145,7 +145,7 @@ impl<T: Config> SwapStep<T> {
145145 if self . edge_quantity < self . lim_quantity {
146146 // Cross at edge price
147147 self . action = SwapStepAction :: Crossing ;
148- self . delta_in = Pallet :: < T > :: delta_in (
148+ self . delta_in = Self :: delta_in (
149149 self . order_type ,
150150 self . current_liquidity ,
151151 self . current_price ,
@@ -156,7 +156,7 @@ impl<T: Config> SwapStep<T> {
156156 } else if self . edge_quantity > self . lim_quantity {
157157 // Stop at limit price (refund needed)
158158 self . action = SwapStepAction :: StopIn ;
159- self . delta_in = Pallet :: < T > :: delta_in (
159+ self . delta_in = Self :: delta_in (
160160 self . order_type ,
161161 self . current_liquidity ,
162162 self . current_price ,
@@ -167,7 +167,7 @@ impl<T: Config> SwapStep<T> {
167167 } else {
168168 // Stop on edge (refund needed)
169169 self . action = SwapStepAction :: StopOn ;
170- self . delta_in = Pallet :: < T > :: delta_in (
170+ self . delta_in = Self :: delta_in (
171171 self . order_type ,
172172 self . current_liquidity ,
173173 self . current_price ,
@@ -181,7 +181,7 @@ impl<T: Config> SwapStep<T> {
181181 else {
182182 if self . target_quantity <= self . lim_quantity {
183183 // Stop on edge price
184- self . delta_in = Pallet :: < T > :: delta_in (
184+ self . delta_in = Self :: delta_in (
185185 self . order_type ,
186186 self . current_liquidity ,
187187 self . current_price ,
@@ -197,7 +197,7 @@ impl<T: Config> SwapStep<T> {
197197 } else {
198198 // Stop at limit price (refund needed)
199199 self . action = SwapStepAction :: StopIn ;
200- self . delta_in = Pallet :: < T > :: delta_in (
200+ self . delta_in = Self :: delta_in (
201201 self . order_type ,
202202 self . current_liquidity ,
203203 self . current_price ,
@@ -226,9 +226,6 @@ impl<T: Config> SwapStep<T> {
226226 Pallet :: < T > :: add_fees ( self . netuid , self . order_type , fee) ;
227227 let delta_out = Pallet :: < T > :: convert_deltas ( self . netuid , self . order_type , self . delta_in ) ;
228228
229- // TODO (look inside method)
230- // Self::update_reserves(netuid, order_type, self.delta_in, delta_out);
231-
232229 // Get current tick
233230 let current_tick_index = TickIndex :: current_bounded :: < T > ( self . netuid ) ;
234231
@@ -284,6 +281,27 @@ impl<T: Config> SwapStep<T> {
284281 delta_out,
285282 } )
286283 }
284+
285+ /// Get the input amount needed to reach the target price
286+ fn delta_in (
287+ order_type : OrderType ,
288+ liquidity_curr : U64F64 ,
289+ sqrt_price_curr : U64F64 ,
290+ sqrt_price_target : SqrtPrice ,
291+ ) -> u64 {
292+ let one = U64F64 :: saturating_from_num ( 1 ) ;
293+
294+ ( match order_type {
295+ OrderType :: Sell => liquidity_curr. saturating_mul (
296+ one. safe_div ( sqrt_price_target. into ( ) )
297+ . saturating_sub ( one. safe_div ( sqrt_price_curr) ) ,
298+ ) ,
299+ OrderType :: Buy => {
300+ liquidity_curr. saturating_mul ( sqrt_price_target. saturating_sub ( sqrt_price_curr) )
301+ }
302+ } )
303+ . saturating_to_num :: < u64 > ( )
304+ }
287305}
288306
289307impl < T : Config > Pallet < T > {
@@ -573,27 +591,6 @@ impl<T: Config> Pallet<T> {
573591 }
574592 }
575593
576- /// Get the input amount needed to reach the target price
577- fn delta_in (
578- order_type : OrderType ,
579- liquidity_curr : U64F64 ,
580- sqrt_price_curr : U64F64 ,
581- sqrt_price_target : SqrtPrice ,
582- ) -> u64 {
583- let one = U64F64 :: saturating_from_num ( 1 ) ;
584-
585- ( match order_type {
586- OrderType :: Sell => liquidity_curr. saturating_mul (
587- one. safe_div ( sqrt_price_target. into ( ) )
588- . saturating_sub ( one. safe_div ( sqrt_price_curr) ) ,
589- ) ,
590- OrderType :: Buy => {
591- liquidity_curr. saturating_mul ( sqrt_price_target. saturating_sub ( sqrt_price_curr) )
592- }
593- } )
594- . saturating_to_num :: < u64 > ( )
595- }
596-
597594 /// Update liquidity when crossing a tick
598595 fn update_liquidity_at_crossing ( netuid : NetUid , order_type : OrderType ) -> Result < ( ) , Error < T > > {
599596 let mut liquidity_curr = CurrentLiquidity :: < T > :: get ( netuid) ;
0 commit comments