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
2 changes: 1 addition & 1 deletion pallets/admin-utils/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ impl pallet_subtensor_swap::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AdminOrigin = EnsureRoot<AccountId>;
type LiquidityDataProvider = SubtensorModule;
type BalanceOps = SubtensorModule;
type BalanceOps = SubtensorModule;
type ProtocolId = SwapProtocolId;
type MaxFeeRate = SwapMaxFeeRate;
type MaxPositions = SwapMaxPositions;
Expand Down
1 change: 0 additions & 1 deletion pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ impl<T: Config> Pallet<T> {
let pending_alpha: U96F32 = alpha_out_i.saturating_sub(root_alpha);
log::debug!("pending_alpha: {:?}", pending_alpha);
// Sell root emission through the pool.

let swap_result = Self::swap_alpha_for_tao(
*netuid_i,
tou64!(root_alpha),
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2438,7 +2438,7 @@ impl<T: Config + pallet_balances::Config<Balance = u64>>

fn subnet_mechanism(netuid: u16) -> u16 {
SubnetMechanism::<T>::get(netuid)
}
}
}

impl<T: Config + pallet_balances::Config<Balance = u64>>
Expand Down Expand Up @@ -2490,5 +2490,5 @@ impl<T: Config + pallet_balances::Config<Balance = u64>>
Ok(Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey, &coldkey, netuid, alpha,
))
}
}
}
2 changes: 1 addition & 1 deletion pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2023,5 +2023,5 @@ mod dispatches {
) -> DispatchResult {
Self::do_burn_alpha(origin, hotkey, amount, netuid)
}
}
}
}
11 changes: 7 additions & 4 deletions pallets/subtensor/src/staking/add_stake.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
use substrate_fixed::types::I96F32;
use subtensor_swap_interface::{SwapHandler, OrderType};
use subtensor_swap_interface::{OrderType, SwapHandler};

impl<T: Config> Pallet<T> {
/// ---- The implementation for the extrinsic add_stake: Adds stake to a hotkey account.
Expand Down Expand Up @@ -185,11 +185,14 @@ impl<T: Config> Pallet<T> {
}

// Use reverting swap to estimate max limit amount
if let Ok(swap_result) = T::SwapInterface::swap(netuid, OrderType::Buy, u64::MAX, limit_price, true) {
swap_result.amount_paid_in.saturating_add(swap_result.fee_paid)
if let Ok(swap_result) =
T::SwapInterface::swap(netuid, OrderType::Buy, u64::MAX, limit_price, true)
{
swap_result
.amount_paid_in
.saturating_add(swap_result.fee_paid)
} else {
0
}
}
}

26 changes: 13 additions & 13 deletions pallets/subtensor/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,14 @@ impl<T: Config> Pallet<T> {
let alpha_stake = Self::get_stake_for_hotkey_and_coldkey_on_subnet(
hotkey, coldkey, netuid,
);
Self::sim_swap_alpha_for_tao(
netuid,
alpha_stake,
)
.map(|r| {
let fee: u64 = U96F32::saturating_from_num(r.fee_paid)
.saturating_mul(T::SwapInterface::current_alpha_price(netuid))
.saturating_to_num();
r.amount_paid_out.saturating_add(fee)
})
.unwrap_or_default()
Self::sim_swap_alpha_for_tao(netuid, alpha_stake)
.map(|r| {
let fee: u64 = U96F32::saturating_from_num(r.fee_paid)
.saturating_mul(T::SwapInterface::current_alpha_price(netuid))
.saturating_to_num();
r.amount_paid_out.saturating_add(fee)
})
.unwrap_or_default()
})
.sum::<u64>()
})
Expand Down Expand Up @@ -199,8 +196,11 @@ impl<T: Config> Pallet<T> {
Self::add_balance_to_coldkey_account(coldkey, cleared_stake);
} else {
// Just clear small alpha
let alpha = Self::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid);
Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid, alpha);
let alpha =
Self::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid);
Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(
hotkey, coldkey, netuid, alpha,
);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions pallets/subtensor/src/staking/move_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ impl<T: Config> Pallet<T> {
///
/// In the corner case when SubnetTAO(2) == SubnetTAO(1), no slippage is going to occur.
///
/// TODO: This formula only works for a single swap step, so it is not 100% correct for swap v3. We need an updated one.
///
pub fn get_max_amount_move(
origin_netuid: u16,
destination_netuid: u16,
Expand Down
23 changes: 11 additions & 12 deletions pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;
use subtensor_swap_interface::{SwapHandler, OrderType};
use subtensor_swap_interface::{OrderType, SwapHandler};

impl<T: Config> Pallet<T> {
/// ---- The implementation for the extrinsic remove_stake: Removes stake from a hotkey account and adds it onto a coldkey.
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<T: Config> Pallet<T> {
&coldkey,
netuid,
alpha_unstaked,
T::SwapInterface::max_price(),
T::SwapInterface::min_price(),
)?;

// Add the balance to the coldkey. If the above fails we will not credit this coldkey.
Expand Down Expand Up @@ -247,7 +247,7 @@ impl<T: Config> Pallet<T> {
&coldkey,
netuid,
alpha_unstaked,
T::SwapInterface::max_price(),
T::SwapInterface::min_price(),
)?;

// Increment total
Expand Down Expand Up @@ -346,13 +346,8 @@ impl<T: Config> Pallet<T> {
)?;

// 4. Swap the alpha to tao and update counters for this subnet.
let tao_unstaked = Self::unstake_from_subnet(
&hotkey,
&coldkey,
netuid,
possible_alpha,
T::SwapInterface::max_price(),
)?;
let tao_unstaked =
Self::unstake_from_subnet(&hotkey, &coldkey, netuid, possible_alpha, limit_price)?;

// 5. We add the balance to the coldkey. If the above fails we will not credit this coldkey.
Self::add_balance_to_coldkey_account(&coldkey, tao_unstaked);
Expand Down Expand Up @@ -385,8 +380,12 @@ impl<T: Config> Pallet<T> {
}

// Use reverting swap to estimate max limit amount
if let Ok(swap_result) = T::SwapInterface::swap(netuid, OrderType::Sell, u64::MAX, limit_price, true) {
swap_result.amount_paid_in.saturating_add(swap_result.fee_paid)
if let Ok(swap_result) =
T::SwapInterface::swap(netuid, OrderType::Sell, u64::MAX, limit_price, true)
{
swap_result
.amount_paid_in
.saturating_add(swap_result.fee_paid)
} else {
0
}
Expand Down
48 changes: 24 additions & 24 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ impl<T: Config> Pallet<T> {
// Step 1: Get the mechanism type for the subnet (0 for Stable, 1 for Dynamic)
let mechanism_id: u16 = SubnetMechanism::<T>::get(netuid);
if mechanism_id == 1 {
let swap_result = T::SwapInterface::swap(netuid, OrderType::Buy, tao, price_limit, false)?;
let swap_result =
T::SwapInterface::swap(netuid, OrderType::Buy, tao, price_limit, false)?;

// update Alpha reserves.
SubnetAlphaIn::<T>::set(netuid, swap_result.new_alpha_reserve);
Expand Down Expand Up @@ -714,7 +715,8 @@ impl<T: Config> Pallet<T> {
let mechanism_id: u16 = SubnetMechanism::<T>::get(netuid);
// Step 2: Swap alpha and attain tao
if mechanism_id == 1 {
let swap_result = T::SwapInterface::swap(netuid, OrderType::Sell, alpha, price_limit, false)?;
let swap_result =
T::SwapInterface::swap(netuid, OrderType::Sell, alpha, price_limit, false)?;

// Increase Alpha reserves.
SubnetAlphaIn::<T>::set(netuid, swap_result.new_alpha_reserve);
Expand All @@ -728,7 +730,9 @@ impl<T: Config> Pallet<T> {
SubnetTAO::<T>::set(netuid, swap_result.new_tao_reserve);

// Reduce total TAO reserves.
TotalStake::<T>::mutate(|total| *total = total.saturating_sub(swap_result.amount_paid_out));
TotalStake::<T>::mutate(|total| {
*total = total.saturating_sub(swap_result.amount_paid_out)
});

// Increase total subnet TAO volume.
SubnetVolume::<T>::mutate(netuid, |total| {
Expand Down Expand Up @@ -766,6 +770,14 @@ impl<T: Config> Pallet<T> {
// Swap the alpha for TAO.
let swap_result = Self::swap_alpha_for_tao(netuid, actual_alpha_decrease, price_limit)?;

// Refund the unused alpha (in case if limit price is hit)
let refund = actual_alpha_decrease.saturating_sub(
swap_result
.amount_paid_in
.saturating_add(swap_result.fee_paid),
);
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid, refund);

// Step 3: Update StakingHotkeys if the hotkey's total alpha, across all subnets, is zero
// TODO const: fix.
// if Self::get_stake(hotkey, coldkey) == 0 {
Expand Down Expand Up @@ -888,12 +900,9 @@ impl<T: Config> Pallet<T> {
// Get the minimum balance (and amount) that satisfies the transaction
let min_amount = {
let min_stake = DefaultMinStake::<T>::get();
let fee = Self::sim_swap_tao_for_alpha(
netuid,
min_stake,
)
.map(|res| res.fee_paid)
.unwrap_or(T::SwapInterface::approx_fee_amount(netuid, min_stake));
let fee = Self::sim_swap_tao_for_alpha(netuid, min_stake)
.map(|res| res.fee_paid)
.unwrap_or(T::SwapInterface::approx_fee_amount(netuid, min_stake));
min_stake.saturating_add(fee)
};

Expand All @@ -918,11 +927,8 @@ impl<T: Config> Pallet<T> {
Error::<T>::HotKeyAccountNotExists
);

let expected_alpha = Self::sim_swap_tao_for_alpha(
netuid,
stake_to_be_added,
)
.map_err(|_| Error::<T>::InsufficientLiquidity)?;
let expected_alpha = Self::sim_swap_tao_for_alpha(netuid, stake_to_be_added)
.map_err(|_| Error::<T>::InsufficientLiquidity)?;

ensure!(
expected_alpha.amount_paid_out > 0,
Expand Down Expand Up @@ -954,10 +960,7 @@ impl<T: Config> Pallet<T> {
ensure!(Self::if_subnet_exist(netuid), Error::<T>::SubnetNotExists);

// Ensure that the stake amount to be removed is above the minimum in tao equivalent.
match Self::sim_swap_alpha_for_tao(
netuid,
alpha_unstaked,
) {
match Self::sim_swap_alpha_for_tao(netuid, alpha_unstaked) {
Ok(res) => ensure!(
res.amount_paid_out > DefaultMinStake::<T>::get(),
Error::<T>::AmountTooLow
Expand Down Expand Up @@ -1037,12 +1040,9 @@ impl<T: Config> Pallet<T> {
);

// Ensure that the stake amount to be removed is above the minimum in tao equivalent.
let tao_equivalent = Self::sim_swap_alpha_for_tao(
origin_netuid,
alpha_amount,
)
.map(|res| res.amount_paid_out)
.map_err(|_| Error::<T>::InsufficientLiquidity)?;
let tao_equivalent = Self::sim_swap_alpha_for_tao(origin_netuid, alpha_amount)
.map(|res| res.amount_paid_out)
.map_err(|_| Error::<T>::InsufficientLiquidity)?;
ensure!(
tao_equivalent > DefaultMinStake::<T>::get(),
Error::<T>::AmountTooLow
Expand Down
8 changes: 6 additions & 2 deletions pallets/subtensor/src/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2223,9 +2223,13 @@ fn test_do_remove_stake_clears_pending_childkeys() {
StakeThreshold::<Test>::get() * 2
));

let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
let alpha =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);

println!("StakeThreshold::<Test>::get() = {:?}", StakeThreshold::<Test>::get());
println!(
"StakeThreshold::<Test>::get() = {:?}",
StakeThreshold::<Test>::get()
);
println!("alpha = {:?}", alpha);

// Attempt to set child
Expand Down
6 changes: 1 addition & 5 deletions pallets/subtensor/src/tests/coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,7 @@ fn test_pending_swapped() {
1_000_000_000 - 125000000,
epsilon = 1
); // 1 - swapped.
assert_abs_diff_eq!(
PendingRootDivs::<Test>::get(netuid),
125000000,
epsilon = 1
); // swapped * (price = 1)
assert_abs_diff_eq!(PendingRootDivs::<Test>::get(netuid), 125000000, epsilon = 1); // swapped * (price = 1)
});
}

Expand Down
5 changes: 4 additions & 1 deletion pallets/subtensor/src/tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,10 @@ fn test_1_graph() {
let stake_amount: u64 = 1_000_000_000;
add_network(netuid, u16::MAX - 1, 0); // set higher tempo to avoid built-in epoch, then manual epoch instead
SubtensorModule::set_max_allowed_uids(netuid, 1);
SubtensorModule::add_balance_to_coldkey_account(&coldkey, stake_amount + ExistentialDeposit::get());
SubtensorModule::add_balance_to_coldkey_account(
&coldkey,
stake_amount + ExistentialDeposit::get(),
);
register_ok_neuron(netuid, hotkey, coldkey, 1);
SubtensorModule::set_weights_set_rate_limit(netuid, 0);

Expand Down
24 changes: 11 additions & 13 deletions pallets/subtensor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,7 @@ pub(crate) fn setup_reserves(netuid: u16, tao: u64, alpha: u64) {

pub(crate) fn swap_tao_to_alpha(netuid: u16, tao: u64) -> (u64, u64) {
match netuid {
0 => {
(tao, 0)
},
0 => (tao, 0),
_ => {
let result = <Test as pallet::Config>::SwapInterface::swap(
netuid,
Expand All @@ -876,27 +874,27 @@ pub(crate) fn swap_tao_to_alpha(netuid: u16, tao: u64) -> (u64, u64) {
<Test as pallet::Config>::SwapInterface::max_price(),
true,
);

assert_ok!(&result);

let result = result.unwrap();

// we don't want to have silent 0 comparissons in tests
assert!(result.amount_paid_out > 0);

(result.amount_paid_out, result.fee_paid)
}
}
}
}

pub(crate) fn swap_alpha_to_tao(netuid: u16, alpha: u64) -> (u64, u64) {
match netuid {
0 => {
(alpha, 0)
},
0 => (alpha, 0),
_ => {

println!("<Test as pallet::Config>::SwapInterface::min_price() = {:?}", <Test as pallet::Config>::SwapInterface::min_price());
println!(
"<Test as pallet::Config>::SwapInterface::min_price() = {:?}",
<Test as pallet::Config>::SwapInterface::min_price()
);

let result = <Test as pallet::Config>::SwapInterface::swap(
netuid,
Expand Down
Loading
Loading