-
Notifications
You must be signed in to change notification settings - Fork 272
Chain buys go to owners of other subnets #2327
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
Open
unconst
wants to merge
1
commit into
devnet-ready
Choose a base branch
from
buys_to_owners
base: devnet-ready
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -78,7 +78,7 @@ impl<T: Config> Pallet<T> { | |
| ); | ||
| if let Ok(buy_swap_result_ok) = buy_swap_result { | ||
| let bought_alpha: AlphaCurrency = buy_swap_result_ok.amount_paid_out.into(); | ||
| Self::recycle_subnet_alpha(*netuid_i, bought_alpha); | ||
| Self::distribute_alpha_to_subnet_owners(*netuid_i, bought_alpha); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -111,6 +111,84 @@ impl<T: Config> Pallet<T> { | |
| } | ||
| } | ||
|
|
||
| /// Distributes bought alpha to all subnet owners proportionally based on their | ||
| /// moving average price, excluding the owner of the source subnet. | ||
| /// | ||
| /// # Arguments | ||
| /// * `source_netuid` - The subnet where the alpha was bought (owner of this subnet is excluded) | ||
| /// * `alpha_to_distribute` - The total alpha amount to distribute | ||
| /// | ||
| /// For each other subnet, the alpha is staked to the subnet owner's coldkey | ||
| /// on the subnet owner's hotkey for that subnet. | ||
| pub fn distribute_alpha_to_subnet_owners(source_netuid: NetUid, alpha_to_distribute: AlphaCurrency) { | ||
| if alpha_to_distribute.is_zero() { | ||
| return; | ||
| } | ||
|
|
||
| // Get the owner of the source subnet (to exclude) | ||
| let source_owner = SubnetOwner::<T>::get(source_netuid); | ||
|
|
||
| // Get all subnets except root and the source subnet | ||
| let all_subnets: Vec<NetUid> = Self::get_all_subnet_netuids() | ||
| .into_iter() | ||
| .filter(|netuid| *netuid != NetUid::ROOT && *netuid != source_netuid) | ||
| .collect(); | ||
|
|
||
| // Calculate moving average prices for each subnet (excluding those owned by source owner) | ||
| let mut subnet_prices: Vec<(NetUid, U96F32)> = Vec::new(); | ||
| let mut total_price: U96F32 = asfloat!(0); | ||
|
|
||
| for netuid in all_subnets.iter() { | ||
| // Skip if this subnet is owned by the same owner as the source subnet | ||
| let owner = SubnetOwner::<T>::get(*netuid); | ||
| if owner == source_owner { | ||
| continue; | ||
| } | ||
|
|
||
| let moving_price = Self::get_moving_alpha_price(*netuid); | ||
| if moving_price > asfloat!(0) { | ||
| subnet_prices.push((*netuid, moving_price)); | ||
| total_price = total_price.saturating_add(moving_price); | ||
| } | ||
| } | ||
|
|
||
| // If no valid subnets to distribute to, recycle the alpha instead | ||
| if subnet_prices.is_empty() || total_price == asfloat!(0) { | ||
| Self::recycle_subnet_alpha(source_netuid, alpha_to_distribute); | ||
| return; | ||
| } | ||
|
|
||
| let alpha_float: U96F32 = asfloat!(alpha_to_distribute); | ||
|
|
||
| // Distribute proportionally to each subnet owner | ||
| for (netuid, moving_price) in subnet_prices { | ||
| // Calculate the proportion of alpha for this subnet | ||
| let proportion: U96F32 = moving_price.safe_div_or(total_price, asfloat!(0)); | ||
| let alpha_share: AlphaCurrency = tou64!(alpha_float.saturating_mul(proportion)).into(); | ||
|
|
||
| if alpha_share.is_zero() { | ||
| continue; | ||
| } | ||
|
|
||
| // Get the owner coldkey and hotkey for this subnet | ||
| if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) | ||
| && let Ok(owner_hotkey) = SubnetOwnerHotkey::<T>::try_get(netuid) | ||
| { | ||
| log::debug!( | ||
| "Distributing alpha {alpha_share:?} to subnet {netuid:?} owner (coldkey: {owner_coldkey:?}, hotkey: {owner_hotkey:?})" | ||
| ); | ||
|
|
||
| // Stake the alpha share to the owner coldkey on the owner hotkey for this subnet | ||
| Self::increase_stake_for_hotkey_and_coldkey_on_subnet( | ||
| &owner_hotkey, | ||
| &owner_coldkey, | ||
| netuid, | ||
| alpha_share, | ||
| ); | ||
| } | ||
|
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. and if we cannot get the subnet owner coldkey/hotkey for some reason, then what happens? Shouldn't we accumulate the amount and recycle it at the end of the function? |
||
| } | ||
| } | ||
|
|
||
| pub fn get_subnet_terms( | ||
| subnet_emissions: &BTreeMap<NetUid, U96F32>, | ||
| ) -> ( | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I suggest a global hyperparameter to allow root to choose between
0for full recycle and65535for full distribution. We don't know yet how powerful this is going to be.