Skip to content
77 changes: 40 additions & 37 deletions pallets/subtensor/src/tests/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1812,24 +1812,25 @@ fn massive_dissolve_refund_and_reregistration_flow_is_lossless_and_cleans_state(
];

// Multiple bands/sizes → many positions per cold across nets, using mixed hotkeys.
let bands: [i32; 3] = [5, 13, 30];
let liqs: [u64; 3] = [400_000, 700_000, 1_100_000];
// let bands: [i32; 3] = [5, 13, 30];
// let liqs: [u64; 3] = [400_000, 700_000, 1_100_000];

// TODO: Revise when user liquidity is available
// Helper: add a V3 position via a (hot, cold) pair.
let add_pos = |net: NetUid, hot: U256, cold: U256, band: i32, liq: u64| {
let ct = pallet_subtensor_swap::CurrentTick::<Test>::get(net);
let lo = ct.saturating_sub(band);
let hi = ct.saturating_add(band);
pallet_subtensor_swap::EnabledUserLiquidity::<Test>::insert(net, true);
assert_ok!(pallet_subtensor_swap::Pallet::<Test>::add_liquidity(
RuntimeOrigin::signed(cold),
hot,
net,
lo,
hi,
liq
));
};
// let add_pos = |net: NetUid, hot: U256, cold: U256, band: i32, liq: u64| {
// let ct = pallet_subtensor_swap::CurrentTick::<Test>::get(net);
// let lo = ct.saturating_sub(band);
// let hi = ct.saturating_add(band);
// pallet_subtensor_swap::EnabledUserLiquidity::<Test>::insert(net, true);
// assert_ok!(pallet_subtensor_swap::Pallet::<Test>::add_liquidity(
// RuntimeOrigin::signed(cold),
// hot,
// net,
// lo,
// hi,
// liq
// ));
// };

// ────────────────────────────────────────────────────────────────────
// 1) Create many subnets, enable V3, fix price at tick=0 (sqrt≈1)
Expand Down Expand Up @@ -1954,19 +1955,20 @@ fn massive_dissolve_refund_and_reregistration_flow_is_lossless_and_cleans_state(
// ────────────────────────────────────────────────────────────────────
// 4) Add many V3 positions per cold across nets, alternating hotkeys
// ────────────────────────────────────────────────────────────────────
for (ni, &net) in nets.iter().enumerate() {
let participants = lp_sets_per_net[ni];
for (pi, &cold) in participants.iter().enumerate() {
let [hot1, hot2] = cold_to_hots[&cold];
let hots = [hot1, hot2];
for k in 0..3 {
let band = bands[(pi + k) % bands.len()];
let liq = liqs[(ni + k) % liqs.len()];
let hot = hots[k % hots.len()];
add_pos(net, hot, cold, band, liq);
}
}
}
// TODO: Revise when user liquidity is available
// for (ni, &net) in nets.iter().enumerate() {
// let participants = lp_sets_per_net[ni];
// for (pi, &cold) in participants.iter().enumerate() {
// let [hot1, hot2] = cold_to_hots[&cold];
// let hots = [hot1, hot2];
// for k in 0..3 {
// let band = bands[(pi + k) % bands.len()];
// let liq = liqs[(ni + k) % liqs.len()];
// let hot = hots[k % hots.len()];
// add_pos(net, hot, cold, band, liq);
// }
// }
// }

// Snapshot τ balances AFTER LP adds (to measure actual principal debit).
let mut tao_after_adds: BTreeMap<U256, u64> = BTreeMap::new();
Expand Down Expand Up @@ -2212,14 +2214,15 @@ fn massive_dissolve_refund_and_reregistration_flow_is_lossless_and_cleans_state(
}

// Ensure V3 still functional on new net: add a small position for the first cold using its hot1
let who_cold = cold_lps[0];
let [who_hot, _] = cold_to_hots[&who_cold];
add_pos(net_new, who_hot, who_cold, 8, 123_456);
assert!(
pallet_subtensor_swap::Positions::<Test>::iter()
.any(|((n, owner, _pid), _)| n == net_new && owner == who_cold),
"new position not recorded on the re-registered net"
);
// TODO: Revise when user liquidity is available
// let who_cold = cold_lps[0];
// let [who_hot, _] = cold_to_hots[&who_cold];
// add_pos(net_new, who_hot, who_cold, 8, 123_456);
// assert!(
// pallet_subtensor_swap::Positions::<Test>::iter()
// .any(|((n, owner, _pid), _)| n == net_new && owner == who_cold),
// "new position not recorded on the re-registered net"
// );
});
}

Expand Down
82 changes: 41 additions & 41 deletions pallets/subtensor/src/tests/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ use frame_support::dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays
use frame_support::sp_runtime::DispatchError;
use frame_support::{assert_err, assert_noop, assert_ok, traits::Currency};
use frame_system::RawOrigin;
use pallet_subtensor_swap::Call as SwapCall;
use pallet_subtensor_swap::tick::TickIndex;
use safe_math::FixedExt;
use sp_core::{Get, H256, U256};
use sp_runtime::traits::Dispatchable;
use substrate_fixed::traits::FromFixed;
use substrate_fixed::types::{I96F32, I110F18, U64F64, U96F32};
use subtensor_runtime_common::{
Expand Down Expand Up @@ -5337,44 +5335,45 @@ fn test_update_position_fees() {
});
}

fn setup_positions(netuid: NetUid) {
for (coldkey, hotkey, low_price, high_price, liquidity) in [
(2, 12, 0.1, 0.20, 1_000_000_000_000_u64),
(3, 13, 0.15, 0.25, 200_000_000_000_u64),
(4, 14, 0.25, 0.5, 3_000_000_000_000_u64),
(5, 15, 0.3, 0.6, 300_000_000_000_u64),
(6, 16, 0.4, 0.7, 8_000_000_000_000_u64),
(7, 17, 0.5, 0.8, 600_000_000_000_u64),
(8, 18, 0.6, 0.9, 700_000_000_000_u64),
(9, 19, 0.7, 1.0, 100_000_000_000_u64),
(10, 20, 0.8, 1.1, 300_000_000_000_u64),
] {
SubtensorModule::create_account_if_non_existent(&U256::from(coldkey), &U256::from(hotkey));
SubtensorModule::add_balance_to_coldkey_account(
&U256::from(coldkey),
1_000_000_000_000_000,
);
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&U256::from(hotkey),
&U256::from(coldkey),
netuid.into(),
1_000_000_000_000_000.into(),
);

let tick_low = price_to_tick(low_price);
let tick_high = price_to_tick(high_price);
let add_lq_call = SwapCall::<Test>::add_liquidity {
hotkey: U256::from(hotkey),
netuid: netuid.into(),
tick_low,
tick_high,
liquidity,
};
assert_ok!(
RuntimeCall::Swap(add_lq_call).dispatch(RuntimeOrigin::signed(U256::from(coldkey)))
);
}
}
// TODO: Revise when user liquidity is available
// fn setup_positions(netuid: NetUid) {
// for (coldkey, hotkey, low_price, high_price, liquidity) in [
// (2, 12, 0.1, 0.20, 1_000_000_000_000_u64),
// (3, 13, 0.15, 0.25, 200_000_000_000_u64),
// (4, 14, 0.25, 0.5, 3_000_000_000_000_u64),
// (5, 15, 0.3, 0.6, 300_000_000_000_u64),
// (6, 16, 0.4, 0.7, 8_000_000_000_000_u64),
// (7, 17, 0.5, 0.8, 600_000_000_000_u64),
// (8, 18, 0.6, 0.9, 700_000_000_000_u64),
// (9, 19, 0.7, 1.0, 100_000_000_000_u64),
// (10, 20, 0.8, 1.1, 300_000_000_000_u64),
// ] {
// SubtensorModule::create_account_if_non_existent(&U256::from(coldkey), &U256::from(hotkey));
// SubtensorModule::add_balance_to_coldkey_account(
// &U256::from(coldkey),
// 1_000_000_000_000_000,
// );
// SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
// &U256::from(hotkey),
// &U256::from(coldkey),
// netuid.into(),
// 1_000_000_000_000_000.into(),
// );

// let tick_low = price_to_tick(low_price);
// let tick_high = price_to_tick(high_price);
// let add_lq_call = SwapCall::<Test>::add_liquidity {
// hotkey: U256::from(hotkey),
// netuid: netuid.into(),
// tick_low,
// tick_high,
// liquidity,
// };
// assert_ok!(
// RuntimeCall::Swap(add_lq_call).dispatch(RuntimeOrigin::signed(U256::from(coldkey)))
// );
// }
// }

#[test]
fn test_large_swap() {
Expand All @@ -5397,7 +5396,8 @@ fn test_large_swap() {
)
.unwrap();

setup_positions(netuid.into());
// TODO: Revise when user liquidity is available
// setup_positions(netuid.into());

let swap_amount = TaoCurrency::from(100_000_000_000_000);
assert_ok!(SubtensorModule::add_stake(
Expand Down
47 changes: 24 additions & 23 deletions pallets/swap/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,33 @@ mod benchmarks {
set_fee_rate(RawOrigin::Root, netuid, rate);
}

#[benchmark]
fn add_liquidity() {
let netuid = NetUid::from(1);
// TODO: Revise when user liquidity is available
// #[benchmark]
// fn add_liquidity() {
// let netuid = NetUid::from(1);

if !SwapV3Initialized::<T>::get(netuid) {
SwapV3Initialized::<T>::insert(netuid, true);
AlphaSqrtPrice::<T>::insert(netuid, U64F64::from_num(1));
CurrentTick::<T>::insert(netuid, TickIndex::new(0).unwrap());
CurrentLiquidity::<T>::insert(netuid, T::MinimumLiquidity::get());
}
// if !SwapV3Initialized::<T>::get(netuid) {
// SwapV3Initialized::<T>::insert(netuid, true);
// AlphaSqrtPrice::<T>::insert(netuid, U64F64::from_num(1));
// CurrentTick::<T>::insert(netuid, TickIndex::new(0).unwrap());
// CurrentLiquidity::<T>::insert(netuid, T::MinimumLiquidity::get());
// }

let caller: T::AccountId = whitelisted_caller();
let hotkey: T::AccountId = account("hotkey", 0, 0);
let tick_low = TickIndex::new_unchecked(-1000);
let tick_high = TickIndex::new_unchecked(1000);
// let caller: T::AccountId = whitelisted_caller();
// let hotkey: T::AccountId = account("hotkey", 0, 0);
// let tick_low = TickIndex::new_unchecked(-1000);
// let tick_high = TickIndex::new_unchecked(1000);

#[extrinsic_call]
add_liquidity(
RawOrigin::Signed(caller),
hotkey,
netuid,
tick_low,
tick_high,
1000,
);
}
// #[extrinsic_call]
// add_liquidity(
// RawOrigin::Signed(caller),
// hotkey,
// netuid,
// tick_low,
// tick_high,
// 1000,
// );
// }

#[benchmark]
fn remove_liquidity() {
Expand Down
Loading
Loading