Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update test for per-subnet take emission #356

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 33 additions & 0 deletions pallets/subtensor/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,37 @@ impl<T: Config> Pallet<T> {
pub fn set_delegate_limit(delegate_limit: u32) {
DelegateLimit::<T>::put(delegate_limit);
}

/// Calculates the slippage for both staking and unstaking operations.
///
/// # Arguments
/// * `netuid` - The unique identifier for the network (subnet).
/// * `stake_change` - The amount of stake being added (positive) or removed (negative).
///
/// # Returns
/// * `I64F64` - The slippage amount, which is the difference in price.
pub fn calculate_slippage(
netuid: u16,
stake_change: i64, // Positive for staking, negative for unstaking
) -> I64F64 {
let tao_reserve = DynamicTAOReserve::<T>::get(netuid);
let dynamic_reserve = DynamicAlphaReserve::<T>::get(netuid);
let k = DynamicK::<T>::get(netuid);

// Calculate new reserves based on whether stake is being added or removed
let new_dynamic_reserve = if stake_change > 0 {
dynamic_reserve.saturating_add(stake_change as u64)
} else {
dynamic_reserve.saturating_sub(stake_change.abs() as u64)
};

let new_tao_reserve = (k / new_dynamic_reserve as u128) as u64;

let initial_price = I64F64::from_num(tao_reserve) / I64F64::from_num(dynamic_reserve);
let new_price = I64F64::from_num(new_tao_reserve) / I64F64::from_num(new_dynamic_reserve);

// Slippage is the difference in price
let slippage = initial_price - new_price;
slippage
}
}
10 changes: 10 additions & 0 deletions pallets/subtensor/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,13 @@ pub fn add_network(netuid: u16, tempo: u16, _modality: u16) {
SubtensorModule::set_network_registration_allowed(netuid, true);
SubtensorModule::set_network_pow_registration_allowed(netuid, true);
}

#[allow(dead_code)]
pub fn user_add_network(coldkey: U256, hotkey: U256, netuid: u16) {
SubtensorModule::user_add_network(
<<Test as frame_system::Config>::RuntimeOrigin>::signed(coldkey),
hotkey
);
SubtensorModule::set_network_registration_allowed(netuid, true);
SubtensorModule::set_network_pow_registration_allowed(netuid, true);
}
Loading