Skip to content
Draft
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
231 changes: 221 additions & 10 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pallet-subtensor-swap-runtime-api = { path = "pallets/swap/runtime-api", default
pallet-subtensor-swap-rpc = { path = "pallets/swap/rpc", default-features = false }
procedural-fork = { path = "support/procedural-fork", default-features = false }
safe-math = { path = "primitives/safe-math", default-features = false }
safe-bigmath = { package = "safe-bigmath", git = "https://github.com/sam0x17/safe-math", rev = "4fb5b1c", default-features = false }
share-pool = { path = "primitives/share-pool", default-features = false }
subtensor-macros = { path = "support/macros", default-features = false }
subtensor-custom-rpc = { default-features = false, path = "pallets/subtensor/rpc" }
Expand Down
4 changes: 2 additions & 2 deletions chain-extensions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use pallet_subtensor_proxy as pallet_proxy;
use pallet_subtensor_proxy::WeightInfo;
use sp_runtime::{DispatchError, Weight, traits::StaticLookup};
use sp_std::marker::PhantomData;
use substrate_fixed::types::U96F32;
use substrate_fixed::types::U64F64;
use subtensor_runtime_common::{AlphaCurrency, NetUid, ProxyType, TaoCurrency};
use subtensor_swap_interface::SwapHandler;

Expand Down Expand Up @@ -520,7 +520,7 @@ where
netuid.into(),
);

let price = current_alpha_price.saturating_mul(U96F32::from_num(1_000_000_000));
let price = current_alpha_price.saturating_mul(U64F64::from_num(1_000_000_000));
let price: u64 = price.saturating_to_num();

let encoded_result = price.encode();
Expand Down
4 changes: 2 additions & 2 deletions chain-extensions/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use pallet_subtensor::DefaultMinStake;
use sp_core::Get;
use sp_core::U256;
use sp_runtime::DispatchError;
use substrate_fixed::types::U96F32;
use substrate_fixed::types::U64F64;
use subtensor_runtime_common::{AlphaCurrency, Currency as CurrencyTrait, NetUid, TaoCurrency};
use subtensor_swap_interface::SwapHandler;

Expand Down Expand Up @@ -985,7 +985,7 @@ fn get_alpha_price_returns_encoded_price() {
<pallet_subtensor_swap::Pallet<mock::Test> as SwapHandler>::current_alpha_price(
netuid.into(),
);
let expected_price_scaled = expected_price.saturating_mul(U96F32::from_num(1_000_000_000));
let expected_price_scaled = expected_price.saturating_mul(U64F64::from_num(1_000_000_000));
let expected_price_u64: u64 = expected_price_scaled.saturating_to_num();

let mut env = MockEnv::new(FunctionId::GetAlphaPriceV1, caller, netuid.encode());
Expand Down
18 changes: 9 additions & 9 deletions pallets/subtensor/src/coinbase/block_emission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use frame_support::traits::Get;
use safe_math::*;
use substrate_fixed::{
transcendental::log2,
types::{I96F32, U96F32},
types::{I96F32, U64F64},
};
use subtensor_runtime_common::{NetUid, TaoCurrency};
use subtensor_swap_interface::SwapHandler;
Expand Down Expand Up @@ -36,15 +36,15 @@ impl<T: Config> Pallet<T> {
alpha_block_emission: u64,
) -> (u64, u64, u64) {
// Init terms.
let mut tao_in_emission: U96F32 = U96F32::saturating_from_num(tao_emission);
let float_alpha_block_emission: U96F32 = U96F32::saturating_from_num(alpha_block_emission);
let mut tao_in_emission: U64F64 = U64F64::saturating_from_num(tao_emission);
let float_alpha_block_emission: U64F64 = U64F64::saturating_from_num(alpha_block_emission);

// Get alpha price for subnet.
let alpha_price = T::SwapInterface::current_alpha_price(netuid.into());
log::debug!("{netuid:?} - alpha_price: {alpha_price:?}");

// Get initial alpha_in
let mut alpha_in_emission: U96F32 = U96F32::saturating_from_num(tao_emission)
let mut alpha_in_emission: U64F64 = U64F64::saturating_from_num(tao_emission)
.checked_div(alpha_price)
.unwrap_or(float_alpha_block_emission);

Expand All @@ -62,11 +62,11 @@ impl<T: Config> Pallet<T> {
}

// Avoid rounding errors.
if tao_in_emission < U96F32::saturating_from_num(1)
|| alpha_in_emission < U96F32::saturating_from_num(1)
{
alpha_in_emission = U96F32::saturating_from_num(0);
tao_in_emission = U96F32::saturating_from_num(0);
let zero = U64F64::saturating_from_num(0);
let one = U64F64::saturating_from_num(1);
if tao_in_emission < one || alpha_in_emission < one {
alpha_in_emission = zero;
tao_in_emission = zero;
}

// Set Alpha in emission.
Expand Down
3 changes: 2 additions & 1 deletion pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ impl<T: Config> Pallet<T> {
log::debug!("alpha_emission_i: {alpha_emission_i:?}");

// Get subnet price.
let price_i: U96F32 = T::SwapInterface::current_alpha_price(netuid_i.into());
let price_i: U96F32 =
U96F32::saturating_from_num(T::SwapInterface::current_alpha_price(netuid_i.into()));
log::debug!("price_i: {price_i:?}");

let mut tao_in_i: U96F32 = tao_emission_i;
Expand Down
14 changes: 6 additions & 8 deletions pallets/subtensor/src/staking/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use frame_support::traits::{
},
};
use safe_math::*;
use substrate_fixed::types::U96F32;
use substrate_fixed::types::{U64F64, U96F32};
use subtensor_runtime_common::{NetUid, TaoCurrency};
use subtensor_swap_interface::{Order, SwapHandler};

Expand Down Expand Up @@ -48,15 +48,13 @@ impl<T: Config> Pallet<T> {
Self::get_all_subnet_netuids()
.into_iter()
.map(|netuid| {
let alpha = U96F32::saturating_from_num(Self::get_stake_for_hotkey_on_subnet(
let alpha = U64F64::saturating_from_num(Self::get_stake_for_hotkey_on_subnet(
hotkey, netuid,
));
let alpha_price = U96F32::saturating_from_num(
T::SwapInterface::current_alpha_price(netuid.into()),
);
let alpha_price = T::SwapInterface::current_alpha_price(netuid.into());
alpha.saturating_mul(alpha_price)
})
.sum::<U96F32>()
.sum::<U64F64>()
.saturating_to_num::<u64>()
.into()
}
Expand All @@ -76,7 +74,7 @@ impl<T: Config> Pallet<T> {
let order = GetTaoForAlpha::<T>::with_amount(alpha_stake);
T::SwapInterface::sim_swap(netuid.into(), order)
.map(|r| {
let fee: u64 = U96F32::saturating_from_num(r.fee_paid)
let fee: u64 = U64F64::saturating_from_num(r.fee_paid)
.saturating_mul(T::SwapInterface::current_alpha_price(
netuid.into(),
))
Expand Down Expand Up @@ -186,7 +184,7 @@ impl<T: Config> Pallet<T> {
let alpha_stake =
Self::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid);
let min_alpha_stake =
U96F32::saturating_from_num(Self::get_nominator_min_required_stake())
U64F64::saturating_from_num(Self::get_nominator_min_required_stake())
.safe_div(T::SwapInterface::current_alpha_price(netuid))
.saturating_to_num::<u64>();
if alpha_stake > 0.into() && alpha_stake < min_alpha_stake.into() {
Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/staking/remove_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,9 @@ impl<T: Config> Pallet<T> {
.saturating_to_num::<u64>();

owner_emission_tao = if owner_alpha_u64 > 0 {
let cur_price: U96F32 = T::SwapInterface::current_alpha_price(netuid.into());
let cur_price: U96F32 = U96F32::saturating_from_num(
T::SwapInterface::current_alpha_price(netuid.into()),
);
let val_u64 = U96F32::from_num(owner_alpha_u64)
.saturating_mul(cur_price)
.floor()
Expand Down
8 changes: 4 additions & 4 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ impl<T: Config> Pallet<T> {
// Because alpha = b / (b + h), where b and h > 0, alpha < 1, so 1 - alpha > 0.
// We can use unsigned type here: U96F32
let one_minus_alpha: U96F32 = U96F32::saturating_from_num(1.0).saturating_sub(alpha);
let current_price: U96F32 = alpha.saturating_mul(
let current_price: U96F32 = alpha.saturating_mul(U96F32::saturating_from_num(
T::SwapInterface::current_alpha_price(netuid.into())
.min(U96F32::saturating_from_num(1.0)),
);
.min(U64F64::saturating_from_num(1.0)),
));
let current_moving: U96F32 =
one_minus_alpha.saturating_mul(Self::get_moving_alpha_price(netuid));
// Convert batch to signed I96F32 to avoid migration of SubnetMovingPrice for now``
Expand Down Expand Up @@ -876,7 +876,7 @@ impl<T: Config> Pallet<T> {
let current_price =
<T as pallet::Config>::SwapInterface::current_alpha_price(netuid.into());
let tao_equivalent: TaoCurrency = current_price
.saturating_mul(U96F32::saturating_from_num(actual_alpha_moved))
.saturating_mul(U64F64::saturating_from_num(actual_alpha_moved))
.saturating_to_num::<u64>()
.into();

Expand Down
Loading
Loading