Skip to content

Commit 30be6ea

Browse files
committed
Swap tests in progress
1 parent 9292044 commit 30be6ea

File tree

4 files changed

+1155
-1441
lines changed

4 files changed

+1155
-1441
lines changed

pallets/swap/src/pallet/impls.rs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ impl<T: Config> Pallet<T> {
215215

216216
Self::maybe_initialize_palswap(netuid)?;
217217

218+
println!("Self::current_price(netuid) = {:?}", Self::current_price(netuid));
219+
println!("limit_price = {:?}", limit_price);
220+
218221
// Because user specifies the limit price, check that it is in fact beoynd the current one
219222
ensure!(
220223
order.is_beyond_price_limit(Self::current_price(netuid), limit_price),
@@ -371,6 +374,9 @@ impl<T: Config> Pallet<T> {
371374

372375
/// Dissolve all LPs and clean state.
373376
pub fn do_dissolve_all_liquidity_providers(_netuid: NetUid) -> DispatchResult {
377+
// TODO: Revise when user liquidity is available
378+
Ok(())
379+
374380
// if PalSwapInitialized::<T>::get(netuid) {
375381
// // 1) Snapshot only *non‑protocol* positions: (owner, position_id).
376382
// struct CloseItem<A> {
@@ -485,17 +491,19 @@ impl<T: Config> Pallet<T> {
485491
// );
486492

487493
// Ok(())
488-
489-
todo!();
490494
}
491495

496+
/// TODO: Revise when user liquidity is available
492497
/// Clear **protocol-owned** liquidity and wipe all swap state for `netuid`.
493-
pub fn do_clear_protocol_liquidity(_netuid: NetUid) -> DispatchResult {
498+
pub fn do_clear_protocol_liquidity(netuid: NetUid) -> DispatchResult {
494499
// let protocol_account = Self::protocol_account_id();
495500

496-
// // 1) Force-close only protocol positions, burning proceeds.
497-
// let mut burned_tao = TaoCurrency::ZERO;
498-
// let mut burned_alpha = AlphaCurrency::ZERO;
501+
// 1) Force-close only protocol positions, burning proceeds.
502+
let burned_tao = T::TaoReserve::reserve(netuid.into());
503+
let burned_alpha = T::AlphaReserve::reserve(netuid.into());
504+
505+
T::TaoReserve::decrease_provided(netuid.into(), burned_tao);
506+
T::AlphaReserve::decrease_provided(netuid.into(), burned_alpha);
499507

500508
// // Collect protocol position IDs first to avoid mutating while iterating.
501509
// let protocol_pos_ids: sp_std::vec::Vec<PositionId> = Positions::<T>::iter_prefix((netuid,))
@@ -535,34 +543,20 @@ impl<T: Config> Pallet<T> {
535543
// }
536544
// }
537545

538-
// // 2) Clear active tick index entries, then all swap state (idempotent even if empty/non‑V3).
539-
// let active_ticks: sp_std::vec::Vec<TickIndex> =
540-
// Ticks::<T>::iter_prefix(netuid).map(|(ti, _)| ti).collect();
541-
// for ti in active_ticks {
542-
// ActiveTickIndexManager::<T>::remove(netuid, ti);
543-
// }
544-
545-
// let _ = Positions::<T>::clear_prefix((netuid,), u32::MAX, None);
546-
// let _ = Ticks::<T>::clear_prefix(netuid, u32::MAX, None);
546+
let _ = PositionsV2::<T>::clear_prefix((netuid,), u32::MAX, None);
547547

548-
// FeeGlobalTao::<T>::remove(netuid);
549-
// FeeGlobalAlpha::<T>::remove(netuid);
550-
// CurrentLiquidity::<T>::remove(netuid);
551-
// CurrentTick::<T>::remove(netuid);
552-
// AlphaSqrtPrice::<T>::remove(netuid);
553-
// PalSwapInitialized::<T>::remove(netuid);
548+
FeesTao::<T>::remove(netuid);
549+
FeesAlpha::<T>::remove(netuid);
550+
PalSwapInitialized::<T>::remove(netuid);
551+
FeeRate::<T>::remove(netuid);
552+
EnabledUserLiquidity::<T>::remove(netuid);
553+
SwapReserveWeight::<T>::remove(netuid);
554554

555-
// let _ = TickIndexBitmapWords::<T>::clear_prefix((netuid,), u32::MAX, None);
556-
// FeeRate::<T>::remove(netuid);
557-
// EnabledUserLiquidity::<T>::remove(netuid);
558-
559-
// log::debug!(
560-
// "clear_protocol_liquidity: netuid={netuid:?}, protocol_burned: τ={burned_tao:?}, α={burned_alpha:?}; state cleared"
561-
// );
562-
563-
// Ok(())
555+
log::debug!(
556+
"clear_protocol_liquidity: netuid={netuid:?}, protocol_burned: τ={burned_tao:?}, α={burned_alpha:?}; state cleared"
557+
);
564558

565-
todo!();
559+
Ok(())
566560
}
567561
}
568562

pallets/swap/src/pallet/reserve_weights.rs

Lines changed: 95 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,23 @@ impl ReserveWeight {
7171
let w2: u128 = self.get_quote_weight().deconstruct() as u128;
7272
let x_plus_dx = x.saturating_add(dx);
7373

74-
let precision = 256;
74+
let precision = 1024;
7575
let x_safe = SafeInt::from(x);
7676
let w1_safe = SafeInt::from(w1);
7777
let w2_safe = SafeInt::from(w2);
7878
let perquintill_scale = SafeInt::from(ACCURACY as u128);
7979
let denominator = SafeInt::from(x_plus_dx);
8080
let maybe_result_safe_int = if base_quote {
81+
82+
println!("x = {:?}", x);
83+
println!("dx = {:?}", dx);
84+
println!("x_safe = {:?}", x_safe);
85+
println!("denominator = {:?}", denominator);
86+
println!("w1_safe = {:?}", w1_safe);
87+
println!("w2_safe = {:?}", w2_safe);
88+
println!("precision = {:?}", precision);
89+
println!("perquintill_scale = {:?}", perquintill_scale);
90+
8191
SafeInt::pow_ratio_scaled(
8292
&x_safe,
8393
&denominator,
@@ -300,103 +310,111 @@ mod tests {
300310
fn test_exp_base_quote_happy_path() {
301311
// Outer test cases: w_quote
302312
[
303-
Perquintill::from_rational(500_000_000_000_u128, 1_000_000_000_000_u128),
304-
Perquintill::from_rational(500_000_000_001_u128, 1_000_000_000_000_u128),
305-
Perquintill::from_rational(500_000_000_100_u128, 1_000_000_000_000_u128),
306-
Perquintill::from_rational(500_000_001_000_u128, 1_000_000_000_000_u128),
307-
Perquintill::from_rational(500_000_010_000_u128, 1_000_000_000_000_u128),
308-
Perquintill::from_rational(500_000_100_000_u128, 1_000_000_000_000_u128),
309-
Perquintill::from_rational(500_001_000_000_u128, 1_000_000_000_000_u128),
310-
Perquintill::from_rational(500_010_000_000_u128, 1_000_000_000_000_u128),
311-
Perquintill::from_rational(500_100_000_000_u128, 1_000_000_000_000_u128),
312-
Perquintill::from_rational(501_000_000_000_u128, 1_000_000_000_000_u128),
313-
Perquintill::from_rational(510_000_000_000_u128, 1_000_000_000_000_u128),
313+
// Perquintill::from_rational(500_000_000_000_u128, 1_000_000_000_000_u128),
314+
// Perquintill::from_rational(500_000_000_001_u128, 1_000_000_000_000_u128),
315+
// Perquintill::from_rational(499_999_999_999_u128, 1_000_000_000_000_u128),
316+
// Perquintill::from_rational(500_000_000_100_u128, 1_000_000_000_000_u128),
317+
// Perquintill::from_rational(500_000_001_000_u128, 1_000_000_000_000_u128),
318+
// Perquintill::from_rational(500_000_010_000_u128, 1_000_000_000_000_u128),
319+
// Perquintill::from_rational(500_000_100_000_u128, 1_000_000_000_000_u128),
320+
// Perquintill::from_rational(500_001_000_000_u128, 1_000_000_000_000_u128),
321+
// Perquintill::from_rational(500_010_000_000_u128, 1_000_000_000_000_u128),
322+
// Perquintill::from_rational(500_100_000_000_u128, 1_000_000_000_000_u128),
323+
// Perquintill::from_rational(501_000_000_000_u128, 1_000_000_000_000_u128),
324+
// Perquintill::from_rational(510_000_000_000_u128, 1_000_000_000_000_u128),
314325
Perquintill::from_rational(100_000_000_000_u128, 1_000_000_000_000_u128),
315-
Perquintill::from_rational(100_000_000_001_u128, 1_000_000_000_000_u128),
316-
Perquintill::from_rational(200_000_000_000_u128, 1_000_000_000_000_u128),
317-
Perquintill::from_rational(300_000_000_000_u128, 1_000_000_000_000_u128),
318-
Perquintill::from_rational(400_000_000_000_u128, 1_000_000_000_000_u128),
319-
Perquintill::from_rational(600_000_000_000_u128, 1_000_000_000_000_u128),
320-
Perquintill::from_rational(700_000_000_000_u128, 1_000_000_000_000_u128),
321-
Perquintill::from_rational(800_000_000_000_u128, 1_000_000_000_000_u128),
322-
Perquintill::from_rational(899_999_999_999_u128, 1_000_000_000_000_u128),
323-
Perquintill::from_rational(900_000_000_000_u128, 1_000_000_000_000_u128),
324-
Perquintill::from_rational(
325-
102_337_248_363_782_924_u128,
326-
1_000_000_000_000_000_000_u128,
327-
),
326+
// Perquintill::from_rational(100_000_000_001_u128, 1_000_000_000_000_u128),
327+
// Perquintill::from_rational(200_000_000_000_u128, 1_000_000_000_000_u128),
328+
// Perquintill::from_rational(300_000_000_000_u128, 1_000_000_000_000_u128),
329+
// Perquintill::from_rational(400_000_000_000_u128, 1_000_000_000_000_u128),
330+
// Perquintill::from_rational(600_000_000_000_u128, 1_000_000_000_000_u128),
331+
// Perquintill::from_rational(700_000_000_000_u128, 1_000_000_000_000_u128),
332+
// Perquintill::from_rational(800_000_000_000_u128, 1_000_000_000_000_u128),
333+
// Perquintill::from_rational(899_999_999_999_u128, 1_000_000_000_000_u128),
334+
// Perquintill::from_rational(900_000_000_000_u128, 1_000_000_000_000_u128),
335+
// Perquintill::from_rational(
336+
// 102_337_248_363_782_924_u128,
337+
// 1_000_000_000_000_000_000_u128,
338+
// ),
328339
]
329340
.into_iter()
330341
.for_each(|w_quote| {
331342
// Inner test cases: y, x, ∆x
332343
[
333-
(
334-
1_000_000_000_000_u64,
335-
100_000_000_000_000_u64,
336-
100_000_000_u64,
337-
),
338-
(
339-
1_000_000_000_000_u64,
340-
100_000_000_000_000_u64,
341-
100_000_000_u64,
342-
),
343-
(
344-
100_000_000_000_u64,
345-
100_000_000_000_000_u64,
346-
100_000_000_u64,
347-
),
348-
(100_000_000_000_u64, 100_000_000_000_000_u64, 1_000_000_u64),
349-
(
350-
100_000_000_000_u64,
351-
100_000_000_000_000_u64,
352-
1_000_000_000_000_u64,
353-
),
354-
(
355-
1_000_000_000_u64,
356-
100_000_000_000_000_u64,
357-
1_000_000_000_000_u64,
358-
),
359-
(
360-
1_000_000_u64,
361-
100_000_000_000_000_u64,
362-
1_000_000_000_000_u64,
363-
),
364-
(1_000_u64, 100_000_000_000_000_u64, 1_000_000_000_000_u64),
365-
(1_000_u64, 100_000_000_000_000_u64, 1_000_000_000_u64),
366-
(1_000_u64, 100_000_000_000_000_u64, 1_000_000_u64),
367-
(1_000_u64, 100_000_000_000_000_u64, 1_000_u64),
368-
(1_000_u64, 100_000_000_000_000_u64, 100_000_000_000_000_u64),
369-
(10_u64, 100_000_000_000_000_u64, 100_000_000_000_000_u64),
370-
// Extreme values of ∆x for small x
371-
(1_000_000_000_u64, 4_000_000_000_u64, 1_000_000_000_000_u64),
372-
(1_000_000_000_000_u64, 1_000_u64, 1_000_000_000_000_u64),
373-
(
374-
5_628_038_062_729_553_u64,
375-
400_775_553_u64,
376-
14_446_633_907_665_582_u64,
377-
),
378-
(
379-
5_600_000_000_000_000_u64,
380-
400_000_000_u64,
381-
14_000_000_000_000_000_u64,
382-
),
344+
// (1_000_u64, 1_000_u64, 0_u64),
345+
// (1_000_u64, 1_000_u64, 1_u64),
346+
(1_500_u64, 1_000_u64, 1_u64),
347+
// (
348+
// 1_000_000_000_000_u64,
349+
// 100_000_000_000_000_u64,
350+
// 100_000_000_u64,
351+
// ),
352+
// (
353+
// 1_000_000_000_000_u64,
354+
// 100_000_000_000_000_u64,
355+
// 100_000_000_u64,
356+
// ),
357+
// (
358+
// 100_000_000_000_u64,
359+
// 100_000_000_000_000_u64,
360+
// 100_000_000_u64,
361+
// ),
362+
// (100_000_000_000_u64, 100_000_000_000_000_u64, 1_000_000_u64),
363+
// (
364+
// 100_000_000_000_u64,
365+
// 100_000_000_000_000_u64,
366+
// 1_000_000_000_000_u64,
367+
// ),
368+
// (
369+
// 1_000_000_000_u64,
370+
// 100_000_000_000_000_u64,
371+
// 1_000_000_000_000_u64,
372+
// ),
373+
// (
374+
// 1_000_000_u64,
375+
// 100_000_000_000_000_u64,
376+
// 1_000_000_000_000_u64,
377+
// ),
378+
// (1_000_u64, 100_000_000_000_000_u64, 1_000_000_000_000_u64),
379+
// (1_000_u64, 100_000_000_000_000_u64, 1_000_000_000_u64),
380+
// (1_000_u64, 100_000_000_000_000_u64, 1_000_000_u64),
381+
// (1_000_u64, 100_000_000_000_000_u64, 1_000_u64),
382+
// (1_000_u64, 100_000_000_000_000_u64, 100_000_000_000_000_u64),
383+
// (10_u64, 100_000_000_000_000_u64, 100_000_000_000_000_u64),
384+
// // Extreme values of ∆x for small x
385+
// (1_000_000_000_u64, 4_000_000_000_u64, 1_000_000_000_000_u64),
386+
// (1_000_000_000_000_u64, 1_000_u64, 1_000_000_000_000_u64),
387+
// (
388+
// 5_628_038_062_729_553_u64,
389+
// 400_775_553_u64,
390+
// 14_446_633_907_665_582_u64,
391+
// ),
392+
// (
393+
// 5_600_000_000_000_000_u64,
394+
// 400_000_000_u64,
395+
// 14_000_000_000_000_000_u64,
396+
// ),
383397
]
384398
.into_iter()
385399
.for_each(|(y, x, dx)| {
386400
let rw = ReserveWeight::new(w_quote).unwrap();
387401
let e = rw.exp_base_quote(x, dx);
388402
let one = U64F64::from_num(1);
389403
let y_fixed = U64F64::from_num(y);
404+
println!("debug 1: e = {:?}", e);
390405
let dy = y_fixed * (one - e);
406+
println!("debug 2: dy = {:?}", dy);
391407

392408
let w1 = perquintill_to_f64(rw.get_base_weight());
393409
let w2 = perquintill_to_f64(rw.get_quote_weight());
394410
let e_expected = (x as f64 / (x as f64 + dx as f64)).powf(w1 / w2);
395411
let dy_expected = y as f64 * (1. - e_expected);
412+
413+
println!("debug 3: dy_expected = {:?}", dy_expected);
396414

397415
let mut eps = dy_expected / 100000.;
398-
if eps > 0.1 {
399-
eps = 0.1;
416+
if eps > 1.0 {
417+
eps = 1.0;
400418
}
401419
assert_abs_diff_eq!(dy.to_num::<f64>(), dy_expected, epsilon = eps);
402420
})

pallets/swap/src/pallet/swap_step.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::marker::PhantomData;
22

3+
use frame_support::ensure;
34
use safe_math::*;
45
use substrate_fixed::types::U64F64;
56
use subtensor_runtime_common::{AlphaCurrency, Currency, CurrencyReserve, NetUid, TaoCurrency};
@@ -52,6 +53,9 @@ where
5253
let target_price = Self::price_target(netuid, requested_delta_in);
5354
let current_price = Pallet::<T>::current_price(netuid);
5455

56+
println!("requested_delta_in = {}", requested_delta_in);
57+
println!("target_price = {}", target_price);
58+
5559
Self {
5660
netuid,
5761
drop_fees,
@@ -109,16 +113,24 @@ where
109113
.saturating_to_num::<u64>()
110114
.into();
111115
}
116+
117+
118+
println!("=========== debug 1 (end of determine_action)");
112119
}
113120

114121
/// Process a single step of a swap
115122
fn process_swap(&self) -> Result<SwapStepResult<PaidIn, PaidOut>, Error<T>> {
116-
// Hold the fees
117-
Self::add_fees(self.netuid, self.fee);
118-
119123
// Convert amounts, actual swap happens here
120124
let delta_out = Self::convert_deltas(self.netuid, self.delta_in);
121125
log::trace!("\tDelta Out : {delta_out}");
126+
println!("delta_out = {}", delta_out);
127+
ensure!(
128+
delta_out > 0.into(),
129+
Error::<T>::ReservesTooLow
130+
);
131+
132+
// Hold the fees
133+
Self::add_fees(self.netuid, self.fee);
122134

123135
Ok(SwapStepResult {
124136
fee_paid: self.fee,
@@ -214,6 +226,12 @@ impl<T: Config> SwapStep<T, AlphaCurrency, TaoCurrency>
214226
let tao_reserve = T::TaoReserve::reserve(netuid.into());
215227
let reserve_weight = SwapReserveWeight::<T>::get(netuid);
216228
let e = reserve_weight.exp_base_quote(alpha_reserve.into(), delta_in.into());
229+
230+
println!("alpha_reserve = {}", alpha_reserve);
231+
println!("tao_reserve = {}", tao_reserve);
232+
println!("reserve_weight = {:?}", reserve_weight);
233+
println!("e = {}", e);
234+
217235
let one = U64F64::from_num(1);
218236
let tao_reserve_fixed = U64F64::from_num(u64::from(tao_reserve));
219237
TaoCurrency::from(

0 commit comments

Comments
 (0)