Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2189,17 +2189,29 @@ mod dispatches {
);

// Add or remove liquidity
let result = T::SwapInterface::modify_position(netuid, &coldkey, &hotkey, position_id, liquidity_delta)?;
let result = T::SwapInterface::modify_position(
netuid,
&coldkey,
&hotkey,
position_id,
liquidity_delta,
)?;

if liquidity_delta > 0 {
// Remove TAO and Alpha balances or fail transaction if they can't be removed exactly
let tao_provided = Self::remove_balance_from_coldkey_account(&coldkey, result.tao)?;
ensure!(tao_provided == result.tao, Error::<T>::InsufficientBalance);

let alpha_provided = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey, &coldkey, netuid, result.alpha,
&hotkey,
&coldkey,
netuid,
result.alpha,
);
ensure!(
alpha_provided == result.alpha,
Error::<T>::InsufficientBalance
);
ensure!(alpha_provided == result.alpha, Error::<T>::InsufficientBalance);

// Emit an event
Self::deposit_event(Event::LiquidityAdded {
Expand Down
14 changes: 7 additions & 7 deletions pallets/subtensor/src/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2416,14 +2416,14 @@ fn test_revoke_child_no_min_stake_check() {
add_network(netuid, 13, 0);
register_ok_neuron(netuid, parent, coldkey, 0);

let reserve = 1_000_000_000_000_000;
mock::setup_reserves(netuid, reserve, reserve);
mock::setup_reserves(root, reserve, reserve);
let reserve = 1_000_000_000_000_000;
mock::setup_reserves(netuid, reserve, reserve);
mock::setup_reserves(root, reserve, reserve);

// Set minimum stake for setting children
StakeThreshold::<Test>::put(1_000_000_000_000);

let (_, fee) = mock::swap_tao_to_alpha(root, StakeThreshold::<Test>::get());
let (_, fee) = mock::swap_tao_to_alpha(root, StakeThreshold::<Test>::get());
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&parent,
&coldkey,
Expand Down Expand Up @@ -2494,12 +2494,12 @@ fn test_do_set_child_registration_disabled() {
add_network(netuid, 13, 0);
register_ok_neuron(netuid, parent, coldkey, 0);

let reserve = 1_000_000_000_000_000;
mock::setup_reserves(netuid, reserve, reserve);
let reserve = 1_000_000_000_000_000;
mock::setup_reserves(netuid, reserve, reserve);

// Set minimum stake for setting children
StakeThreshold::<Test>::put(1_000_000_000_000);
let (_, fee) = mock::swap_tao_to_alpha(netuid, StakeThreshold::<Test>::get());
let (_, fee) = mock::swap_tao_to_alpha(netuid, StakeThreshold::<Test>::get());
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
&parent,
&coldkey,
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,8 +1980,8 @@ fn test_run_coinbase_not_started() {
// Set weight-set limit to 0.
SubtensorModule::set_weights_set_rate_limit(netuid, 0);

let reserve = init_stake * 1000;
mock::setup_reserves(netuid, reserve, reserve);
let reserve = init_stake * 1000;
mock::setup_reserves(netuid, reserve, reserve);

register_ok_neuron(netuid, hotkey, coldkey, 0);
register_ok_neuron(netuid, miner_hk, miner_ck, 0);
Expand Down
1 change: 0 additions & 1 deletion pallets/subtensor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,4 +899,3 @@ pub(crate) fn swap_alpha_to_tao(netuid: u16, alpha: u64) -> (u64, u64) {

(result.amount_paid_out, result.fee_paid)
}

6 changes: 3 additions & 3 deletions pallets/subtensor/src/tests/senate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ fn test_senate_vote_works() {
stake
));

let approx_expected = stake - fee;
let approx_expected = stake - fee;
assert_abs_diff_eq!(
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey_account_id,
&staker_coldkey,
netuid
),
approx_expected,
approx_expected,
epsilon = approx_expected / 1000
);
assert_abs_diff_eq!(
SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey_account_id, netuid),
approx_expected,
approx_expected,
epsilon = approx_expected / 1000
);

Expand Down
12 changes: 9 additions & 3 deletions pallets/subtensor/src/tests/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn test_swap_total_hotkey_stake() {

// Add stake
let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, amount);
assert!(expected_alpha > 0);
assert!(expected_alpha > 0);
assert_ok!(SubtensorModule::add_stake(
RuntimeOrigin::signed(coldkey),
old_hotkey,
Expand All @@ -88,7 +88,10 @@ fn test_swap_total_hotkey_stake() {
));

// Check if stake has increased
assert_eq!(TotalHotkeyAlpha::<Test>::get(old_hotkey, netuid), expected_alpha);
assert_eq!(
TotalHotkeyAlpha::<Test>::get(old_hotkey, netuid),
expected_alpha
);
assert_abs_diff_eq!(
SubtensorModule::get_total_stake_for_hotkey(&new_hotkey),
0,
Expand All @@ -109,7 +112,10 @@ fn test_swap_total_hotkey_stake() {
0,
epsilon = 1,
);
assert_eq!(TotalHotkeyAlpha::<Test>::get(new_hotkey, netuid), expected_alpha);
assert_eq!(
TotalHotkeyAlpha::<Test>::get(new_hotkey, netuid),
expected_alpha
);
});
}

Expand Down
78 changes: 38 additions & 40 deletions pallets/swap/src/pallet/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sp_arithmetic::helpers_128bit;
use sp_runtime::traits::AccountIdConversion;
use substrate_fixed::types::{U64F64, U96F32};
use subtensor_swap_interface::{
LiquidityDataProvider, UpdateLiquidityResult, SwapHandler, SwapResult,
LiquidityDataProvider, SwapHandler, SwapResult, UpdateLiquidityResult,
};

use super::pallet::*;
Expand Down Expand Up @@ -90,14 +90,14 @@ impl<T: Config> SwapStep<T> {
lq = one.safe_div(TickIndex::min_sqrt_price());
}
lq
},
}
OrderType::Buy => {
let mut lq = TickIndex::max_sqrt_price().min(sqrt_price_limit.into());
if lq < current_price {
lq = TickIndex::max_sqrt_price();
}
lq
},
}
};

Self {
Expand Down Expand Up @@ -228,10 +228,8 @@ impl<T: Config> SwapStep<T> {
delta_fixed.saturating_mul(u16_max.safe_div(u16_max.saturating_sub(fee_rate)));

// Hold the fees
let fee = Pallet::<T>::calculate_fee_amount(
self.netuid,
total_cost.saturating_to_num::<u64>(),
);
let fee =
Pallet::<T>::calculate_fee_amount(self.netuid, total_cost.saturating_to_num::<u64>());
Pallet::<T>::add_fees(self.netuid, self.order_type, fee);
let delta_out = Pallet::<T>::convert_deltas(self.netuid, self.order_type, self.delta_in);

Expand Down Expand Up @@ -336,14 +334,11 @@ impl<T: Config> Pallet<T> {
let epsilon = U64F64::saturating_from_num(0.000001);

let current_sqrt_price = price.checked_sqrt(epsilon).unwrap_or(U64F64::from_num(0));
AlphaSqrtPrice::<T>::set(
netuid,
current_sqrt_price,
);
AlphaSqrtPrice::<T>::set(netuid, current_sqrt_price);

// Set current tick
let current_tick = TickIndex::from_sqrt_price_bounded(current_sqrt_price);
CurrentTick::<T>::set(netuid, current_tick);
CurrentTick::<T>::set(netuid, current_tick);

// Set initial (protocol owned) liquidity and positions
// Protocol liquidity makes one position from TickIndex::MIN to TickIndex::MAX
Expand Down Expand Up @@ -1174,8 +1169,14 @@ impl<T: Config> SwapHandler<T::AccountId> for Pallet<T> {
position_id: u128,
liquidity_delta: i64,
) -> Result<UpdateLiquidityResult, DispatchError> {
Self::modify_position(netuid.into(), coldkey_account_id, hotkey_account_id, position_id.into(), liquidity_delta)
.map_err(Into::into)
Self::modify_position(
netuid.into(),
coldkey_account_id,
hotkey_account_id,
position_id.into(),
liquidity_delta,
)
.map_err(Into::into)
}

fn approx_fee_amount(netuid: u16, amount: u64) -> u64 {
Expand Down Expand Up @@ -1298,7 +1299,7 @@ mod tests {
assert_eq!(sqrt_price, expected_sqrt_price);

// Verify that current tick is set
let current_tick = CurrentTick::<Test>::get(netuid);
let current_tick = CurrentTick::<Test>::get(netuid);
let expected_current_tick = TickIndex::from_sqrt_price_bounded(expected_sqrt_price);
assert_eq!(current_tick, expected_current_tick);

Expand Down Expand Up @@ -1729,15 +1730,14 @@ mod tests {

// Modify liquidity (also causes claiming of fees)
let liquidity_before = CurrentLiquidity::<Test>::get(netuid);
let modify_result =
Pallet::<Test>::modify_position(
netuid,
&OK_COLDKEY_ACCOUNT_ID,
&OK_HOTKEY_ACCOUNT_ID,
position_id,
-1_i64 * ((liquidity / 10) as i64),
)
.unwrap();
let modify_result = Pallet::<Test>::modify_position(
netuid,
&OK_COLDKEY_ACCOUNT_ID,
&OK_HOTKEY_ACCOUNT_ID,
position_id,
-1_i64 * ((liquidity / 10) as i64),
)
.unwrap();
assert_abs_diff_eq!(modify_result.alpha, alpha / 10, epsilon = alpha / 1000);
assert!(modify_result.fee_tao > 0);
assert_eq!(modify_result.fee_alpha, 0);
Expand All @@ -1753,22 +1753,20 @@ mod tests {

// Position liquidity is reduced
let position =
Positions::<Test>::get(&(netuid, OK_COLDKEY_ACCOUNT_ID, position_id))
.unwrap();
Positions::<Test>::get(&(netuid, OK_COLDKEY_ACCOUNT_ID, position_id)).unwrap();
assert_eq!(position.liquidity, liquidity * 9 / 10);
assert_eq!(position.tick_low, tick_low);
assert_eq!(position.tick_high, tick_high);

// Modify liquidity again (ensure fees aren't double-collected)
let modify_result =
Pallet::<Test>::modify_position(
netuid,
&OK_COLDKEY_ACCOUNT_ID,
&OK_HOTKEY_ACCOUNT_ID,
position_id,
-1_i64 * ((liquidity / 100) as i64),
)
.unwrap();
let modify_result = Pallet::<Test>::modify_position(
netuid,
&OK_COLDKEY_ACCOUNT_ID,
&OK_HOTKEY_ACCOUNT_ID,
position_id,
-1_i64 * ((liquidity / 100) as i64),
)
.unwrap();

assert_abs_diff_eq!(modify_result.alpha, alpha / 100, epsilon = alpha / 1000);
assert_eq!(modify_result.fee_tao, 0);
Expand Down Expand Up @@ -1913,9 +1911,10 @@ mod tests {
}

// Assert that current tick is updated
let current_tick = CurrentTick::<Test>::get(netuid);
let expected_current_tick = TickIndex::from_sqrt_price_bounded(sqrt_current_price_after);
assert_eq!(current_tick, expected_current_tick);
let current_tick = CurrentTick::<Test>::get(netuid);
let expected_current_tick =
TickIndex::from_sqrt_price_bounded(sqrt_current_price_after);
assert_eq!(current_tick, expected_current_tick);
},
);
});
Expand Down Expand Up @@ -2390,13 +2389,12 @@ mod tests {

let current_price = SqrtPrice::from_num(0.50000051219212275465);
let tick = TickIndex::try_from_sqrt_price(current_price).unwrap();

let round_trip_price = TickIndex::try_to_sqrt_price(&tick).unwrap();
assert!(round_trip_price <= current_price);

let roundtrip_tick = TickIndex::try_from_sqrt_price(round_trip_price).unwrap();
assert!(tick == roundtrip_tick);
});
}


}
4 changes: 2 additions & 2 deletions pallets/swap/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ mod pallet {
/// Provided liquidity parameter is invalid (likely too small)
InvalidLiquidityValue,

/// Reserves too low for operation.
ReservesTooLow,
/// Reserves too low for operation.
ReservesTooLow,
}

#[pallet::call]
Expand Down
5 changes: 4 additions & 1 deletion pallets/swap/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ impl Position {
fee_tao = liquidity_frac.saturating_mul(fee_tao);
fee_alpha = liquidity_frac.saturating_mul(fee_alpha);

(fee_tao.saturating_to_num::<u64>(), fee_alpha.saturating_to_num::<u64>())
(
fee_tao.saturating_to_num::<u64>(),
fee_alpha.saturating_to_num::<u64>(),
)
}

/// Get fees in a position's range
Expand Down
Loading
Loading