-
Notifications
You must be signed in to change notification settings - Fork 288
Reduce staking fees #1848
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
Reduce staking fees #1848
Changes from all commits
904d7f5
e13a3e7
c47ae2b
21f538b
a2bc486
18f283a
7d33bd2
7748eb2
e3dab17
7e78ab0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -876,6 +876,83 @@ impl<T: Config> Pallet<T> { | |
| Ok(swap_result.amount_paid_out) | ||
| } | ||
|
|
||
| /// Transfers stake between coldkeys and/or hotkey within one subnet without running it | ||
| /// through swap. | ||
| /// | ||
| /// Does not incur any swapping nor fees | ||
| pub fn transfer_stake_within_subnet( | ||
| origin_coldkey: &T::AccountId, | ||
| origin_hotkey: &T::AccountId, | ||
| destination_coldkey: &T::AccountId, | ||
| destination_hotkey: &T::AccountId, | ||
| netuid: NetUid, | ||
| alpha: u64, | ||
| ) -> Result<u64, DispatchError> { | ||
| // Decrease alpha on origin keys | ||
| let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet( | ||
| origin_hotkey, | ||
| origin_coldkey, | ||
| netuid, | ||
| alpha, | ||
| ); | ||
|
|
||
| // Increase alpha on destination keys | ||
| let actual_alpha_moved = Self::increase_stake_for_hotkey_and_coldkey_on_subnet( | ||
| destination_hotkey, | ||
| destination_coldkey, | ||
| netuid, | ||
| actual_alpha_decrease, | ||
| ); | ||
|
|
||
| // Calculate TAO equivalent based on current price (it is accurate because | ||
| // there's no slippage in this move) | ||
| let current_price = | ||
| <T as pallet::Config>::SwapInterface::current_alpha_price(netuid.into()); | ||
| let tao_equivalent = current_price | ||
| .saturating_mul(U96F32::saturating_from_num(actual_alpha_moved)) | ||
| .saturating_to_num::<u64>(); | ||
|
|
||
| // Ensure tao_equivalent is above DefaultMinStake | ||
| ensure!( | ||
| tao_equivalent >= DefaultMinStake::<T>::get(), | ||
| Error::<T>::AmountTooLow | ||
| ); | ||
|
|
||
| // Step 3: Update StakingHotkeys if the hotkey's total alpha, across all subnets, is zero | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we have this snippet commented?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It needs a better fix than just O(n) iteration over all all subnets for this hotkey. Probably we should store aggregate alpha for each hotkey or some king of bitmap that shows whether the hotkey has any alpha in a subnet. This is an older tech debt.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it valid without the update? Can we get away with the postponed solution?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya we can clean up the map later if needed. There's a call for it also |
||
| // TODO: fix. | ||
| // if Self::get_stake(hotkey, coldkey) == 0 { | ||
| // StakingHotkeys::<T>::mutate(coldkey, |hotkeys| { | ||
| // hotkeys.retain(|k| k != hotkey); | ||
| // }); | ||
| // } | ||
|
|
||
| LastColdkeyHotkeyStakeBlock::<T>::insert( | ||
| destination_coldkey, | ||
| destination_hotkey, | ||
| Self::get_current_block_as_u64(), | ||
| ); | ||
|
|
||
| // Deposit and log the unstaking event. | ||
| Self::deposit_event(Event::StakeRemoved( | ||
| origin_coldkey.clone(), | ||
| origin_hotkey.clone(), | ||
| tao_equivalent, | ||
| actual_alpha_decrease, | ||
| netuid, | ||
| 0_u64, // 0 fee | ||
| )); | ||
| Self::deposit_event(Event::StakeAdded( | ||
| destination_coldkey.clone(), | ||
| destination_hotkey.clone(), | ||
| tao_equivalent, | ||
| actual_alpha_moved, | ||
| netuid, | ||
| 0_u64, // 0 fee | ||
| )); | ||
|
|
||
| Ok(tao_equivalent) | ||
| } | ||
|
|
||
| pub fn get_alpha_share_pool( | ||
| hotkey: <T as frame_system::Config>::AccountId, | ||
| netuid: NetUid, | ||
|
|
@@ -1119,21 +1196,24 @@ impl<T: Config> Pallet<T> { | |
| Error::<T>::NotEnoughStakeToWithdraw | ||
| ); | ||
|
|
||
| // Ensure that the stake amount to be removed is above the minimum in tao equivalent. | ||
| let tao_equivalent = | ||
| T::SwapInterface::sim_swap(origin_netuid.into(), OrderType::Sell, alpha_amount) | ||
| .map(|res| res.amount_paid_out) | ||
| .map_err(|_| Error::<T>::InsufficientLiquidity)?; | ||
| ensure!( | ||
| tao_equivalent > DefaultMinStake::<T>::get(), | ||
| Error::<T>::AmountTooLow | ||
| ); | ||
| // If origin and destination netuid are different, do the swap-related checks | ||
| if origin_netuid != destination_netuid { | ||
| // Ensure that the stake amount to be removed is above the minimum in tao equivalent. | ||
| let tao_equivalent = | ||
| T::SwapInterface::sim_swap(origin_netuid.into(), OrderType::Sell, alpha_amount) | ||
| .map(|res| res.amount_paid_out) | ||
| .map_err(|_| Error::<T>::InsufficientLiquidity)?; | ||
| ensure!( | ||
| tao_equivalent > DefaultMinStake::<T>::get(), | ||
| Error::<T>::AmountTooLow | ||
| ); | ||
|
|
||
| // Ensure that if partial execution is not allowed, the amount will not cause | ||
| // slippage over desired | ||
| if let Some(allow_partial) = maybe_allow_partial { | ||
| if !allow_partial { | ||
| ensure!(alpha_amount <= max_amount, Error::<T>::SlippageTooHigh); | ||
| // Ensure that if partial execution is not allowed, the amount will not cause | ||
| // slippage over desired | ||
| if let Some(allow_partial) = maybe_allow_partial { | ||
| if !allow_partial { | ||
| ensure!(alpha_amount <= max_amount, Error::<T>::SlippageTooHigh); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1143,10 +1223,12 @@ impl<T: Config> Pallet<T> { | |
| TransferToggle::<T>::get(origin_netuid), | ||
| Error::<T>::TransferDisallowed | ||
| ); | ||
| ensure!( | ||
| TransferToggle::<T>::get(destination_netuid), | ||
| Error::<T>::TransferDisallowed | ||
| ); | ||
| if origin_netuid != destination_netuid { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Saves one read We need to make sure we check only for coldkey dest != coldkey origin |
||
| ensure!( | ||
| TransferToggle::<T>::get(destination_netuid), | ||
| Error::<T>::TransferDisallowed | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this Pays:No when add is yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then unstake all is Yes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sudo_set_tx_childkey_take_rate_limitis a root-only transaction. No spam risk.