Skip to content
Open
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
274 changes: 262 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pallet-subtensor-swap = { path = "pallets/swap", default-features = false }
pallet-subtensor-swap-runtime-api = { path = "pallets/swap/runtime-api", default-features = false }
pallet-subtensor-swap-rpc = { path = "pallets/swap/rpc", default-features = false }
procedural-fork = { path = "support/procedural-fork", default-features = false }
safe-bigmath = { package = "safe-bigmath", default-features = false, git = "https://github.com/sam0x17/safe-bigmath" }
safe-math = { path = "primitives/safe-math", default-features = false }
share-pool = { path = "primitives/share-pool", default-features = false }
subtensor-macros = { path = "support/macros", default-features = false }
Expand Down Expand Up @@ -316,4 +317,5 @@ pow-faucet = []

[patch.crates-io]
w3f-bls = { git = "https://github.com/opentensor/bls", branch = "fix-no-std" }

zstd-sys = { git = "https://github.com/gztensor/zstd-sys" }
zstd-safe = { git = "https://github.com/gztensor/zstd-safe" }
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::{AlphaBalance, NetUid, ProxyType, TaoBalance};
use subtensor_swap_interface::SwapHandler;

Expand Down Expand Up @@ -513,7 +513,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
1 change: 0 additions & 1 deletion chain-extensions/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,6 @@ impl pallet_subtensor_swap::Config for Test {
type TaoReserve = TaoBalanceReserve<Self>;
type AlphaReserve = AlphaBalanceReserve<Self>;
type MaxFeeRate = SwapMaxFeeRate;
type MaxPositions = SwapMaxPositions;
type MinimumLiquidity = SwapMinimumLiquidity;
type MinimumReserve = SwapMinimumReserve;
type WeightInfo = ();
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 @@ -11,7 +11,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::{AlphaBalance, NetUid, TaoBalance, Token};
use subtensor_swap_interface::SwapHandler;

Expand Down Expand Up @@ -987,7 +987,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
2 changes: 0 additions & 2 deletions pallets/admin-utils/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ impl pallet_balances::Config for Test {
parameter_types! {
pub const SwapProtocolId: PalletId = PalletId(*b"ten/swap");
pub const SwapMaxFeeRate: u16 = 10000; // 15.26%
pub const SwapMaxPositions: u32 = 100;
pub const SwapMinimumLiquidity: u64 = 1_000;
pub const SwapMinimumReserve: NonZeroU64 = NonZeroU64::new(1_000_000).unwrap();
}
Expand All @@ -343,7 +342,6 @@ impl pallet_subtensor_swap::Config for Test {
type TaoReserve = pallet_subtensor::TaoBalanceReserve<Self>;
type AlphaReserve = pallet_subtensor::AlphaBalanceReserve<Self>;
type MaxFeeRate = SwapMaxFeeRate;
type MaxPositions = SwapMaxPositions;
type MinimumLiquidity = SwapMinimumLiquidity;
type MinimumReserve = SwapMinimumReserve;
type WeightInfo = ();
Expand Down
10 changes: 6 additions & 4 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use sp_runtime::{
};
use sp_std::collections::btree_set::BTreeSet;
use sp_std::vec;
use substrate_fixed::types::U96F32;
use substrate_fixed::types::U64F64;
use subtensor_runtime_common::{AlphaBalance, NetUid, TaoBalance};
use subtensor_swap_interface::SwapHandler;

Expand Down Expand Up @@ -790,6 +790,7 @@ mod pallet_benchmarks {
let initial_balance = TaoBalance::from(900_000_000_000_u64);
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), initial_balance);

// Price = 0.01
let tao_reserve = TaoBalance::from(1_000_000_000_000_u64);
let alpha_in = AlphaBalance::from(100_000_000_000_000_u64);
set_reserves::<T>(netuid, tao_reserve, alpha_in);
Expand All @@ -804,7 +805,7 @@ mod pallet_benchmarks {
// by swapping 100 TAO
let current_price = T::SwapInterface::current_alpha_price(netuid);
let limit = current_price
.saturating_mul(U96F32::saturating_from_num(1_001_000_000))
.saturating_mul(U64F64::saturating_from_num(1_001_000_000))
.saturating_to_num::<u64>()
.into();
let amount_to_be_staked = TaoBalance::from(100_000_000_000_u64);
Expand Down Expand Up @@ -893,6 +894,7 @@ mod pallet_benchmarks {
let hotkey: T::AccountId = account("Alice", 0, seed);
Subtensor::<T>::set_burn(netuid, benchmark_registration_burn());

// Price = 0.01
let tao_reserve = TaoBalance::from(1_000_000_000_000_u64);
let alpha_in = AlphaBalance::from(100_000_000_000_000_u64);
set_reserves::<T>(netuid, tao_reserve, alpha_in);
Expand Down Expand Up @@ -920,7 +922,7 @@ mod pallet_benchmarks {
// by swapping 100 Alpha
let current_price = T::SwapInterface::current_alpha_price(netuid);
let limit = current_price
.saturating_mul(U96F32::saturating_from_num(999_900_000))
.saturating_mul(U64F64::saturating_from_num(999_900_000))
.saturating_to_num::<u64>()
.into();
let amount_unstaked = AlphaBalance::from(100_000_000_000_u64);
Expand Down Expand Up @@ -1456,7 +1458,7 @@ mod pallet_benchmarks {
// by swapping 1 TAO
let current_price = T::SwapInterface::current_alpha_price(netuid);
let limit = current_price
.saturating_mul(U96F32::saturating_from_num(500_000_000))
.saturating_mul(U64F64::saturating_from_num(500_000_000))
.saturating_to_num::<u64>()
.into();
let staked_amt = TaoBalance::from(1_000_000_000_u64);
Expand Down
2 changes: 0 additions & 2 deletions pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ impl<T: Config> Pallet<T> {
Self::finalize_all_subnet_root_dividends(netuid);

// --- Perform the cleanup before removing the network.
T::SwapInterface::dissolve_all_liquidity_providers(netuid)?;
Self::destroy_alpha_in_out_stakes(netuid)?;
T::SwapInterface::clear_protocol_liquidity(netuid)?;
T::CommitmentsInterface::purge_netuid(netuid);
Expand Down Expand Up @@ -300,7 +299,6 @@ impl<T: Config> Pallet<T> {
SubnetMovingPrice::<T>::remove(netuid);
SubnetTaoFlow::<T>::remove(netuid);
SubnetEmaTaoFlow::<T>::remove(netuid);
SubnetTaoProvided::<T>::remove(netuid);

// --- 13. Token / mechanism / registration toggles.
TokenSymbol::<T>::remove(netuid);
Expand Down
12 changes: 8 additions & 4 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ impl<T: Config> Pallet<T> {
let tao_to_swap_with: TaoBalance =
tou64!(excess_tao.get(netuid_i).unwrap_or(&asfloat!(0))).into();

T::SwapInterface::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i);
let (actual_injected_tao, actual_injected_alpha) =
T::SwapInterface::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i);

if tao_to_swap_with > TaoBalance::ZERO {
let buy_swap_result = Self::swap_tao_for_alpha(
Expand All @@ -86,15 +87,17 @@ impl<T: Config> Pallet<T> {
AlphaBalance::from(tou64!(*alpha_in.get(netuid_i).unwrap_or(&asfloat!(0))));
SubnetAlphaInEmission::<T>::insert(*netuid_i, alpha_in_i);
SubnetAlphaIn::<T>::mutate(*netuid_i, |total| {
*total = total.saturating_add(alpha_in_i);
// Reserves also received fees in addition to alpha_in_i
*total = total.saturating_add(actual_injected_alpha);
});

// Inject TAO in.
let injected_tao: TaoBalance =
tou64!(*tao_in.get(netuid_i).unwrap_or(&asfloat!(0))).into();
SubnetTaoInEmission::<T>::insert(*netuid_i, injected_tao);
SubnetTAO::<T>::mutate(*netuid_i, |total| {
*total = total.saturating_add(injected_tao);
// Reserves also received fees in addition to injected_tao
*total = total.saturating_add(actual_injected_tao);
});
TotalStake::<T>::mutate(|total| {
*total = total.saturating_add(injected_tao);
Expand Down Expand Up @@ -139,7 +142,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: 2 additions & 12 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1291,11 +1291,6 @@ pub mod pallet {
pub type SubnetTAO<T: Config> =
StorageMap<_, Identity, NetUid, TaoBalance, ValueQuery, DefaultZeroTao<T>>;

/// --- MAP ( netuid ) --> tao_in_user_subnet | Returns the amount of TAO in the subnet reserve provided by users as liquidity.
#[pallet::storage]
pub type SubnetTaoProvided<T: Config> =
StorageMap<_, Identity, NetUid, TaoBalance, ValueQuery, DefaultZeroTao<T>>;

/// --- MAP ( netuid ) --> alpha_in_emission | Returns the amount of alph in emission into the pool per block.
#[pallet::storage]
pub type SubnetAlphaInEmission<T: Config> =
Expand All @@ -1316,11 +1311,6 @@ pub mod pallet {
pub type SubnetAlphaIn<T: Config> =
StorageMap<_, Identity, NetUid, AlphaBalance, ValueQuery, DefaultZeroAlpha<T>>;

/// --- MAP ( netuid ) --> alpha_supply_user_in_pool | Returns the amount of alpha in the pool provided by users as liquidity.
#[pallet::storage]
pub type SubnetAlphaInProvided<T: Config> =
StorageMap<_, Identity, NetUid, AlphaBalance, ValueQuery, DefaultZeroAlpha<T>>;

/// --- MAP ( netuid ) --> alpha_supply_in_subnet | Returns the amount of alpha in the subnet.
#[pallet::storage]
pub type SubnetAlphaOut<T: Config> =
Expand Down Expand Up @@ -2529,7 +2519,7 @@ pub struct TaoBalanceReserve<T: Config>(PhantomData<T>);
impl<T: Config> TokenReserve<TaoBalance> for TaoBalanceReserve<T> {
#![deny(clippy::expect_used)]
fn reserve(netuid: NetUid) -> TaoBalance {
SubnetTAO::<T>::get(netuid).saturating_add(SubnetTaoProvided::<T>::get(netuid))
SubnetTAO::<T>::get(netuid)
}

fn increase_provided(netuid: NetUid, tao: TaoBalance) {
Expand All @@ -2547,7 +2537,7 @@ pub struct AlphaBalanceReserve<T: Config>(PhantomData<T>);
impl<T: Config> TokenReserve<AlphaBalance> for AlphaBalanceReserve<T> {
#![deny(clippy::expect_used)]
fn reserve(netuid: NetUid) -> AlphaBalance {
SubnetAlphaIn::<T>::get(netuid).saturating_add(SubnetAlphaInProvided::<T>::get(netuid))
SubnetAlphaIn::<T>::get(netuid)
}

fn increase_provided(netuid: NetUid, alpha: AlphaBalance) {
Expand Down
70 changes: 35 additions & 35 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,9 @@ mod dispatches {
/// - Errors stemming from transaction pallet.
///
#[pallet::call_index(2)]
#[pallet::weight((Weight::from_parts(340_800_000, 0)
.saturating_add(T::DbWeight::get().reads(27_u64))
.saturating_add(T::DbWeight::get().writes(15_u64)), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((Weight::from_parts(536_700_000, 0)
.saturating_add(T::DbWeight::get().reads(22_u64))
.saturating_add(T::DbWeight::get().writes(14_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn add_stake(
origin: OriginFor<T>,
hotkey: T::AccountId,
Expand Down Expand Up @@ -1040,9 +1040,9 @@ mod dispatches {

/// User register a new subnetwork via burning token
#[pallet::call_index(7)]
#[pallet::weight((Weight::from_parts(354_200_000, 0)
.saturating_add(T::DbWeight::get().reads(47_u64))
.saturating_add(T::DbWeight::get().writes(39_u64)), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((Weight::from_parts(315_200_000, 0)
.saturating_add(T::DbWeight::get().reads(34_u64))
.saturating_add(T::DbWeight::get().writes(29_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn burned_register(
origin: OriginFor<T>,
netuid: NetUid,
Expand Down Expand Up @@ -1246,9 +1246,9 @@ mod dispatches {

/// User register a new subnetwork
#[pallet::call_index(59)]
#[pallet::weight((Weight::from_parts(235_400_000, 0)
#[pallet::weight((Weight::from_parts(238_500_000, 0)
.saturating_add(T::DbWeight::get().reads(36_u64))
.saturating_add(T::DbWeight::get().writes(52_u64)), DispatchClass::Normal, Pays::Yes))]
.saturating_add(T::DbWeight::get().writes(50_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn register_network(origin: OriginFor<T>, hotkey: T::AccountId) -> DispatchResult {
Self::do_register_network(origin, &hotkey, 1, None)
}
Expand Down Expand Up @@ -1455,9 +1455,9 @@ mod dispatches {

/// User register a new subnetwork
#[pallet::call_index(79)]
#[pallet::weight((Weight::from_parts(396_000_000, 0)
#[pallet::weight((Weight::from_parts(235_700_000, 0)
.saturating_add(T::DbWeight::get().reads(35_u64))
.saturating_add(T::DbWeight::get().writes(51_u64)), DispatchClass::Normal, Pays::Yes))]
.saturating_add(T::DbWeight::get().writes(49_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn register_network_with_identity(
origin: OriginFor<T>,
hotkey: T::AccountId,
Expand Down Expand Up @@ -1525,9 +1525,9 @@ mod dispatches {
/// * `TxRateLimitExceeded`:
/// - Thrown if key has hit transaction rate limit
#[pallet::call_index(84)]
#[pallet::weight((Weight::from_parts(358_500_000, 0)
.saturating_add(T::DbWeight::get().reads(44_u64))
.saturating_add(T::DbWeight::get().writes(24_u64)), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((Weight::from_parts(512_500_000, 0)
.saturating_add(T::DbWeight::get().reads(39_u64))
.saturating_add(T::DbWeight::get().writes(23_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn unstake_all_alpha(origin: OriginFor<T>, hotkey: T::AccountId) -> DispatchResult {
Self::do_unstake_all_alpha(origin, hotkey)
}
Expand All @@ -1554,8 +1554,8 @@ mod dispatches {
/// - The alpha stake amount to move.
///
#[pallet::call_index(85)]
#[pallet::weight((Weight::from_parts(164_300_000, 0)
.saturating_add(T::DbWeight::get().reads(19_u64))
#[pallet::weight((Weight::from_parts(215_700_000, 0)
.saturating_add(T::DbWeight::get().reads(20_u64))
.saturating_add(T::DbWeight::get().writes(7_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn move_stake(
origin: T::RuntimeOrigin,
Expand Down Expand Up @@ -1597,8 +1597,8 @@ mod dispatches {
/// # Events
/// May emit a `StakeTransferred` event on success.
#[pallet::call_index(86)]
#[pallet::weight((Weight::from_parts(160_300_000, 0)
.saturating_add(T::DbWeight::get().reads(16_u64))
#[pallet::weight((Weight::from_parts(212_400_000, 0)
.saturating_add(T::DbWeight::get().reads(17_u64))
.saturating_add(T::DbWeight::get().writes(6_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn transfer_stake(
origin: T::RuntimeOrigin,
Expand Down Expand Up @@ -1639,9 +1639,9 @@ mod dispatches {
/// May emit a `StakeSwapped` event on success.
#[pallet::call_index(87)]
#[pallet::weight((
Weight::from_parts(351_300_000, 0)
.saturating_add(T::DbWeight::get().reads(40_u64))
.saturating_add(T::DbWeight::get().writes(22_u64)),
Weight::from_parts(503_300_000, 0)
.saturating_add(T::DbWeight::get().reads(35_u64))
.saturating_add(T::DbWeight::get().writes(21_u64)),
DispatchClass::Normal,
Pays::Yes
))]
Expand Down Expand Up @@ -1704,9 +1704,9 @@ mod dispatches {
/// - Errors stemming from transaction pallet.
///
#[pallet::call_index(88)]
#[pallet::weight((Weight::from_parts(402_900_000, 0)
.saturating_add(T::DbWeight::get().reads(27_u64))
.saturating_add(T::DbWeight::get().writes(15_u64)), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((Weight::from_parts(704_000_000, 0)
.saturating_add(T::DbWeight::get().reads(22_u64))
.saturating_add(T::DbWeight::get().writes(14_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn add_stake_limit(
origin: OriginFor<T>,
hotkey: T::AccountId,
Expand Down Expand Up @@ -1769,9 +1769,9 @@ mod dispatches {
/// - Thrown if there is not enough stake on the hotkey to withdwraw this amount.
///
#[pallet::call_index(89)]
#[pallet::weight((Weight::from_parts(377_400_000, 0)
.saturating_add(T::DbWeight::get().reads(30_u64))
.saturating_add(T::DbWeight::get().writes(13_u64)), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((Weight::from_parts(641_600_000, 0)
.saturating_add(T::DbWeight::get().reads(25_u64))
.saturating_add(T::DbWeight::get().writes(12_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn remove_stake_limit(
origin: OriginFor<T>,
hotkey: T::AccountId,
Expand Down Expand Up @@ -1813,9 +1813,9 @@ mod dispatches {
/// May emit a `StakeSwapped` event on success.
#[pallet::call_index(90)]
#[pallet::weight((
Weight::from_parts(411_500_000, 0)
.saturating_add(T::DbWeight::get().reads(40_u64))
.saturating_add(T::DbWeight::get().writes(22_u64)),
Weight::from_parts(699_300_000, 0)
.saturating_add(T::DbWeight::get().reads(35_u64))
.saturating_add(T::DbWeight::get().writes(21_u64)),
DispatchClass::Normal,
Pays::Yes
))]
Expand Down Expand Up @@ -1991,9 +1991,9 @@ mod dispatches {
/// at which or better (higher) the staking should execute.
/// Without limit_price it remove all the stake similar to `remove_stake` extrinsic
#[pallet::call_index(103)]
#[pallet::weight((Weight::from_parts(395_300_000, 10142)
.saturating_add(T::DbWeight::get().reads(30_u64))
.saturating_add(T::DbWeight::get().writes(13_u64)), DispatchClass::Normal, Pays::Yes))]
#[pallet::weight((Weight::from_parts(657_100_000, 10142)
.saturating_add(T::DbWeight::get().reads(25_u64))
.saturating_add(T::DbWeight::get().writes(12_u64)), DispatchClass::Normal, Pays::Yes))]
pub fn remove_stake_full_limit(
origin: T::RuntimeOrigin,
hotkey: T::AccountId,
Expand Down Expand Up @@ -2589,9 +2589,9 @@ mod dispatches {
/// alpha token first and immediately burn the acquired amount of alpha (aka Subnet buyback).
#[pallet::call_index(132)]
#[pallet::weight((
Weight::from_parts(368_000_000, 8556)
.saturating_add(T::DbWeight::get().reads(30_u64))
.saturating_add(T::DbWeight::get().writes(16_u64)),
Weight::from_parts(787_900_000, 8556)
.saturating_add(T::DbWeight::get().reads(25_u64))
.saturating_add(T::DbWeight::get().writes(15_u64)),
DispatchClass::Normal,
Pays::Yes
))]
Expand Down
Loading
Loading