@@ -7,7 +7,7 @@ use sp_arithmetic::helpers_128bit;
77use sp_runtime:: traits:: AccountIdConversion ;
88use substrate_fixed:: types:: { U64F64 , U96F32 } ;
99use subtensor_swap_interface:: {
10- LiquidityDataProvider , UpdateLiquidityResult , SwapHandler , SwapResult ,
10+ LiquidityDataProvider , SwapHandler , SwapResult , UpdateLiquidityResult ,
1111} ;
1212
1313use super :: pallet:: * ;
@@ -90,14 +90,14 @@ impl<T: Config> SwapStep<T> {
9090 lq = one. safe_div ( TickIndex :: min_sqrt_price ( ) ) ;
9191 }
9292 lq
93- } ,
93+ }
9494 OrderType :: Buy => {
9595 let mut lq = TickIndex :: max_sqrt_price ( ) . min ( sqrt_price_limit. into ( ) ) ;
9696 if lq < current_price {
9797 lq = TickIndex :: max_sqrt_price ( ) ;
9898 }
9999 lq
100- } ,
100+ }
101101 } ;
102102
103103 Self {
@@ -228,10 +228,8 @@ impl<T: Config> SwapStep<T> {
228228 delta_fixed. saturating_mul ( u16_max. safe_div ( u16_max. saturating_sub ( fee_rate) ) ) ;
229229
230230 // Hold the fees
231- let fee = Pallet :: < T > :: calculate_fee_amount (
232- self . netuid ,
233- total_cost. saturating_to_num :: < u64 > ( ) ,
234- ) ;
231+ let fee =
232+ Pallet :: < T > :: calculate_fee_amount ( self . netuid , total_cost. saturating_to_num :: < u64 > ( ) ) ;
235233 Pallet :: < T > :: add_fees ( self . netuid , self . order_type , fee) ;
236234 let delta_out = Pallet :: < T > :: convert_deltas ( self . netuid , self . order_type , self . delta_in ) ;
237235
@@ -336,14 +334,11 @@ impl<T: Config> Pallet<T> {
336334 let epsilon = U64F64 :: saturating_from_num ( 0.000001 ) ;
337335
338336 let current_sqrt_price = price. checked_sqrt ( epsilon) . unwrap_or ( U64F64 :: from_num ( 0 ) ) ;
339- AlphaSqrtPrice :: < T > :: set (
340- netuid,
341- current_sqrt_price,
342- ) ;
337+ AlphaSqrtPrice :: < T > :: set ( netuid, current_sqrt_price) ;
343338
344339 // Set current tick
345340 let current_tick = TickIndex :: from_sqrt_price_bounded ( current_sqrt_price) ;
346- CurrentTick :: < T > :: set ( netuid, current_tick) ;
341+ CurrentTick :: < T > :: set ( netuid, current_tick) ;
347342
348343 // Set initial (protocol owned) liquidity and positions
349344 // Protocol liquidity makes one position from TickIndex::MIN to TickIndex::MAX
@@ -1174,8 +1169,14 @@ impl<T: Config> SwapHandler<T::AccountId> for Pallet<T> {
11741169 position_id : u128 ,
11751170 liquidity_delta : i64 ,
11761171 ) -> Result < UpdateLiquidityResult , DispatchError > {
1177- Self :: modify_position ( netuid. into ( ) , coldkey_account_id, hotkey_account_id, position_id. into ( ) , liquidity_delta)
1178- . map_err ( Into :: into)
1172+ Self :: modify_position (
1173+ netuid. into ( ) ,
1174+ coldkey_account_id,
1175+ hotkey_account_id,
1176+ position_id. into ( ) ,
1177+ liquidity_delta,
1178+ )
1179+ . map_err ( Into :: into)
11791180 }
11801181
11811182 fn approx_fee_amount ( netuid : u16 , amount : u64 ) -> u64 {
@@ -1298,7 +1299,7 @@ mod tests {
12981299 assert_eq ! ( sqrt_price, expected_sqrt_price) ;
12991300
13001301 // Verify that current tick is set
1301- let current_tick = CurrentTick :: < Test > :: get ( netuid) ;
1302+ let current_tick = CurrentTick :: < Test > :: get ( netuid) ;
13021303 let expected_current_tick = TickIndex :: from_sqrt_price_bounded ( expected_sqrt_price) ;
13031304 assert_eq ! ( current_tick, expected_current_tick) ;
13041305
@@ -1729,15 +1730,14 @@ mod tests {
17291730
17301731 // Modify liquidity (also causes claiming of fees)
17311732 let liquidity_before = CurrentLiquidity :: < Test > :: get ( netuid) ;
1732- let modify_result =
1733- Pallet :: < Test > :: modify_position (
1734- netuid,
1735- & OK_COLDKEY_ACCOUNT_ID ,
1736- & OK_HOTKEY_ACCOUNT_ID ,
1737- position_id,
1738- -1_i64 * ( ( liquidity / 10 ) as i64 ) ,
1739- )
1740- . unwrap ( ) ;
1733+ let modify_result = Pallet :: < Test > :: modify_position (
1734+ netuid,
1735+ & OK_COLDKEY_ACCOUNT_ID ,
1736+ & OK_HOTKEY_ACCOUNT_ID ,
1737+ position_id,
1738+ -1_i64 * ( ( liquidity / 10 ) as i64 ) ,
1739+ )
1740+ . unwrap ( ) ;
17411741 assert_abs_diff_eq ! ( modify_result. alpha, alpha / 10 , epsilon = alpha / 1000 ) ;
17421742 assert ! ( modify_result. fee_tao > 0 ) ;
17431743 assert_eq ! ( modify_result. fee_alpha, 0 ) ;
@@ -1753,22 +1753,20 @@ mod tests {
17531753
17541754 // Position liquidity is reduced
17551755 let position =
1756- Positions :: < Test > :: get ( & ( netuid, OK_COLDKEY_ACCOUNT_ID , position_id) )
1757- . unwrap ( ) ;
1756+ Positions :: < Test > :: get ( & ( netuid, OK_COLDKEY_ACCOUNT_ID , position_id) ) . unwrap ( ) ;
17581757 assert_eq ! ( position. liquidity, liquidity * 9 / 10 ) ;
17591758 assert_eq ! ( position. tick_low, tick_low) ;
17601759 assert_eq ! ( position. tick_high, tick_high) ;
17611760
17621761 // Modify liquidity again (ensure fees aren't double-collected)
1763- let modify_result =
1764- Pallet :: < Test > :: modify_position (
1765- netuid,
1766- & OK_COLDKEY_ACCOUNT_ID ,
1767- & OK_HOTKEY_ACCOUNT_ID ,
1768- position_id,
1769- -1_i64 * ( ( liquidity / 100 ) as i64 ) ,
1770- )
1771- . unwrap ( ) ;
1762+ let modify_result = Pallet :: < Test > :: modify_position (
1763+ netuid,
1764+ & OK_COLDKEY_ACCOUNT_ID ,
1765+ & OK_HOTKEY_ACCOUNT_ID ,
1766+ position_id,
1767+ -1_i64 * ( ( liquidity / 100 ) as i64 ) ,
1768+ )
1769+ . unwrap ( ) ;
17721770
17731771 assert_abs_diff_eq ! ( modify_result. alpha, alpha / 100 , epsilon = alpha / 1000 ) ;
17741772 assert_eq ! ( modify_result. fee_tao, 0 ) ;
@@ -1913,9 +1911,10 @@ mod tests {
19131911 }
19141912
19151913 // Assert that current tick is updated
1916- let current_tick = CurrentTick :: < Test > :: get ( netuid) ;
1917- let expected_current_tick = TickIndex :: from_sqrt_price_bounded ( sqrt_current_price_after) ;
1918- assert_eq ! ( current_tick, expected_current_tick) ;
1914+ let current_tick = CurrentTick :: < Test > :: get ( netuid) ;
1915+ let expected_current_tick =
1916+ TickIndex :: from_sqrt_price_bounded ( sqrt_current_price_after) ;
1917+ assert_eq ! ( current_tick, expected_current_tick) ;
19191918 } ,
19201919 ) ;
19211920 } ) ;
@@ -2390,13 +2389,12 @@ mod tests {
23902389
23912390 let current_price = SqrtPrice :: from_num ( 0.50000051219212275465 ) ;
23922391 let tick = TickIndex :: try_from_sqrt_price ( current_price) . unwrap ( ) ;
2392+
23932393 let round_trip_price = TickIndex :: try_to_sqrt_price ( & tick) . unwrap ( ) ;
23942394 assert ! ( round_trip_price <= current_price) ;
23952395
23962396 let roundtrip_tick = TickIndex :: try_from_sqrt_price ( round_trip_price) . unwrap ( ) ;
23972397 assert ! ( tick == roundtrip_tick) ;
23982398 } ) ;
23992399 }
2400-
2401-
24022400}
0 commit comments