From 3d1b50e020ddafdd616d9b66473d0d77f6a40731 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Mon, 21 Jul 2025 15:02:05 +0300 Subject: [PATCH 1/3] Implement TaoCurrency --- common/src/currency.rs | 231 ++++++++++++++++++++++++----------------- 1 file changed, 133 insertions(+), 98 deletions(-) diff --git a/common/src/currency.rs b/common/src/currency.rs index f50e2bc9a1..317185e716 100644 --- a/common/src/currency.rs +++ b/common/src/currency.rs @@ -30,103 +30,94 @@ use subtensor_macros::freeze_struct; )] pub struct AlphaCurrency(u64); -impl TypeInfo for AlphaCurrency { - type Identity = ::Identity; - fn type_info() -> scale_info::Type { - ::type_info() - } -} - -impl Display for AlphaCurrency { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - Display::fmt(&self.0, f) - } -} - -impl CompactAs for AlphaCurrency { - type As = u64; - - fn encode_as(&self) -> &Self::As { - &self.0 - } - - fn decode_from(v: Self::As) -> Result { - Ok(Self(v)) - } -} - -impl From> for AlphaCurrency { - fn from(c: Compact) -> Self { - c.0 - } -} - -impl From for u64 { - fn from(val: AlphaCurrency) -> Self { - val.0 - } -} - -impl From for AlphaCurrency { - fn from(value: u64) -> Self { - Self(value) - } -} - -impl ToFixed for AlphaCurrency { - fn to_fixed(self) -> F { - self.0.to_fixed() - } - - fn checked_to_fixed(self) -> Option { - self.0.checked_to_fixed() - } - - fn saturating_to_fixed(self) -> F { - self.0.saturating_to_fixed() - } - fn wrapping_to_fixed(self) -> F { - self.0.wrapping_to_fixed() - } - - fn overflowing_to_fixed(self) -> (F, bool) { - self.0.overflowing_to_fixed() - } -} - -impl Currency for AlphaCurrency { - const MAX: Self = Self(u64::MAX); - const ZERO: Self = Self(0); -} - -pub trait Currency: ToFixed + Into + From + Clone + Copy { - const MAX: Self; - const ZERO: Self; - - fn is_zero(&self) -> bool { - Into::::into(*self) == 0 - } - - fn to_u64(&self) -> u64 { - (*self).into() - } - - fn saturating_add(&self, rhv: Self) -> Self { - Into::::into(*self).saturating_add(rhv.into()).into() - } - - #[allow(clippy::arithmetic_side_effects)] - fn saturating_div(&self, rhv: Self) -> Self { - Into::::into(*self).saturating_div(rhv.into()).into() - } - - fn saturating_sub(&self, rhv: Self) -> Self { - Into::::into(*self).saturating_sub(rhv.into()).into() - } +#[freeze_struct("e0ed7f58888e7d7f")] +#[repr(transparent)] +#[derive( + Deserialize, + Serialize, + Clone, + Copy, + Decode, + Default, + Encode, + Eq, + Hash, + MaxEncodedLen, + Ord, + PartialEq, + PartialOrd, + RuntimeDebug, +)] +pub struct TaoCurrency(u64); - fn saturating_mul(&self, rhv: Self) -> Self { - Into::::into(*self).saturating_mul(rhv.into()).into() - } +// implements traits required by the Currency trait (ToFixed + Into + From) and CompactAs, +// TypeInfo and Display. It expects a wrapper structure for u64 (CurrencyT(u64)). +macro_rules! impl_currency_reqs { + ($currency_type:ident) => { + impl TypeInfo for $currency_type { + type Identity = ::Identity; + fn type_info() -> scale_info::Type { + ::type_info() + } + } + + impl Display for $currency_type { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + Display::fmt(&self.0, f) + } + } + + impl CompactAs for $currency_type { + type As = u64; + + fn encode_as(&self) -> &Self::As { + &self.0 + } + + fn decode_from(v: Self::As) -> Result { + Ok(Self(v)) + } + } + + impl From> for $currency_type { + fn from(c: Compact<$currency_type>) -> Self { + c.0 + } + } + + impl From<$currency_type> for u64 { + fn from(val: $currency_type) -> Self { + val.0 + } + } + + impl From for $currency_type { + fn from(value: u64) -> Self { + Self(value) + } + } + + impl ToFixed for $currency_type { + fn to_fixed(self) -> F { + self.0.to_fixed() + } + + fn checked_to_fixed(self) -> Option { + self.0.checked_to_fixed() + } + + fn saturating_to_fixed(self) -> F { + self.0.saturating_to_fixed() + } + fn wrapping_to_fixed(self) -> F { + self.0.wrapping_to_fixed() + } + + fn overflowing_to_fixed(self) -> (F, bool) { + self.0.overflowing_to_fixed() + } + } + } } macro_rules! impl_arithmetic_operators { @@ -205,8 +196,6 @@ macro_rules! impl_arithmetic_operators { }; } -impl_arithmetic_operators!(AlphaCurrency); - macro_rules! impl_approx { ($currency_type:ident) => { #[cfg(feature = "approx")] @@ -228,4 +217,50 @@ macro_rules! impl_approx { }; } +pub trait Currency: ToFixed + Into + From + Clone + Copy { + const MAX: Self; + const ZERO: Self; + + fn is_zero(&self) -> bool { + Into::::into(*self) == 0 + } + + fn to_u64(&self) -> u64 { + (*self).into() + } + + fn saturating_add(&self, rhv: Self) -> Self { + Into::::into(*self).saturating_add(rhv.into()).into() + } + + #[allow(clippy::arithmetic_side_effects)] + fn saturating_div(&self, rhv: Self) -> Self { + Into::::into(*self).saturating_div(rhv.into()).into() + } + + fn saturating_sub(&self, rhv: Self) -> Self { + Into::::into(*self).saturating_sub(rhv.into()).into() + } + + fn saturating_mul(&self, rhv: Self) -> Self { + Into::::into(*self).saturating_mul(rhv.into()).into() + } +} + +impl_arithmetic_operators!(AlphaCurrency); impl_approx!(AlphaCurrency); +impl_currency_reqs!(AlphaCurrency); + +impl_arithmetic_operators!(TaoCurrency); +impl_approx!(TaoCurrency); +impl_currency_reqs!(TaoCurrency); + +impl Currency for AlphaCurrency { + const MAX: Self = Self(u64::MAX); + const ZERO: Self = Self(0); +} + +impl Currency for TaoCurrency { + const MAX: Self = Self(u64::MAX); + const ZERO: Self = Self(0); +} From dc5cb8f88b97b205e0b30adb3289e003d15d140a Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Mon, 21 Jul 2025 17:39:07 +0300 Subject: [PATCH 2/3] Make tao type-safe using TaoCurrency wrapper --- common/src/currency.rs | 138 +-- common/src/lib.rs | 15 +- pallets/admin-utils/src/benchmarking.rs | 4 +- pallets/admin-utils/src/lib.rs | 12 +- pallets/admin-utils/src/tests/mod.rs | 20 +- pallets/subtensor/rpc/src/lib.rs | 6 +- pallets/subtensor/runtime-api/src/lib.rs | 4 +- pallets/subtensor/src/benchmarks.rs | 158 ++-- .../subtensor/src/coinbase/block_emission.rs | 6 +- pallets/subtensor/src/coinbase/block_step.rs | 25 +- pallets/subtensor/src/coinbase/root.rs | 31 +- .../subtensor/src/coinbase/run_coinbase.rs | 35 +- pallets/subtensor/src/lib.rs | 118 ++- pallets/subtensor/src/macros/dispatches.rs | 22 +- pallets/subtensor/src/macros/events.rs | 38 +- pallets/subtensor/src/macros/genesis.rs | 4 +- .../migrations/migrate_fix_root_subnet_tao.rs | 4 +- .../migrations/migrate_init_total_issuance.rs | 8 +- .../subtensor/src/migrations/migrate_rao.rs | 10 +- .../src/migrations/migrate_reset_max_burn.rs | 2 +- .../src/migrations/migrate_set_min_burn.rs | 2 +- .../src/migrations/migrate_total_issuance.rs | 15 +- .../subtensor/src/rpc_info/dynamic_info.rs | 10 +- pallets/subtensor/src/rpc_info/metagraph.rs | 111 +-- pallets/subtensor/src/rpc_info/show_subnet.rs | 28 +- pallets/subtensor/src/rpc_info/stake_info.rs | 10 +- pallets/subtensor/src/rpc_info/subnet_info.rs | 34 +- pallets/subtensor/src/staking/add_stake.rs | 29 +- pallets/subtensor/src/staking/helpers.rs | 24 +- pallets/subtensor/src/staking/move_stake.rs | 32 +- pallets/subtensor/src/staking/remove_stake.rs | 34 +- pallets/subtensor/src/staking/set_children.rs | 2 +- pallets/subtensor/src/staking/stake_utils.rs | 85 +- pallets/subtensor/src/subnets/leasing.rs | 12 +- pallets/subtensor/src/subnets/registration.rs | 19 +- pallets/subtensor/src/subnets/subnet.rs | 17 +- pallets/subtensor/src/swap/swap_coldkey.rs | 7 +- pallets/subtensor/src/swap/swap_hotkey.rs | 5 +- pallets/subtensor/src/tests/children.rs | 100 +- pallets/subtensor/src/tests/coinbase.rs | 265 ++++-- pallets/subtensor/src/tests/delegate_info.rs | 2 +- pallets/subtensor/src/tests/epoch.rs | 20 +- pallets/subtensor/src/tests/leasing.rs | 15 +- pallets/subtensor/src/tests/migration.rs | 23 +- pallets/subtensor/src/tests/mock.rs | 32 +- pallets/subtensor/src/tests/move_stake.rs | 202 ++-- pallets/subtensor/src/tests/networks.rs | 21 +- pallets/subtensor/src/tests/recycle_alpha.rs | 32 +- pallets/subtensor/src/tests/registration.rs | 47 +- pallets/subtensor/src/tests/senate.rs | 113 +-- pallets/subtensor/src/tests/staking.rs | 892 ++++++++++-------- pallets/subtensor/src/tests/staking2.rs | 28 +- pallets/subtensor/src/tests/subnet.rs | 64 +- pallets/subtensor/src/tests/swap_coldkey.rs | 259 ++--- pallets/subtensor/src/tests/swap_hotkey.rs | 61 +- .../src/tests/swap_hotkey_with_subnet.rs | 56 +- pallets/subtensor/src/tests/weights.rs | 49 +- pallets/subtensor/src/utils/misc.rs | 43 +- pallets/subtensor/src/utils/try_state.rs | 24 +- pallets/swap-interface/src/lib.rs | 8 +- pallets/swap/src/mock.rs | 19 +- pallets/swap/src/pallet/impls.rs | 45 +- pallets/swap/src/pallet/mod.rs | 19 +- pallets/swap/src/pallet/tests.rs | 30 +- precompiles/src/alpha.rs | 8 +- precompiles/src/metagraph.rs | 6 +- precompiles/src/staking.rs | 33 +- precompiles/src/subnet.rs | 6 +- runtime/src/lib.rs | 8 +- runtime/tests/pallet_proxy.rs | 2 +- 70 files changed, 2037 insertions(+), 1631 deletions(-) diff --git a/common/src/currency.rs b/common/src/currency.rs index 317185e716..f80cc7a1ba 100644 --- a/common/src/currency.rs +++ b/common/src/currency.rs @@ -54,70 +54,76 @@ pub struct TaoCurrency(u64); // TypeInfo and Display. It expects a wrapper structure for u64 (CurrencyT(u64)). macro_rules! impl_currency_reqs { ($currency_type:ident) => { - impl TypeInfo for $currency_type { - type Identity = ::Identity; - fn type_info() -> scale_info::Type { - ::type_info() - } - } - - impl Display for $currency_type { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - Display::fmt(&self.0, f) - } - } - - impl CompactAs for $currency_type { - type As = u64; - - fn encode_as(&self) -> &Self::As { - &self.0 - } - - fn decode_from(v: Self::As) -> Result { - Ok(Self(v)) - } - } - - impl From> for $currency_type { - fn from(c: Compact<$currency_type>) -> Self { - c.0 - } - } - - impl From<$currency_type> for u64 { - fn from(val: $currency_type) -> Self { - val.0 - } - } - - impl From for $currency_type { - fn from(value: u64) -> Self { - Self(value) - } - } - - impl ToFixed for $currency_type { - fn to_fixed(self) -> F { - self.0.to_fixed() - } - - fn checked_to_fixed(self) -> Option { - self.0.checked_to_fixed() - } - - fn saturating_to_fixed(self) -> F { - self.0.saturating_to_fixed() - } - fn wrapping_to_fixed(self) -> F { - self.0.wrapping_to_fixed() - } - - fn overflowing_to_fixed(self) -> (F, bool) { - self.0.overflowing_to_fixed() - } - } - } + impl $currency_type { + pub const fn new(inner: u64) -> Self { + Self(inner) + } + } + + impl TypeInfo for $currency_type { + type Identity = ::Identity; + fn type_info() -> scale_info::Type { + ::type_info() + } + } + + impl Display for $currency_type { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + Display::fmt(&self.0, f) + } + } + + impl CompactAs for $currency_type { + type As = u64; + + fn encode_as(&self) -> &Self::As { + &self.0 + } + + fn decode_from(v: Self::As) -> Result { + Ok(Self(v)) + } + } + + impl From> for $currency_type { + fn from(c: Compact<$currency_type>) -> Self { + c.0 + } + } + + impl From<$currency_type> for u64 { + fn from(val: $currency_type) -> Self { + val.0 + } + } + + impl From for $currency_type { + fn from(value: u64) -> Self { + Self(value) + } + } + + impl ToFixed for $currency_type { + fn to_fixed(self) -> F { + self.0.to_fixed() + } + + fn checked_to_fixed(self) -> Option { + self.0.checked_to_fixed() + } + + fn saturating_to_fixed(self) -> F { + self.0.saturating_to_fixed() + } + fn wrapping_to_fixed(self) -> F { + self.0.wrapping_to_fixed() + } + + fn overflowing_to_fixed(self) -> (F, bool) { + self.0.overflowing_to_fixed() + } + } + }; } macro_rules! impl_arithmetic_operators { @@ -261,6 +267,6 @@ impl Currency for AlphaCurrency { } impl Currency for TaoCurrency { - const MAX: Self = Self(u64::MAX); - const ZERO: Self = Self(0); + const MAX: Self = Self(u64::MAX); + const ZERO: Self = Self(0); } diff --git a/common/src/lib.rs b/common/src/lib.rs index 44b7fb879a..d21cee4f2c 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -154,7 +154,7 @@ impl Default for ProxyType { } pub trait SubnetInfo { - fn tao_reserve(netuid: NetUid) -> u64; + fn tao_reserve(netuid: NetUid) -> TaoCurrency; fn alpha_reserve(netuid: NetUid) -> AlphaCurrency; fn exists(netuid: NetUid) -> bool; fn mechanism(netuid: NetUid) -> u16; @@ -162,10 +162,13 @@ pub trait SubnetInfo { } pub trait BalanceOps { - fn tao_balance(account_id: &AccountId) -> u64; + fn tao_balance(account_id: &AccountId) -> TaoCurrency; fn alpha_balance(netuid: NetUid, coldkey: &AccountId, hotkey: &AccountId) -> AlphaCurrency; - fn increase_balance(coldkey: &AccountId, tao: u64); - fn decrease_balance(coldkey: &AccountId, tao: u64) -> Result; + fn increase_balance(coldkey: &AccountId, tao: TaoCurrency); + fn decrease_balance( + coldkey: &AccountId, + tao: TaoCurrency, + ) -> Result; fn increase_stake( coldkey: &AccountId, hotkey: &AccountId, @@ -178,8 +181,8 @@ pub trait BalanceOps { netuid: NetUid, alpha: AlphaCurrency, ) -> Result; - fn increase_provided_tao_reserve(netuid: NetUid, tao: u64); - fn decrease_provided_tao_reserve(netuid: NetUid, tao: u64); + fn increase_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency); + fn decrease_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency); fn increase_provided_alpha_reserve(netuid: NetUid, alpha: AlphaCurrency); fn decrease_provided_alpha_reserve(netuid: NetUid, alpha: AlphaCurrency); } diff --git a/pallets/admin-utils/src/benchmarking.rs b/pallets/admin-utils/src/benchmarking.rs index 917d9008ba..73b4235d86 100644 --- a/pallets/admin-utils/src/benchmarking.rs +++ b/pallets/admin-utils/src/benchmarking.rs @@ -266,7 +266,7 @@ mod benchmarks { ); #[extrinsic_call] - _(RawOrigin::Root, 1u16.into()/*netuid*/, 10u64/*max_burn*/)/*sudo_set_max_burn*/; + _(RawOrigin::Root, 1u16.into()/*netuid*/, 10.into()/*max_burn*/)/*sudo_set_max_burn*/; } #[benchmark] @@ -277,7 +277,7 @@ mod benchmarks { ); #[extrinsic_call] - _(RawOrigin::Root, 1u16.into()/*netuid*/, 10u64/*min_burn*/)/*sudo_set_min_burn*/; + _(RawOrigin::Root, 1u16.into()/*netuid*/, 10.into()/*min_burn*/)/*sudo_set_min_burn*/; } #[benchmark] diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index e4d7b225e7..4df250eac1 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -31,7 +31,7 @@ pub mod pallet { use pallet_subtensor::utils::rate_limiting::TransactionType; use sp_runtime::BoundedVec; use substrate_fixed::types::I96F32; - use subtensor_runtime_common::NetUid; + use subtensor_runtime_common::{NetUid, TaoCurrency}; /// The main data structure of the module. #[pallet::pallet] @@ -691,7 +691,7 @@ pub mod pallet { pub fn sudo_set_min_burn( origin: OriginFor, netuid: NetUid, - min_burn: u64, + min_burn: TaoCurrency, ) -> DispatchResult { ensure_root(origin)?; @@ -718,7 +718,7 @@ pub mod pallet { pub fn sudo_set_max_burn( origin: OriginFor, netuid: NetUid, - max_burn: u64, + max_burn: TaoCurrency, ) -> DispatchResult { ensure_root(origin)?; @@ -955,7 +955,7 @@ pub mod pallet { #[pallet::weight((0, DispatchClass::Operational, Pays::No))] pub fn sudo_set_total_issuance( origin: OriginFor, - total_issuance: u64, + total_issuance: TaoCurrency, ) -> DispatchResult { ensure_root(origin)?; @@ -999,7 +999,7 @@ pub mod pallet { ))] pub fn sudo_set_network_min_lock_cost( origin: OriginFor, - lock_cost: u64, + lock_cost: TaoCurrency, ) -> DispatchResult { ensure_root(origin)?; @@ -1056,7 +1056,7 @@ pub mod pallet { pub fn sudo_set_rao_recycled( origin: OriginFor, netuid: NetUid, - rao_recycled: u64, + rao_recycled: TaoCurrency, ) -> DispatchResult { ensure_root(origin)?; ensure!( diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 51f2f7364c..72a57f4d39 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -11,7 +11,7 @@ use pallet_subtensor::Event; use sp_consensus_grandpa::AuthorityId as GrandpaId; use sp_core::{Get, Pair, U256, ed25519}; use substrate_fixed::types::I96F32; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{Currency, NetUid, TaoCurrency}; use crate::Error; use crate::pallet::PrecompileEnable; @@ -426,7 +426,7 @@ fn test_sudo_set_max_weight_limit() { #[test] fn test_sudo_set_issuance() { new_test_ext().execute_with(|| { - let to_be_set: u64 = 10; + let to_be_set = TaoCurrency::from(10); assert_eq!( AdminUtils::sudo_set_total_issuance( <::RuntimeOrigin>::signed(U256::from(0)), @@ -894,9 +894,9 @@ fn test_sudo_set_bonds_penalty() { fn test_sudo_set_rao_recycled() { new_test_ext().execute_with(|| { let netuid = NetUid::from(1); - let to_be_set: u64 = 10; + let to_be_set = TaoCurrency::from(10); add_network(netuid, 10); - let init_value: u64 = SubtensorModule::get_rao_recycled(netuid); + let init_value = SubtensorModule::get_rao_recycled(netuid); // Need to run from genesis block run_to_block(1); @@ -1035,7 +1035,7 @@ mod sudo_set_nominator_min_required_stake { let default_min_stake = pallet_subtensor::DefaultMinStake::::get(); assert_eq!( SubtensorModule::get_nominator_min_required_stake(), - 10 * default_min_stake / 1_000_000 + 10 * default_min_stake.to_u64() / 1_000_000 ); assert_ok!(AdminUtils::sudo_set_nominator_min_required_stake( @@ -1044,7 +1044,7 @@ mod sudo_set_nominator_min_required_stake { )); assert_eq!( SubtensorModule::get_nominator_min_required_stake(), - 5 * default_min_stake / 1_000_000 + 5 * default_min_stake.to_u64() / 1_000_000 ); }); } @@ -1060,7 +1060,7 @@ mod sudo_set_nominator_min_required_stake { )); assert_eq!( SubtensorModule::get_nominator_min_required_stake(), - to_be_set * default_min_stake / 1_000_000 + to_be_set * default_min_stake.to_u64() / 1_000_000 ); }); } @@ -1656,7 +1656,7 @@ fn test_sets_a_lower_value_clears_small_nominations() { )); assert_eq!( SubtensorModule::get_nominator_min_required_stake(), - initial_nominator_min_required_stake * default_min_stake / 1_000_000_u64 + initial_nominator_min_required_stake * default_min_stake.to_u64() / 1_000_000 ); // Stake to the hotkey as staker_coldkey @@ -1674,7 +1674,7 @@ fn test_sets_a_lower_value_clears_small_nominations() { )); assert_eq!( SubtensorModule::get_nominator_min_required_stake(), - nominator_min_required_stake_0 * default_min_stake / 1_000_000_u64 + nominator_min_required_stake_0 * default_min_stake.to_u64() / 1_000_000 ); // Check this nomination is not cleared @@ -1692,7 +1692,7 @@ fn test_sets_a_lower_value_clears_small_nominations() { )); assert_eq!( SubtensorModule::get_nominator_min_required_stake(), - nominator_min_required_stake_1 * default_min_stake / 1_000_000_u64 + nominator_min_required_stake_1 * default_min_stake.to_u64() / 1_000_000 ); // Check this nomination is cleared diff --git a/pallets/subtensor/rpc/src/lib.rs b/pallets/subtensor/rpc/src/lib.rs index eb3c7b11ca..2ca16050e8 100644 --- a/pallets/subtensor/rpc/src/lib.rs +++ b/pallets/subtensor/rpc/src/lib.rs @@ -9,7 +9,7 @@ use jsonrpsee::{ use sp_blockchain::HeaderBackend; use sp_runtime::{AccountId32, traits::Block as BlockT}; use std::sync::Arc; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{NetUid, TaoCurrency}; use sp_api::ProvideRuntimeApi; @@ -75,7 +75,7 @@ pub trait SubtensorCustomApi { #[method(name = "subnetInfo_getSubnetState")] fn get_subnet_state(&self, netuid: NetUid, at: Option) -> RpcResult>; #[method(name = "subnetInfo_getLockCost")] - fn get_network_lock_cost(&self, at: Option) -> RpcResult; + fn get_network_lock_cost(&self, at: Option) -> RpcResult; #[method(name = "subnetInfo_getSelectiveMetagraph")] fn get_selective_metagraph( &self, @@ -421,7 +421,7 @@ where } } - fn get_network_lock_cost(&self, at: Option<::Hash>) -> RpcResult { + fn get_network_lock_cost(&self, at: Option<::Hash>) -> RpcResult { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); diff --git a/pallets/subtensor/runtime-api/src/lib.rs b/pallets/subtensor/runtime-api/src/lib.rs index aa85aeffa8..42d12eb686 100644 --- a/pallets/subtensor/runtime-api/src/lib.rs +++ b/pallets/subtensor/runtime-api/src/lib.rs @@ -12,7 +12,7 @@ use pallet_subtensor::rpc_info::{ subnet_info::{SubnetHyperparams, SubnetHyperparamsV2, SubnetInfo, SubnetInfov2}, }; use sp_runtime::AccountId32; -use subtensor_runtime_common::{AlphaCurrency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; // Here we declare the runtime API. It is implemented it the `impl` block in // src/neuron_info.rs, src/subnet_info.rs, and src/delegate_info.rs @@ -53,6 +53,6 @@ sp_api::decl_runtime_apis! { } pub trait SubnetRegistrationRuntimeApi { - fn get_network_registration_cost() -> u64; + fn get_network_registration_cost() -> TaoCurrency; } } diff --git a/pallets/subtensor/src/benchmarks.rs b/pallets/subtensor/src/benchmarks.rs index 7a6686f46c..ed593a1ada 100644 --- a/pallets/subtensor/src/benchmarks.rs +++ b/pallets/subtensor/src/benchmarks.rs @@ -15,7 +15,7 @@ use sp_runtime::{ traits::{BlakeTwo256, Hash}, }; use sp_std::vec; -use subtensor_runtime_common::{AlphaCurrency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; #[frame_benchmarking::v2::benchmarks] mod pallet_benchmarks { @@ -72,7 +72,7 @@ mod pallet_benchmarks { let coldkey: T::AccountId = account("Test", 0, seed); seed += 1; - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); let amount_to_be_staked: u64 = 1_000_000; Subtensor::::add_balance_to_coldkey_account(&coldkey, amount_to_be_staked); @@ -105,7 +105,7 @@ mod pallet_benchmarks { Subtensor::::init_new_network(netuid, tempo); SubtokenEnabled::::insert(netuid, true); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); Subtensor::::set_max_allowed_uids(netuid, 4096); Subtensor::::set_network_registration_allowed(netuid, true); @@ -132,17 +132,17 @@ mod pallet_benchmarks { Subtensor::::init_new_network(netuid, tempo); SubtokenEnabled::::insert(netuid, true); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); Subtensor::::set_network_registration_allowed(netuid, true); Subtensor::::set_max_allowed_uids(netuid, 4096); let seed: u32 = 1; let coldkey: T::AccountId = account("Test", 0, seed); let hotkey: T::AccountId = account("Alice", 0, seed); - let total_stake: u64 = 1_000_000_000; - let amount: u64 = 60_000_000; + let total_stake = TaoCurrency::from(1_000_000_000); + let amount = TaoCurrency::from(60_000_000); - Subtensor::::add_balance_to_coldkey_account(&coldkey, total_stake); + Subtensor::::add_balance_to_coldkey_account(&coldkey, total_stake.into()); assert_ok!(Subtensor::::do_burned_registration( RawOrigin::Signed(coldkey.clone()).into(), netuid, @@ -174,9 +174,9 @@ mod pallet_benchmarks { SubtokenEnabled::::insert(netuid, true); Subtensor::::set_max_allowed_uids(netuid, 4096); - let reg_fee: u64 = Subtensor::::get_burn_as_u64(netuid); - let deposit = reg_fee.saturating_mul(2); - Subtensor::::add_balance_to_coldkey_account(&caller, deposit); + let reg_fee = Subtensor::::get_burn(netuid); + let deposit = reg_fee.saturating_mul(2.into()); + Subtensor::::add_balance_to_coldkey_account(&caller, deposit.into()); assert_ok!(Subtensor::::do_burned_registration( RawOrigin::Signed(caller.clone()).into(), @@ -212,9 +212,9 @@ mod pallet_benchmarks { SubtokenEnabled::::insert(netuid, true); Subtensor::::set_max_allowed_uids(netuid, 4096); - let reg_fee: u64 = Subtensor::::get_burn_as_u64(netuid); - let deposit = reg_fee.saturating_mul(2); - Subtensor::::add_balance_to_coldkey_account(&caller, deposit); + let reg_fee = Subtensor::::get_burn(netuid); + let deposit = reg_fee.saturating_mul(2.into()); + Subtensor::::add_balance_to_coldkey_account(&caller, deposit.into()); assert_ok!(Subtensor::::do_burned_registration( RawOrigin::Signed(caller.clone()).into(), @@ -243,7 +243,7 @@ mod pallet_benchmarks { Subtensor::::init_new_network(netuid, 1); SubtokenEnabled::::insert(netuid, true); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); let amount: u64 = 1_000_000; Subtensor::::add_balance_to_coldkey_account(&coldkey, amount); @@ -261,7 +261,7 @@ mod pallet_benchmarks { Subtensor::::init_new_network(netuid, 1); SubtokenEnabled::::insert(netuid, true); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); Subtensor::::set_network_registration_allowed(netuid, true); Subtensor::::set_max_allowed_uids(netuid, 4096); assert_eq!(Subtensor::::get_max_allowed_uids(netuid), 4096); @@ -425,9 +425,9 @@ mod pallet_benchmarks { Subtensor::::set_network_registration_allowed(netuid, true); SubtokenEnabled::::insert(netuid, true); - let reg_fee: u64 = Subtensor::::get_burn_as_u64(netuid); - let deposit = reg_fee.saturating_mul(2); - Subtensor::::add_balance_to_coldkey_account(&coldkey, deposit); + let reg_fee = Subtensor::::get_burn(netuid); + let deposit = reg_fee.saturating_mul(2.into()); + Subtensor::::add_balance_to_coldkey_account(&coldkey, deposit.into()); assert_ok!(Subtensor::::do_burned_registration( RawOrigin::Signed(coldkey.clone()).into(), @@ -450,14 +450,14 @@ mod pallet_benchmarks { let new_coldkey: T::AccountId = account("new_coldkey", 0, 0); let hotkey1: T::AccountId = account("hotkey1", 0, 0); let netuid = NetUid::from(1); - let swap_cost: u64 = Subtensor::::get_key_swap_cost(); - let free_balance_old: u64 = 12345 + swap_cost; + let swap_cost = Subtensor::::get_key_swap_cost(); + let free_balance_old = swap_cost + 12345.into(); Subtensor::::init_new_network(netuid, 1); Subtensor::::set_network_registration_allowed(netuid, true); Subtensor::::set_network_pow_registration_allowed(netuid, true); - let block_number: u64 = Subtensor::::get_current_block_as_u64(); + let block_number = Subtensor::::get_current_block_as_u64(); let (nonce, work) = Subtensor::::create_work_for_block_number(netuid, block_number, 3, &hotkey1); let _ = Subtensor::::register( @@ -470,7 +470,7 @@ mod pallet_benchmarks { old_coldkey.clone(), ); - Subtensor::::add_balance_to_coldkey_account(&old_coldkey, free_balance_old); + Subtensor::::add_balance_to_coldkey_account(&old_coldkey, free_balance_old.into()); let name: Vec = b"The fourth Coolest Identity".to_vec(); let identity = ChainIdentity { name, @@ -574,9 +574,9 @@ mod pallet_benchmarks { Subtensor::::init_new_network(netuid, 1); SubtokenEnabled::::insert(netuid, true); Subtensor::::set_network_registration_allowed(netuid, true); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); - let amount_to_be_staked: u64 = 1_000_000_000; + let amount_to_be_staked = 1_000_000_000; Subtensor::::add_balance_to_coldkey_account(&coldkey, amount_to_be_staked); assert_ok!(Subtensor::::do_burned_registration( RawOrigin::Signed(coldkey.clone()).into(), @@ -617,7 +617,7 @@ mod pallet_benchmarks { Subtensor::::init_new_network(netuid, 1); SubtokenEnabled::::insert(netuid, true); Subtensor::::set_network_registration_allowed(netuid, true); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); let amount_to_be_staked: u64 = 1_000_000_000; Subtensor::::add_balance_to_coldkey_account(&coldkey, amount_to_be_staked); @@ -659,8 +659,8 @@ mod pallet_benchmarks { SubtokenEnabled::::insert(netuid, true); Subtensor::::set_network_registration_allowed(netuid, true); - Subtensor::::set_burn(netuid, 1); - let amount_to_be_staked: u64 = 1_000_000; + Subtensor::::set_burn(netuid, 1.into()); + let amount_to_be_staked = 1_000_000; Subtensor::::add_balance_to_coldkey_account(&coldkey, amount_to_be_staked); SubnetOwner::::set(netuid, coldkey.clone()); @@ -704,7 +704,7 @@ mod pallet_benchmarks { Subtensor::::init_new_network(netuid, tempo); SubtokenEnabled::::insert(netuid, true); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); Subtensor::::set_network_registration_allowed(netuid, true); Subtensor::::set_max_allowed_uids(netuid, 4096); @@ -712,11 +712,11 @@ mod pallet_benchmarks { let hotkey: T::AccountId = account("Alice", 0, seed); let amount = 900_000_000_000; - let limit: u64 = 6_000_000_000; - let amount_to_be_staked = 44_000_000_000; + let limit = TaoCurrency::from(6_000_000_000); + let amount_to_be_staked = TaoCurrency::from(44_000_000_000); Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), amount); - let tao_reserve = 150_000_000_000_u64; + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); @@ -748,10 +748,10 @@ mod pallet_benchmarks { SubtokenEnabled::::insert(netuid, true); Subtensor::::init_new_network(netuid, 1); - let burn_fee = Subtensor::::get_burn_as_u64(netuid); - let stake_tao: u64 = DefaultMinStake::::get().saturating_mul(10); - let deposit = burn_fee.saturating_mul(2).saturating_add(stake_tao); - Subtensor::::add_balance_to_coldkey_account(&coldkey, deposit); + let burn_fee = Subtensor::::get_burn(netuid); + let stake_tao = DefaultMinStake::::get().saturating_mul(10.into()); + let deposit = burn_fee.saturating_mul(2.into()).saturating_add(stake_tao); + Subtensor::::add_balance_to_coldkey_account(&coldkey, deposit.into()); assert_ok!(Subtensor::::burned_register( RawOrigin::Signed(coldkey.clone()).into(), @@ -760,7 +760,7 @@ mod pallet_benchmarks { )); SubnetTAO::::insert(netuid, deposit); - SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(deposit)); + SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(deposit.to_u64())); TotalStake::::set(deposit); assert_ok!(Subtensor::::add_stake_limit( @@ -768,7 +768,7 @@ mod pallet_benchmarks { origin.clone(), netuid, stake_tao, - u64::MAX, + TaoCurrency::MAX, false )); @@ -798,7 +798,7 @@ mod pallet_benchmarks { let seed: u32 = 1; // Set our total stake to 1000 TAO - Subtensor::::increase_total_stake(1_000_000_000_000); + Subtensor::::increase_total_stake(1_000_000_000_000.into()); Subtensor::::init_new_network(netuid, tempo); Subtensor::::set_network_registration_allowed(netuid, true); @@ -809,10 +809,10 @@ mod pallet_benchmarks { let coldkey: T::AccountId = account("Test", 0, seed); let hotkey: T::AccountId = account("Alice", 0, seed); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); - let limit: u64 = 1_000_000_000; - let tao_reserve = 150_000_000_000_u64; + let limit = TaoCurrency::from(1_000_000_000); + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); @@ -833,7 +833,7 @@ mod pallet_benchmarks { RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone(), netuid, - u64_staked_amt + u64_staked_amt.into() )); let amount_unstaked = AlphaCurrency::from(30_000_000_000); @@ -865,18 +865,18 @@ mod pallet_benchmarks { SubtokenEnabled::::insert(netuid2, true); Subtensor::::init_new_network(netuid2, 1); - let tao_reserve = 150_000_000_000_u64; + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); SubnetTAO::::insert(netuid1, tao_reserve); SubnetAlphaIn::::insert(netuid1, alpha_in); SubnetTAO::::insert(netuid2, tao_reserve); - Subtensor::::increase_total_stake(1_000_000_000_000); + Subtensor::::increase_total_stake(1_000_000_000_000.into()); let amount = 900_000_000_000; - let limit_stake: u64 = 6_000_000_000; - let limit_swap: u64 = 1_000_000_000; - let amount_to_be_staked = 440_000_000_000; + let limit_stake = TaoCurrency::from(6_000_000_000); + let limit_swap = TaoCurrency::from(1_000_000_000); + let amount_to_be_staked = TaoCurrency::from(440_000_000_000); let amount_swapped = AlphaCurrency::from(30_000_000_000); Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), amount); @@ -926,10 +926,10 @@ mod pallet_benchmarks { SubtokenEnabled::::insert(netuid, true); Subtensor::::init_new_network(netuid, 1); - let reg_fee = Subtensor::::get_burn_as_u64(netuid); - let stake_tao: u64 = DefaultMinStake::::get().saturating_mul(10); - let deposit = reg_fee.saturating_mul(2).saturating_add(stake_tao); - Subtensor::::add_balance_to_coldkey_account(&coldkey, deposit); + let reg_fee = Subtensor::::get_burn(netuid); + let stake_tao = DefaultMinStake::::get().saturating_mul(10.into()); + let deposit = reg_fee.saturating_mul(2.into()).saturating_add(stake_tao); + Subtensor::::add_balance_to_coldkey_account(&coldkey, deposit.into()); assert_ok!(Subtensor::::burned_register( RawOrigin::Signed(coldkey.clone()).into(), @@ -938,7 +938,7 @@ mod pallet_benchmarks { )); SubnetTAO::::insert(netuid, deposit); - SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(deposit)); + SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(deposit.to_u64())); TotalStake::::set(deposit); assert_ok!(Subtensor::::add_stake_limit( @@ -946,7 +946,7 @@ mod pallet_benchmarks { hot.clone(), netuid, stake_tao, - u64::MAX, + TaoCurrency::MAX, false )); @@ -981,10 +981,10 @@ mod pallet_benchmarks { SubtokenEnabled::::insert(netuid2, true); Subtensor::::init_new_network(netuid2, 1); - let reg_fee = Subtensor::::get_burn_as_u64(netuid1); - let stake_tao: u64 = DefaultMinStake::::get().saturating_mul(10); - let deposit = reg_fee.saturating_mul(2).saturating_add(stake_tao); - Subtensor::::add_balance_to_coldkey_account(&coldkey, deposit); + let reg_fee = Subtensor::::get_burn(netuid1); + let stake_tao = DefaultMinStake::::get().saturating_mul(10.into()); + let deposit = reg_fee.saturating_mul(2.into()).saturating_add(stake_tao); + Subtensor::::add_balance_to_coldkey_account(&coldkey, deposit.into()); assert_ok!(Subtensor::::burned_register( RawOrigin::Signed(coldkey.clone()).into(), @@ -993,9 +993,9 @@ mod pallet_benchmarks { )); SubnetTAO::::insert(netuid1, deposit); - SubnetAlphaIn::::insert(netuid1, AlphaCurrency::from(deposit)); + SubnetAlphaIn::::insert(netuid1, AlphaCurrency::from(deposit.to_u64())); SubnetTAO::::insert(netuid2, deposit); - SubnetAlphaIn::::insert(netuid2, AlphaCurrency::from(deposit)); + SubnetAlphaIn::::insert(netuid2, AlphaCurrency::from(deposit.to_u64())); TotalStake::::set(deposit); assert_ok!(Subtensor::::add_stake_limit( @@ -1003,7 +1003,7 @@ mod pallet_benchmarks { hot.clone(), netuid1, stake_tao, - u64::MAX, + TaoCurrency::MAX, false )); @@ -1035,8 +1035,8 @@ mod pallet_benchmarks { Subtensor::::set_network_pow_registration_allowed(netuid, true); SubtokenEnabled::::insert(netuid, true); - let reg_fee = Subtensor::::get_burn_as_u64(netuid); - Subtensor::::add_balance_to_coldkey_account(&hotkey, reg_fee.saturating_mul(2)); + let reg_fee = Subtensor::::get_burn(netuid); + Subtensor::::add_balance_to_coldkey_account(&hotkey, reg_fee.to_u64().saturating_mul(2)); assert_ok!(Subtensor::::burned_register( RawOrigin::Signed(hotkey.clone()).into(), @@ -1074,8 +1074,8 @@ mod pallet_benchmarks { Subtensor::::set_network_registration_allowed(netuid, true); SubtokenEnabled::::insert(netuid, true); - let reg_fee = Subtensor::::get_burn_as_u64(netuid); - Subtensor::::add_balance_to_coldkey_account(&hotkey, reg_fee.saturating_mul(2)); + let reg_fee = Subtensor::::get_burn(netuid); + Subtensor::::add_balance_to_coldkey_account(&hotkey, reg_fee.to_u64().saturating_mul(2)); assert_ok!(Subtensor::::burned_register( RawOrigin::Signed(hotkey.clone()).into(), @@ -1104,8 +1104,8 @@ mod pallet_benchmarks { Subtensor::::set_network_pow_registration_allowed(netuid, true); SubtokenEnabled::::insert(netuid, true); - let reg_fee = Subtensor::::get_burn_as_u64(netuid); - Subtensor::::add_balance_to_coldkey_account(&hotkey, reg_fee.saturating_mul(2)); + let reg_fee = Subtensor::::get_burn(netuid); + Subtensor::::add_balance_to_coldkey_account(&hotkey, reg_fee.to_u64().saturating_mul(2)); assert_ok!(Subtensor::::burned_register( RawOrigin::Signed(hotkey.clone()).into(), @@ -1186,9 +1186,9 @@ mod pallet_benchmarks { Subtensor::::set_network_registration_allowed(netuid, true); SubtokenEnabled::::insert(netuid, true); - let reg_fee = Subtensor::::get_burn_as_u64(netuid); - let deposit: u64 = reg_fee.saturating_mul(2); - Subtensor::::add_balance_to_coldkey_account(&caller, deposit); + let reg_fee = Subtensor::::get_burn(netuid); + let deposit = reg_fee.saturating_mul(2.into()); + Subtensor::::add_balance_to_coldkey_account(&caller, deposit.into()); assert_ok!(Subtensor::::burned_register( RawOrigin::Signed(caller.clone()).into(), @@ -1306,8 +1306,8 @@ mod pallet_benchmarks { let old: T::AccountId = account("A", 0, 7); let new: T::AccountId = account("B", 0, 8); Owner::::insert(&old, &coldkey); - let cost: u64 = Subtensor::::get_key_swap_cost(); - Subtensor::::add_balance_to_coldkey_account(&coldkey, cost); + let cost = Subtensor::::get_key_swap_cost(); + Subtensor::::add_balance_to_coldkey_account(&coldkey, cost.into()); #[extrinsic_call] _( @@ -1352,9 +1352,9 @@ mod pallet_benchmarks { let coldkey: T::AccountId = account("Test", 0, seed); let hotkey: T::AccountId = account("Alice", 0, seed); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); - SubnetTAO::::insert(netuid, 150_000_000_000); + SubnetTAO::::insert(netuid, TaoCurrency::from(150_000_000_000)); SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(100_000_000_000)); Subtensor::::add_balance_to_coldkey_account(&coldkey.clone(), 1000000u32.into()); @@ -1372,7 +1372,7 @@ mod pallet_benchmarks { RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone(), netuid, - staked_amt + staked_amt.into() )); // Remove stake limit for benchmark @@ -1399,7 +1399,7 @@ mod pallet_benchmarks { let seed: u32 = 1; // Set our total stake to 1000 TAO - Subtensor::::increase_total_stake(1_000_000_000_000); + Subtensor::::increase_total_stake(1_000_000_000_000.into()); Subtensor::::init_new_network(netuid, tempo); Subtensor::::set_network_registration_allowed(netuid, true); @@ -1410,10 +1410,10 @@ mod pallet_benchmarks { let coldkey: T::AccountId = account("Test", 0, seed); let hotkey: T::AccountId = account("Alice", 0, seed); - Subtensor::::set_burn(netuid, 1); + Subtensor::::set_burn(netuid, 1.into()); - let limit: u64 = 1_000_000_000; - let tao_reserve = 150_000_000_000_u64; + let limit = TaoCurrency::from(1_000_000_000); + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); @@ -1434,7 +1434,7 @@ mod pallet_benchmarks { RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone(), netuid, - u64_staked_amt + u64_staked_amt.into() )); StakingOperationRateLimiter::::remove((hotkey.clone(), coldkey.clone(), netuid)); diff --git a/pallets/subtensor/src/coinbase/block_emission.rs b/pallets/subtensor/src/coinbase/block_emission.rs index 4f6fb9d95f..174a1d57d3 100644 --- a/pallets/subtensor/src/coinbase/block_emission.rs +++ b/pallets/subtensor/src/coinbase/block_emission.rs @@ -5,7 +5,7 @@ use substrate_fixed::{ transcendental::log2, types::{I96F32, U96F32}, }; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{NetUid, TaoCurrency}; use subtensor_swap_interface::SwapHandler; impl Pallet { @@ -103,9 +103,9 @@ impl Pallet { /// # Returns /// * 'Result': The calculated block emission rate or error. /// - pub fn get_block_emission() -> Result { + pub fn get_block_emission() -> Result { // Convert the total issuance to a fixed-point number for calculation. - Self::get_block_emission_for_issuance(Self::get_total_issuance()) + Self::get_block_emission_for_issuance(Self::get_total_issuance().into()).map(Into::into) } /// Returns the block emission for an issuance value. diff --git a/pallets/subtensor/src/coinbase/block_step.rs b/pallets/subtensor/src/coinbase/block_step.rs index d003a698e3..575cf308e1 100644 --- a/pallets/subtensor/src/coinbase/block_step.rs +++ b/pallets/subtensor/src/coinbase/block_step.rs @@ -1,7 +1,7 @@ use super::*; use safe_math::*; use substrate_fixed::types::{U96F32, U110F18}; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{NetUid, TaoCurrency}; impl Pallet { /// Executes the necessary operations for each block. @@ -11,8 +11,11 @@ impl Pallet { // --- 1. Adjust difficulties. Self::adjust_registration_terms_for_networks(); // --- 2. Get the current coinbase emission. - let block_emission: U96F32 = - U96F32::saturating_from_num(Self::get_block_emission().unwrap_or(0)); + let block_emission: U96F32 = U96F32::saturating_from_num( + Self::get_block_emission() + .unwrap_or(TaoCurrency::ZERO) + .to_u64(), + ); log::debug!("Block emission: {:?}", block_emission); // --- 3. Run emission through network. Self::run_coinbase(block_emission); @@ -56,7 +59,7 @@ impl Pallet { log::debug!("interval reached."); // --- 4. Get the current counters for this network w.r.t burn and difficulty values. - let current_burn: u64 = Self::get_burn_as_u64(netuid); + let current_burn = Self::get_burn(netuid); let current_difficulty: u64 = Self::get_difficulty_as_u64(netuid); let registrations_this_interval: u16 = Self::get_registrations_this_interval(netuid); @@ -228,10 +231,10 @@ impl Pallet { /// pub fn upgraded_burn( netuid: NetUid, - current_burn: u64, + current_burn: TaoCurrency, registrations_this_interval: u16, target_registrations_per_interval: u16, - ) -> u64 { + ) -> TaoCurrency { let updated_burn: U110F18 = U110F18::saturating_from_num(current_burn) .saturating_mul(U110F18::saturating_from_num( registrations_this_interval.saturating_add(target_registrations_per_interval), @@ -248,12 +251,12 @@ impl Pallet { .saturating_sub(alpha) .saturating_mul(updated_burn), ); - if next_value >= U110F18::saturating_from_num(Self::get_max_burn_as_u64(netuid)) { - Self::get_max_burn_as_u64(netuid) - } else if next_value <= U110F18::saturating_from_num(Self::get_min_burn_as_u64(netuid)) { - return Self::get_min_burn_as_u64(netuid); + if next_value >= U110F18::saturating_from_num(Self::get_max_burn(netuid)) { + Self::get_max_burn(netuid) + } else if next_value <= U110F18::saturating_from_num(Self::get_min_burn(netuid)) { + return Self::get_min_burn(netuid); } else { - return next_value.saturating_to_num::(); + return next_value.saturating_to_num::().into(); } } } diff --git a/pallets/subtensor/src/coinbase/root.rs b/pallets/subtensor/src/coinbase/root.rs index 279132e938..9d94bcd064 100644 --- a/pallets/subtensor/src/coinbase/root.rs +++ b/pallets/subtensor/src/coinbase/root.rs @@ -22,7 +22,7 @@ use frame_support::weights::Weight; use safe_math::*; use sp_core::Get; use substrate_fixed::types::I64F64; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; impl Pallet { /// Fetches the total count of root network validators @@ -427,7 +427,7 @@ impl Pallet { pub fn remove_network(netuid: NetUid) { // --- 1. Return balance to subnet owner. let owner_coldkey: T::AccountId = SubnetOwner::::get(netuid); - let reserved_amount: u64 = Self::get_subnet_locked_balance(netuid); + let reserved_amount = Self::get_subnet_locked_balance(netuid); // --- 2. Remove network count. SubnetworkN::::remove(netuid); @@ -502,8 +502,8 @@ impl Pallet { BurnRegistrationsThisInterval::::remove(netuid); // --- 12. Add the balance back to the owner. - Self::add_balance_to_coldkey_account(&owner_coldkey, reserved_amount); - Self::set_subnet_locked_balance(netuid, 0); + Self::add_balance_to_coldkey_account(&owner_coldkey, reserved_amount.into()); + Self::set_subnet_locked_balance(netuid, TaoCurrency::ZERO); SubnetOwner::::remove(netuid); // --- 13. Remove subnet identity if it exists. @@ -532,18 +532,20 @@ impl Pallet { /// * 'u64': /// - The lock cost for the network. /// - pub fn get_network_lock_cost() -> u64 { + pub fn get_network_lock_cost() -> TaoCurrency { let last_lock = Self::get_network_last_lock(); let min_lock = Self::get_network_min_lock(); let last_lock_block = Self::get_network_last_lock_block(); let current_block = Self::get_current_block_as_u64(); let lock_reduction_interval = Self::get_lock_reduction_interval(); - let mult = if last_lock_block == 0 { 1 } else { 2 }; + let mult: TaoCurrency = if last_lock_block == 0 { 1 } else { 2 }.into(); let mut lock_cost = last_lock.saturating_mul(mult).saturating_sub( last_lock + .to_u64() .safe_div(lock_reduction_interval) - .saturating_mul(current_block.saturating_sub(last_lock_block)), + .saturating_mul(current_block.saturating_sub(last_lock_block)) + .into(), ); if lock_cost < min_lock { @@ -574,17 +576,17 @@ impl Pallet { NetworkImmunityPeriod::::set(net_immunity_period); Self::deposit_event(Event::NetworkImmunityPeriodSet(net_immunity_period)); } - pub fn set_network_min_lock(net_min_lock: u64) { + pub fn set_network_min_lock(net_min_lock: TaoCurrency) { NetworkMinLockCost::::set(net_min_lock); Self::deposit_event(Event::NetworkMinLockCostSet(net_min_lock)); } - pub fn get_network_min_lock() -> u64 { + pub fn get_network_min_lock() -> TaoCurrency { NetworkMinLockCost::::get() } - pub fn set_network_last_lock(net_last_lock: u64) { + pub fn set_network_last_lock(net_last_lock: TaoCurrency) { NetworkLastLockCost::::set(net_last_lock); } - pub fn get_network_last_lock() -> u64 { + pub fn get_network_last_lock() -> TaoCurrency { NetworkLastLockCost::::get() } pub fn get_network_last_lock_block() -> u64 { @@ -600,8 +602,11 @@ impl Pallet { pub fn get_lock_reduction_interval() -> u64 { let interval: I64F64 = I64F64::saturating_from_num(NetworkLockReductionInterval::::get()); - let block_emission: I64F64 = - I64F64::saturating_from_num(Self::get_block_emission().unwrap_or(1_000_000_000)); + let block_emission: I64F64 = I64F64::saturating_from_num( + Self::get_block_emission() + .unwrap_or(1_000_000_000.into()) + .to_u64(), + ); let halving: I64F64 = block_emission .checked_div(I64F64::saturating_from_num(1_000_000_000)) .unwrap_or(I64F64::saturating_from_num(0.0)); diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index aaccb1f749..c25ddf77ef 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -2,7 +2,7 @@ use super::*; use alloc::collections::BTreeMap; use safe_math::*; use substrate_fixed::types::U96F32; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; use subtensor_swap_interface::SwapHandler; // Distribute dividends to each hotkey @@ -115,16 +115,17 @@ impl Pallet { *total = total.saturating_add(alpha_out_i); }); // Inject TAO in. - let tao_in_i: u64 = tou64!(*tao_in.get(netuid_i).unwrap_or(&asfloat!(0))); - SubnetTaoInEmission::::insert(*netuid_i, tao_in_i); + let tao_in_i: TaoCurrency = + tou64!(*tao_in.get(netuid_i).unwrap_or(&asfloat!(0))).into(); + SubnetTaoInEmission::::insert(*netuid_i, TaoCurrency::from(tao_in_i)); SubnetTAO::::mutate(*netuid_i, |total| { - *total = total.saturating_add(tao_in_i); + *total = total.saturating_add(tao_in_i.into()); }); TotalStake::::mutate(|total| { - *total = total.saturating_add(tao_in_i); + *total = total.saturating_add(tao_in_i.into()); }); TotalIssuance::::mutate(|total| { - *total = total.saturating_add(tao_in_i); + *total = total.saturating_add(tao_in_i.into()); }); // Adjust protocol liquidity based on new reserves T::SwapInterface::adjust_protocol_liquidity(*netuid_i, tao_in_i, alpha_in_i); @@ -185,11 +186,11 @@ impl Pallet { let swap_result = Self::swap_alpha_for_tao( *netuid_i, tou64!(root_alpha).into(), - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), true, ); if let Ok(ok_result) = swap_result { - let root_tao: u64 = ok_result.amount_paid_out; + let root_tao = ok_result.amount_paid_out; log::debug!("root_tao: {:?}", root_tao); // Accumulate alpha emission in pending. @@ -202,7 +203,7 @@ impl Pallet { }); // Accumulate root divs for subnet. PendingRootDivs::::mutate(*netuid_i, |total| { - *total = total.saturating_add(root_tao); + *total = total.saturating_add(root_tao.into()); }); } } @@ -236,8 +237,8 @@ impl Pallet { PendingEmission::::insert(netuid, AlphaCurrency::ZERO); // Get and drain the subnet pending root divs. - let pending_tao: u64 = PendingRootDivs::::get(netuid); - PendingRootDivs::::insert(netuid, 0); + let pending_tao = PendingRootDivs::::get(netuid); + PendingRootDivs::::insert(netuid, TaoCurrency::ZERO); // Get this amount as alpha that was swapped for pending root divs. let pending_swapped = PendingAlphaSwapped::::get(netuid); @@ -297,7 +298,7 @@ impl Pallet { pub fn calculate_dividend_distribution( pending_alpha: AlphaCurrency, - pending_tao: u64, + pending_tao: TaoCurrency, tao_weight: U96F32, stake_map: BTreeMap, dividends: BTreeMap, @@ -504,13 +505,13 @@ impl Pallet { ); // Record root dividends for this validator on this subnet. TaoDividendsPerSubnet::::mutate(netuid, hotkey.clone(), |divs| { - *divs = divs.saturating_add(tou64!(root_tao)); + *divs = divs.saturating_add(tou64!(root_tao).into()); }); // Update the total TAO on the subnet with root tao dividends. SubnetTAO::::mutate(NetUid::ROOT, |total| { *total = total - .saturating_add(validator_stake.to_u64()) - .saturating_add(tou64!(root_tao)); + .saturating_add(validator_stake.to_u64().into()) + .saturating_add(tou64!(root_tao).into()); }); } } @@ -532,7 +533,7 @@ impl Pallet { pub fn calculate_dividend_and_incentive_distribution( netuid: NetUid, - pending_tao: u64, + pending_tao: TaoCurrency, pending_validator_alpha: AlphaCurrency, hotkey_emission: Vec<(T::AccountId, AlphaCurrency, AlphaCurrency)>, tao_weight: U96F32, @@ -562,7 +563,7 @@ impl Pallet { pub fn drain_pending_emission( netuid: NetUid, pending_alpha: AlphaCurrency, - pending_tao: u64, + pending_tao: TaoCurrency, pending_swapped: AlphaCurrency, owner_cut: AlphaCurrency, ) { diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index edd50d7033..17336bf04f 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -32,7 +32,7 @@ use sp_runtime::{ transaction_validity::{TransactionValidity, TransactionValidityError}, }; use sp_std::marker::PhantomData; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; // ============================ // ==== Benchmark Imports ===== @@ -92,7 +92,7 @@ pub mod pallet { use sp_std::vec::Vec; use substrate_fixed::types::{I96F32, U64F64}; use subtensor_macros::freeze_struct; - use subtensor_runtime_common::{AlphaCurrency, NetUid}; + use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; #[cfg(not(feature = "std"))] use alloc::boxed::Box; @@ -111,7 +111,7 @@ pub mod pallet { const STORAGE_VERSION: StorageVersion = StorageVersion::new(7); /// Minimum balance required to perform a coldkey swap - pub const MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP: u64 = 100_000_000; // 0.1 TAO in RAO + pub const MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP: TaoCurrency = TaoCurrency::new(100_000_000); // 0.1 TAO in RAO #[pallet::pallet] #[pallet::without_storage_info] @@ -314,7 +314,12 @@ pub mod pallet { /// Default value for Alpha cyrrency. #[pallet::type_value] pub fn DefaultZeroAlpha() -> AlphaCurrency { - 0.into() + AlphaCurrency::ZERO + } + /// Default value for Tao cyrrency. + #[pallet::type_value] + pub fn DefaultZeroTao() -> TaoCurrency { + TaoCurrency::ZERO } #[pallet::type_value] /// Default value for zero. @@ -392,8 +397,8 @@ pub mod pallet { } #[pallet::type_value] /// Default total issuance. - pub fn DefaultTotalIssuance() -> u64 { - T::InitialIssuance::get() + pub fn DefaultTotalIssuance() -> TaoCurrency { + T::InitialIssuance::get().into() } #[pallet::type_value] /// Default account, derived from zero trailing bytes. @@ -441,18 +446,18 @@ pub mod pallet { } #[pallet::type_value] /// Default registrations this block. - pub fn DefaultBurn() -> u64 { - T::InitialBurn::get() + pub fn DefaultBurn() -> TaoCurrency { + T::InitialBurn::get().into() } #[pallet::type_value] /// Default burn token. - pub fn DefaultMinBurn() -> u64 { - T::InitialMinBurn::get() + pub fn DefaultMinBurn() -> TaoCurrency { + T::InitialMinBurn::get().into() } #[pallet::type_value] /// Default min burn token. - pub fn DefaultMaxBurn() -> u64 { - T::InitialMaxBurn::get() + pub fn DefaultMaxBurn() -> TaoCurrency { + T::InitialMaxBurn::get().into() } #[pallet::type_value] /// Default max burn token. @@ -476,8 +481,8 @@ pub mod pallet { } #[pallet::type_value] /// Default max registrations per block. - pub fn DefaultRAORecycledForRegistration() -> u64 { - T::InitialRAORecycledForRegistration::get() + pub fn DefaultRAORecycledForRegistration() -> TaoCurrency { + T::InitialRAORecycledForRegistration::get().into() } #[pallet::type_value] /// Default number of networks. @@ -531,8 +536,8 @@ pub mod pallet { } #[pallet::type_value] /// Default value for network min lock cost. - pub fn DefaultNetworkMinLockCost() -> u64 { - T::InitialNetworkMinLockCost::get() + pub fn DefaultNetworkMinLockCost() -> TaoCurrency { + T::InitialNetworkMinLockCost::get().into() } #[pallet::type_value] /// Default value for network lock reduction interval. @@ -807,8 +812,8 @@ pub mod pallet { #[pallet::type_value] /// Default minimum stake. - pub fn DefaultMinStake() -> u64 { - 2_000_000 + pub fn DefaultMinStake() -> TaoCurrency { + 2_000_000.into() } #[pallet::type_value] @@ -998,9 +1003,9 @@ pub mod pallet { NetUid, Blake2_128Concat, T::AccountId, - u64, + TaoCurrency, ValueQuery, - DefaultZeroU64, + DefaultZeroTao, >; /// ================== @@ -1035,9 +1040,9 @@ pub mod pallet { /// Eventually, Bittensor should migrate to using Holds afterwhich time we will not require this /// separate accounting. #[pallet::storage] // --- ITEM ( total_issuance ) - pub type TotalIssuance = StorageValue<_, u64, ValueQuery, DefaultTotalIssuance>; + pub type TotalIssuance = StorageValue<_, TaoCurrency, ValueQuery, DefaultTotalIssuance>; #[pallet::storage] // --- ITEM ( total_stake ) - pub type TotalStake = StorageValue<_, u64, ValueQuery>; + pub type TotalStake = StorageValue<_, TaoCurrency, ValueQuery>; #[pallet::storage] // --- ITEM ( moving_alpha ) -- subnet moving alpha. pub type SubnetMovingAlpha = StorageValue<_, I96F32, ValueQuery, DefaultMovingAlpha>; #[pallet::storage] // --- MAP ( netuid ) --> moving_price | The subnet moving price. @@ -1048,10 +1053,10 @@ pub mod pallet { StorageMap<_, Identity, NetUid, u128, ValueQuery, DefaultZeroU128>; #[pallet::storage] // --- MAP ( netuid ) --> tao_in_subnet | Returns the amount of TAO in the subnet. pub type SubnetTAO = - StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultZeroU64>; + StorageMap<_, Identity, NetUid, TaoCurrency, ValueQuery, DefaultZeroTao>; #[pallet::storage] // --- MAP ( netuid ) --> tao_in_user_subnet | Returns the amount of TAO in the subnet reserve provided by users as liquidity. pub type SubnetTaoProvided = - StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultZeroU64>; + StorageMap<_, Identity, NetUid, TaoCurrency, ValueQuery, DefaultZeroTao>; #[pallet::storage] // --- MAP ( netuid ) --> alpha_in_emission | Returns the amount of alph in emission into the pool per block. pub type SubnetAlphaInEmission = StorageMap<_, Identity, NetUid, AlphaCurrency, ValueQuery, DefaultZeroAlpha>; @@ -1060,7 +1065,7 @@ pub mod pallet { StorageMap<_, Identity, NetUid, AlphaCurrency, ValueQuery, DefaultZeroAlpha>; #[pallet::storage] // --- MAP ( netuid ) --> tao_in_emission | Returns the amount of tao emitted into this subent on the last block. pub type SubnetTaoInEmission = - StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultZeroU64>; + StorageMap<_, Identity, NetUid, TaoCurrency, ValueQuery, DefaultZeroTao>; #[pallet::storage] // --- MAP ( netuid ) --> alpha_supply_in_pool | Returns the amount of alpha in the pool. pub type SubnetAlphaIn = StorageMap<_, Identity, NetUid, AlphaCurrency, ValueQuery, DefaultZeroAlpha>; @@ -1160,11 +1165,12 @@ pub mod pallet { StorageValue<_, u64, ValueQuery, DefaultNetworkLastRegistered>; #[pallet::storage] /// ITEM( min_network_lock_cost ) - pub type NetworkMinLockCost = StorageValue<_, u64, ValueQuery, DefaultNetworkMinLockCost>; + pub type NetworkMinLockCost = + StorageValue<_, TaoCurrency, ValueQuery, DefaultNetworkMinLockCost>; #[pallet::storage] /// ITEM( last_network_lock_cost ) pub type NetworkLastLockCost = - StorageValue<_, u64, ValueQuery, DefaultNetworkMinLockCost>; + StorageValue<_, TaoCurrency, ValueQuery, DefaultNetworkMinLockCost>; #[pallet::storage] /// ITEM( network_lock_reduction_interval ) pub type NetworkLockReductionInterval = @@ -1200,7 +1206,7 @@ pub mod pallet { StorageMap<_, Identity, NetUid, bool, ValueQuery, DefaultTrue>; #[pallet::storage] // --- MAP ( netuid ) --> total_subnet_locked pub type SubnetLocked = - StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultZeroU64>; + StorageMap<_, Identity, NetUid, TaoCurrency, ValueQuery, DefaultZeroTao>; #[pallet::storage] // --- MAP ( netuid ) --> largest_locked pub type LargestLocked = StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultZeroU64>; @@ -1264,7 +1270,7 @@ pub mod pallet { #[pallet::storage] /// --- MAP ( netuid ) --> pending_root_emission pub type PendingRootDivs = - StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultZeroU64>; + StorageMap<_, Identity, NetUid, TaoCurrency, ValueQuery, DefaultZeroTao>; #[pallet::storage] /// --- MAP ( netuid ) --> pending_alpha_swapped pub type PendingAlphaSwapped = @@ -1385,16 +1391,18 @@ pub mod pallet { StorageMap<_, Identity, NetUid, bool, ValueQuery, DefaultCommitRevealWeightsEnabled>; #[pallet::storage] /// --- MAP ( netuid ) --> Burn - pub type Burn = StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultBurn>; + pub type Burn = StorageMap<_, Identity, NetUid, TaoCurrency, ValueQuery, DefaultBurn>; #[pallet::storage] /// --- MAP ( netuid ) --> Difficulty pub type Difficulty = StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultDifficulty>; #[pallet::storage] /// --- MAP ( netuid ) --> MinBurn - pub type MinBurn = StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultMinBurn>; + pub type MinBurn = + StorageMap<_, Identity, NetUid, TaoCurrency, ValueQuery, DefaultMinBurn>; #[pallet::storage] /// --- MAP ( netuid ) --> MaxBurn - pub type MaxBurn = StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultMaxBurn>; + pub type MaxBurn = + StorageMap<_, Identity, NetUid, TaoCurrency, ValueQuery, DefaultMaxBurn>; #[pallet::storage] /// --- MAP ( netuid ) --> MinDifficulty pub type MinDifficulty = @@ -1417,8 +1425,14 @@ pub mod pallet { StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultEMAPriceMovingBlocks>; #[pallet::storage] /// --- MAP ( netuid ) --> global_RAO_recycled_for_registration - pub type RAORecycledForRegistration = - StorageMap<_, Identity, NetUid, u64, ValueQuery, DefaultRAORecycledForRegistration>; + pub type RAORecycledForRegistration = StorageMap< + _, + Identity, + NetUid, + TaoCurrency, + ValueQuery, + DefaultRAORecycledForRegistration, + >; #[pallet::storage] /// --- ITEM ( tx_rate_limit ) pub type TxRateLimit = StorageValue<_, u64, ValueQuery, DefaultTxRateLimit>; @@ -1631,6 +1645,7 @@ pub mod pallet { /// --- MAP ( key ) --> last_tx_block_delegate_take pub type LastTxBlockDelegateTake = StorageMap<_, Identity, T::AccountId, u64, ValueQuery, DefaultLastTxBlock>; + // FIXME: this storage is used interchangably for alpha/tao #[pallet::storage] /// ITEM( weights_min_stake ) pub type StakeThreshold = StorageValue<_, u64, ValueQuery, DefaultStakeThreshold>; @@ -1771,14 +1786,14 @@ pub mod pallet { /// Stakes record in genesis. pub stakes: Vec<(T::AccountId, Vec<(T::AccountId, (u64, u16))>)>, /// The total issued balance in genesis - pub balances_issuance: u64, + pub balances_issuance: TaoCurrency, } impl Default for GenesisConfig { fn default() -> Self { Self { stakes: Default::default(), - balances_issuance: 0, + balances_issuance: TaoCurrency::ZERO, } } } @@ -1798,6 +1813,8 @@ pub mod pallet { 0 } + // FIXME this function is used both to calculate for alpha stake amount as well as tao + // amount /// Returns the transaction priority for stake operations. pub fn get_priority_staking( coldkey: &T::AccountId, @@ -2136,7 +2153,7 @@ where *amount_staked, false, ), - Self::get_priority_staking(who, hotkey, *amount_staked), + Self::get_priority_staking(who, hotkey, (*amount_staked).to_u64()), ) .map(|validity| (validity, Some(who.clone()), origin.clone())) } @@ -2163,10 +2180,10 @@ where hotkey, *netuid, *amount_staked, - max_amount, + max_amount.into(), *allow_partial, ), - Self::get_priority_staking(who, hotkey, *amount_staked), + Self::get_priority_staking(who, hotkey, (*amount_staked).to_u64()), ) .map(|validity| (validity, Some(who.clone()), origin.clone())) } @@ -2185,7 +2202,7 @@ where *amount_unstaked, false, ), - Self::get_priority_staking(who, hotkey, (*amount_unstaked).into()), + Self::get_priority_staking(who, hotkey, (*amount_unstaked).to_u64()), ) .map(|validity| (validity, Some(who.clone()), origin.clone())) } @@ -2592,7 +2609,7 @@ impl CollectiveInterface for () { impl> subtensor_runtime_common::SubnetInfo for Pallet { - fn tao_reserve(netuid: NetUid) -> u64 { + fn tao_reserve(netuid: NetUid) -> TaoCurrency { SubnetTAO::::get(netuid).saturating_add(SubnetTaoProvided::::get(netuid)) } @@ -2616,8 +2633,8 @@ impl> impl> subtensor_runtime_common::BalanceOps for Pallet { - fn tao_balance(account_id: &T::AccountId) -> u64 { - pallet_balances::Pallet::::free_balance(account_id) + fn tao_balance(account_id: &T::AccountId) -> TaoCurrency { + pallet_balances::Pallet::::free_balance(account_id).into() } fn alpha_balance( @@ -2628,12 +2645,15 @@ impl> Self::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid) } - fn increase_balance(coldkey: &T::AccountId, tao: u64) { - Self::add_balance_to_coldkey_account(coldkey, tao) + fn increase_balance(coldkey: &T::AccountId, tao: TaoCurrency) { + Self::add_balance_to_coldkey_account(coldkey, tao.into()) } - fn decrease_balance(coldkey: &T::AccountId, tao: u64) -> Result { - Self::remove_balance_from_coldkey_account(coldkey, tao) + fn decrease_balance( + coldkey: &T::AccountId, + tao: TaoCurrency, + ) -> Result { + Self::remove_balance_from_coldkey_account(coldkey, tao.into()) } fn increase_stake( @@ -2678,11 +2698,11 @@ impl> )) } - fn increase_provided_tao_reserve(netuid: NetUid, tao: u64) { + fn increase_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency) { Self::increase_provided_tao_reserve(netuid, tao); } - fn decrease_provided_tao_reserve(netuid: NetUid, tao: u64) { + fn decrease_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency) { Self::decrease_provided_tao_reserve(netuid, tao); } diff --git a/pallets/subtensor/src/macros/dispatches.rs b/pallets/subtensor/src/macros/dispatches.rs index 86ec02b738..fdb7765094 100644 --- a/pallets/subtensor/src/macros/dispatches.rs +++ b/pallets/subtensor/src/macros/dispatches.rs @@ -592,7 +592,7 @@ mod dispatches { origin: OriginFor, hotkey: T::AccountId, netuid: NetUid, - amount_staked: u64, + amount_staked: TaoCurrency, ) -> DispatchResult { Self::do_add_stake(origin, hotkey, netuid, amount_staked) } @@ -972,7 +972,7 @@ mod dispatches { origin: OriginFor, old_coldkey: T::AccountId, new_coldkey: T::AccountId, - swap_cost: u64, + swap_cost: TaoCurrency, ) -> DispatchResultWithPostInfo { // Ensure it's called with root privileges (scheduler has root privileges) ensure_root(origin)?; @@ -1015,9 +1015,9 @@ mod dispatches { Weight::from_parts(46_330_000, 0) .saturating_add(T::DbWeight::get().reads(5)) .saturating_add(T::DbWeight::get().writes(2)), - DispatchClass::Normal, - Pays::Yes -))] + DispatchClass::Normal, + Pays::Yes + ))] pub fn set_childkey_take( origin: OriginFor, hotkey: T::AccountId, @@ -1348,7 +1348,7 @@ mod dispatches { // Calculate the swap cost and ensure sufficient balance let swap_cost = Self::get_key_swap_cost(); ensure!( - Self::can_remove_balance_from_coldkey_account(&who, swap_cost), + Self::can_remove_balance_from_coldkey_account(&who, swap_cost.into()), Error::::NotEnoughBalanceToPaySwapColdKey ); @@ -1794,8 +1794,8 @@ mod dispatches { origin: OriginFor, hotkey: T::AccountId, netuid: NetUid, - amount_staked: u64, - limit_price: u64, + amount_staked: TaoCurrency, + limit_price: TaoCurrency, allow_partial: bool, ) -> DispatchResult { Self::do_add_stake_limit( @@ -1859,7 +1859,7 @@ mod dispatches { hotkey: T::AccountId, netuid: NetUid, amount_unstaked: AlphaCurrency, - limit_price: u64, + limit_price: TaoCurrency, allow_partial: bool, ) -> DispatchResult { Self::do_remove_stake_limit( @@ -1907,7 +1907,7 @@ mod dispatches { origin_netuid: NetUid, destination_netuid: NetUid, alpha_amount: AlphaCurrency, - limit_price: u64, + limit_price: TaoCurrency, allow_partial: bool, ) -> DispatchResult { Self::do_swap_stake_limit( @@ -2082,7 +2082,7 @@ mod dispatches { origin: T::RuntimeOrigin, hotkey: T::AccountId, netuid: NetUid, - limit_price: Option, + limit_price: Option, ) -> DispatchResult { Self::do_remove_stake_full_limit(origin, hotkey, netuid, limit_price) } diff --git a/pallets/subtensor/src/macros/events.rs b/pallets/subtensor/src/macros/events.rs index 4b50275e0f..cd6bd714ef 100644 --- a/pallets/subtensor/src/macros/events.rs +++ b/pallets/subtensor/src/macros/events.rs @@ -14,9 +14,23 @@ mod events { /// a network is removed. NetworkRemoved(NetUid), /// stake has been transferred from the a coldkey account onto the hotkey staking account. - StakeAdded(T::AccountId, T::AccountId, u64, AlphaCurrency, NetUid, u64), + StakeAdded( + T::AccountId, + T::AccountId, + TaoCurrency, + AlphaCurrency, + NetUid, + u64, + ), /// stake has been removed from the hotkey staking account onto the coldkey account. - StakeRemoved(T::AccountId, T::AccountId, u64, AlphaCurrency, NetUid, u64), + StakeRemoved( + T::AccountId, + T::AccountId, + TaoCurrency, + AlphaCurrency, + NetUid, + u64, + ), /// stake has been moved from origin (hotkey, subnet ID) to destination (hotkey, subnet ID) of this amount (in TAO). StakeMoved( T::AccountId, @@ -24,7 +38,7 @@ mod events { NetUid, T::AccountId, NetUid, - u64, + TaoCurrency, ), /// a caller successfully sets their weights on a subnetwork. WeightsSet(NetUid, u16), @@ -89,11 +103,11 @@ mod events { /// setting the prometheus serving rate limit. ServingRateLimitSet(NetUid, u64), /// setting burn on a network. - BurnSet(NetUid, u64), + BurnSet(NetUid, TaoCurrency), /// setting max burn on a network. - MaxBurnSet(NetUid, u64), + MaxBurnSet(NetUid, TaoCurrency), /// setting min burn on a network. - MinBurnSet(NetUid, u64), + MinBurnSet(NetUid, TaoCurrency), /// setting the transaction rate limit. TxRateLimitSet(u64), /// setting the delegate take transaction rate limit. @@ -115,7 +129,7 @@ mod events { /// setting tempo on a network TempoSet(NetUid, u16), /// setting the RAO recycled for registration. - RAORecycledForRegistrationSet(NetUid, u64), + RAORecycledForRegistrationSet(NetUid, TaoCurrency), /// min stake is set for validators to set weights. StakeThresholdSet(u64), /// setting the minimum required stake amount for senate registration. @@ -131,7 +145,7 @@ mod events { /// the network immunity period is set. NetworkImmunityPeriodSet(u64), /// the network minimum locking cost is set. - NetworkMinLockCostSet(u64), + NetworkMinLockCostSet(TaoCurrency), /// the maximum number of subnets is set // SubnetLimitSet(u16), /// the lock cost reduction is set @@ -167,7 +181,7 @@ mod events { /// the account ID of new coldkey new_coldkey: T::AccountId, /// the swap cost - swap_cost: u64, + swap_cost: TaoCurrency, }, /// All balance of a hotkey has been unstaked and transferred to a new coldkey AllBalanceUnstakedAndTransferredToNewColdkey { @@ -189,7 +203,7 @@ mod events { /// The arbitration block for the coldkey swap execution_block: BlockNumberFor, /// The swap cost - swap_cost: u64, + swap_cost: TaoCurrency, }, /// The arbitration period has been extended ArbitrationPeriodExtended { @@ -273,14 +287,14 @@ mod events { T::AccountId, NetUid, NetUid, - u64, + TaoCurrency, ), /// Stake has been swapped from one subnet to another for the same coldkey-hotkey pair. /// /// Parameters: /// (coldkey, hotkey, origin_netuid, destination_netuid, amount) - StakeSwapped(T::AccountId, T::AccountId, NetUid, NetUid, u64), + StakeSwapped(T::AccountId, T::AccountId, NetUid, NetUid, TaoCurrency), /// Event called when transfer is toggled on a subnet. /// diff --git a/pallets/subtensor/src/macros/genesis.rs b/pallets/subtensor/src/macros/genesis.rs index db7c798fbc..e50bf01d7d 100644 --- a/pallets/subtensor/src/macros/genesis.rs +++ b/pallets/subtensor/src/macros/genesis.rs @@ -52,7 +52,7 @@ mod genesis { SubnetMechanism::::insert(netuid, 1); // Make dynamic. Owner::::insert(hotkey.clone(), hotkey.clone()); SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(10_000_000_000)); - SubnetTAO::::insert(netuid, 10_000_000_000); + SubnetTAO::::insert(netuid, TaoCurrency::from(10_000_000_000)); NetworksAdded::::insert(netuid, true); TotalNetworks::::mutate(|n| *n = n.saturating_add(1)); SubnetworkN::::insert(netuid, 0); @@ -63,7 +63,7 @@ mod genesis { Tempo::::insert(netuid, 100); NetworkRegistrationAllowed::::insert(netuid, true); SubnetOwner::::insert(netuid, hotkey.clone()); - SubnetLocked::::insert(netuid, 1); + SubnetLocked::::insert(netuid, TaoCurrency::from(1)); LargestLocked::::insert(netuid, 1); Alpha::::insert( // Lock the initial funds making this key the owner. diff --git a/pallets/subtensor/src/migrations/migrate_fix_root_subnet_tao.rs b/pallets/subtensor/src/migrations/migrate_fix_root_subnet_tao.rs index 96593b9e5c..04fb21ef09 100644 --- a/pallets/subtensor/src/migrations/migrate_fix_root_subnet_tao.rs +++ b/pallets/subtensor/src/migrations/migrate_fix_root_subnet_tao.rs @@ -19,12 +19,12 @@ pub fn migrate_fix_root_subnet_tao() -> Weight { String::from_utf8_lossy(&migration_name) ); - let mut total_stake: u64 = 0; + let mut total_stake = TaoCurrency::ZERO; let mut hotkey_count: u64 = 0; // We accumulate the total stake for all hotkeys on the root subnet. for hotkey in Owner::::iter_keys() { let hotkey_stake = TotalHotkeyAlpha::::get(&hotkey, NetUid::ROOT); - total_stake = total_stake.saturating_add(hotkey_stake.to_u64()); + total_stake = total_stake.saturating_add(hotkey_stake.to_u64().into()); hotkey_count = hotkey_count.saturating_add(1); } diff --git a/pallets/subtensor/src/migrations/migrate_init_total_issuance.rs b/pallets/subtensor/src/migrations/migrate_init_total_issuance.rs index 5bccfff9a0..062c5cb6ea 100644 --- a/pallets/subtensor/src/migrations/migrate_init_total_issuance.rs +++ b/pallets/subtensor/src/migrations/migrate_init_total_issuance.rs @@ -26,8 +26,8 @@ pub(crate) fn migrate_init_total_issuance() -> Weight { let prev_total_stake = crate::TotalStake::::get(); // Calculate new total stake using the sum of all subnet TAO - let total_subnet_tao: u64 = - crate::SubnetTAO::::iter().fold(0, |acc, (_, v)| acc.saturating_add(v)); + let total_subnet_tao = + crate::SubnetTAO::::iter().fold(TaoCurrency::ZERO, |acc, (_, v)| acc.saturating_add(v)); let total_stake = total_subnet_tao; // Update the total stake in storage @@ -41,7 +41,9 @@ pub(crate) fn migrate_init_total_issuance() -> Weight { let prev_total_issuance = crate::TotalIssuance::::get(); // Calculate the new total issuance - let new_total_issuance = total_account_balances.saturating_add(total_stake); + let new_total_issuance: TaoCurrency = total_account_balances + .saturating_add(total_stake.to_u64()) + .into(); // Update the total issuance in storage crate::TotalIssuance::::put(new_total_issuance); diff --git a/pallets/subtensor/src/migrations/migrate_rao.rs b/pallets/subtensor/src/migrations/migrate_rao.rs index d529e77098..3e2576d174 100644 --- a/pallets/subtensor/src/migrations/migrate_rao.rs +++ b/pallets/subtensor/src/migrations/migrate_rao.rs @@ -59,7 +59,7 @@ pub fn migrate_rao() -> Weight { // Convert subnets and give them lock. // Set global weight to 18% from the start // Set min lock - NetworkMinLockCost::::set(1_000_000_000); + NetworkMinLockCost::::set(TaoCurrency::from(1_000_000_000)); // Set tao weight. TaoWeight::::set(3_320_413_933_267_719_290); for netuid in netuids.iter() { @@ -77,7 +77,7 @@ pub fn migrate_rao() -> Weight { // The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO. let pool_initial_tao = Pallet::::get_network_min_lock(); if lock < pool_initial_tao { - let difference: u64 = pool_initial_tao.saturating_sub(lock); + let difference = pool_initial_tao.saturating_sub(lock); TotalIssuance::::mutate(|total| { *total = total.saturating_add(difference); }); @@ -91,13 +91,13 @@ pub fn migrate_rao() -> Weight { // .checked_div(I96F32::from_num(1_000_000_000)) // .unwrap_or(I96F32::from_num(0.0)), // ); - Pallet::::add_balance_to_coldkey_account(&owner, remaining_lock); - SubnetLocked::::insert(netuid, 0); // Clear lock amount. + Pallet::::add_balance_to_coldkey_account(&owner, remaining_lock.into()); + SubnetLocked::::insert(netuid, TaoCurrency::ZERO); // Clear lock amount. SubnetTAO::::insert(netuid, pool_initial_tao); TotalStake::::mutate(|total| { *total = total.saturating_add(pool_initial_tao); }); // Increase total stake. - SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(pool_initial_tao)); // Set initial alpha to pool initial tao. + SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(pool_initial_tao.to_u64())); // Set initial alpha to pool initial tao. SubnetAlphaOut::::insert(netuid, AlphaCurrency::ZERO); // Set zero subnet alpha out. SubnetMechanism::::insert(netuid, 1); // Convert to dynamic immediately with initialization. diff --git a/pallets/subtensor/src/migrations/migrate_reset_max_burn.rs b/pallets/subtensor/src/migrations/migrate_reset_max_burn.rs index 4de473b2fb..1a8e3a9979 100644 --- a/pallets/subtensor/src/migrations/migrate_reset_max_burn.rs +++ b/pallets/subtensor/src/migrations/migrate_reset_max_burn.rs @@ -31,7 +31,7 @@ pub fn migrate_reset_max_burn() -> Weight { for netuid in MaxBurn::::iter_keys() { MaxBurn::::mutate(netuid, |max| { - *max = 100_000_000_000; + *max = 100_000_000_000.into(); }); reset_entries_count = reset_entries_count.saturating_add(1); } diff --git a/pallets/subtensor/src/migrations/migrate_set_min_burn.rs b/pallets/subtensor/src/migrations/migrate_set_min_burn.rs index 505ea94a74..80518eb188 100644 --- a/pallets/subtensor/src/migrations/migrate_set_min_burn.rs +++ b/pallets/subtensor/src/migrations/migrate_set_min_burn.rs @@ -35,7 +35,7 @@ pub fn migrate_set_min_burn() -> Weight { continue; } // Set min burn to the newest initial min burn - Pallet::::set_min_burn(*netuid, T::InitialMinBurn::get()); + Pallet::::set_min_burn(*netuid, T::InitialMinBurn::get().into()); weight = weight.saturating_add(T::DbWeight::get().writes(1)); } diff --git a/pallets/subtensor/src/migrations/migrate_total_issuance.rs b/pallets/subtensor/src/migrations/migrate_total_issuance.rs index a7adfe8ffb..bd69c60436 100644 --- a/pallets/subtensor/src/migrations/migrate_total_issuance.rs +++ b/pallets/subtensor/src/migrations/migrate_total_issuance.rs @@ -43,17 +43,18 @@ pub fn migrate_total_issuance(test: bool) -> Weight { // Execute migration if the current storage version is 5 or if in test mode if Pallet::::on_chain_storage_version() == StorageVersion::new(5) || test { // Calculate the sum of all stake values - let stake_sum: u64 = Owner::::iter() + let stake_sum = Owner::::iter() .map(|(hotkey, _coldkey)| Pallet::::get_total_stake_for_hotkey(&hotkey)) - .fold(0, |acc, stake| acc.saturating_add(stake)); + .fold(TaoCurrency::ZERO, |acc, stake| acc.saturating_add(stake)); // Add weight for reading all Owner and TotalHotkeyStake entries weight = weight.saturating_add( T::DbWeight::get().reads((Owner::::iter().count() as u64).saturating_mul(2)), ); // Calculate the sum of all locked subnet values - let locked_sum: u64 = - SubnetLocked::::iter().fold(0, |acc, (_, locked)| acc.saturating_add(locked)); + let locked_sum = SubnetLocked::::iter().fold(TaoCurrency::ZERO, |acc, (_, locked)| { + acc.saturating_add(locked) + }); // Add weight for reading all subnet locked entries weight = weight .saturating_add(T::DbWeight::get().reads(SubnetLocked::::iter().count() as u64)); @@ -67,9 +68,9 @@ pub fn migrate_total_issuance(test: bool) -> Weight { match TryInto::::try_into(total_balance) { Ok(total_balance_sum) => { // Compute the total issuance value - let total_issuance_value: u64 = stake_sum - .saturating_add(total_balance_sum) - .saturating_add(locked_sum); + let total_issuance_value = stake_sum + .saturating_add(total_balance_sum.into()) + .saturating_add(locked_sum.into()); // Update the total issuance in storage TotalIssuance::::put(total_issuance_value); diff --git a/pallets/subtensor/src/rpc_info/dynamic_info.rs b/pallets/subtensor/src/rpc_info/dynamic_info.rs index b4394bc68c..28b3990082 100644 --- a/pallets/subtensor/src/rpc_info/dynamic_info.rs +++ b/pallets/subtensor/src/rpc_info/dynamic_info.rs @@ -4,9 +4,9 @@ use codec::Compact; use frame_support::pallet_prelude::{Decode, Encode}; use substrate_fixed::types::I96F32; use subtensor_macros::freeze_struct; -use subtensor_runtime_common::{AlphaCurrency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; -#[freeze_struct("944ecd330621c61e")] +#[freeze_struct("e526a1c6d2303d32")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct DynamicInfo { netuid: Compact, @@ -20,12 +20,12 @@ pub struct DynamicInfo { emission: Compact, alpha_in: Compact, alpha_out: Compact, - tao_in: Compact, + tao_in: Compact, alpha_out_emission: Compact, alpha_in_emission: Compact, - tao_in_emission: Compact, + tao_in_emission: Compact, pending_alpha_emission: Compact, - pending_root_emission: Compact, + pending_root_emission: Compact, subnet_volume: Compact, network_registered_at: Compact, subnet_identity: Option, diff --git a/pallets/subtensor/src/rpc_info/metagraph.rs b/pallets/subtensor/src/rpc_info/metagraph.rs index 3f48c9ad45..18fd9c6d4b 100644 --- a/pallets/subtensor/src/rpc_info/metagraph.rs +++ b/pallets/subtensor/src/rpc_info/metagraph.rs @@ -7,9 +7,9 @@ use frame_support::pallet_prelude::{Decode, Encode}; use substrate_fixed::types::I64F64; use substrate_fixed::types::I96F32; use subtensor_macros::freeze_struct; -use subtensor_runtime_common::{AlphaCurrency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; -#[freeze_struct("c25c5560ffd47ccb")] +#[freeze_struct("6fc49d5a7dc0e339")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct Metagraph { // Subnet index @@ -35,12 +35,12 @@ pub struct Metagraph { subnet_emission: Compact, // subnet emission via stao alpha_in: Compact, // amount of alpha in reserve alpha_out: Compact, // amount of alpha outstanding - tao_in: Compact, // amount of tao injected per block + tao_in: Compact, // amount of tao injected per block alpha_out_emission: Compact, // amount injected in alpha reserves per block alpha_in_emission: Compact, // amount injected outstanding per block - tao_in_emission: Compact, // amount of tao injected per block + tao_in_emission: Compact, // amount of tao injected per block pending_alpha_emission: Compact, // pending alpha to be distributed - pending_root_emission: Compact, // panding tao for root divs to be distributed + pending_root_emission: Compact, // panding tao for root divs to be distributed subnet_volume: Compact, // volume of the subnet in TAO moving_price: I96F32, // subnet moving price. @@ -59,15 +59,15 @@ pub struct Metagraph { // Registration num_uids: Compact, max_uids: Compact, - burn: Compact, // current burn cost.. + burn: Compact, // current burn cost.. difficulty: Compact, // current difficulty. registration_allowed: bool, // allows registrations. pow_registration_allowed: bool, // pow registration enabled. immunity_period: Compact, // subnet miner immunity period min_difficulty: Compact, // min pow difficulty max_difficulty: Compact, // max pow difficulty - min_burn: Compact, // min tao burn - max_burn: Compact, // max tao burn + min_burn: Compact, // min tao burn + max_burn: Compact, // max tao burn adjustment_alpha: Compact, // adjustment speed for registration params. adjustment_interval: Compact, // pow and burn adjustment interval target_regs_per_interval: Compact, // target registrations per interval @@ -100,16 +100,16 @@ pub struct Metagraph { trust: Vec>, // Trust per UID rank: Vec>, // Rank per UID block_at_registration: Vec>, // Reg block per UID - alpha_stake: Vec>, // Alpha staked per UID - tao_stake: Vec>, // TAO staked per UID - total_stake: Vec>, // Total stake per UID + alpha_stake: Vec>, // Alpha staked per UID + tao_stake: Vec>, // TAO staked per UID + total_stake: Vec>, // Total stake per UID // Dividend break down. - tao_dividends_per_hotkey: Vec<(AccountId, Compact)>, // List of dividend payouts in tao via root. + tao_dividends_per_hotkey: Vec<(AccountId, Compact)>, // List of dividend payouts in tao via root. alpha_dividends_per_hotkey: Vec<(AccountId, Compact)>, // List of dividend payout in alpha via subnet. } -#[freeze_struct("2fa92e896b40a104")] +#[freeze_struct("7604bd3817c55848")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SelectiveMetagraph { // Subnet index @@ -135,14 +135,14 @@ pub struct SelectiveMetagraph { subnet_emission: Option>, // subnet emission via stao alpha_in: Option>, // amount of alpha in reserve alpha_out: Option>, // amount of alpha outstanding - tao_in: Option>, // amount of tao injected per block + tao_in: Option>, // amount of tao injected per block alpha_out_emission: Option>, // amount injected in alpha reserves per block alpha_in_emission: Option>, // amount injected outstanding per block - tao_in_emission: Option>, // amount of tao injected per block + tao_in_emission: Option>, // amount of tao injected per block pending_alpha_emission: Option>, // pending alpha to be distributed - pending_root_emission: Option>, // panding tao for root divs to be distributed - subnet_volume: Option>, // volume of the subnet in TAO - moving_price: Option, // subnet moving price. + pending_root_emission: Option>, // panding tao for root divs to be distributed + subnet_volume: Option>, // volume of the subnet in TAO + moving_price: Option, // subnet moving price. // Hparams for epoch rho: Option>, // subnet rho param @@ -159,20 +159,20 @@ pub struct SelectiveMetagraph { // Registration num_uids: Option>, max_uids: Option>, - burn: Option>, // current burn cost.. - difficulty: Option>, // current difficulty. - registration_allowed: Option, // allows registrations. - pow_registration_allowed: Option, // pow registration enabled. - immunity_period: Option>, // subnet miner immunity period - min_difficulty: Option>, // min pow difficulty - max_difficulty: Option>, // max pow difficulty - min_burn: Option>, // min tao burn - max_burn: Option>, // max tao burn - adjustment_alpha: Option>, // adjustment speed for registration params. - adjustment_interval: Option>, // pow and burn adjustment interval + burn: Option>, // current burn cost.. + difficulty: Option>, // current difficulty. + registration_allowed: Option, // allows registrations. + pow_registration_allowed: Option, // pow registration enabled. + immunity_period: Option>, // subnet miner immunity period + min_difficulty: Option>, // min pow difficulty + max_difficulty: Option>, // max pow difficulty + min_burn: Option>, // min tao burn + max_burn: Option>, // max tao burn + adjustment_alpha: Option>, // adjustment speed for registration params. + adjustment_interval: Option>, // pow and burn adjustment interval target_regs_per_interval: Option>, // target registrations per interval - max_regs_per_block: Option>, // max registrations per block. - serving_rate_limit: Option>, // axon serving rate limit + max_regs_per_block: Option>, // max registrations per block. + serving_rate_limit: Option>, // axon serving rate limit // CR commit_reveal_weights_enabled: Option, // Is CR enabled. @@ -200,12 +200,12 @@ pub struct SelectiveMetagraph { trust: Option>>, // Trust per UID rank: Option>>, // Rank per UID block_at_registration: Option>>, // Reg block per UID - alpha_stake: Option>>, // Alpha staked per UID - tao_stake: Option>>, // TAO staked per UID - total_stake: Option>>, // Total stake per UID + alpha_stake: Option>>, // Alpha staked per UID + tao_stake: Option>>, // TAO staked per UID + total_stake: Option>>, // Total stake per UID // Dividend break down. - tao_dividends_per_hotkey: Option)>>, // List of dividend payouts in tao via root. + tao_dividends_per_hotkey: Option)>>, // List of dividend payouts in tao via root. alpha_dividends_per_hotkey: Option)>>, // List of dividend payout in alpha via subnet. // validators @@ -632,7 +632,7 @@ impl Pallet { identities.push(IdentitiesV2::::get(coldkey.clone())); axons.push(Self::get_axon_info(netuid, &hotkey)); } - let mut tao_dividends_per_hotkey: Vec<(T::AccountId, Compact)> = vec![]; + let mut tao_dividends_per_hotkey: Vec<(T::AccountId, Compact)> = vec![]; let mut alpha_dividends_per_hotkey: Vec<(T::AccountId, Compact)> = vec![]; for hotkey in hotkeys.clone() { let tao_divs = TaoDividendsPerSubnet::::get(netuid, hotkey.clone()); @@ -707,12 +707,12 @@ impl Pallet { registration_allowed: Self::get_network_registration_allowed(netuid), // allows registrations. pow_registration_allowed: Self::get_network_pow_registration_allowed(netuid), // allows pow registrations. difficulty: Self::get_difficulty_as_u64(netuid).into(), // current difficulty. - burn: Self::get_burn_as_u64(netuid).into(), + burn: Self::get_burn(netuid).into(), immunity_period: Self::get_immunity_period(netuid).into(), // subnet miner immunity period min_difficulty: Self::get_min_difficulty(netuid).into(), // min pow difficulty max_difficulty: Self::get_max_difficulty(netuid).into(), // max pow difficulty - min_burn: Self::get_min_burn_as_u64(netuid).into(), // min tao burn - max_burn: Self::get_max_burn_as_u64(netuid).into(), // max tao burn + min_burn: Self::get_min_burn(netuid).into(), // min tao burn + max_burn: Self::get_max_burn(netuid).into(), // max tao burn adjustment_alpha: Self::get_adjustment_alpha(netuid).into(), // adjustment speed for registration params. adjustment_interval: Self::get_adjustment_interval(netuid).into(), // pow and burn adjustment interval target_regs_per_interval: Self::get_target_registrations_per_interval(netuid).into(), // target registrations per interval @@ -771,16 +771,16 @@ impl Pallet { block_at_registration, // Reg block per UID alpha_stake: alpha_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(), // Alpha staked per UID + .map(|xi| Compact::from(AlphaCurrency::from(fixed64_to_u64(*xi)))) + .collect::>>(), // Alpha staked per UID tao_stake: tao_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(), // TAO staked per UID + .map(|xi| Compact::from(TaoCurrency::from(fixed64_to_u64(*xi)))) + .collect::>>(), // TAO staked per UID total_stake: total_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(), // Total stake per UID + .map(|xi| Compact::from(TaoCurrency::from(fixed64_to_u64(*xi)))) + .collect::>>(), // Total stake per UID // Dividend break down. tao_dividends_per_hotkey, @@ -1024,7 +1024,7 @@ impl Pallet { Some(SelectiveMetagraphIndex::Burn) => SelectiveMetagraph { netuid: netuid.into(), - burn: Some(Self::get_burn_as_u64(netuid).into()), + burn: Some(Self::get_burn(netuid).into()), ..Default::default() }, @@ -1045,12 +1045,12 @@ impl Pallet { }, Some(SelectiveMetagraphIndex::MinBurn) => SelectiveMetagraph { netuid: netuid.into(), - min_burn: Some(Self::get_min_burn_as_u64(netuid).into()), + min_burn: Some(Self::get_min_burn(netuid).into()), ..Default::default() }, Some(SelectiveMetagraphIndex::MaxBurn) => SelectiveMetagraph { netuid: netuid.into(), - max_burn: Some(Self::get_max_burn_as_u64(netuid).into()), + max_burn: Some(Self::get_max_burn(netuid).into()), ..Default::default() }, Some(SelectiveMetagraphIndex::AdjustmentAlpha) => SelectiveMetagraph { @@ -1290,8 +1290,8 @@ impl Pallet { alpha_stake: Some( alpha_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(), + .map(|xi| Compact::from(AlphaCurrency::from(fixed64_to_u64(*xi)))) + .collect::>>(), ), ..Default::default() } @@ -1304,8 +1304,8 @@ impl Pallet { tao_stake: Some( tao_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(), + .map(|xi| Compact::from(TaoCurrency::from(fixed64_to_u64(*xi)))) + .collect::>>(), ), ..Default::default() } @@ -1318,8 +1318,8 @@ impl Pallet { total_stake: Some( total_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(), + .map(|xi| Compact::from(TaoCurrency::from(fixed64_to_u64(*xi)))) + .collect::>>(), ), ..Default::default() } @@ -1333,7 +1333,8 @@ impl Pallet { let hotkey = Keys::::get(netuid, uid); hotkeys.push(hotkey.clone()); } - let mut tao_dividends_per_hotkey: Vec<(T::AccountId, Compact)> = vec![]; + let mut tao_dividends_per_hotkey: Vec<(T::AccountId, Compact)> = + vec![]; for hotkey in hotkeys.clone() { let tao_divs = TaoDividendsPerSubnet::::get(netuid, hotkey.clone()); tao_dividends_per_hotkey.push((hotkey.clone(), tao_divs.into())); diff --git a/pallets/subtensor/src/rpc_info/show_subnet.rs b/pallets/subtensor/src/rpc_info/show_subnet.rs index 4c39542ace..2123345a4e 100644 --- a/pallets/subtensor/src/rpc_info/show_subnet.rs +++ b/pallets/subtensor/src/rpc_info/show_subnet.rs @@ -4,9 +4,9 @@ use crate::epoch::math::*; use codec::Compact; use frame_support::pallet_prelude::{Decode, Encode}; use substrate_fixed::types::I64F64; -use subtensor_runtime_common::{AlphaCurrency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; -#[freeze_struct("c990700ae235dee9")] +#[freeze_struct("9354762261420485")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SubnetState { netuid: Compact, @@ -23,9 +23,9 @@ pub struct SubnetState { trust: Vec>, rank: Vec>, block_at_registration: Vec>, - alpha_stake: Vec>, - tao_stake: Vec>, - total_stake: Vec>, + alpha_stake: Vec>, + tao_stake: Vec>, + total_stake: Vec>, emission_history: Vec>>, // identities: Vec, // tao_stake: Compact, @@ -136,18 +136,18 @@ impl Pallet { Vec, Vec, ) = Self::get_stake_weights_for_network(netuid); - let alpha_stake: Vec> = alpha_stake_fl + let alpha_stake: Vec> = alpha_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(); - let tao_stake: Vec> = tao_stake_fl + .map(|xi| Compact::from(AlphaCurrency::from(fixed64_to_u64(*xi)))) + .collect(); + let tao_stake: Vec> = tao_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(); - let total_stake: Vec> = total_stake_fl + .map(|xi| Compact::from(TaoCurrency::from(fixed64_to_u64(*xi)))) + .collect(); + let total_stake: Vec> = total_stake_fl .iter() - .map(|xi| Compact::from(fixed64_to_u64(*xi))) - .collect::>>(); + .map(|xi| Compact::from(TaoCurrency::from(fixed64_to_u64(*xi)))) + .collect(); let emission_history = Self::get_emissions_history(hotkeys.clone()); Some(SubnetState { netuid: netuid.into(), diff --git a/pallets/subtensor/src/rpc_info/stake_info.rs b/pallets/subtensor/src/rpc_info/stake_info.rs index 23c2a7df0e..84e9e6d6b7 100644 --- a/pallets/subtensor/src/rpc_info/stake_info.rs +++ b/pallets/subtensor/src/rpc_info/stake_info.rs @@ -2,12 +2,12 @@ extern crate alloc; use codec::Compact; use frame_support::pallet_prelude::{Decode, Encode}; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; use subtensor_swap_interface::SwapHandler; use super::*; -#[freeze_struct("cff08b08c64eb867")] +#[freeze_struct("28269be895d7b5ba")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct StakeInfo { hotkey: AccountId, @@ -16,7 +16,7 @@ pub struct StakeInfo { stake: Compact, locked: Compact, emission: Compact, - tao_emission: Compact, + tao_emission: Compact, drain: Compact, is_registered: bool, } @@ -43,7 +43,7 @@ impl Pallet { continue; } let emission = AlphaDividendsPerSubnet::::get(*netuid_i, &hotkey_i); - let tao_emission: u64 = TaoDividendsPerSubnet::::get(*netuid_i, &hotkey_i); + let tao_emission = TaoDividendsPerSubnet::::get(*netuid_i, &hotkey_i); let is_registered: bool = Self::is_hotkey_registered_on_network(*netuid_i, hotkey_i); stake_info_for_coldkey.push(StakeInfo { @@ -101,7 +101,7 @@ impl Pallet { netuid, ); let emission = AlphaDividendsPerSubnet::::get(netuid, &hotkey_account); - let tao_emission: u64 = TaoDividendsPerSubnet::::get(netuid, &hotkey_account); + let tao_emission = TaoDividendsPerSubnet::::get(netuid, &hotkey_account); let is_registered: bool = Self::is_hotkey_registered_on_network(netuid, &hotkey_account); Some(StakeInfo { diff --git a/pallets/subtensor/src/rpc_info/subnet_info.rs b/pallets/subtensor/src/rpc_info/subnet_info.rs index cab1f8800b..d1e0a05419 100644 --- a/pallets/subtensor/src/rpc_info/subnet_info.rs +++ b/pallets/subtensor/src/rpc_info/subnet_info.rs @@ -4,9 +4,9 @@ use frame_support::storage::IterableStorageMap; extern crate alloc; use codec::Compact; use substrate_fixed::types::I32F32; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{NetUid, TaoCurrency}; -#[freeze_struct("dd2293544ffd8f2e")] +#[freeze_struct("edd6bd3273dfea76")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SubnetInfo { netuid: Compact, @@ -25,11 +25,11 @@ pub struct SubnetInfo { network_modality: Compact, network_connect: Vec<[u16; 2]>, emission_values: Compact, - burn: Compact, + burn: Compact, owner: AccountId, } -#[freeze_struct("4e60a45245fc2ad1")] +#[freeze_struct("e5f66b14b33331c3")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SubnetInfov2 { netuid: Compact, @@ -48,12 +48,12 @@ pub struct SubnetInfov2 { network_modality: Compact, network_connect: Vec<[u16; 2]>, emission_value: Compact, - burn: Compact, + burn: Compact, owner: AccountId, identity: Option, } -#[freeze_struct("7b506df55bd44646")] +#[freeze_struct("24f0815487879ed3")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SubnetHyperparams { rho: Compact, @@ -70,8 +70,8 @@ pub struct SubnetHyperparams { activity_cutoff: Compact, pub registration_allowed: bool, target_regs_per_interval: Compact, - min_burn: Compact, - max_burn: Compact, + min_burn: Compact, + max_burn: Compact, bonds_moving_avg: Compact, max_regs_per_block: Compact, serving_rate_limit: Compact, @@ -85,7 +85,7 @@ pub struct SubnetHyperparams { liquid_alpha_enabled: bool, } -#[freeze_struct("a13c536303dec16f")] +#[freeze_struct("2153c3f3bb01ef66")] #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SubnetHyperparamsV2 { rho: Compact, @@ -102,8 +102,8 @@ pub struct SubnetHyperparamsV2 { activity_cutoff: Compact, pub registration_allowed: bool, target_regs_per_interval: Compact, - min_burn: Compact, - max_burn: Compact, + min_burn: Compact, + max_burn: Compact, bonds_moving_avg: Compact, max_regs_per_block: Compact, serving_rate_limit: Compact, @@ -142,7 +142,7 @@ impl Pallet { let blocks_since_last_step = Self::get_blocks_since_last_step(netuid); let tempo = Self::get_tempo(netuid); let network_modality = >::get(netuid); - let burn: Compact = Self::get_burn_as_u64(netuid).into(); + let burn = Compact::from(Self::get_burn(netuid)); // DEPRECATED let network_connect: Vec<[u16; 2]> = Vec::<[u16; 2]>::new(); // DEPRECATED for ( _netuid_, con_req) in < NetworkConnect as IterableStorageDoubleMap >::iter_prefix(netuid) { @@ -211,7 +211,7 @@ impl Pallet { let blocks_since_last_step = Self::get_blocks_since_last_step(netuid); let tempo = Self::get_tempo(netuid); let network_modality = >::get(netuid); - let burn: Compact = Self::get_burn_as_u64(netuid).into(); + let burn = Compact::from(Self::get_burn(netuid)); let identity: Option = SubnetIdentitiesV3::::get(netuid); // DEPRECATED @@ -284,8 +284,8 @@ impl Pallet { let activity_cutoff = Self::get_activity_cutoff(netuid); let registration_allowed = Self::get_network_registration_allowed(netuid); let target_regs_per_interval = Self::get_target_registrations_per_interval(netuid); - let min_burn = Self::get_min_burn_as_u64(netuid); - let max_burn = Self::get_max_burn_as_u64(netuid); + let min_burn = Self::get_min_burn(netuid); + let max_burn = Self::get_max_burn(netuid); let bonds_moving_avg = Self::get_bonds_moving_average(netuid); let max_regs_per_block = Self::get_max_registrations_per_block(netuid); let serving_rate_limit = Self::get_serving_rate_limit(netuid); @@ -347,8 +347,8 @@ impl Pallet { let activity_cutoff = Self::get_activity_cutoff(netuid); let registration_allowed = Self::get_network_registration_allowed(netuid); let target_regs_per_interval = Self::get_target_registrations_per_interval(netuid); - let min_burn = Self::get_min_burn_as_u64(netuid); - let max_burn = Self::get_max_burn_as_u64(netuid); + let min_burn = Self::get_min_burn(netuid); + let max_burn = Self::get_max_burn(netuid); let bonds_moving_avg = Self::get_bonds_moving_average(netuid); let max_regs_per_block = Self::get_max_registrations_per_block(netuid); let serving_rate_limit = Self::get_serving_rate_limit(netuid); diff --git a/pallets/subtensor/src/staking/add_stake.rs b/pallets/subtensor/src/staking/add_stake.rs index 7388c9484a..92f64c060b 100644 --- a/pallets/subtensor/src/staking/add_stake.rs +++ b/pallets/subtensor/src/staking/add_stake.rs @@ -1,5 +1,5 @@ use substrate_fixed::types::I96F32; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{NetUid, TaoCurrency}; use subtensor_swap_interface::{OrderType, SwapHandler}; use super::*; @@ -41,7 +41,7 @@ impl Pallet { origin: T::RuntimeOrigin, hotkey: T::AccountId, netuid: NetUid, - stake_to_be_added: u64, + stake_to_be_added: TaoCurrency, ) -> dispatch::DispatchResult { // 1. We check that the transaction is signed by the caller and retrieve the T::AccountId coldkey information. let coldkey = ensure_signed(origin)?; @@ -67,7 +67,9 @@ impl Pallet { // 3. Ensure the remove operation from the coldkey is a success. let tao_staked: I96F32 = - Self::remove_balance_from_coldkey_account(&coldkey, stake_to_be_added)?.into(); + Self::remove_balance_from_coldkey_account(&coldkey, stake_to_be_added.into())? + .to_u64() + .into(); // 4. Swap the stake into alpha on the subnet and increase counters. // Emit the staking event. @@ -75,8 +77,8 @@ impl Pallet { &hotkey, &coldkey, netuid, - tao_staked.saturating_to_num::(), - T::SwapInterface::max_price(), + tao_staked.saturating_to_num::().into(), + T::SwapInterface::max_price().into(), true, )?; @@ -128,8 +130,8 @@ impl Pallet { origin: T::RuntimeOrigin, hotkey: T::AccountId, netuid: NetUid, - stake_to_be_added: u64, - limit_price: u64, + stake_to_be_added: TaoCurrency, + limit_price: TaoCurrency, allow_partial: bool, ) -> dispatch::DispatchResult { // 1. We check that the transaction is signed by the caller and retrieve the T::AccountId coldkey information. @@ -143,7 +145,7 @@ impl Pallet { ); // 2. Calculate the maximum amount that can be executed with price limit - let max_amount = Self::get_max_amount_add(netuid, limit_price)?; + let max_amount: TaoCurrency = Self::get_max_amount_add(netuid, limit_price)?.into(); let mut possible_stake = stake_to_be_added; if possible_stake > max_amount { possible_stake = max_amount; @@ -155,7 +157,7 @@ impl Pallet { &hotkey, netuid, stake_to_be_added, - max_amount, + max_amount.into(), allow_partial, )?; @@ -165,7 +167,8 @@ impl Pallet { } // 5. Ensure the remove operation from the coldkey is a success. - let tao_staked: u64 = Self::remove_balance_from_coldkey_account(&coldkey, possible_stake)?; + let tao_staked = + Self::remove_balance_from_coldkey_account(&coldkey, possible_stake.into())?; // 6. Swap the stake into alpha on the subnet and increase counters. // Emit the staking event. @@ -176,12 +179,12 @@ impl Pallet { } // Returns the maximum amount of RAO that can be executed with price limit - pub fn get_max_amount_add(netuid: NetUid, limit_price: u64) -> Result> { + pub fn get_max_amount_add(netuid: NetUid, limit_price: TaoCurrency) -> Result> { // Corner case: root and stao // There's no slippage for root or stable subnets, so if limit price is 1e9 rao or // higher, then max_amount equals u64::MAX, otherwise it is 0. if netuid.is_root() || SubnetMechanism::::get(netuid) == 0 { - if limit_price >= 1_000_000_000 { + if limit_price >= 1_000_000_000.into() { return Ok(u64::MAX); } else { return Err(Error::ZeroMaxStakeAmount); @@ -193,7 +196,7 @@ impl Pallet { netuid.into(), OrderType::Buy, u64::MAX, - limit_price, + limit_price.into(), false, true, ) diff --git a/pallets/subtensor/src/staking/helpers.rs b/pallets/subtensor/src/staking/helpers.rs index 4b897fef96..7b8d0ba1de 100644 --- a/pallets/subtensor/src/staking/helpers.rs +++ b/pallets/subtensor/src/staking/helpers.rs @@ -7,7 +7,7 @@ use frame_support::traits::{ }; use safe_math::*; use substrate_fixed::types::U96F32; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{NetUid, TaoCurrency}; use subtensor_swap_interface::{OrderType, SwapHandler}; use super::*; @@ -27,24 +27,24 @@ impl Pallet { // Returns the total amount of stake in the staking table. // - pub fn get_total_stake() -> u64 { + pub fn get_total_stake() -> TaoCurrency { TotalStake::::get() } // Increases the total amount of stake by the passed amount. // - pub fn increase_total_stake(increment: u64) { + pub fn increase_total_stake(increment: TaoCurrency) { TotalStake::::put(Self::get_total_stake().saturating_add(increment)); } // Decreases the total amount of stake by the passed amount. // - pub fn decrease_total_stake(decrement: u64) { + pub fn decrease_total_stake(decrement: TaoCurrency) { TotalStake::::put(Self::get_total_stake().saturating_sub(decrement)); } /// Returns the total amount of stake (in TAO) under a hotkey (delegative or otherwise) - pub fn get_total_stake_for_hotkey(hotkey: &T::AccountId) -> u64 { + pub fn get_total_stake_for_hotkey(hotkey: &T::AccountId) -> TaoCurrency { Self::get_all_subnet_netuids() .into_iter() .map(|netuid| { @@ -58,11 +58,12 @@ impl Pallet { }) .sum::() .saturating_to_num::() + .into() } // Returns the total amount of stake under a coldkey // - pub fn get_total_stake_for_coldkey(coldkey: &T::AccountId) -> u64 { + pub fn get_total_stake_for_coldkey(coldkey: &T::AccountId) -> TaoCurrency { let hotkeys = StakingHotkeys::::get(coldkey); hotkeys .iter() @@ -90,6 +91,7 @@ impl Pallet { .sum::() }) .sum::() + .into() } // Creates a cold - hot pairing account if the hotkey is not already an active account. @@ -200,13 +202,13 @@ impl Pallet { coldkey, netuid, alpha_stake, - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), false, ); if let Ok(cleared_stake) = maybe_cleared_stake { // Add the stake to the coldkey account. - Self::add_balance_to_coldkey_account(coldkey, cleared_stake); + Self::add_balance_to_coldkey_account(coldkey, cleared_stake.into()); } else { // Just clear small alpha let alpha = @@ -269,9 +271,9 @@ impl Pallet { pub fn remove_balance_from_coldkey_account( coldkey: &T::AccountId, amount: <::Currency as fungible::Inspect<::AccountId>>::Balance, - ) -> Result { + ) -> Result { if amount == 0 { - return Ok(0); + return Ok(TaoCurrency::ZERO); } let credit = ::Currency::withdraw( @@ -288,7 +290,7 @@ impl Pallet { return Err(Error::::ZeroBalanceAfterWithdrawn.into()); } - Ok(credit) + Ok(credit.into()) } pub fn kill_coldkey_account( diff --git a/pallets/subtensor/src/staking/move_stake.rs b/pallets/subtensor/src/staking/move_stake.rs index 15f2dd511a..b0dd71d003 100644 --- a/pallets/subtensor/src/staking/move_stake.rs +++ b/pallets/subtensor/src/staking/move_stake.rs @@ -2,7 +2,7 @@ use super::*; use safe_math::*; use sp_core::Get; use substrate_fixed::types::U64F64; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; use subtensor_swap_interface::SwapHandler; impl Pallet { @@ -271,7 +271,7 @@ impl Pallet { origin_netuid: NetUid, destination_netuid: NetUid, alpha_amount: AlphaCurrency, - limit_price: u64, + limit_price: TaoCurrency, allow_partial: bool, ) -> dispatch::DispatchResult { // Ensure the extrinsic is signed by the coldkey. @@ -323,11 +323,11 @@ impl Pallet { origin_netuid: NetUid, destination_netuid: NetUid, alpha_amount: AlphaCurrency, - maybe_limit_price: Option, + maybe_limit_price: Option, maybe_allow_partial: Option, check_transfer_toggle: bool, set_limit: bool, - ) -> Result { + ) -> Result { // Calculate the maximum amount that can be executed let max_amount = if origin_netuid != destination_netuid { if let Some(limit_price) = maybe_limit_price { @@ -367,7 +367,7 @@ impl Pallet { origin_coldkey, origin_netuid, move_amount, - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), true, )?; @@ -385,7 +385,7 @@ impl Pallet { destination_coldkey, destination_netuid, tao_unstaked, - T::SwapInterface::max_price(), + T::SwapInterface::max_price().into(), set_limit, )?; } @@ -429,7 +429,7 @@ impl Pallet { pub fn get_max_amount_move( origin_netuid: NetUid, destination_netuid: NetUid, - limit_price: u64, + limit_price: TaoCurrency, ) -> Result> { let tao: U64F64 = U64F64::saturating_from_num(1_000_000_000); @@ -439,7 +439,7 @@ impl Pallet { if (origin_netuid.is_root() || SubnetMechanism::::get(origin_netuid) == 0) && (destination_netuid.is_root() || SubnetMechanism::::get(destination_netuid) == 0) { - if limit_price > tao.saturating_to_num::() { + if limit_price > tao.saturating_to_num::().into() { return Err(Error::ZeroMaxStakeAmount); } else { return Ok(AlphaCurrency::MAX); @@ -451,7 +451,7 @@ impl Pallet { if (origin_netuid.is_root() || SubnetMechanism::::get(origin_netuid) == 0) && (SubnetMechanism::::get(destination_netuid) == 1) { - if limit_price == 0 { + if limit_price.is_zero() { return Ok(AlphaCurrency::MAX); } else { // The destination price is reverted because the limit_price is origin_price / destination_price @@ -459,8 +459,12 @@ impl Pallet { .safe_div(U64F64::saturating_from_num(limit_price)) .saturating_mul(tao) .saturating_to_num::(); - return Self::get_max_amount_add(destination_netuid, destination_subnet_price) - .map(Into::into); + // FIXME: mixed types alpha/tao + return Self::get_max_amount_add( + destination_netuid, + destination_subnet_price.into(), + ) + .map(Into::into); } } @@ -469,7 +473,7 @@ impl Pallet { if (destination_netuid.is_root() || SubnetMechanism::::get(destination_netuid) == 0) && (SubnetMechanism::::get(origin_netuid) == 1) { - return Self::get_max_amount_remove(origin_netuid, limit_price); + return Self::get_max_amount_remove(origin_netuid, limit_price).into(); } // Corner case: SubnetTAO for any of two subnets is zero @@ -477,7 +481,7 @@ impl Pallet { .saturating_add(SubnetTaoProvided::::get(origin_netuid)); let subnet_tao_2 = SubnetTAO::::get(destination_netuid) .saturating_add(SubnetTaoProvided::::get(destination_netuid)); - if (subnet_tao_1 == 0) || (subnet_tao_2 == 0) { + if subnet_tao_1.is_zero() || subnet_tao_2.is_zero() { return Err(Error::ZeroMaxStakeAmount); } let subnet_tao_1_float: U64F64 = U64F64::saturating_from_num(subnet_tao_1); @@ -509,7 +513,7 @@ impl Pallet { } // Corner case: limit_price is zero - if limit_price == 0 { + if limit_price.is_zero() { return Ok(AlphaCurrency::MAX); } diff --git a/pallets/subtensor/src/staking/remove_stake.rs b/pallets/subtensor/src/staking/remove_stake.rs index afe9d19308..e0809efc36 100644 --- a/pallets/subtensor/src/staking/remove_stake.rs +++ b/pallets/subtensor/src/staking/remove_stake.rs @@ -1,7 +1,7 @@ use subtensor_swap_interface::{OrderType, SwapHandler}; use super::*; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; impl Pallet { /// ---- The implementation for the extrinsic remove_stake: Removes stake from a hotkey account and adds it onto a coldkey. @@ -70,18 +70,18 @@ impl Pallet { &coldkey, netuid, alpha_unstaked, - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), false, )?; // 4. We add the balance to the coldkey. If the above fails we will not credit this coldkey. - Self::add_balance_to_coldkey_account(&coldkey, tao_unstaked); + Self::add_balance_to_coldkey_account(&coldkey, tao_unstaked.into()); // 5. If the stake is below the minimum, we clear the nomination from storage. Self::clear_small_nomination_if_required(&hotkey, &coldkey, netuid); // 6. Check if stake lowered below MinStake and remove Pending children if it did - if Self::get_total_stake_for_hotkey(&hotkey) < StakeThreshold::::get() { + if Self::get_total_stake_for_hotkey(&hotkey) < StakeThreshold::::get().into() { Self::get_all_subnet_netuids().iter().for_each(|netuid| { PendingChildKeys::::remove(netuid, &hotkey); }) @@ -160,17 +160,17 @@ impl Pallet { if !alpha_unstaked.is_zero() { // Swap the alpha to tao and update counters for this subnet. - let tao_unstaked: u64 = Self::unstake_from_subnet( + let tao_unstaked = Self::unstake_from_subnet( &hotkey, &coldkey, netuid, alpha_unstaked, - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), false, )?; // Add the balance to the coldkey. If the above fails we will not credit this coldkey. - Self::add_balance_to_coldkey_account(&coldkey, tao_unstaked); + Self::add_balance_to_coldkey_account(&coldkey, tao_unstaked.into()); // If the stake is below the minimum, we clear the nomination from storage. Self::clear_small_nomination_if_required(&hotkey, &coldkey, netuid); @@ -226,7 +226,7 @@ impl Pallet { log::debug!("All subnet netuids: {:?}", netuids); // 4. Iterate through all subnets and remove stake. - let mut total_tao_unstaked: u64 = 0; + let mut total_tao_unstaked = TaoCurrency::ZERO; for netuid in netuids.into_iter() { if !SubtokenEnabled::::get(netuid) { continue; @@ -258,7 +258,7 @@ impl Pallet { &coldkey, netuid, alpha_unstaked, - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), false, )?; @@ -277,7 +277,7 @@ impl Pallet { &coldkey, NetUid::ROOT, total_tao_unstaked, - T::SwapInterface::max_price(), + T::SwapInterface::max_price().into(), false, // no limit for Root subnet )?; @@ -331,7 +331,7 @@ impl Pallet { hotkey: T::AccountId, netuid: NetUid, alpha_unstaked: AlphaCurrency, - limit_price: u64, + limit_price: TaoCurrency, allow_partial: bool, ) -> dispatch::DispatchResult { // 1. We check the transaction is signed by the caller and retrieve the T::AccountId coldkey information. @@ -372,13 +372,13 @@ impl Pallet { )?; // 5. We add the balance to the coldkey. If the above fails we will not credit this coldkey. - Self::add_balance_to_coldkey_account(&coldkey, tao_unstaked); + Self::add_balance_to_coldkey_account(&coldkey, tao_unstaked.into()); // 6. If the stake is below the minimum, we clear the nomination from storage. Self::clear_small_nomination_if_required(&hotkey, &coldkey, netuid); // 7. Check if stake lowered below MinStake and remove Pending children if it did - if Self::get_total_stake_for_hotkey(&hotkey) < StakeThreshold::::get() { + if Self::get_total_stake_for_hotkey(&hotkey) < StakeThreshold::::get().into() { Self::get_all_subnet_netuids().iter().for_each(|netuid| { PendingChildKeys::::remove(netuid, &hotkey); }) @@ -391,13 +391,13 @@ impl Pallet { // Returns the maximum amount of RAO that can be executed with price limit pub fn get_max_amount_remove( netuid: NetUid, - limit_price: u64, + limit_price: TaoCurrency, ) -> Result> { // Corner case: root and stao // There's no slippage for root or stable subnets, so if limit price is 1e9 rao or // lower, then max_amount equals u64::MAX, otherwise it is 0. if netuid.is_root() || SubnetMechanism::::get(netuid) == 0 { - if limit_price <= 1_000_000_000 { + if limit_price <= 1_000_000_000.into() { return Ok(AlphaCurrency::MAX); } else { return Err(Error::ZeroMaxStakeAmount); @@ -409,7 +409,7 @@ impl Pallet { netuid.into(), OrderType::Sell, u64::MAX, - limit_price, + limit_price.into(), false, true, ) @@ -427,7 +427,7 @@ impl Pallet { origin: T::RuntimeOrigin, hotkey: T::AccountId, netuid: NetUid, - limit_price: Option, + limit_price: Option, ) -> DispatchResult { let coldkey = ensure_signed(origin.clone())?; diff --git a/pallets/subtensor/src/staking/set_children.rs b/pallets/subtensor/src/staking/set_children.rs index 35867f82ca..a5369a9e4c 100644 --- a/pallets/subtensor/src/staking/set_children.rs +++ b/pallets/subtensor/src/staking/set_children.rs @@ -107,7 +107,7 @@ impl Pallet { // grandparent stake in this case) ensure!( children.is_empty() - || Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::::get() + || Self::get_total_stake_for_hotkey(&hotkey) >= StakeThreshold::::get().into() || SubnetOwnerHotkey::::try_get(netuid) .is_ok_and(|owner_hotkey| owner_hotkey.eq(&hotkey)), Error::::NotEnoughStakeToSetChildkeys diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index 38027f489f..cfcb383bc6 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -3,7 +3,7 @@ use safe_math::*; use share_pool::{SharePool, SharePoolDataOperations}; use sp_std::ops::Neg; use substrate_fixed::types::{I64F64, I96F32, U64F64, U96F32}; -use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, Currency, NetUid, TaoCurrency}; use subtensor_swap_interface::{OrderType, SwapHandler, SwapResult}; impl Pallet { @@ -228,7 +228,10 @@ impl Pallet { /// /// # Note /// This function uses saturating arithmetic to prevent overflows. - pub fn get_tao_inherited_for_hotkey_on_subnet(hotkey: &T::AccountId, netuid: NetUid) -> u64 { + pub fn get_tao_inherited_for_hotkey_on_subnet( + hotkey: &T::AccountId, + netuid: NetUid, + ) -> TaoCurrency { let initial_tao: U96F32 = U96F32::saturating_from_num(Self::get_stake_for_hotkey_on_subnet(hotkey, NetUid::ROOT)); @@ -319,7 +322,7 @@ impl Pallet { ); // Step 6: Return the final inherited tao value. - finalized_tao.saturating_to_num::() + finalized_tao.saturating_to_num::().into() } pub fn get_inherited_for_hotkey_on_subnet( @@ -627,8 +630,8 @@ impl Pallet { /// Updates TaoIn, AlphaIn, and AlphaOut pub fn swap_tao_for_alpha( netuid: NetUid, - tao: u64, - price_limit: u64, + tao: TaoCurrency, + price_limit: TaoCurrency, ) -> Result { // Step 1: Get the mechanism type for the subnet (0 for Stable, 1 for Dynamic) let mechanism_id: u16 = SubnetMechanism::::get(netuid); @@ -636,8 +639,8 @@ impl Pallet { let swap_result = T::SwapInterface::swap( netuid.into(), OrderType::Buy, - tao, - price_limit, + tao.into(), + price_limit.into(), false, false, )?; @@ -656,7 +659,7 @@ impl Pallet { // (SubnetTAO + SubnetTaoProvided) in tao_reserve(), so it is irrelevant // which one to increase. SubnetTAO::::mutate(netuid, |total| { - *total = total.saturating_add(swap_result.tao_reserve_delta as u64); + *total = total.saturating_add((swap_result.tao_reserve_delta as u64).into()); }); // Increase Total Tao reserves. @@ -664,7 +667,7 @@ impl Pallet { // Increase total subnet TAO volume. SubnetVolume::::mutate(netuid, |total| { - *total = total.saturating_add(tao.into()); + *total = total.saturating_add(tao.to_u64() as u128); }); // Return the alpha received. @@ -672,8 +675,8 @@ impl Pallet { } else { // Step 3.b.1: Stable mechanism, just return the value 1:1 Ok(SwapResult { - amount_paid_in: tao, - amount_paid_out: tao, + amount_paid_in: tao.into(), + amount_paid_out: tao.into(), fee_paid: 0, tao_reserve_delta: 0, alpha_reserve_delta: 0, @@ -687,7 +690,7 @@ impl Pallet { pub fn swap_alpha_for_tao( netuid: NetUid, alpha: AlphaCurrency, - price_limit: u64, + price_limit: TaoCurrency, drop_fees: bool, ) -> Result { // Step 1: Get the mechanism type for the subnet (0 for Stable, 1 for Dynamic) @@ -698,7 +701,7 @@ impl Pallet { netuid.into(), OrderType::Sell, alpha.into(), - price_limit, + price_limit.into(), drop_fees, false, )?; @@ -719,12 +722,17 @@ impl Pallet { // Decrease tao reserves. Self::decrease_provided_tao_reserve( netuid.into(), - swap_result.tao_reserve_delta.abs().try_into().unwrap_or(0), + swap_result + .tao_reserve_delta + .abs() + .try_into() + .unwrap_or(0) + .into(), ); // Reduce total TAO reserves. TotalStake::::mutate(|total| { - *total = total.saturating_sub(swap_result.amount_paid_out) + *total = total.saturating_sub(swap_result.amount_paid_out.into()) }); // Increase total subnet TAO volume. @@ -754,9 +762,9 @@ impl Pallet { coldkey: &T::AccountId, netuid: NetUid, alpha: AlphaCurrency, - price_limit: u64, + price_limit: TaoCurrency, drop_fees: bool, - ) -> Result { + ) -> Result { // Decrease alpha on subnet let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid, alpha); @@ -790,7 +798,7 @@ impl Pallet { Self::deposit_event(Event::StakeRemoved( coldkey.clone(), hotkey.clone(), - swap_result.amount_paid_out, + swap_result.amount_paid_out.into(), actual_alpha_decrease, netuid, swap_result.fee_paid, @@ -806,7 +814,7 @@ impl Pallet { swap_result.fee_paid ); - Ok(swap_result.amount_paid_out) + Ok(swap_result.amount_paid_out.into()) } /// Stakes TAO into a subnet for a given hotkey and coldkey pair. @@ -816,8 +824,8 @@ impl Pallet { hotkey: &T::AccountId, coldkey: &T::AccountId, netuid: NetUid, - tao: u64, - price_limit: u64, + tao: TaoCurrency, + price_limit: TaoCurrency, set_limit: bool, ) -> Result { // Swap the tao to alpha. @@ -894,7 +902,7 @@ impl Pallet { destination_hotkey: &T::AccountId, netuid: NetUid, alpha: AlphaCurrency, - ) -> Result { + ) -> Result { // Decrease alpha on origin keys let actual_alpha_decrease = Self::decrease_stake_for_hotkey_and_coldkey_on_subnet( origin_hotkey, @@ -915,9 +923,10 @@ impl Pallet { // there's no slippage in this move) let current_price = ::SwapInterface::current_alpha_price(netuid.into()); - let tao_equivalent = current_price + let tao_equivalent: TaoCurrency = current_price .saturating_mul(U96F32::saturating_from_num(actual_alpha_moved)) - .saturating_to_num::(); + .saturating_to_num::() + .into(); // Ensure tao_equivalent is above DefaultMinStake ensure!( @@ -973,8 +982,8 @@ impl Pallet { coldkey: &T::AccountId, hotkey: &T::AccountId, netuid: NetUid, - mut stake_to_be_added: u64, - max_amount: u64, + mut stake_to_be_added: TaoCurrency, + max_amount: TaoCurrency, allow_partial: bool, ) -> Result<(), Error> { // Ensure that the subnet exists. @@ -986,13 +995,13 @@ impl Pallet { // Get the minimum balance (and amount) that satisfies the transaction let min_stake = DefaultMinStake::::get(); let min_amount = { - let fee = T::SwapInterface::sim_swap(netuid.into(), OrderType::Buy, min_stake) + let fee = T::SwapInterface::sim_swap(netuid.into(), OrderType::Buy, min_stake.into()) .map(|res| res.fee_paid) .unwrap_or(T::SwapInterface::approx_fee_amount( netuid.into(), - min_stake, + min_stake.into(), )); - min_stake.saturating_add(fee) + min_stake.saturating_add(fee.into()) }; // Ensure that the stake_to_be_added is at least the min_amount @@ -1008,7 +1017,7 @@ impl Pallet { // Ensure the callers coldkey has enough stake to perform the transaction. ensure!( - Self::can_remove_balance_from_coldkey_account(coldkey, stake_to_be_added), + Self::can_remove_balance_from_coldkey_account(coldkey, stake_to_be_added.into()), Error::::NotEnoughBalanceToStake ); @@ -1019,12 +1028,12 @@ impl Pallet { ); let swap_result = - T::SwapInterface::sim_swap(netuid.into(), OrderType::Buy, stake_to_be_added) + T::SwapInterface::sim_swap(netuid.into(), OrderType::Buy, stake_to_be_added.into()) .map_err(|_| Error::::InsufficientLiquidity)?; // Check that actual withdrawn TAO amount is not lower than the minimum stake ensure!( - swap_result.amount_paid_in >= min_stake, + TaoCurrency::from(swap_result.amount_paid_in) >= min_stake, Error::::AmountTooLow ); @@ -1073,7 +1082,7 @@ impl Pallet { Ok(res) => { if !remaining_alpha_stake.is_zero() { ensure!( - res.amount_paid_out >= DefaultMinStake::::get(), + TaoCurrency::from(res.amount_paid_out) >= DefaultMinStake::::get(), Error::::AmountTooLow ); } @@ -1214,7 +1223,7 @@ impl Pallet { .map(|res| res.amount_paid_out) .map_err(|_| Error::::InsufficientLiquidity)?; ensure!( - tao_equivalent > DefaultMinStake::::get(), + TaoCurrency::from(tao_equivalent) > DefaultMinStake::::get(), Error::::AmountTooLow ); @@ -1244,22 +1253,22 @@ impl Pallet { Ok(()) } - pub fn increase_provided_tao_reserve(netuid: NetUid, tao: u64) { + pub fn increase_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency) { SubnetTaoProvided::::mutate(netuid, |total| { *total = total.saturating_add(tao); }); } - pub fn decrease_provided_tao_reserve(netuid: NetUid, tao: u64) { + pub fn decrease_provided_tao_reserve(netuid: NetUid, tao: TaoCurrency) { // First, decrease SubnetTaoProvided, then deduct the rest from SubnetTAO let subnet_tao = SubnetTAO::::get(netuid); let subnet_tao_provided = SubnetTaoProvided::::get(netuid); let remainder = subnet_tao_provided.saturating_sub(tao); let carry_over = tao.saturating_sub(subnet_tao_provided); - if carry_over == 0 { + if carry_over.is_zero() { SubnetTaoProvided::::set(netuid, remainder); } else { - SubnetTaoProvided::::set(netuid, 0_u64); + SubnetTaoProvided::::set(netuid, TaoCurrency::ZERO); SubnetTAO::::set(netuid, subnet_tao.saturating_sub(carry_over)); } } diff --git a/pallets/subtensor/src/subnets/leasing.rs b/pallets/subtensor/src/subnets/leasing.rs index de4cbf3e5b..a96e82dada 100644 --- a/pallets/subtensor/src/subnets/leasing.rs +++ b/pallets/subtensor/src/subnets/leasing.rs @@ -7,7 +7,7 @@ use frame_system::pallet_prelude::*; use sp_core::blake2_256; use sp_runtime::{Percent, traits::TrailingZeroInput}; use substrate_fixed::types::U64F64; -use subtensor_runtime_common::{AlphaCurrency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; use subtensor_swap_interface::SwapHandler; pub type LeaseId = u32; @@ -293,7 +293,7 @@ impl Pallet { &lease.coldkey, lease.netuid, total_contributors_cut_alpha, - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), false, ) { Ok(tao_unstaked) => tao_unstaked, @@ -306,19 +306,19 @@ impl Pallet { // Distribute the contributors cut to the contributors and accumulate the tao // distributed so far to obtain how much tao is left to distribute to the beneficiary - let mut tao_distributed = 0u64; + let mut tao_distributed = TaoCurrency::ZERO; for (contributor, share) in SubnetLeaseShares::::iter_prefix(lease_id) { let tao_for_contributor = share - .saturating_mul(U64F64::from(tao_unstaked)) + .saturating_mul(U64F64::from(tao_unstaked.to_u64())) .floor() .saturating_to_num::(); Self::add_balance_to_coldkey_account(&contributor, tao_for_contributor); - tao_distributed = tao_distributed.saturating_add(tao_for_contributor); + tao_distributed = tao_distributed.saturating_add(tao_for_contributor.into()); } // Distribute the leftover tao to the beneficiary let beneficiary_cut_tao = tao_unstaked.saturating_sub(tao_distributed); - Self::add_balance_to_coldkey_account(&lease.beneficiary, beneficiary_cut_tao); + Self::add_balance_to_coldkey_account(&lease.beneficiary, beneficiary_cut_tao.into()); // Reset the accumulated dividends AccumulatedLeaseDividends::::insert(lease_id, AlphaCurrency::ZERO); diff --git a/pallets/subtensor/src/subnets/registration.rs b/pallets/subtensor/src/subnets/registration.rs index d999c299e5..45e41b233d 100644 --- a/pallets/subtensor/src/subnets/registration.rs +++ b/pallets/subtensor/src/subnets/registration.rs @@ -115,9 +115,9 @@ impl Pallet { ); // --- 7. Ensure the callers coldkey has enough stake to perform the transaction. - let registration_cost = Self::get_burn_as_u64(netuid); + let registration_cost = Self::get_burn(netuid); ensure!( - Self::can_remove_balance_from_coldkey_account(&coldkey, registration_cost), + Self::can_remove_balance_from_coldkey_account(&coldkey, registration_cost.into()), Error::::NotEnoughBalanceToStake ); @@ -138,12 +138,15 @@ impl Pallet { // --- 10. Ensure the remove operation from the coldkey is a success. let actual_burn_amount = - Self::remove_balance_from_coldkey_account(&coldkey, registration_cost)?; + Self::remove_balance_from_coldkey_account(&coldkey, registration_cost.into())?; // Tokens are swapped and then burned. - let burned_alpha = - Self::swap_tao_for_alpha(netuid, actual_burn_amount, T::SwapInterface::max_price())? - .amount_paid_out; + let burned_alpha = Self::swap_tao_for_alpha( + netuid, + actual_burn_amount, + T::SwapInterface::max_price().into(), + )? + .amount_paid_out; SubnetAlphaOut::::mutate(netuid, |total| { *total = total.saturating_sub(burned_alpha.into()) }); @@ -155,7 +158,7 @@ impl Pallet { BurnRegistrationsThisInterval::::mutate(netuid, |val| val.saturating_inc()); RegistrationsThisInterval::::mutate(netuid, |val| val.saturating_inc()); RegistrationsThisBlock::::mutate(netuid, |val| val.saturating_inc()); - Self::increase_rao_recycled(netuid, Self::get_burn_as_u64(netuid)); + Self::increase_rao_recycled(netuid, Self::get_burn(netuid).into()); // --- 15. Deposit successful event. log::debug!( @@ -384,7 +387,7 @@ impl Pallet { // --- 5. Add Balance via faucet. let balance_to_add: u64 = 1_000_000_000_000; - Self::coinbase(100_000_000_000); // We are creating tokens here from the coinbase. + Self::coinbase(100_000_000_000.into()); // We are creating tokens here from the coinbase. Self::add_balance_to_coldkey_account(&coldkey, balance_to_add); diff --git a/pallets/subtensor/src/subnets/subnet.rs b/pallets/subtensor/src/subnets/subnet.rs index 1e9aaca229..62b328d03f 100644 --- a/pallets/subtensor/src/subnets/subnet.rs +++ b/pallets/subtensor/src/subnets/subnet.rs @@ -1,6 +1,6 @@ use super::*; use sp_core::Get; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{NetUid, TaoCurrency}; impl Pallet { /// Fetches the total count of subnets. @@ -141,10 +141,10 @@ impl Pallet { ); // --- 5. Calculate and lock the required tokens. - let lock_amount: u64 = Self::get_network_lock_cost(); + let lock_amount = Self::get_network_lock_cost(); log::debug!("network lock_amount: {:?}", lock_amount); ensure!( - Self::can_remove_balance_from_coldkey_account(&coldkey, lock_amount), + Self::can_remove_balance_from_coldkey_account(&coldkey, lock_amount.into()), Error::::NotEnoughBalanceToStake ); @@ -152,8 +152,8 @@ impl Pallet { let netuid_to_register = Self::get_next_netuid(); // --- 7. Perform the lock operation. - let actual_tao_lock_amount: u64 = - Self::remove_balance_from_coldkey_account(&coldkey, lock_amount)?; + let actual_tao_lock_amount = + Self::remove_balance_from_coldkey_account(&coldkey, lock_amount.into())?; log::debug!("actual_tao_lock_amount: {:?}", actual_tao_lock_amount); // --- 8. Set the lock amount for use to determine pricing. @@ -193,7 +193,8 @@ impl Pallet { // Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha // The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO. let pool_initial_tao = Self::get_network_min_lock(); - let pool_initial_alpha = AlphaCurrency::from(Self::get_network_min_lock()); + // FIXME: the result from function is used as a mixed type alpha/tao + let pool_initial_alpha = AlphaCurrency::from(Self::get_network_min_lock().to_u64()); let actual_tao_lock_amount_less_pool_tao = actual_tao_lock_amount.saturating_sub(pool_initial_tao); SubnetTAO::::insert(netuid_to_register, pool_initial_tao); @@ -201,11 +202,11 @@ impl Pallet { SubnetOwner::::insert(netuid_to_register, coldkey.clone()); SubnetOwnerHotkey::::insert(netuid_to_register, hotkey.clone()); - if actual_tao_lock_amount_less_pool_tao > 0 { + if actual_tao_lock_amount_less_pool_tao > TaoCurrency::ZERO { Self::burn_tokens(actual_tao_lock_amount_less_pool_tao); } - if actual_tao_lock_amount > 0 && pool_initial_tao > 0 { + if actual_tao_lock_amount > TaoCurrency::ZERO && pool_initial_tao > TaoCurrency::ZERO { // Record in TotalStake the initial TAO in the pool. Self::increase_total_stake(pool_initial_tao); } diff --git a/pallets/subtensor/src/swap/swap_coldkey.rs b/pallets/subtensor/src/swap/swap_coldkey.rs index 7047a204e1..91da83e4e7 100644 --- a/pallets/subtensor/src/swap/swap_coldkey.rs +++ b/pallets/subtensor/src/swap/swap_coldkey.rs @@ -33,7 +33,7 @@ impl Pallet { pub fn do_swap_coldkey( old_coldkey: &T::AccountId, new_coldkey: &T::AccountId, - swap_cost: u64, + swap_cost: TaoCurrency, ) -> DispatchResultWithPostInfo { // 2. Initialize the weight for this operation let mut weight: Weight = T::DbWeight::get().reads(2); @@ -58,12 +58,13 @@ impl Pallet { // 6. Ensure sufficient balance for the swap cost ensure!( - Self::can_remove_balance_from_coldkey_account(old_coldkey, swap_cost), + Self::can_remove_balance_from_coldkey_account(old_coldkey, swap_cost.into()), Error::::NotEnoughBalanceToPaySwapColdKey ); // 7. Remove and burn the swap cost from the old coldkey's account - let actual_burn_amount = Self::remove_balance_from_coldkey_account(old_coldkey, swap_cost)?; + let actual_burn_amount = + Self::remove_balance_from_coldkey_account(old_coldkey, swap_cost.into())?; Self::burn_tokens(actual_burn_amount); // 8. Update the weight for the balance operations diff --git a/pallets/subtensor/src/swap/swap_hotkey.rs b/pallets/subtensor/src/swap/swap_hotkey.rs index 1b6274db54..b4a8aa1095 100644 --- a/pallets/subtensor/src/swap/swap_hotkey.rs +++ b/pallets/subtensor/src/swap/swap_hotkey.rs @@ -93,14 +93,15 @@ impl Pallet { // 13. Ensure the coldkey has enough balance to pay for the swap ensure!( - Self::can_remove_balance_from_coldkey_account(&coldkey, swap_cost), + Self::can_remove_balance_from_coldkey_account(&coldkey, swap_cost.into()), Error::::NotEnoughBalanceToPaySwapHotKey ); weight.saturating_accrue(T::DbWeight::get().reads_writes(3, 0)); // 14. Remove the swap cost from the coldkey's account - let actual_burn_amount = Self::remove_balance_from_coldkey_account(&coldkey, swap_cost)?; + let actual_burn_amount = + Self::remove_balance_from_coldkey_account(&coldkey, swap_cost.into())?; // 18. Burn the tokens Self::burn_tokens(actual_burn_amount); diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index 12991094e5..d23d755c4a 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -6,7 +6,7 @@ use super::mock::*; use approx::assert_abs_diff_eq; use frame_support::{assert_err, assert_noop, assert_ok}; use substrate_fixed::types::{I64F64, I96F32, U96F32}; -use subtensor_runtime_common::AlphaCurrency; +use subtensor_runtime_common::{AlphaCurrency, TaoCurrency}; use subtensor_swap_interface::SwapHandler; use crate::{utils::rate_limiting::TransactionType, *}; @@ -2258,7 +2258,7 @@ fn test_do_remove_stake_clears_pending_childkeys() { SubtensorModule::add_balance_to_coldkey_account(&coldkey, 10_000_000_000_000); let reserve = 1_000_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Set non-default value for childkey stake threshold StakeThreshold::::set(1_000_000_000_000); @@ -2470,13 +2470,13 @@ fn test_revoke_child_no_min_stake_check() { register_ok_neuron(netuid, parent, coldkey, 0); let reserve = 1_000_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); - mock::setup_reserves(NetUid::ROOT, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); + mock::setup_reserves(NetUid::ROOT, reserve.into(), reserve.into()); // Set minimum stake for setting children StakeThreshold::::put(1_000_000_000_000); - let (_, fee) = mock::swap_tao_to_alpha(NetUid::ROOT, StakeThreshold::::get()); + let (_, fee) = mock::swap_tao_to_alpha(NetUid::ROOT, StakeThreshold::::get().into()); SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( &parent, &coldkey, @@ -2548,11 +2548,11 @@ fn test_do_set_child_registration_disabled() { register_ok_neuron(netuid, parent, coldkey, 0); let reserve = 1_000_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Set minimum stake for setting children StakeThreshold::::put(1_000_000_000_000); - let (_, fee) = mock::swap_tao_to_alpha(netuid, StakeThreshold::::get()); + let (_, fee) = mock::swap_tao_to_alpha(netuid, StakeThreshold::::get().into()); SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet( &parent, &coldkey, @@ -2883,7 +2883,7 @@ fn test_childkey_take_drain() { // Add network, register hotkeys, and setup network parameters add_network(netuid, subnet_tempo, 0); - mock::setup_reserves(netuid, stake * 10_000, (stake * 10_000).into()); + mock::setup_reserves(netuid, (stake * 10_000).into(), (stake * 10_000).into()); register_ok_neuron(netuid, child_hotkey, child_coldkey, 0); register_ok_neuron(netuid, parent_hotkey, parent_coldkey, 1); register_ok_neuron(netuid, miner_hotkey, miner_coldkey, 1); @@ -2928,13 +2928,13 @@ fn test_childkey_take_drain() { RuntimeOrigin::signed(parent_coldkey), parent_hotkey, netuid, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(nominator), child_hotkey, netuid, - stake + stake.into() )); // Setup YUMA so that it creates emissions @@ -2972,9 +2972,17 @@ fn test_childkey_take_drain() { SubtensorModule::get_total_stake_for_coldkey(&nominator) - nominator_stake_before; let total_emission = child_emission + parent_emission + nominator_emission; - assert_abs_diff_eq!(child_emission, 0, epsilon = 10); - assert_abs_diff_eq!(parent_emission, total_emission * 9 / 20, epsilon = 10); - assert_abs_diff_eq!(nominator_emission, total_emission * 11 / 20, epsilon = 10); + assert_abs_diff_eq!(child_emission, TaoCurrency::ZERO, epsilon = 10.into()); + assert_abs_diff_eq!( + parent_emission, + total_emission * 9.into() / 20.into(), + epsilon = 10.into() + ); + assert_abs_diff_eq!( + nominator_emission, + total_emission * 11.into() / 20.into(), + epsilon = 10.into() + ); }); }); } @@ -2997,7 +3005,7 @@ fn test_parent_child_chain_emission() { Tempo::::insert(netuid, 1); // Setup large LPs to prevent slippage - SubnetTAO::::insert(netuid, 1_000_000_000_000_000); + SubnetTAO::::insert(netuid, TaoCurrency::from(1_000_000_000_000_000)); SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(1_000_000_000_000_000)); // Set owner cut to 0 @@ -3029,8 +3037,8 @@ fn test_parent_child_chain_emission() { let total_alpha: I96F32 = I96F32::from_num( SubtensorModule::swap_tao_for_alpha( netuid, - total_tao.to_num::(), - ::SwapInterface::max_price(), + total_tao.to_num::().into(), + ::SwapInterface::max_price().into(), ) .unwrap() .amount_paid_out, @@ -3064,9 +3072,9 @@ fn test_parent_child_chain_emission() { ); // Get old stakes - let stake_a: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_a); - let stake_b: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_b); - let stake_c: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_c); + let stake_a = SubtensorModule::get_total_stake_for_hotkey(&hotkey_a); + let stake_b = SubtensorModule::get_total_stake_for_hotkey(&hotkey_b); + let stake_c = SubtensorModule::get_total_stake_for_hotkey(&hotkey_c); let _total_stake: I96F32 = I96F32::from_num(stake_a + stake_b + stake_c); @@ -3090,11 +3098,11 @@ fn test_parent_child_chain_emission() { mock_set_children_no_epochs(netuid, &hotkey_b, &[(u64::MAX / 2, hotkey_c)]); // Get old stakes after children are scheduled - let stake_a_old: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_a); - let stake_b_old: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_b); - let stake_c_old: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_c); + let stake_a_old = SubtensorModule::get_total_stake_for_hotkey(&hotkey_a); + let stake_b_old = SubtensorModule::get_total_stake_for_hotkey(&hotkey_b); + let stake_c_old = SubtensorModule::get_total_stake_for_hotkey(&hotkey_c); - let total_stake_old: I96F32 = I96F32::from_num(stake_a_old + stake_b_old + stake_c_old); + let total_stake_old = I96F32::from_num((stake_a_old + stake_b_old + stake_c_old).to_u64()); log::info!("Old stake for hotkey A: {:?}", stake_a_old); log::info!("Old stake for hotkey B: {:?}", stake_b_old); log::info!("Old stake for hotkey C: {:?}", stake_c_old); @@ -3109,7 +3117,11 @@ fn test_parent_child_chain_emission() { // Set the weight of root TAO to be 0%, so only alpha is effective. SubtensorModule::set_tao_weight(0); - let emission: U96F32 = U96F32::from_num(SubtensorModule::get_block_emission().unwrap_or(0)); + let emission = U96F32::from_num( + SubtensorModule::get_block_emission() + .unwrap_or(TaoCurrency::ZERO) + .to_u64(), + ); // Set pending emission to 0 PendingEmission::::insert(netuid, AlphaCurrency::ZERO); @@ -3118,18 +3130,18 @@ fn test_parent_child_chain_emission() { SubtensorModule::run_coinbase(emission); // Log new stake - let stake_a_new: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_a); - let stake_b_new: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_b); - let stake_c_new: u64 = SubtensorModule::get_total_stake_for_hotkey(&hotkey_c); - let total_stake_new: I96F32 = I96F32::from_num(stake_a_new + stake_b_new + stake_c_new); + let stake_a_new = SubtensorModule::get_total_stake_for_hotkey(&hotkey_a); + let stake_b_new = SubtensorModule::get_total_stake_for_hotkey(&hotkey_b); + let stake_c_new = SubtensorModule::get_total_stake_for_hotkey(&hotkey_c); + let total_stake_new = I96F32::from_num((stake_a_new + stake_b_new + stake_c_new).to_u64()); log::info!("Stake for hotkey A: {:?}", stake_a_new); log::info!("Stake for hotkey B: {:?}", stake_b_new); log::info!("Stake for hotkey C: {:?}", stake_c_new); - let stake_inc_a: u64 = stake_a_new - stake_a_old; - let stake_inc_b: u64 = stake_b_new - stake_b_old; - let stake_inc_c: u64 = stake_c_new - stake_c_old; - let total_stake_inc: I96F32 = total_stake_new - total_stake_old; + let stake_inc_a = stake_a_new - stake_a_old; + let stake_inc_b = stake_b_new - stake_b_old; + let stake_inc_c = stake_c_new - stake_c_old; + let total_stake_inc = total_stake_new - total_stake_old; log::info!("Stake increase for hotkey A: {:?}", stake_inc_a); log::info!("Stake increase for hotkey B: {:?}", stake_inc_b); log::info!("Stake increase for hotkey C: {:?}", stake_inc_c); @@ -3236,11 +3248,11 @@ fn test_parent_child_chain_epoch() { SubtensorModule::add_balance_to_coldkey_account(&coldkey_b, 1_000); SubtensorModule::add_balance_to_coldkey_account(&coldkey_c, 1_000); - mock::setup_reserves(netuid, 1_000_000_000_000, 1_000_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000_000.into(), 1_000_000_000_000.into()); // Swap to alpha let total_tao = I96F32::from_num(300_000 + 100_000 + 50_000); - let (total_alpha, _) = mock::swap_tao_to_alpha(netuid, total_tao.to_num()); + let (total_alpha, _) = mock::swap_tao_to_alpha(netuid, total_tao.to_num::().into()); let total_alpha = I96F32::from_num(total_alpha); // Set the stakes directly @@ -3359,7 +3371,11 @@ fn test_dividend_distribution_with_children() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); add_network(netuid, 1, 0); - mock::setup_reserves(netuid, 1_000_000_000_000_000, 1_000_000_000_000_000.into()); + mock::setup_reserves( + netuid, + 1_000_000_000_000_000.into(), + 1_000_000_000_000_000.into(), + ); // Set owner cut to 0 SubtensorModule::set_subnet_owner_cut(0_u16); @@ -3383,7 +3399,7 @@ fn test_dividend_distribution_with_children() { // Swap to alpha let total_tao = I96F32::from_num(300_000 + 100_000 + 50_000); - let (total_alpha, _) = mock::swap_tao_to_alpha(netuid, total_tao.to_num()); + let (total_alpha, _) = mock::swap_tao_to_alpha(netuid, total_tao.to_num::().into()); let total_alpha = I96F32::from_num(total_alpha); // Set the stakes directly @@ -3615,11 +3631,11 @@ fn test_dynamic_parent_child_relationships() { SubtensorModule::add_balance_to_coldkey_account(&coldkey_child2, 30_000 + 1_000); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Swap to alpha let total_tao = I96F32::from_num(500_000 + 50_000 + 30_000); - let (total_alpha, _) = mock::swap_tao_to_alpha(netuid, total_tao.to_num()); + let (total_alpha, _) = mock::swap_tao_to_alpha(netuid, total_tao.to_num::().into()); let total_alpha = I96F32::from_num(total_alpha); log::info!("total_alpha: {:?}", total_alpha); @@ -3854,7 +3870,7 @@ fn test_do_set_child_as_sn_owner_not_enough_stake() { // Verify stake of sn_owner_hotkey is NOT enough assert!( SubtensorModule::get_total_stake_for_hotkey(&sn_owner_hotkey) - < StakeThreshold::::get() + < StakeThreshold::::get().into() ); // Verify that we can set child as sn owner, even though sn_owner_hotkey has insufficient stake @@ -3872,7 +3888,7 @@ fn test_do_set_child_as_sn_owner_not_enough_stake() { // Verify stake of other_sn_owner_hotkey is NOT enough assert!( SubtensorModule::get_total_stake_for_hotkey(&other_sn_owner_hotkey) - < StakeThreshold::::get() + < StakeThreshold::::get().into() ); // Can't set child as sn owner, because it is not in SubnetOwnerHotkey map @@ -3897,7 +3913,7 @@ fn test_dividend_distribution_with_children_same_coldkey_owner() { add_network(netuid, 1, 0); // Set SN owner cut to 0 SubtensorModule::set_subnet_owner_cut(0_u16); - mock::setup_reserves(netuid, 1_000_000_000_000, 1_000_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000_000.into(), 1_000_000_000_000.into()); // Define hotkeys and coldkeys let hotkey_a: U256 = U256::from(1); @@ -3914,7 +3930,7 @@ fn test_dividend_distribution_with_children_same_coldkey_owner() { // Swap to alpha let total_tao = 300_000 + 100_000; - let total_alpha = I96F32::from_num(mock::swap_tao_to_alpha(netuid, total_tao).0); + let total_alpha = I96F32::from_num(mock::swap_tao_to_alpha(netuid, total_tao.into()).0); let total_tao = I96F32::from_num(total_tao); // Set the stakes directly diff --git a/pallets/subtensor/src/tests/coinbase.rs b/pallets/subtensor/src/tests/coinbase.rs index 03fdf4227e..369e7d0d4f 100644 --- a/pallets/subtensor/src/tests/coinbase.rs +++ b/pallets/subtensor/src/tests/coinbase.rs @@ -52,7 +52,7 @@ fn test_dynamic_function_various_values() { for &alpha_emission in alpha_emission_values.iter() { // Set the price. SubnetMechanism::::insert(NetUid::from(1), 1); - SubnetTAO::::insert(NetUid::from(1), (price * 1_000_000_000.0) as u64); + SubnetTAO::::insert(NetUid::from(1), TaoCurrency::from((price * 1_000_000_000.0) as u64)); SubnetAlphaIn::::insert(NetUid::from(1), AlphaCurrency::from(1_000_000_000)); let (tao_in_emission, alpha_in_emission, alpha_out_emission) = SubtensorModule::get_dynamic_tao_emission(1.into(), tao_in, alpha_emission); assert!(tao_in_emission <= tao_in, "tao_in_emission is greater than tao_in"); @@ -90,9 +90,9 @@ fn test_coinbase_basecase() { fn test_coinbase_tao_issuance_base() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); - let emission: u64 = 1_234_567; + let emission = TaoCurrency::from(1_234_567); add_network(netuid, 1, 0); - assert_eq!(SubnetTAO::::get(netuid), 0); + assert_eq!(SubnetTAO::::get(netuid), TaoCurrency::ZERO); SubtensorModule::run_coinbase(U96F32::from_num(emission)); assert_eq!(SubnetTAO::::get(netuid), emission); assert_eq!(TotalIssuance::::get(), emission); @@ -105,9 +105,9 @@ fn test_coinbase_tao_issuance_base() { fn test_coinbase_tao_issuance_base_low() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); - let emission: u64 = 1; + let emission = TaoCurrency::from(1); add_network(netuid, 1, 0); - assert_eq!(SubnetTAO::::get(netuid), 0); + assert_eq!(SubnetTAO::::get(netuid), TaoCurrency::ZERO); SubtensorModule::run_coinbase(U96F32::from_num(emission)); assert_eq!(SubnetTAO::::get(netuid), emission); assert_eq!(TotalIssuance::::get(), emission); @@ -128,17 +128,17 @@ fn test_coinbase_tao_issuance_multiple() { let netuid1 = NetUid::from(1); let netuid2 = NetUid::from(2); let netuid3 = NetUid::from(3); - let emission: u64 = 3_333_333; + let emission = TaoCurrency::from(3_333_333); add_network(netuid1, 1, 0); add_network(netuid2, 1, 0); add_network(netuid3, 1, 0); - assert_eq!(SubnetTAO::::get(netuid1), 0); - assert_eq!(SubnetTAO::::get(netuid2), 0); - assert_eq!(SubnetTAO::::get(netuid3), 0); + assert_eq!(SubnetTAO::::get(netuid1), TaoCurrency::ZERO); + assert_eq!(SubnetTAO::::get(netuid2), TaoCurrency::ZERO); + assert_eq!(SubnetTAO::::get(netuid3), TaoCurrency::ZERO); SubtensorModule::run_coinbase(U96F32::from_num(emission)); - assert_eq!(SubnetTAO::::get(netuid1), emission / 3); - assert_eq!(SubnetTAO::::get(netuid2), emission / 3); - assert_eq!(SubnetTAO::::get(netuid3), emission / 3); + assert_eq!(SubnetTAO::::get(netuid1), emission / 3.into()); + assert_eq!(SubnetTAO::::get(netuid2), emission / 3.into()); + assert_eq!(SubnetTAO::::get(netuid3), emission / 3.into()); assert_eq!(TotalIssuance::::get(), emission); assert_eq!(TotalStake::::get(), emission); }); @@ -155,7 +155,7 @@ fn test_coinbase_tao_issuance_different_prices() { new_test_ext(1).execute_with(|| { let netuid1 = NetUid::from(1); let netuid2 = NetUid::from(2); - let emission: u64 = 100_000_000; + let emission = TaoCurrency::from(100_000_000); add_network(netuid1, 1, 0); add_network(netuid2, 1, 0); // Make subnets dynamic. @@ -165,15 +165,18 @@ fn test_coinbase_tao_issuance_different_prices() { SubnetMovingPrice::::insert(netuid1, I96F32::from_num(1)); SubnetMovingPrice::::insert(netuid2, I96F32::from_num(2)); // Assert initial TAO reserves. - assert_eq!(SubnetTAO::::get(netuid1), 0); - assert_eq!(SubnetTAO::::get(netuid2), 0); + assert_eq!(SubnetTAO::::get(netuid1), TaoCurrency::ZERO); + assert_eq!(SubnetTAO::::get(netuid2), TaoCurrency::ZERO); // Run the coinbase with the emission amount. SubtensorModule::run_coinbase(U96F32::from_num(emission)); // Assert tao emission is split evenly. - assert_eq!(SubnetTAO::::get(netuid1), emission / 3); - assert_eq!(SubnetTAO::::get(netuid2), emission / 3 + emission / 3); - close(TotalIssuance::::get(), emission, 2); - close(TotalStake::::get(), emission, 2); + assert_eq!(SubnetTAO::::get(netuid1), emission / 3.into()); + assert_eq!( + SubnetTAO::::get(netuid2), + emission / 3.into() + emission / 3.into() + ); + close(TotalIssuance::::get().into(), emission.into(), 2); + close(TotalStake::::get().into(), emission.into(), 2); }); } @@ -189,7 +192,7 @@ fn test_coinbase_moving_prices() { let netuid = NetUid::from(1); add_network(netuid, 1, 0); // Set price to 1.0 - SubnetTAO::::insert(netuid, 1_000_000); + SubnetTAO::::insert(netuid, TaoCurrency::from(1_000_000)); SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(1_000_000)); SubnetMechanism::::insert(netuid, 1); SubnetMovingPrice::::insert(netuid, I96F32::from_num(1)); @@ -245,7 +248,7 @@ fn test_update_moving_price_initial() { let netuid = NetUid::from(1); add_network(netuid, 1, 0); // Set current price to 1.0 - SubnetTAO::::insert(netuid, 1_000_000); + SubnetTAO::::insert(netuid, TaoCurrency::from(1_000_000)); SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(1_000_000)); SubnetMechanism::::insert(netuid, 1); SubnetMovingAlpha::::set(I96F32::from_num(0.5)); @@ -270,7 +273,7 @@ fn test_update_moving_price_after_time() { let netuid = NetUid::from(1); add_network(netuid, 1, 0); // Set current price to 1.0 - SubnetTAO::::insert(netuid, 1_000_000); + SubnetTAO::::insert(netuid, TaoCurrency::from(1_000_000)); SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(1_000_000)); SubnetMechanism::::insert(netuid, 1); SubnetMovingAlpha::::set(I96F32::from_num(0.5)); @@ -303,9 +306,9 @@ fn test_coinbase_alpha_issuance_base() { add_network(netuid2, 1, 0); // Set up prices 1 and 1 let initial: u64 = 1_000_000; - SubnetTAO::::insert(netuid1, initial); + SubnetTAO::::insert(netuid1, TaoCurrency::from(initial)); SubnetAlphaIn::::insert(netuid1, AlphaCurrency::from(initial)); - SubnetTAO::::insert(netuid2, initial); + SubnetTAO::::insert(netuid2, TaoCurrency::from(initial)); SubnetAlphaIn::::insert(netuid2, AlphaCurrency::from(initial)); // Check initial SubtensorModule::run_coinbase(U96F32::from_num(emission)); @@ -341,9 +344,9 @@ fn test_coinbase_alpha_issuance_different() { SubnetMechanism::::insert(netuid2, 1); // Setup prices 1 and 1 let initial: u64 = 1_000_000; - SubnetTAO::::insert(netuid1, initial); + SubnetTAO::::insert(netuid1, TaoCurrency::from(initial)); SubnetAlphaIn::::insert(netuid1, AlphaCurrency::from(initial)); - SubnetTAO::::insert(netuid2, initial); + SubnetTAO::::insert(netuid2, TaoCurrency::from(initial)); SubnetAlphaIn::::insert(netuid2, AlphaCurrency::from(initial)); // Set subnet prices. SubnetMovingPrice::::insert(netuid1, I96F32::from_num(1)); @@ -380,9 +383,9 @@ fn test_coinbase_alpha_issuance_with_cap_trigger() { // Setup prices 1000000 let initial: u64 = 1_000; let initial_alpha: u64 = initial * 1000000; - SubnetTAO::::insert(netuid1, initial); + SubnetTAO::::insert(netuid1, TaoCurrency::from(initial)); SubnetAlphaIn::::insert(netuid1, AlphaCurrency::from(initial_alpha)); // Make price extremely low. - SubnetTAO::::insert(netuid2, initial); + SubnetTAO::::insert(netuid2, TaoCurrency::from(initial)); SubnetAlphaIn::::insert(netuid2, AlphaCurrency::from(initial_alpha)); // Make price extremely low. // Set subnet prices. SubnetMovingPrice::::insert(netuid1, I96F32::from_num(1)); @@ -421,9 +424,9 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() { // Setup prices 1000000 let initial: u64 = 1_000; let initial_alpha = AlphaCurrency::from(initial * 1000000); - SubnetTAO::::insert(netuid1, initial); + SubnetTAO::::insert(netuid1, TaoCurrency::from(initial)); SubnetAlphaIn::::insert(netuid1, initial_alpha); // Make price extremely low. - SubnetTAO::::insert(netuid2, initial); + SubnetTAO::::insert(netuid2, TaoCurrency::from(initial)); SubnetAlphaIn::::insert(netuid2, initial_alpha); // Make price extremely low. // Set issuance to greater than 21M SubnetAlphaOut::::insert(netuid1, AlphaCurrency::from(22_000_000_000_000_000)); // Set issuance above 21M @@ -457,7 +460,7 @@ fn test_owner_cut_base() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); add_network(netuid, 1, 0); - mock::setup_reserves(netuid, 1_000_000_000_000, 1_000_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000_000.into(), 1_000_000_000_000.into()); SubtensorModule::set_tempo(netuid, 10000); // Large number (dont drain) SubtensorModule::set_subnet_owner_cut(0); SubtensorModule::run_coinbase(U96F32::from_num(0)); @@ -475,10 +478,10 @@ fn test_pending_swapped() { let netuid = NetUid::from(1); let emission: u64 = 1_000_000; add_network(netuid, 1, 0); - mock::setup_reserves(netuid, 1_000_000, 1.into()); + mock::setup_reserves(netuid, 1_000_000.into(), 1.into()); SubtensorModule::run_coinbase(U96F32::from_num(0)); assert_eq!(PendingAlphaSwapped::::get(netuid), 0.into()); // Zero tao weight and no root. - SubnetTAO::::insert(NetUid::ROOT, 1_000_000_000); // Add root weight. + SubnetTAO::::insert(NetUid::ROOT, TaoCurrency::from(1_000_000_000)); // Add root weight. SubtensorModule::run_coinbase(U96F32::from_num(0)); assert_eq!(PendingAlphaSwapped::::get(netuid), 0.into()); // Zero tao weight with 1 root. SubtensorModule::set_tempo(netuid, 10000); // Large number (dont drain) @@ -507,7 +510,13 @@ fn test_pending_swapped() { #[test] fn test_drain_base() { new_test_ext(1).execute_with(|| { - SubtensorModule::drain_pending_emission(0.into(), 0.into(), 0, 0.into(), 0.into()) + SubtensorModule::drain_pending_emission( + 0.into(), + AlphaCurrency::ZERO, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ) }); } @@ -517,7 +526,13 @@ fn test_drain_base_with_subnet() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); add_network(netuid, 1, 0); - SubtensorModule::drain_pending_emission(netuid, 0.into(), 0, 0.into(), 0.into()) + SubtensorModule::drain_pending_emission( + netuid, + AlphaCurrency::ZERO, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ) }); } @@ -540,9 +555,9 @@ fn test_drain_base_with_subnet_with_single_staker_not_registered() { SubtensorModule::drain_pending_emission( netuid, pending_alpha.into(), - 0, - 0.into(), - 0.into(), + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, ); let stake_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); @@ -567,7 +582,13 @@ fn test_drain_base_with_subnet_with_single_staker_registered() { stake_before, ); let pending_alpha = AlphaCurrency::from(1_000_000_000); - SubtensorModule::drain_pending_emission(netuid, pending_alpha, 0, 0.into(), 0.into()); + SubtensorModule::drain_pending_emission( + netuid, + pending_alpha, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ); let stake_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); close( @@ -603,15 +624,15 @@ fn test_drain_base_with_subnet_with_single_staker_registered_root_weight() { netuid, stake_before, ); - let pending_tao = 1_000_000_000; + let pending_tao = TaoCurrency::from(1_000_000_000); let pending_alpha = AlphaCurrency::from(1_000_000_000); - assert_eq!(SubnetTAO::::get(NetUid::ROOT), 0); + assert_eq!(SubnetTAO::::get(NetUid::ROOT), TaoCurrency::ZERO); SubtensorModule::drain_pending_emission( netuid, pending_alpha, pending_tao, - 0.into(), - 0.into(), + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, ); let stake_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); @@ -625,7 +646,11 @@ fn test_drain_base_with_subnet_with_single_staker_registered_root_weight() { stake_after.into(), 10, ); // Registered gets all alpha emission. - close(u64::from(stake_before) + pending_tao, root_after.into(), 10); // Registered gets all tao emission + close( + stake_before.to_u64() + pending_tao.to_u64(), + root_after.into(), + 10, + ); // Registered gets all tao emission assert_eq!(SubnetTAO::::get(NetUid::ROOT), pending_tao); }); } @@ -655,7 +680,13 @@ fn test_drain_base_with_subnet_with_two_stakers_registered() { stake_before, ); let pending_alpha = AlphaCurrency::from(1_000_000_000); - SubtensorModule::drain_pending_emission(netuid, pending_alpha, 0, 0.into(), 0.into()); + SubtensorModule::drain_pending_emission( + netuid, + pending_alpha, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ); let stake_after1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, netuid); let stake_after2 = @@ -712,15 +743,15 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root() { NetUid::ROOT, stake_before, ); - let pending_tao: u64 = 1_000_000_000; + let pending_tao = TaoCurrency::from(1_000_000_000); let pending_alpha = AlphaCurrency::from(1_000_000_000); - assert_eq!(SubnetTAO::::get(NetUid::ROOT), 0); + assert_eq!(SubnetTAO::::get(NetUid::ROOT), TaoCurrency::ZERO); SubtensorModule::drain_pending_emission( netuid, pending_alpha, pending_tao, - 0.into(), - 0.into(), + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, ); let stake_after1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, netuid); @@ -747,12 +778,12 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root() { 10, ); // Registered gets 1/2 emission. close( - u64::from(stake_before) + pending_tao / 2, + stake_before.to_u64() + pending_tao.to_u64() / 2, root_after1.into(), 10, ); // Registered gets 1/2 tao emission close( - u64::from(stake_before) + pending_tao / 2, + stake_before.to_u64() + pending_tao.to_u64() / 2, root_after2.into(), 10, ); // Registered gets 1/2 tao emission @@ -799,9 +830,9 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am NetUid::ROOT, stake_before, ); - let pending_tao: u64 = 1_000_000_000; + let pending_tao = TaoCurrency::from(1_000_000_000); let pending_alpha = AlphaCurrency::from(1_000_000_000); - assert_eq!(SubnetTAO::::get(NetUid::ROOT), 0); + assert_eq!(SubnetTAO::::get(NetUid::ROOT), TaoCurrency::ZERO); SubtensorModule::drain_pending_emission( netuid, pending_alpha, @@ -838,14 +869,14 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am epsilon = 10 ); // Registered gets 50% emission let expected_root1 = I96F32::from_num(2 * u64::from(stake_before)) - + I96F32::from_num(pending_tao) * I96F32::from_num(2.0 / 3.0); + + I96F32::from_num(pending_tao.to_u64()) * I96F32::from_num(2.0 / 3.0); assert_abs_diff_eq!( expected_root1.to_num::(), root_after1.into(), epsilon = 10 ); // Registered gets 2/3 tao emission let expected_root2 = I96F32::from_num(u64::from(stake_before)) - + I96F32::from_num(pending_tao) * I96F32::from_num(1.0 / 3.0); + + I96F32::from_num(pending_tao.to_u64()) * I96F32::from_num(1.0 / 3.0); assert_abs_diff_eq!( expected_root2.to_num::(), root_after2.into(), @@ -854,7 +885,7 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am assert_abs_diff_eq!( SubnetTAO::::get(NetUid::ROOT), pending_tao, - epsilon = 10 + epsilon = 10.into() ); }); } @@ -899,15 +930,15 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am NetUid::ROOT, stake_before, ); - let pending_tao: u64 = 1_000_000_000; + let pending_tao = TaoCurrency::from(1_000_000_000); let pending_alpha = AlphaCurrency::from(1_000_000_000); - assert_eq!(SubnetTAO::::get(NetUid::ROOT), 0); + assert_eq!(SubnetTAO::::get(NetUid::ROOT), TaoCurrency::ZERO); SubtensorModule::drain_pending_emission( netuid, pending_alpha, pending_tao, - 0.into(), - 0.into(), + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, ); let stake_after1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey1, &coldkey, netuid); @@ -956,7 +987,7 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am assert_abs_diff_eq!( SubnetTAO::::get(NetUid::ROOT), pending_tao, - epsilon = 10 + epsilon = 10.into() ); }); } @@ -984,7 +1015,13 @@ fn test_drain_alpha_childkey_parentkey() { ChildkeyTake::::insert(child, netuid, u16::MAX / 10); let pending_alpha = AlphaCurrency::from(1_000_000_000); - SubtensorModule::drain_pending_emission(netuid, pending_alpha, 0, 0.into(), 0.into()); + SubtensorModule::drain_pending_emission( + netuid, + pending_alpha, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ); let parent_stake_after = SubtensorModule::get_stake_for_hotkey_on_subnet(&parent, netuid); let child_stake_after = SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid); @@ -1106,11 +1143,11 @@ fn test_get_root_children() { // Assert Alice and Bob TAO inherited stakes assert_eq!( SubtensorModule::get_tao_inherited_for_hotkey_on_subnet(&alice, alpha), - 0 + TaoCurrency::ZERO ); assert_eq!( SubtensorModule::get_tao_inherited_for_hotkey_on_subnet(&bob, alpha), - u64::from(bob_root_stake + alice_root_stake) + u64::from(bob_root_stake + alice_root_stake).into() ); // Get Alice stake amounts on subnet alpha. @@ -1206,7 +1243,7 @@ fn test_get_root_children_drain() { SubtensorModule::drain_pending_emission( alpha, pending_alpha, - 0, + TaoCurrency::ZERO, AlphaCurrency::ZERO, AlphaCurrency::ZERO, ); @@ -1222,11 +1259,11 @@ fn test_get_root_children_drain() { ); // There should be no TAO on the root subnet. - assert_eq!(SubnetTAO::::get(NetUid::ROOT), 0); + assert_eq!(SubnetTAO::::get(NetUid::ROOT), TaoCurrency::ZERO); // Lets drain let pending_alpha = AlphaCurrency::from(1_000_000_000); - let pending_root1: u64 = 1_000_000_000; + let pending_root1 = TaoCurrency::from(1_000_000_000); SubtensorModule::drain_pending_emission( alpha, pending_alpha, @@ -1238,11 +1275,11 @@ fn test_get_root_children_drain() { // Alice and Bob both made half of the dividends. assert_eq!( SubtensorModule::get_stake_for_hotkey_on_subnet(&alice, NetUid::ROOT), - AlphaCurrency::from(alice_root_stake + pending_root1 / 2) + AlphaCurrency::from(alice_root_stake + pending_root1.to_u64() / 2) ); assert_eq!( SubtensorModule::get_stake_for_hotkey_on_subnet(&bob, NetUid::ROOT), - AlphaCurrency::from(bob_root_stake + pending_root1 / 2) + AlphaCurrency::from(bob_root_stake + pending_root1.to_u64() / 2) ); // The pending root dividends should be present in root subnet. @@ -1253,7 +1290,7 @@ fn test_get_root_children_drain() { // Lets drain let pending_alpha = AlphaCurrency::from(1_000_000_000); - let pending_root2: u64 = 1_000_000_000; + let pending_root2 = TaoCurrency::from(1_000_000_000); SubtensorModule::drain_pending_emission( alpha, pending_alpha, @@ -1267,7 +1304,10 @@ fn test_get_root_children_drain() { AlphaDividendsPerSubnet::::get(alpha, alice), AlphaCurrency::ZERO ); - assert_eq!(TaoDividendsPerSubnet::::get(alpha, alice), 0); + assert_eq!( + TaoDividendsPerSubnet::::get(alpha, alice), + TaoCurrency::ZERO + ); // Bob makes it all. assert_abs_diff_eq!( AlphaDividendsPerSubnet::::get(alpha, bob), @@ -1352,7 +1392,13 @@ fn test_get_root_children_drain_half_proportion() { // Lets drain! let pending_alpha = AlphaCurrency::from(1_000_000_000); - SubtensorModule::drain_pending_emission(alpha, pending_alpha, 0, 0.into(), 0.into()); + SubtensorModule::drain_pending_emission( + alpha, + pending_alpha, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ); // Alice and Bob make the same amount. close( @@ -1433,7 +1479,13 @@ fn test_get_root_children_drain_with_take() { // Lets drain! let pending_alpha = AlphaCurrency::from(1_000_000_000); - SubtensorModule::drain_pending_emission(alpha, pending_alpha, 0, 0.into(), 0.into()); + SubtensorModule::drain_pending_emission( + alpha, + pending_alpha, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ); // Bob makes it all. close( @@ -1514,7 +1566,13 @@ fn test_get_root_children_drain_with_half_take() { // Lets drain! let pending_alpha = AlphaCurrency::from(1_000_000_000); - SubtensorModule::drain_pending_emission(alpha, pending_alpha, 0, 0.into(), 0.into()); + SubtensorModule::drain_pending_emission( + alpha, + pending_alpha, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ); // Alice and Bob make the same amount. close( @@ -1682,7 +1740,7 @@ fn test_calculate_dividend_distribution_totals() { let mut dividends: BTreeMap = BTreeMap::new(); let pending_validator_alpha = AlphaCurrency::from(183_123_567_452); - let pending_tao: u64 = 837_120_949_872; + let pending_tao = TaoCurrency::from(837_120_949_872); let tao_weight: U96F32 = U96F32::saturating_from_num(0.18); // 18% let hotkeys = [U256::from(0), U256::from(1)]; @@ -1712,7 +1770,7 @@ fn test_calculate_dividend_distribution_totals() { ); assert_abs_diff_eq!( total_tao_dividends.saturating_to_num::(), - pending_tao, + pending_tao.to_u64(), epsilon = 1_000 ); }); @@ -1725,7 +1783,7 @@ fn test_calculate_dividend_distribution_total_only_tao() { let mut dividends: BTreeMap = BTreeMap::new(); let pending_validator_alpha = AlphaCurrency::ZERO; - let pending_tao: u64 = 837_120_949_872; + let pending_tao = TaoCurrency::from(837_120_949_872); let tao_weight: U96F32 = U96F32::saturating_from_num(0.18); // 18% let hotkeys = [U256::from(0), U256::from(1)]; @@ -1755,7 +1813,7 @@ fn test_calculate_dividend_distribution_total_only_tao() { ); assert_abs_diff_eq!( total_tao_dividends.saturating_to_num::(), - pending_tao, + pending_tao.to_u64(), epsilon = 1_000 ); }); @@ -1768,7 +1826,7 @@ fn test_calculate_dividend_distribution_total_no_tao_weight() { let mut dividends: BTreeMap = BTreeMap::new(); let pending_validator_alpha = AlphaCurrency::from(183_123_567_452); - let pending_tao: u64 = 0; // If tao weight is 0, then only alpha dividends should be input. + let pending_tao = TaoCurrency::ZERO; // If tao weight is 0, then only alpha dividends should be input. let tao_weight: U96F32 = U96F32::saturating_from_num(0.0); // 0% let hotkeys = [U256::from(0), U256::from(1)]; @@ -1798,7 +1856,7 @@ fn test_calculate_dividend_distribution_total_no_tao_weight() { ); assert_abs_diff_eq!( total_tao_dividends.saturating_to_num::(), - pending_tao, + pending_tao.to_u64(), epsilon = 1_000 ); }); @@ -1811,7 +1869,7 @@ fn test_calculate_dividend_distribution_total_only_alpha() { let mut dividends: BTreeMap = BTreeMap::new(); let pending_validator_alpha = AlphaCurrency::from(183_123_567_452); - let pending_tao: u64 = 0; + let pending_tao = TaoCurrency::ZERO; let tao_weight: U96F32 = U96F32::saturating_from_num(0.18); // 18% let hotkeys = [U256::from(0), U256::from(1)]; @@ -1841,7 +1899,7 @@ fn test_calculate_dividend_distribution_total_only_alpha() { ); assert_abs_diff_eq!( total_tao_dividends.saturating_to_num::(), - pending_tao, + pending_tao.to_u64(), epsilon = 1_000 ); }); @@ -1868,7 +1926,7 @@ fn test_calculate_dividend_and_incentive_distribution() { let pending_alpha = AlphaCurrency::from(123_456_789); let pending_validator_alpha = pending_alpha / 2.into(); // Pay half to validators. - let pending_tao: u64 = 0; + let pending_tao = TaoCurrency::ZERO; let pending_swapped = 0; // Only alpha output. let tao_weight: U96F32 = U96F32::saturating_from_num(0.0); // 0% @@ -1919,7 +1977,7 @@ fn test_calculate_dividend_and_incentive_distribution_all_to_validators() { let pending_alpha = AlphaCurrency::from(123_456_789); let pending_validator_alpha = pending_alpha; // Pay all to validators. - let pending_tao: u64 = 0; + let pending_tao = TaoCurrency::ZERO; let tao_weight: U96F32 = U96F32::saturating_from_num(0.0); // 0% // Hotkey, Incentive, Dividend @@ -2099,7 +2157,7 @@ fn test_drain_pending_emission_no_miners_all_drained() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey), - init_stake + init_stake.into() ); // Set the weight of root TAO to be 0%, so only alpha is effective. @@ -2108,7 +2166,13 @@ fn test_drain_pending_emission_no_miners_all_drained() { // Set the emission to be 1 million. let emission = AlphaCurrency::from(1_000_000); // Run drain pending without any miners. - SubtensorModule::drain_pending_emission(netuid, emission, 0, 0.into(), 0.into()); + SubtensorModule::drain_pending_emission( + netuid, + emission, + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ); // Get the new stake of the hotkey. let new_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey); @@ -2116,8 +2180,8 @@ fn test_drain_pending_emission_no_miners_all_drained() { // Slight epsilon due to rounding (hotkey_take). assert_abs_diff_eq!( new_stake, - u64::from(emission.saturating_add(init_stake.into())), - epsilon = 1 + u64::from(emission.saturating_add(init_stake.into())).into(), + epsilon = 1.into() ); }); } @@ -2147,7 +2211,7 @@ fn test_drain_pending_emission_zero_emission() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey), - init_stake + init_stake.into() ); // Set the weight of root TAO to be 0%, so only alpha is effective. @@ -2174,12 +2238,18 @@ fn test_drain_pending_emission_zero_emission() { Dividends::::remove(netuid); // Set the emission to be ZERO. - SubtensorModule::drain_pending_emission(netuid, 0.into(), 0, 0.into(), 0.into()); + SubtensorModule::drain_pending_emission( + netuid, + 0.into(), + TaoCurrency::ZERO, + AlphaCurrency::ZERO, + AlphaCurrency::ZERO, + ); // Get the new stake of the hotkey. let new_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey); // We expect the stake to remain unchanged. - assert_eq!(new_stake, init_stake); + assert_eq!(new_stake, init_stake.into()); // Check that the incentive and dividends are set by epoch. assert!(Incentive::::get(netuid).iter().sum::() > 0); @@ -2213,7 +2283,7 @@ fn test_run_coinbase_not_started() { SubtensorModule::set_weights_set_rate_limit(netuid, 0); let reserve = init_stake * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); register_ok_neuron(netuid, hotkey, coldkey, 0); register_ok_neuron(netuid, miner_hk, miner_ck, 0); @@ -2227,7 +2297,7 @@ fn test_run_coinbase_not_started() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey), - init_stake + init_stake.into() ); // Set the weight of root TAO to be 0%, so only alpha is effective. @@ -2267,7 +2337,7 @@ fn test_run_coinbase_not_started() { // Get the new stake of the hotkey. We expect no emissions. let new_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey); // We expect the stake to remain unchanged. - assert_eq!(new_stake, init_stake); + assert_eq!(new_stake, init_stake.into()); // Check that the incentive and dividends are set. assert!(Incentive::::get(netuid).iter().sum::() > 0); @@ -2312,7 +2382,7 @@ fn test_run_coinbase_not_started_start_after() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey), - init_stake + init_stake.into() ); // Set the weight of root TAO to be 0%, so only alpha is effective. @@ -2371,7 +2441,7 @@ fn test_run_coinbase_not_started_start_after() { // Get the new stake of the hotkey. We expect no emissions. let new_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey); // We expect the stake to remain unchanged. - assert!(new_stake > init_stake); + assert!(new_stake > init_stake.into()); log::info!("new_stake: {}", new_stake); }); } @@ -2388,7 +2458,8 @@ fn test_coinbase_v3_liquidity_update() { let netuid = add_dynamic_network(&owner_hotkey, &owner_coldkey); // Force the swap to initialize - SubtensorModule::swap_tao_for_alpha(netuid, 0, 1_000_000_000_000).unwrap(); + SubtensorModule::swap_tao_for_alpha(netuid, TaoCurrency::ZERO, 1_000_000_000_000.into()) + .unwrap(); let protocol_account_id = pallet_subtensor_swap::Pallet::::protocol_account_id(); let position = pallet_subtensor_swap::Positions::::get(( diff --git a/pallets/subtensor/src/tests/delegate_info.rs b/pallets/subtensor/src/tests/delegate_info.rs index 22b2aab2bf..f0c17825c0 100644 --- a/pallets/subtensor/src/tests/delegate_info.rs +++ b/pallets/subtensor/src/tests/delegate_info.rs @@ -125,7 +125,7 @@ fn test_get_delegated() { RuntimeOrigin::signed(*delegatee), *delegate, *netuid, - *amount + (*amount).into() )); let expected_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( delegate, delegatee, *netuid, diff --git a/pallets/subtensor/src/tests/epoch.rs b/pallets/subtensor/src/tests/epoch.rs index c682417bab..31d30da880 100644 --- a/pallets/subtensor/src/tests/epoch.rs +++ b/pallets/subtensor/src/tests/epoch.rs @@ -11,7 +11,7 @@ use frame_support::{assert_err, assert_ok}; use rand::{Rng, SeedableRng, distributions::Uniform, rngs::StdRng, seq::SliceRandom, thread_rng}; use sp_core::{Get, U256}; use substrate_fixed::types::I32F32; -use subtensor_runtime_common::AlphaCurrency; +use subtensor_runtime_common::{AlphaCurrency, TaoCurrency}; use subtensor_swap_interface::SwapHandler; use super::mock::*; @@ -576,7 +576,7 @@ fn test_1_graph() { RuntimeOrigin::signed(coldkey), hotkey, netuid, - stake_amount + stake_amount.into() )); assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 1); @@ -593,7 +593,7 @@ fn test_1_graph() { SubtensorModule::epoch(netuid, 1_000_000_000.into()); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey), - stake_amount + stake_amount.into() ); assert_eq!(SubtensorModule::get_rank_for_uid(netuid, uid), 0); assert_eq!(SubtensorModule::get_trust_for_uid(netuid, uid), 0); @@ -655,7 +655,7 @@ fn test_10_graph() { for i in 0..n { assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&(U256::from(i))), - 1 + TaoCurrency::from(1) ); assert_eq!(SubtensorModule::get_rank_for_uid(netuid, i as u16), 0); assert_eq!(SubtensorModule::get_trust_for_uid(netuid, i as u16), 0); @@ -712,7 +712,7 @@ fn test_512_graph() { for uid in validators { assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&(U256::from(uid))), - max_stake_per_validator + max_stake_per_validator.into() ); assert_eq!(SubtensorModule::get_rank_for_uid(netuid, uid), 0); assert_eq!(SubtensorModule::get_trust_for_uid(netuid, uid), 0); @@ -730,7 +730,7 @@ fn test_512_graph() { for uid in servers { assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&(U256::from(uid))), - 0 + TaoCurrency::ZERO ); assert_eq!(SubtensorModule::get_rank_for_uid(netuid, uid), 146); // Note R = floor(1 / (512 - 64) * 65_535) = 146 assert_eq!(SubtensorModule::get_trust_for_uid(netuid, uid), 65535); @@ -1315,13 +1315,13 @@ fn test_set_alpha_disabled() { assert_ok!(SubtensorModule::root_register(signer.clone(), hotkey,)); let fee = ::SwapInterface::approx_fee_amount( netuid.into(), - DefaultMinStake::::get(), + DefaultMinStake::::get().into(), ); assert_ok!(SubtensorModule::add_stake( signer.clone(), hotkey, netuid, - 5 * DefaultMinStake::::get() + fee + (5 * DefaultMinStake::::get().to_u64() + fee).into() )); // Only owner can set alpha values assert_ok!(SubtensorModule::register_network(signer.clone(), hotkey)); @@ -2290,14 +2290,14 @@ fn test_get_set_alpha() { let fee = ::SwapInterface::approx_fee_amount( netuid.into(), - DefaultMinStake::::get(), + DefaultMinStake::::get().into(), ); assert_ok!(SubtensorModule::add_stake( signer.clone(), hotkey, netuid, - DefaultMinStake::::get() + fee * 2 + (DefaultMinStake::::get().to_u64() + fee * 2).into() )); assert_ok!(SubtensorModule::do_set_alpha_values( diff --git a/pallets/subtensor/src/tests/leasing.rs b/pallets/subtensor/src/tests/leasing.rs index ac27347055..4ffc18230a 100644 --- a/pallets/subtensor/src/tests/leasing.rs +++ b/pallets/subtensor/src/tests/leasing.rs @@ -528,26 +528,27 @@ fn test_distribute_lease_network_dividends_multiple_contributors_works() { assert_eq!( distributed_tao, - beneficiary_balance_delta + contributor1_balance_delta + contributor2_balance_delta + (beneficiary_balance_delta + contributor1_balance_delta + contributor2_balance_delta) + .into() ); let expected_contributor1_balance = SubnetLeaseShares::::get(lease_id, contributions[0].0) - .saturating_mul(U64F64::from(distributed_tao)) + .saturating_mul(U64F64::from(distributed_tao.to_u64())) .floor() .to_num::(); assert_eq!(contributor1_balance_delta, expected_contributor1_balance); let expected_contributor2_balance = SubnetLeaseShares::::get(lease_id, contributions[1].0) - .saturating_mul(U64F64::from(distributed_tao)) + .saturating_mul(U64F64::from(distributed_tao.to_u64())) .floor() .to_num::(); assert_eq!(contributor2_balance_delta, expected_contributor2_balance); // The beneficiary should have received the remaining dividends - let expected_beneficiary_balance = - distributed_tao - (expected_contributor1_balance + expected_contributor2_balance); + let expected_beneficiary_balance = distributed_tao.to_u64() + - (expected_contributor1_balance + expected_contributor2_balance); assert_eq!(beneficiary_balance_delta, expected_beneficiary_balance); // Ensure nothing was accumulated for later distribution @@ -599,7 +600,7 @@ fn test_distribute_lease_network_dividends_only_beneficiary_works() { let distributed_tao = subnet_tao_before - SubnetTAO::::get(lease.netuid); let beneficiary_balance_delta = SubtensorModule::get_coldkey_balance(&beneficiary) .saturating_sub(beneficiary_balance_before); - assert_eq!(distributed_tao, beneficiary_balance_delta); + assert_eq!(distributed_tao, beneficiary_balance_delta.into()); // Ensure nothing was accumulated for later distribution assert_eq!( @@ -916,7 +917,7 @@ fn setup_leased_network( RuntimeOrigin::signed(lease.coldkey), lease.hotkey, netuid, - tao_to_stake + tao_to_stake.into() )); } diff --git a/pallets/subtensor/src/tests/migration.rs b/pallets/subtensor/src/tests/migration.rs index 895d48b2eb..cad3402895 100644 --- a/pallets/subtensor/src/tests/migration.rs +++ b/pallets/subtensor/src/tests/migration.rs @@ -22,6 +22,7 @@ use sp_io::hashing::twox_128; use sp_runtime::traits::Zero; use substrate_fixed::types::I96F32; use substrate_fixed::types::extra::U2; +use subtensor_runtime_common::TaoCurrency; #[allow(clippy::arithmetic_side_effects)] fn close(value: u64, target: u64, eps: u64) { @@ -40,20 +41,19 @@ fn test_initialise_ti() { new_test_ext(1).execute_with(|| { pallet_balances::TotalIssuance::::put(1000); - crate::SubnetTAO::::insert(NetUid::from(1), 100); - crate::SubnetTAO::::insert(NetUid::from(2), 5); + crate::SubnetTAO::::insert(NetUid::from(1), TaoCurrency::from(100)); + crate::SubnetTAO::::insert(NetUid::from(2), TaoCurrency::from(5)); // Ensure values are NOT initialized prior to running migration - assert!(crate::TotalIssuance::::get() == 0); - assert!(crate::TotalStake::::get() == 0); + assert!(crate::TotalIssuance::::get().is_zero()); + assert!(crate::TotalStake::::get().is_zero()); crate::migrations::migrate_init_total_issuance::initialise_total_issuance::Migration::::on_runtime_upgrade(); // Ensure values were initialized correctly - assert!(crate::TotalStake::::get() == 105); - assert!( - crate::TotalIssuance::::get() - == 105u64.saturating_add(1000) + assert_eq!(crate::TotalStake::::get(), TaoCurrency::from(105)); + assert_eq!( + crate::TotalIssuance::::get(), TaoCurrency::from(105 + 1000) ); }); } @@ -842,7 +842,7 @@ fn test_migrate_fix_root_subnet_tao() { expected_total_stake += stake; } - assert_eq!(SubnetTAO::::get(NetUid::ROOT), 0); + assert_eq!(SubnetTAO::::get(NetUid::ROOT), TaoCurrency::ZERO); assert!( !HasMigrationRun::::get(MIGRATION_NAME.as_bytes().to_vec()), "Migration should not have run yet" @@ -858,7 +858,10 @@ fn test_migrate_fix_root_subnet_tao() { "Migration should be marked as run" ); assert!(!weight.is_zero(), "Migration weight should be non-zero"); - assert_eq!(SubnetTAO::::get(NetUid::ROOT), expected_total_stake); + assert_eq!( + SubnetTAO::::get(NetUid::ROOT), + expected_total_stake.into() + ); }); } diff --git a/pallets/subtensor/src/tests/mock.rs b/pallets/subtensor/src/tests/mock.rs index 10193f1ee3..24d0448f7c 100644 --- a/pallets/subtensor/src/tests/mock.rs +++ b/pallets/subtensor/src/tests/mock.rs @@ -23,7 +23,7 @@ use sp_runtime::{ traits::{BlakeTwo256, IdentityLookup}, }; use sp_std::{cell::RefCell, cmp::Ordering}; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{NetUid, TaoCurrency}; use subtensor_swap_interface::{OrderType, SwapHandler}; type Block = frame_system::mocking::MockBlock; @@ -852,7 +852,7 @@ pub fn add_network_disable_subtoken(netuid: NetUid, tempo: u16, _modality: u16) pub fn add_dynamic_network(hotkey: &U256, coldkey: &U256) -> NetUid { let netuid = SubtensorModule::get_next_netuid(); let lock_cost = SubtensorModule::get_network_lock_cost(); - SubtensorModule::add_balance_to_coldkey_account(coldkey, lock_cost); + SubtensorModule::add_balance_to_coldkey_account(coldkey, lock_cost.into()); assert_ok!(SubtensorModule::register_network( RawOrigin::Signed(*coldkey).into(), @@ -869,7 +869,7 @@ pub fn add_dynamic_network(hotkey: &U256, coldkey: &U256) -> NetUid { pub fn add_dynamic_network_without_emission_block(hotkey: &U256, coldkey: &U256) -> NetUid { let netuid = SubtensorModule::get_next_netuid(); let lock_cost = SubtensorModule::get_network_lock_cost(); - SubtensorModule::add_balance_to_coldkey_account(coldkey, lock_cost); + SubtensorModule::add_balance_to_coldkey_account(coldkey, lock_cost.into()); assert_ok!(SubtensorModule::register_network( RawOrigin::Signed(*coldkey).into(), @@ -882,8 +882,8 @@ pub fn add_dynamic_network_without_emission_block(hotkey: &U256, coldkey: &U256) // Helper function to set up a neuron with stake #[allow(dead_code)] -pub fn setup_neuron_with_stake(netuid: NetUid, hotkey: U256, coldkey: U256, stake: u64) { - register_ok_neuron(netuid, hotkey, coldkey, stake); +pub fn setup_neuron_with_stake(netuid: NetUid, hotkey: U256, coldkey: U256, stake: TaoCurrency) { + register_ok_neuron(netuid, hotkey, coldkey, stake.into()); increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); } @@ -952,7 +952,7 @@ pub fn step_rate_limit(transaction_type: &TransactionType, netuid: NetUid) { pub fn increase_stake_on_coldkey_hotkey_account( coldkey: &U256, hotkey: &U256, - tao_staked: u64, + tao_staked: TaoCurrency, netuid: NetUid, ) { SubtensorModule::stake_into_subnet( @@ -960,7 +960,7 @@ pub fn increase_stake_on_coldkey_hotkey_account( coldkey, netuid, tao_staked, - ::SwapInterface::max_price(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -972,7 +972,7 @@ pub fn increase_stake_on_coldkey_hotkey_account( /// * `hotkey` - The hotkey account ID. /// * `increment` - The amount to be incremented. #[allow(dead_code)] -pub fn increase_stake_on_hotkey_account(hotkey: &U256, increment: u64, netuid: NetUid) { +pub fn increase_stake_on_hotkey_account(hotkey: &U256, increment: TaoCurrency, netuid: NetUid) { increase_stake_on_coldkey_hotkey_account( &SubtensorModule::get_owning_coldkey_for_hotkey(hotkey), hotkey, @@ -985,20 +985,20 @@ pub(crate) fn remove_stake_rate_limit_for_tests(hotkey: &U256, coldkey: &U256, n StakingOperationRateLimiter::::remove((hotkey, coldkey, netuid)); } -pub(crate) fn setup_reserves(netuid: NetUid, tao: u64, alpha: AlphaCurrency) { +pub(crate) fn setup_reserves(netuid: NetUid, tao: TaoCurrency, alpha: AlphaCurrency) { SubnetTAO::::set(netuid, tao); SubnetAlphaIn::::set(netuid, alpha); } -pub(crate) fn swap_tao_to_alpha(netuid: NetUid, tao: u64) -> (AlphaCurrency, u64) { +pub(crate) fn swap_tao_to_alpha(netuid: NetUid, tao: TaoCurrency) -> (AlphaCurrency, u64) { if netuid.is_root() { - return (tao.into(), 0); + return (tao.to_u64().into(), 0); } let result = ::SwapInterface::swap( netuid.into(), OrderType::Buy, - tao, + tao.into(), ::SwapInterface::max_price(), false, true, @@ -1018,9 +1018,9 @@ pub(crate) fn swap_alpha_to_tao_ext( netuid: NetUid, alpha: AlphaCurrency, drop_fees: bool, -) -> (u64, u64) { +) -> (TaoCurrency, u64) { if netuid.is_root() { - return (alpha.into(), 0); + return (alpha.to_u64().into(), 0); } println!( @@ -1044,10 +1044,10 @@ pub(crate) fn swap_alpha_to_tao_ext( // we don't want to have silent 0 comparissons in tests assert!(result.amount_paid_out > 0); - (result.amount_paid_out, result.fee_paid) + (result.amount_paid_out.into(), result.fee_paid) } -pub(crate) fn swap_alpha_to_tao(netuid: NetUid, alpha: AlphaCurrency) -> (u64, u64) { +pub(crate) fn swap_alpha_to_tao(netuid: NetUid, alpha: AlphaCurrency) -> (TaoCurrency, u64) { swap_alpha_to_tao_ext(netuid, alpha, false) } diff --git a/pallets/subtensor/src/tests/move_stake.rs b/pallets/subtensor/src/tests/move_stake.rs index d80571c594..0c0a5c7de9 100644 --- a/pallets/subtensor/src/tests/move_stake.rs +++ b/pallets/subtensor/src/tests/move_stake.rs @@ -6,6 +6,7 @@ use frame_system::RawOrigin; use sp_core::{Get, U256}; use sp_runtime::traits::TxBaseImplication; use substrate_fixed::types::{U64F64, U96F32}; +use subtensor_runtime_common::TaoCurrency; use subtensor_swap_interface::SwapHandler; use super::mock; @@ -24,7 +25,7 @@ fn test_do_move_success() { let coldkey = U256::from(1); let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get() * 10.into(); // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey); @@ -34,7 +35,7 @@ fn test_do_move_success() { &coldkey, netuid.into(), stake_amount, - ::SwapInterface::max_price(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -89,16 +90,16 @@ fn test_do_move_different_subnets() { let coldkey = U256::from(1); let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; mock::setup_reserves( origin_netuid, - stake_amount * 100, + (stake_amount * 100).into(), (stake_amount * 100).into(), ); mock::setup_reserves( destination_netuid, - stake_amount * 100, + (stake_amount * 100).into(), (stake_amount * 100).into(), ); @@ -109,8 +110,8 @@ fn test_do_move_different_subnets() { &origin_hotkey, &coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -171,15 +172,15 @@ fn test_do_move_nonexistent_subnet() { let stake_amount = 1_000_000; let reserve = stake_amount * 1000; - mock::setup_reserves(origin_netuid, reserve, reserve.into()); + mock::setup_reserves(origin_netuid, reserve.into(), reserve.into()); // Set up initial stake SubtensorModule::stake_into_subnet( &origin_hotkey, &coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -273,7 +274,7 @@ fn test_do_move_nonexistent_destination_hotkey() { let stake_amount = 1_000_000; let reserve = stake_amount * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey); @@ -281,8 +282,8 @@ fn test_do_move_nonexistent_destination_hotkey() { &origin_hotkey, &coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -338,15 +339,15 @@ fn test_do_move_partial_stake() { let coldkey = U256::from(1); let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); - let total_stake = DefaultMinStake::::get() * 20; + let total_stake = DefaultMinStake::::get().to_u64() * 20; // Set up initial stake SubtensorModule::stake_into_subnet( &origin_hotkey, &coldkey, netuid, - total_stake, - ::SwapInterface::max_price(), + total_stake.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -404,7 +405,7 @@ fn test_do_move_multiple_times() { let coldkey = U256::from(1); let hotkey1 = U256::from(2); let hotkey2 = U256::from(3); - let initial_stake = DefaultMinStake::::get() * 10; + let initial_stake = DefaultMinStake::::get().to_u64() * 10; // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey1); @@ -413,8 +414,8 @@ fn test_do_move_multiple_times() { &hotkey1, &coldkey, netuid, - initial_stake, - ::SwapInterface::max_price(), + initial_stake.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -474,18 +475,18 @@ fn test_do_move_wrong_origin() { let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); let netuid = NetUid::from(1); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; let reserve = stake_amount * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Set up initial stake SubtensorModule::stake_into_subnet( &origin_hotkey, &coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -542,7 +543,7 @@ fn test_do_move_same_hotkey_fails() { let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); let coldkey = U256::from(1); let hotkey = U256::from(2); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); @@ -550,8 +551,8 @@ fn test_do_move_same_hotkey_fails() { &hotkey, &coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -591,7 +592,7 @@ fn test_do_move_event_emission() { let coldkey = U256::from(1); let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; // Set up initial stake SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey); @@ -600,8 +601,8 @@ fn test_do_move_event_emission() { &origin_hotkey, &coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -633,7 +634,7 @@ fn test_do_move_event_emission() { netuid, destination_hotkey, netuid, - tao_equivalent, // Should be TAO equivalent + tao_equivalent.into(), // Should be TAO equivalent ) .into(), ); @@ -653,15 +654,15 @@ fn test_do_move_storage_updates() { let coldkey = U256::from(1); let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; // Set up initial stake SubtensorModule::stake_into_subnet( &origin_hotkey, &coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -717,7 +718,7 @@ fn test_move_full_amount_same_netuid() { let coldkey = U256::from(1); let origin_hotkey = U256::from(2); let destination_hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&coldkey, &origin_hotkey); SubtensorModule::create_account_if_non_existent(&coldkey, &destination_hotkey); @@ -726,8 +727,8 @@ fn test_move_full_amount_same_netuid() { &origin_hotkey, &coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -787,14 +788,14 @@ fn test_do_move_max_values() { // Add lots of liquidity to bypass low liquidity check let reserve = u64::MAX / 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); SubtensorModule::stake_into_subnet( &origin_hotkey, &coldkey, netuid, - max_stake, - ::SwapInterface::max_price(), + max_stake.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -850,13 +851,16 @@ fn test_moving_too_little_unstakes() { let (_, fee) = mock::swap_tao_to_alpha(netuid, amount); - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount + fee * 2); + SubtensorModule::add_balance_to_coldkey_account( + &coldkey_account_id, + amount.to_u64() + fee * 2, + ); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + fee * 2 + (amount.to_u64() + fee * 2).into() )); remove_stake_rate_limit_for_tests(&hotkey_account_id, &coldkey_account_id, netuid); @@ -886,7 +890,7 @@ fn test_do_transfer_success() { let origin_coldkey = U256::from(1); let destination_coldkey = U256::from(2); let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; // 3. Set up initial stake: (origin_coldkey, hotkey) on netuid. SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); @@ -895,8 +899,8 @@ fn test_do_transfer_success() { &hotkey, &origin_coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -945,7 +949,7 @@ fn test_do_transfer_nonexistent_subnet() { let destination_coldkey = U256::from(2); let hotkey = U256::from(3); let nonexistent_netuid = NetUid::from(9999); - let stake_amount = DefaultMinStake::::get() * 5; + let stake_amount = DefaultMinStake::::get().to_u64() * 5; assert_noop!( SubtensorModule::do_transfer_stake( @@ -996,15 +1000,15 @@ fn test_do_transfer_insufficient_stake() { let origin_coldkey = U256::from(1); let destination_coldkey = U256::from(2); let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); SubtensorModule::stake_into_subnet( &hotkey, &origin_coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1035,7 +1039,7 @@ fn test_do_transfer_wrong_origin() { let wrong_coldkey = U256::from(9999); let destination_coldkey = U256::from(2); let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); @@ -1044,8 +1048,8 @@ fn test_do_transfer_wrong_origin() { &hotkey, &origin_coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1082,7 +1086,7 @@ fn test_do_transfer_minimum_stake_check() { &origin_coldkey, netuid, stake_amount, - ::SwapInterface::max_price(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1114,7 +1118,7 @@ fn test_do_transfer_different_subnets() { let origin_coldkey = U256::from(1); let destination_coldkey = U256::from(2); let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; // 3. Create accounts if needed. SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); @@ -1128,8 +1132,8 @@ fn test_do_transfer_different_subnets() { &hotkey, &origin_coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1186,15 +1190,15 @@ fn test_do_swap_success() { let coldkey = U256::from(1); let hotkey = U256::from(2); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( &hotkey, &coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1292,7 +1296,7 @@ fn test_do_swap_insufficient_stake() { let coldkey = U256::from(1); let hotkey = U256::from(2); - let stake_amount = DefaultMinStake::::get() * 5; + let stake_amount = DefaultMinStake::::get().to_u64() * 5; let attempted_swap = stake_amount * 2; SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); @@ -1300,8 +1304,8 @@ fn test_do_swap_insufficient_stake() { &hotkey, &coldkey, netuid1, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1337,8 +1341,8 @@ fn test_do_swap_wrong_origin() { &hotkey, &real_coldkey, netuid1, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1375,7 +1379,7 @@ fn test_do_swap_minimum_stake_check() { &coldkey, netuid1, total_stake, - ::SwapInterface::max_price(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1402,15 +1406,15 @@ fn test_do_swap_same_subnet() { let coldkey = U256::from(1); let hotkey = U256::from(2); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( &hotkey, &coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1446,15 +1450,15 @@ fn test_do_swap_partial_stake() { let coldkey = U256::from(1); let hotkey = U256::from(2); - let total_stake_tao = DefaultMinStake::::get() * 10; + let total_stake_tao = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( &hotkey, &coldkey, origin_netuid, - total_stake_tao, - ::SwapInterface::max_price(), + total_stake_tao.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1497,15 +1501,15 @@ fn test_do_swap_storage_updates() { let coldkey = U256::from(1); let hotkey = U256::from(2); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( &hotkey, &coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1556,15 +1560,15 @@ fn test_do_swap_multiple_times() { let coldkey = U256::from(1); let hotkey = U256::from(2); - let initial_stake = DefaultMinStake::::get() * 10; + let initial_stake = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::stake_into_subnet( &hotkey, &coldkey, netuid1, - initial_stake, - ::SwapInterface::max_price(), + initial_stake.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1626,15 +1630,15 @@ fn test_do_swap_allows_non_owned_hotkey() { let coldkey = U256::from(1); let hotkey = U256::from(2); let foreign_coldkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&foreign_coldkey, &hotkey); SubtensorModule::stake_into_subnet( &hotkey, &coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1671,16 +1675,16 @@ fn test_swap_stake_limit_validate() { let stake_amount = 100_000_000_000; let reserve = 1_000_000_000_000; - mock::setup_reserves(origin_netuid, reserve, reserve.into()); - mock::setup_reserves(destination_netuid, reserve, reserve.into()); + mock::setup_reserves(origin_netuid, reserve.into(), reserve.into()); + mock::setup_reserves(destination_netuid, reserve.into(), reserve.into()); SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); let unstake_amount = SubtensorModule::stake_into_subnet( &hotkey, &coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1701,7 +1705,7 @@ fn test_swap_stake_limit_validate() { origin_netuid, destination_netuid, alpha_amount: unstake_amount, - limit_price, + limit_price: limit_price.into(), allow_partial: false, }); @@ -1749,8 +1753,8 @@ fn test_stake_transfers_disabled_validate() { &hotkey, &coldkey, origin_netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -1839,7 +1843,7 @@ fn test_move_stake_specific_stake_into_subnet_fail() { U64F64::from_num(161_986_254).saturating_div(U64F64::from_num(u64::MAX)); let existing_stake = AlphaCurrency::from(36_711_495_953); - let tao_in = 2_409_892_148_947; + let tao_in = TaoCurrency::from(2_409_892_148_947); let alpha_in = AlphaCurrency::from(15_358_708_513_716); let tao_staked = 200_000_000; @@ -1857,7 +1861,7 @@ fn test_move_stake_specific_stake_into_subnet_fail() { // Check we have zero staked assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); // Set a hotkey pool for the hotkey on destination subnet @@ -1883,14 +1887,14 @@ fn test_move_stake_specific_stake_into_subnet_fail() { // Setup Subnet pool for origin netuid SubnetAlphaIn::::insert(origin_netuid, alpha_in + 10_000_000.into()); - SubnetTAO::::insert(origin_netuid, tao_in + 10_000_000); + SubnetTAO::::insert(origin_netuid, tao_in + 10_000_000.into()); // Add stake as new hotkey assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, origin_netuid, - tao_staked, + tao_staked.into(), ),); let alpha_to_move = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey_account_id, @@ -1943,7 +1947,7 @@ fn test_transfer_stake_rate_limited() { let origin_coldkey = U256::from(1); let destination_coldkey = U256::from(2); let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); SubtensorModule::create_account_if_non_existent(&destination_coldkey, &hotkey); @@ -1951,8 +1955,8 @@ fn test_transfer_stake_rate_limited() { &hotkey, &origin_coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), true, ) .unwrap(); @@ -1987,7 +1991,7 @@ fn test_transfer_stake_doesnt_limit_destination_coldkey() { let origin_coldkey = U256::from(1); let destination_coldkey = U256::from(2); let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); SubtensorModule::create_account_if_non_existent(&destination_coldkey, &hotkey); @@ -1995,8 +1999,8 @@ fn test_transfer_stake_doesnt_limit_destination_coldkey() { &hotkey, &origin_coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -2033,15 +2037,15 @@ fn test_swap_stake_limits_destination_netuid() { let origin_coldkey = U256::from(1); let hotkey = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::create_account_if_non_existent(&origin_coldkey, &hotkey); SubtensorModule::stake_into_subnet( &hotkey, &origin_coldkey, netuid, - stake_amount, - ::SwapInterface::max_price(), + stake_amount.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); diff --git a/pallets/subtensor/src/tests/networks.rs b/pallets/subtensor/src/tests/networks.rs index aa6a65918c..4f0634c64c 100644 --- a/pallets/subtensor/src/tests/networks.rs +++ b/pallets/subtensor/src/tests/networks.rs @@ -3,6 +3,7 @@ use crate::*; use frame_support::assert_ok; use frame_system::Config; use sp_core::U256; +use subtensor_runtime_common::TaoCurrency; #[test] fn test_registration_ok() { @@ -285,12 +286,12 @@ fn test_registration_ok() { #[test] fn test_register_subnet_low_lock_cost() { new_test_ext(1).execute_with(|| { - NetworkMinLockCost::::set(1_000); - NetworkLastLockCost::::set(1_000); + NetworkMinLockCost::::set(TaoCurrency::from(1_000)); + NetworkLastLockCost::::set(TaoCurrency::from(1_000)); // Make sure lock cost is lower than 100 TAO let lock_cost = SubtensorModule::get_network_lock_cost(); - assert!(lock_cost < 100_000_000_000); + assert!(lock_cost < 100_000_000_000.into()); let subnet_owner_coldkey = U256::from(1); let subnet_owner_hotkey = U256::from(2); @@ -299,7 +300,10 @@ fn test_register_subnet_low_lock_cost() { // Ensure that both Subnet TAO and Subnet Alpha In equal to (actual) lock_cost assert_eq!(SubnetTAO::::get(netuid), lock_cost); - assert_eq!(SubnetAlphaIn::::get(netuid), lock_cost.into()); + assert_eq!( + SubnetAlphaIn::::get(netuid), + lock_cost.to_u64().into() + ); }) } @@ -307,13 +311,13 @@ fn test_register_subnet_low_lock_cost() { #[test] fn test_register_subnet_high_lock_cost() { new_test_ext(1).execute_with(|| { - let lock_cost: u64 = 1_000_000_000_000; + let lock_cost = TaoCurrency::from(1_000_000_000_000); NetworkMinLockCost::::set(lock_cost); NetworkLastLockCost::::set(lock_cost); // Make sure lock cost is higher than 100 TAO let lock_cost = SubtensorModule::get_network_lock_cost(); - assert!(lock_cost >= 1_000_000_000_000); + assert!(lock_cost >= 1_000_000_000_000.into()); let subnet_owner_coldkey = U256::from(1); let subnet_owner_hotkey = U256::from(2); @@ -322,7 +326,10 @@ fn test_register_subnet_high_lock_cost() { // Ensure that both Subnet TAO and Subnet Alpha In equal to 100 TAO assert_eq!(SubnetTAO::::get(netuid), lock_cost); - assert_eq!(SubnetAlphaIn::::get(netuid), lock_cost.into()); + assert_eq!( + SubnetAlphaIn::::get(netuid), + lock_cost.to_u64().into() + ); }) } diff --git a/pallets/subtensor/src/tests/recycle_alpha.rs b/pallets/subtensor/src/tests/recycle_alpha.rs index e2dd644fa7..c1b9b6039b 100644 --- a/pallets/subtensor/src/tests/recycle_alpha.rs +++ b/pallets/subtensor/src/tests/recycle_alpha.rs @@ -28,7 +28,7 @@ fn test_recycle_success() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; - increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid); // get initial total issuance and alpha out let initial_alpha = TotalHotkeyAlpha::::get(hotkey, netuid); @@ -84,11 +84,11 @@ fn test_recycle_two_stakers() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; - let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake); - increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake.into()); + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid); // add some stake to other coldkey on same hotkey. - increase_stake_on_coldkey_hotkey_account(&other_coldkey, &hotkey, stake, netuid); + increase_stake_on_coldkey_hotkey_account(&other_coldkey, &hotkey, stake.into(), netuid); // get initial total issuance and alpha out let initial_alpha = TotalHotkeyAlpha::::get(hotkey, netuid); @@ -154,12 +154,12 @@ fn test_recycle_staker_is_nominator() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; - let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake); - increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake.into()); + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid); // add some stake to other coldkey on same hotkey. // Note: this coldkey DOES NOT own the hotkey, so it is a nominator. - increase_stake_on_coldkey_hotkey_account(&other_coldkey, &hotkey, stake, netuid); + increase_stake_on_coldkey_hotkey_account(&other_coldkey, &hotkey, stake.into(), netuid); // Verify the ownership assert_ne!( SubtensorModule::get_owning_coldkey_for_hotkey(&hotkey), @@ -227,7 +227,7 @@ fn test_burn_success() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; - increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid); // get initial total issuance and alpha out let initial_alpha = TotalHotkeyAlpha::::get(hotkey, netuid); @@ -283,12 +283,12 @@ fn test_burn_staker_is_nominator() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; - let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake); - increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake.into()); + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid); // add some stake to other coldkey on same hotkey. // Note: this coldkey DOES NOT own the hotkey, so it is a nominator. - increase_stake_on_coldkey_hotkey_account(&other_coldkey, &hotkey, stake, netuid); + increase_stake_on_coldkey_hotkey_account(&other_coldkey, &hotkey, stake.into(), netuid); // get initial total issuance and alpha out let initial_alpha = TotalHotkeyAlpha::::get(hotkey, netuid); @@ -353,11 +353,11 @@ fn test_burn_two_stakers() { // add stake to coldkey-hotkey pair so we can recycle it let stake = 200_000; - let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake); - increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, stake.into()); + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake.into(), netuid); // add some stake to other coldkey on same hotkey. - increase_stake_on_coldkey_hotkey_account(&other_coldkey, &hotkey, stake, netuid); + increase_stake_on_coldkey_hotkey_account(&other_coldkey, &hotkey, stake.into(), netuid); // get initial total issuance and alpha out let initial_alpha = TotalHotkeyAlpha::::get(hotkey, netuid); @@ -421,7 +421,7 @@ fn test_recycle_errors() { register_ok_neuron(netuid, hotkey, coldkey, 0); let stake_amount = 200_000; - increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_amount, netuid); + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_amount.into(), netuid); assert_noop!( SubtensorModule::recycle_alpha( @@ -503,7 +503,7 @@ fn test_burn_errors() { register_ok_neuron(netuid, hotkey, coldkey, 0); let stake_amount = 200_000; - increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_amount, netuid); + increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_amount.into(), netuid); assert_noop!( SubtensorModule::burn_alpha( diff --git a/pallets/subtensor/src/tests/registration.rs b/pallets/subtensor/src/tests/registration.rs index 997a71268e..373a58de20 100644 --- a/pallets/subtensor/src/tests/registration.rs +++ b/pallets/subtensor/src/tests/registration.rs @@ -317,10 +317,10 @@ fn test_burned_registration_under_limit() { let who: ::AccountId = coldkey_account_id; let burn_cost = 1000; // Set the burn cost - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); add_network(netuid, 13, 0); // Add the network // Give it some TAO to the coldkey balance; more than the burn cost @@ -417,10 +417,10 @@ fn test_burned_registration_rate_allows_burn_adjustment() { let burn_cost = 1000; // Set the burn cost - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); add_network(netuid, 13, 0); // Add the network // Give it some TAO to the coldkey balance; more than the burn cost @@ -473,11 +473,11 @@ fn test_burned_registration_ok() { let burn_cost = 1000; let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); @@ -524,7 +524,7 @@ fn test_burn_registration_without_neuron_slot() { let burn_cost = 1000; let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); @@ -553,7 +553,7 @@ fn test_burn_registration_doesnt_write_on_failure() { // Add network and set burn cost add_network(netuid, tempo, 0); - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); // Give coldkey balance to pay for registration SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, initial_balance); // Set max allowed uids to 0 so registration will fail, but only on last check. @@ -590,7 +590,7 @@ fn test_burn_adjustment() { let adjustment_interval = 1; let target_registrations_per_interval = 1; add_network(netuid, tempo, 0); - SubtensorModule::set_burn(netuid, init_burn_cost); + SubtensorModule::set_burn(netuid, init_burn_cost.into()); SubtensorModule::set_adjustment_interval(netuid, adjustment_interval); SubtensorModule::set_adjustment_alpha(netuid, 58000); // Set to old value. SubtensorModule::set_target_registrations_per_interval( @@ -599,7 +599,7 @@ fn test_burn_adjustment() { ); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Register key 1. let hotkey_account_id_1 = U256::from(1); @@ -626,11 +626,11 @@ fn test_burn_adjustment() { step_block(1); // Check the adjusted burn is above the initial min burn. - assert!(SubtensorModule::get_burn_as_u64(netuid) > init_burn_cost); + assert!(SubtensorModule::get_burn(netuid) > init_burn_cost.into()); assert_abs_diff_eq!( - SubtensorModule::get_burn_as_u64(netuid), - init_burn_cost.saturating_mul(3).saturating_div(2), // 1.5x - epsilon = 1000 + SubtensorModule::get_burn(netuid), + (init_burn_cost.saturating_mul(3).saturating_div(2)).into(), // 1.5x + epsilon = 1000.into() ); }); } @@ -649,17 +649,17 @@ fn test_burn_registration_pruning_scenarios() { const NOT_IMMUNE: bool = false; // Initial setup - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); SubtensorModule::set_max_allowed_uids(netuid, max_allowed_uids); SubtensorModule::set_target_registrations_per_interval(netuid, max_allowed_uids); SubtensorModule::set_immunity_period(netuid, immunity_period); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); add_network(netuid, tempo, 0); - let mint_balance = burn_cost * u64::from(max_allowed_uids) + 1_000_000_000; + let mint_balance = burn_cost * max_allowed_uids as u64 + 1_000_000_000; SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, mint_balance); // Register first half of neurons @@ -1560,8 +1560,8 @@ fn test_burn_registration_increase_recycled_rao() { Balances::deposit_creating(&coldkey_account_id, Balance::from(1_000_000_000_000_u64)); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); - mock::setup_reserves(netuid2, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); + mock::setup_reserves(netuid2, reserve.into(), reserve.into()); add_network(netuid, 13, 0); assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 0); @@ -1571,7 +1571,7 @@ fn test_burn_registration_increase_recycled_rao() { run_to_block(1); - let burn_amount = SubtensorModule::get_burn_as_u64(netuid); + let burn_amount = SubtensorModule::get_burn(netuid); assert_ok!(SubtensorModule::burned_register( <::RuntimeOrigin>::signed(hotkey_account_id), netuid, @@ -1581,7 +1581,7 @@ fn test_burn_registration_increase_recycled_rao() { run_to_block(2); - let burn_amount2 = SubtensorModule::get_burn_as_u64(netuid2); + let burn_amount2 = SubtensorModule::get_burn(netuid2); assert_ok!(SubtensorModule::burned_register( <::RuntimeOrigin>::signed(hotkey_account_id), netuid2, @@ -1592,7 +1592,10 @@ fn test_burn_registration_increase_recycled_rao() { netuid2, U256::from(2) )); - assert_eq!(SubtensorModule::get_rao_recycled(netuid2), burn_amount2 * 2); + assert_eq!( + SubtensorModule::get_rao_recycled(netuid2), + burn_amount2 * 2.into() + ); // Validate netuid is not affected. assert_eq!(SubtensorModule::get_rao_recycled(netuid), burn_amount); }); diff --git a/pallets/subtensor/src/tests/senate.rs b/pallets/subtensor/src/tests/senate.rs index be1a90ded4..1b1f85e5ab 100644 --- a/pallets/subtensor/src/tests/senate.rs +++ b/pallets/subtensor/src/tests/senate.rs @@ -12,6 +12,7 @@ use sp_runtime::{ BuildStorage, traits::{BlakeTwo256, Hash}, }; +use subtensor_runtime_common::TaoCurrency; use subtensor_swap_interface::SwapHandler; use super::mock; @@ -67,16 +68,16 @@ fn test_senate_join_works() { let hotkey_account_id = U256::from(6); let burn_cost = 1000; let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har - let stake = DefaultMinStake::::get() * 100; + let stake = DefaultMinStake::::get() * 100.into(); //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); let reserve = 1_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Subscribe and check extrinsic output assert_ok!(SubtensorModule::burned_register( @@ -101,7 +102,7 @@ fn test_senate_join_works() { Delegates::::insert(hotkey_account_id, u16::MAX / 10); let staker_coldkey = U256::from(7); - SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake); + SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake.into()); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(staker_coldkey), @@ -116,12 +117,12 @@ fn test_senate_join_works() { &staker_coldkey, netuid ), - AlphaCurrency::from(stake), + AlphaCurrency::from(stake.to_u64()), epsilon = 1.into() ); assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey_account_id, netuid), - AlphaCurrency::from(stake), + AlphaCurrency::from(stake.to_u64()), epsilon = 1.into() ); @@ -146,13 +147,13 @@ fn test_senate_vote_works() { let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Subscribe and check extrinsic output assert_ok!(SubtensorModule::burned_register( @@ -177,8 +178,8 @@ fn test_senate_vote_works() { Delegates::::insert(hotkey_account_id, u16::MAX / 10); let staker_coldkey = U256::from(7); - let stake = DefaultMinStake::::get() * 10; - SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake); + let stake = DefaultMinStake::::get() * 10.into(); + SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake.into()); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(staker_coldkey), @@ -193,12 +194,12 @@ fn test_senate_vote_works() { &staker_coldkey, netuid ), - AlphaCurrency::from(stake), + AlphaCurrency::from(stake.to_u64()), epsilon = 1.into() ); assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey_account_id, netuid), - AlphaCurrency::from(stake), + AlphaCurrency::from(stake.to_u64()), epsilon = 1.into() ); @@ -262,13 +263,13 @@ fn test_senate_vote_not_member() { let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Subscribe and check extrinsic output assert_ok!(SubtensorModule::burned_register( @@ -323,16 +324,16 @@ fn test_senate_leave_works() { let hotkey_account_id = U256::from(6); let burn_cost = 1000; let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get() * 10.into(); //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); - let reserve = stake * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + let reserve = stake.to_u64() * 1000; + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Subscribe and check extrinsic output assert_ok!(SubtensorModule::burned_register( @@ -357,7 +358,7 @@ fn test_senate_leave_works() { Delegates::::insert(hotkey_account_id, u16::MAX / 10); let staker_coldkey = U256::from(7); - SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake); + SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake.into()); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(staker_coldkey), @@ -372,12 +373,12 @@ fn test_senate_leave_works() { &staker_coldkey, netuid ), - AlphaCurrency::from(stake), + AlphaCurrency::from(stake.to_u64()), epsilon = 1.into() ); assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey_account_id, netuid), - AlphaCurrency::from(stake), + AlphaCurrency::from(stake.to_u64()), epsilon = 1.into() ); @@ -401,17 +402,17 @@ fn test_senate_leave_vote_removal() { let burn_cost = 1000; let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har let coldkey_origin = <::RuntimeOrigin>::signed(coldkey_account_id); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get() * 10.into(); //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give it some $$$ in his coldkey balance - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, stake); + SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, stake.into()); SubtokenEnabled::::insert(netuid, true); - let reserve = stake * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + let reserve = stake.to_u64() * 1000; + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Subscribe and check extrinsic output assert_ok!(SubtensorModule::burned_register( @@ -422,7 +423,7 @@ fn test_senate_leave_vote_removal() { // Check if balance has decreased to pay for the burn. assert_eq!( SubtensorModule::get_coldkey_balance(&coldkey_account_id), - (stake - burn_cost) + (stake.to_u64() - burn_cost) ); // funds drained on reg. // Check if neuron has added to the specified network(netuid) assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 1); @@ -436,7 +437,7 @@ fn test_senate_leave_vote_removal() { Delegates::::insert(hotkey_account_id, u16::MAX / 10); let staker_coldkey = U256::from(7); - SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake); + SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake.into()); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(staker_coldkey), @@ -451,12 +452,12 @@ fn test_senate_leave_vote_removal() { &staker_coldkey, netuid ), - AlphaCurrency::from(stake), + AlphaCurrency::from(stake.to_u64()), epsilon = 1.into() ); assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey_account_id, netuid), - AlphaCurrency::from(stake), + AlphaCurrency::from(stake.to_u64()), epsilon = 1.into() ); @@ -489,15 +490,15 @@ fn test_senate_leave_vote_removal() { // Add two networks. let other_netuid = NetUid::from(5); add_network(other_netuid, 1, 0); - SubtensorModule::set_burn(other_netuid, 0); + SubtensorModule::set_burn(other_netuid, TaoCurrency::ZERO); SubtensorModule::set_max_registrations_per_block(other_netuid, 1000); SubtensorModule::set_target_registrations_per_interval(other_netuid, 1000); SubtensorModule::set_max_registrations_per_block(NetUid::ROOT, 1000); SubtensorModule::set_target_registrations_per_interval(NetUid::ROOT, 1000); let reserve = 1_000_000_000_000; - mock::setup_reserves(other_netuid, reserve, reserve.into()); - mock::setup_reserves(NetUid::ROOT, reserve, reserve.into()); + mock::setup_reserves(other_netuid, reserve.into(), reserve.into()); + mock::setup_reserves(NetUid::ROOT, reserve.into(), reserve.into()); SubtokenEnabled::::insert(NetUid::ROOT, true); SubtokenEnabled::::insert(other_netuid, true); @@ -517,7 +518,7 @@ fn test_senate_leave_vote_removal() { <::RuntimeOrigin>::signed(cold), hot, NetUid::ROOT, - 100_000_000 + (i as u64) + (100_000_000 + (i as u64)).into() )); // Register them on the root network. assert_ok!(SubtensorModule::root_register( @@ -556,13 +557,13 @@ fn test_senate_not_leave_when_stake_removed() { let coldkey_account_id = U256::from(667); // Neighbour of the beast, har har //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Subscribe and check extrinsic output assert_ok!(SubtensorModule::burned_register( @@ -587,8 +588,8 @@ fn test_senate_not_leave_when_stake_removed() { Delegates::::insert(hotkey_account_id, u16::MAX / 10); let staker_coldkey = U256::from(7); - let stake_amount: u64 = DefaultMinStake::::get() * 10; - SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake_amount); + let stake_amount = DefaultMinStake::::get() * 10.into(); + SubtensorModule::add_balance_to_coldkey_account(&staker_coldkey, stake_amount.into()); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(staker_coldkey), @@ -603,12 +604,12 @@ fn test_senate_not_leave_when_stake_removed() { &staker_coldkey, netuid ), - AlphaCurrency::from(stake_amount), + AlphaCurrency::from(stake_amount.to_u64()), epsilon = 1.into() ); assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_on_subnet(&hotkey_account_id, netuid), - AlphaCurrency::from(stake_amount), + AlphaCurrency::from(stake_amount.to_u64()), epsilon = 1.into() ); @@ -624,7 +625,7 @@ fn test_senate_not_leave_when_stake_removed() { <::RuntimeOrigin>::signed(staker_coldkey), hotkey_account_id, netuid, - (stake_amount - 1).into() + (stake_amount - 1.into()).to_u64().into() )); assert!(Senate::is_member(&hotkey_account_id)); }); @@ -643,13 +644,13 @@ fn test_senate_join_current_delegate() { let coldkey_account_id = U256::from(667); //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give some coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); let reserve = 1_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Subscribe and check extrinsic output assert_ok!(SubtensorModule::burned_register( @@ -721,23 +722,23 @@ fn test_adjust_senate_events() { let max_senate_size: u16 = SenateMaxMembers::get() as u16; let stake_threshold = { - let default_stake = DefaultMinStake::::get(); + let default_stake = DefaultMinStake::::get().to_u64(); let fee = ::SwapInterface::approx_fee_amount( - netuid.into(), - default_stake, + netuid, + default_stake.into(), ); default_stake + fee }; // We will be registering MaxMembers hotkeys and two more to try a replace - let balance_to_add = DefaultMinStake::::get() * 10 + let balance_to_add = DefaultMinStake::::get().to_u64() * 10 + 50_000 + (stake_threshold + burn_cost) * (max_senate_size + 2) as u64; let replacement_hotkey_account_id = U256::from(7); // Will be added to the senate to replace hotkey_account_id //add network - SubtensorModule::set_burn(netuid, burn_cost); + SubtensorModule::set_burn(netuid, burn_cost.into()); add_network(netuid, tempo, 0); // Give some coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, balance_to_add); @@ -751,7 +752,7 @@ fn test_adjust_senate_events() { SubtokenEnabled::::insert(NetUid::ROOT, true); let reserve = 100_000_000_000_000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Subscribe and check extrinsic output assert_ok!(SubtensorModule::burned_register( @@ -804,7 +805,7 @@ fn test_adjust_senate_events() { <::RuntimeOrigin>::signed(coldkey_account_id), new_hotkey_account_id, netuid, - stake_threshold + 1 + i as u64 // Increasing with i to make them ordered + (stake_threshold + 1 + i as u64).into() // Increasing with i to make them ordered )); // Join senate assert_ok!(SubtensorModule::root_register( @@ -838,10 +839,10 @@ fn test_adjust_senate_events() { // as they have no stake assert!(!Senate::is_member(&replacement_hotkey_account_id)); // Add/delegate enough stake to join the senate - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get() * 10.into(); let reserve = 100_000_000_000_000; - mock::setup_reserves(NetUid::ROOT, reserve, reserve.into()); + mock::setup_reserves(NetUid::ROOT, reserve.into(), reserve.into()); let (_, fee) = mock::swap_tao_to_alpha(NetUid::ROOT, stake); @@ -857,16 +858,16 @@ fn test_adjust_senate_events() { &coldkey_account_id, NetUid::ROOT ), - AlphaCurrency::from(stake - fee), - epsilon = (stake / 1000).into() + AlphaCurrency::from(stake.to_u64() - fee), + epsilon = (stake.to_u64() / 1000).into() ); assert_abs_diff_eq!( SubtensorModule::get_stake_for_hotkey_on_subnet( &replacement_hotkey_account_id, NetUid::ROOT ), - AlphaCurrency::from(stake - fee), - epsilon = (stake / 1000).into() + AlphaCurrency::from(stake.to_u64() - fee), + epsilon = (stake.to_u64() / 1000).into() ); System::reset_events(); diff --git a/pallets/subtensor/src/tests/staking.rs b/pallets/subtensor/src/tests/staking.rs index c7447b51cc..1ee540ff62 100644 --- a/pallets/subtensor/src/tests/staking.rs +++ b/pallets/subtensor/src/tests/staking.rs @@ -14,7 +14,7 @@ use safe_math::FixedExt; use sp_core::{Get, H256, U256}; use substrate_fixed::traits::FromFixed; use substrate_fixed::types::{I96F32, I110F18, U64F64, U96F32}; -use subtensor_runtime_common::{AlphaCurrency, Currency as CurrencyT, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, Currency as CurrencyT, NetUid, TaoCurrency}; use subtensor_swap_interface::{OrderType, SwapHandler}; use super::mock; @@ -29,7 +29,7 @@ use crate::*; fn test_add_stake_dispatch_info_ok() { new_test_ext(1).execute_with(|| { let hotkey = U256::from(0); - let amount_staked = 5000; + let amount_staked = TaoCurrency::from(5000); let netuid = NetUid::from(1); let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake { hotkey, @@ -52,12 +52,16 @@ fn test_add_stake_ok_no_emission() { new_test_ext(1).execute_with(|| { let hotkey_account_id = U256::from(533453); let coldkey_account_id = U256::from(55453); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get().to_u64() * 10; //add network let netuid = add_dynamic_network(&hotkey_account_id, &coldkey_account_id); - mock::setup_reserves(netuid, amount * 1_000_000, (amount * 10_000_000).into()); + mock::setup_reserves( + netuid, + (amount * 1_000_000).into(), + (amount * 10_000_000).into(), + ); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount); @@ -65,7 +69,7 @@ fn test_add_stake_ok_no_emission() { // Check we have zero staked before transfer assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); // Also total stake should be equal to the network initial lock @@ -75,12 +79,12 @@ fn test_add_stake_ok_no_emission() { ); // Transfer to hotkey account, and check if the result is ok - let (alpha_staked, fee) = mock::swap_tao_to_alpha(netuid, amount); + let (alpha_staked, fee) = mock::swap_tao_to_alpha(netuid, amount.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() )); let (tao_expected, _) = mock::swap_alpha_to_tao(netuid, alpha_staked); @@ -89,15 +93,15 @@ fn test_add_stake_ok_no_emission() { assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - tao_expected + approx_fee, // swap returns value after fee, so we need to compensate it - epsilon = 10000, + tao_expected + approx_fee.into(), // swap returns value after fee, so we need to compensate it + epsilon = 10000.into(), ); // Check if stake has increased assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - amount - fee, - epsilon = 10000 + (amount - fee).into(), + epsilon = 10000.into() ); // Check if balance has decreased @@ -106,7 +110,7 @@ fn test_add_stake_ok_no_emission() { // Check if total stake has increased accordingly. assert_eq!( SubtensorModule::get_total_stake(), - amount + SubtensorModule::get_network_min_lock() + SubtensorModule::get_network_min_lock() + amount.into() ); }); } @@ -142,8 +146,8 @@ fn test_dividends_with_run_to_block() { // Check if the initial stake has arrived assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&neuron_src_hotkey_id), - initial_stake, - epsilon = 2 + initial_stake.into(), + epsilon = 2.into() ); // Check if all three neurons are registered @@ -155,14 +159,14 @@ fn test_dividends_with_run_to_block() { // Check if the stake is equal to the inital stake + transfer assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&neuron_src_hotkey_id), - initial_stake, - epsilon = 2 + initial_stake.into(), + epsilon = 2.into() ); // Check if the stake is equal to the inital stake + transfer assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&neuron_dest_hotkey_id), - 0 + TaoCurrency::ZERO ); }); } @@ -175,7 +179,12 @@ fn test_add_stake_err_signature() { let netuid = NetUid::from(1); assert_err!( - SubtensorModule::add_stake(RawOrigin::None.into(), hotkey_account_id, netuid, amount), + SubtensorModule::add_stake( + RawOrigin::None.into(), + hotkey_account_id, + netuid, + amount.into() + ), DispatchError::BadOrigin ); }); @@ -189,15 +198,14 @@ fn test_add_stake_not_registered_key_pair() { let coldkey_account_id = U256::from(435445); let hotkey_account_id = U256::from(54544); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let amount = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount + fee); + let amount = DefaultMinStake::::get().to_u64() * 10; + SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount); assert_err!( SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() ), Error::::HotKeyAccountNotExists ); @@ -211,10 +219,10 @@ fn test_add_stake_ok_neuron_does_not_belong_to_coldkey() { let hotkey_id = U256::from(54544); let other_cold_key = U256::from(99498); let netuid = add_dynamic_network(&hotkey_id, &coldkey_id); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get() * 10.into(); // Give it some $$$ in his coldkey balance - SubtensorModule::add_balance_to_coldkey_account(&other_cold_key, stake); + SubtensorModule::add_balance_to_coldkey_account(&other_cold_key, stake.into()); // Perform the request which is signed by a different cold key assert_ok!(SubtensorModule::add_stake( @@ -231,11 +239,11 @@ fn test_add_stake_err_not_enough_belance() { new_test_ext(1).execute_with(|| { let coldkey_id = U256::from(544); let hotkey_id = U256::from(54544); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get() * 10.into(); let netuid = add_dynamic_network(&hotkey_id, &coldkey_id); // Lets try to stake with 0 balance in cold key account - assert!(SubtensorModule::get_coldkey_balance(&coldkey_id) < stake); + assert!(SubtensorModule::get_coldkey_balance(&coldkey_id) < stake.to_u64()); assert_err!( SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_id), @@ -264,33 +272,33 @@ fn test_add_stake_total_balance_no_change() { // Check we have zero staked before transfer let initial_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id); - assert_eq!(initial_stake, 0); + assert_eq!(initial_stake, TaoCurrency::ZERO); // Check total balance is equal to initial balance let initial_total_balance = Balances::total_balance(&coldkey_account_id); assert_eq!(initial_total_balance, initial_balance); // Also total stake should be zero - assert_eq!(SubtensorModule::get_total_stake(), 0); + assert_eq!(SubtensorModule::get_total_stake(), TaoCurrency::ZERO); // Stake to hotkey account, and check if the result is ok assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - 10000 + 10000.into() )); // Check if stake has increased let new_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id); - assert_eq!(new_stake, 10000); + assert_eq!(new_stake, 10000.into()); // Check if free balance has decreased let new_free_balance = SubtensorModule::get_coldkey_balance(&coldkey_account_id); assert_eq!(new_free_balance, 0); // Check if total stake has increased accordingly. - assert_eq!(SubtensorModule::get_total_stake(), 10000); + assert_eq!(SubtensorModule::get_total_stake(), 10000.into()); // Check if total balance has remained the same. (no fee, includes reserved/locked balance) let total_balance = Balances::total_balance(&coldkey_account_id); @@ -314,7 +322,7 @@ fn test_add_stake_total_issuance_no_change() { // Check we have zero staked before transfer let initial_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id); - assert_eq!(initial_stake, 0); + assert_eq!(initial_stake, TaoCurrency::ZERO); // Check total balance is equal to initial balance let initial_total_balance = Balances::total_balance(&coldkey_account_id); @@ -325,26 +333,26 @@ fn test_add_stake_total_issuance_no_change() { assert_eq!(initial_total_issuance, initial_balance); // Also total stake should be zero - assert_eq!(SubtensorModule::get_total_stake(), 0); + assert_eq!(SubtensorModule::get_total_stake(), TaoCurrency::ZERO); // Stake to hotkey account, and check if the result is ok assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - 10000 + 10000.into() )); // Check if stake has increased let new_stake = SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id); - assert_eq!(new_stake, 10000); + assert_eq!(new_stake, 10000.into()); // Check if free balance has decreased let new_free_balance = SubtensorModule::get_coldkey_balance(&coldkey_account_id); assert_eq!(new_free_balance, 0); // Check if total stake has increased accordingly. - assert_eq!(SubtensorModule::get_total_stake(), 10000); + assert_eq!(SubtensorModule::get_total_stake(), 10000.into()); // Check if total issuance has remained the same. (no fee, includes reserved/locked balance) let total_issuance = Balances::total_issuance(); @@ -383,7 +391,7 @@ fn test_remove_stake_ok_no_emission() { let subnet_owner_hotkey = U256::from(2); let coldkey_account_id = U256::from(4343); let hotkey_account_id = U256::from(4968585); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get() * 10.into(); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, 192213123); @@ -394,7 +402,7 @@ fn test_remove_stake_ok_no_emission() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 0); @@ -403,38 +411,41 @@ fn test_remove_stake_ok_no_emission() { &hotkey_account_id, &coldkey_account_id, netuid, - amount.into(), + amount.to_u64().into(), ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), amount, - epsilon = amount / 1000 + epsilon = amount / 1000.into() ); // Add subnet TAO for the equivalent amount added at price - let (amount_tao, fee) = mock::swap_alpha_to_tao(netuid, amount.into()); - SubnetTAO::::mutate(netuid, |v| *v += amount_tao + fee); - TotalStake::::mutate(|v| *v += amount_tao + fee); + let (amount_tao, fee) = mock::swap_alpha_to_tao(netuid, amount.to_u64().into()); + SubnetTAO::::mutate(netuid, |v| *v += amount_tao + fee.into()); + TotalStake::::mutate(|v| *v += amount_tao + fee.into()); // Do the magic assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount.into() + amount.to_u64().into() )); // we do not expect the exact amount due to slippage - assert!(SubtensorModule::get_coldkey_balance(&coldkey_account_id) > amount / 10 * 9 - fee); + assert!( + SubtensorModule::get_coldkey_balance(&coldkey_account_id) + > amount.to_u64() / 10 * 9 - fee + ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0, - epsilon = 20000 + TaoCurrency::ZERO, + epsilon = 20000.into() ); assert_abs_diff_eq!( SubtensorModule::get_total_stake(), - SubtensorModule::get_network_min_lock() + fee, - epsilon = SubtensorModule::get_total_stake() / 100_000 + SubtensorModule::get_network_min_lock() + fee.into(), + epsilon = SubtensorModule::get_total_stake() / 100_000.into() ); }); } @@ -457,7 +468,7 @@ fn test_remove_stake_amount_too_low() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 0); @@ -493,7 +504,7 @@ fn test_remove_stake_below_min_stake() { register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, 192213123); let min_stake = DefaultMinStake::::get(); - let amount = AlphaCurrency::from(min_stake / 2); + let amount = AlphaCurrency::from(min_stake.to_u64() / 2); // Some basic assertions assert_eq!( @@ -502,7 +513,7 @@ fn test_remove_stake_below_min_stake() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 0); @@ -555,17 +566,18 @@ fn test_add_stake_partial_below_min_stake_fails() { // Stake TAO amount is above min stake let min_stake = DefaultMinStake::::get(); - let amount = min_stake * 2; + let amount = min_stake.to_u64() * 2; SubtensorModule::add_balance_to_coldkey_account( &coldkey_account_id, amount + ExistentialDeposit::get(), ); // Setup reserves so that price is 1.0 and init swap - mock::setup_reserves(netuid, amount * 10, (amount * 10).into()); + mock::setup_reserves(netuid, (amount * 10).into(), (amount * 10).into()); // Force the swap to initialize - SubtensorModule::swap_tao_for_alpha(netuid, 0, 1_000_000_000_000).unwrap(); + SubtensorModule::swap_tao_for_alpha(netuid, TaoCurrency::ZERO, 1_000_000_000_000.into()) + .unwrap(); // Get the current price (should be 1.0) let current_price = @@ -581,8 +593,8 @@ fn test_add_stake_partial_below_min_stake_fails() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount, - limit_price, + amount.into(), + limit_price.into(), true ), Error::::AmountTooLow @@ -619,7 +631,7 @@ fn test_remove_stake_ok_hotkey_does_not_belong_to_coldkey() { let coldkey_id = U256::from(544); let hotkey_id = U256::from(54544); let other_cold_key = U256::from(99498); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get().to_u64() * 10; let netuid = add_dynamic_network(&hotkey_id, &coldkey_id); // Give the neuron some stake to remove @@ -644,10 +656,13 @@ fn test_remove_stake_no_enough_stake() { new_test_ext(1).execute_with(|| { let coldkey_id = U256::from(544); let hotkey_id = U256::from(54544); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get().to_u64() * 10; let netuid = add_dynamic_network(&hotkey_id, &coldkey_id); - assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), 0); + assert_eq!( + SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), + TaoCurrency::ZERO + ); assert_err!( SubtensorModule::remove_stake( @@ -672,7 +687,7 @@ fn test_remove_stake_total_balance_no_change() { let subnet_owner_hotkey = U256::from(2); let hotkey_account_id = U256::from(571337); let coldkey_account_id = U256::from(71337); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get().to_u64() * 10; let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, 192213123); @@ -683,7 +698,7 @@ fn test_remove_stake_total_balance_no_change() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 0); let initial_total_balance = Balances::total_balance(&coldkey_account_id); @@ -700,8 +715,10 @@ fn test_remove_stake_total_balance_no_change() { // Add subnet TAO for the equivalent amount added at price let amount_tao = U96F32::saturating_from_num(amount) * ::SwapInterface::current_alpha_price(netuid.into()); - SubnetTAO::::mutate(netuid, |v| *v += amount_tao.saturating_to_num::()); - TotalStake::::mutate(|v| *v += amount_tao.saturating_to_num::()); + SubnetTAO::::mutate(netuid, |v| { + *v += amount_tao.saturating_to_num::().into() + }); + TotalStake::::mutate(|v| *v += amount_tao.saturating_to_num::().into()); // Do the magic assert_ok!(SubtensorModule::remove_stake( @@ -719,12 +736,12 @@ fn test_remove_stake_total_balance_no_change() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); assert_abs_diff_eq!( SubtensorModule::get_total_stake(), - SubtensorModule::get_network_min_lock() + fee, - epsilon = SubtensorModule::get_total_stake() / 10_000_000 + SubtensorModule::get_network_min_lock() + fee.into(), + epsilon = SubtensorModule::get_total_stake() / 10_000_000.into() ); // Check total balance is equal to the added stake. Even after remove stake (no fee, includes reserved/locked balance) @@ -740,7 +757,7 @@ fn test_add_stake_insufficient_liquidity() { let subnet_owner_hotkey = U256::from(1002); let hotkey = U256::from(2); let coldkey = U256::from(3); - let amount_staked = DefaultMinStake::::get() * 10; + let amount_staked = DefaultMinStake::::get().to_u64() * 10; let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); @@ -748,7 +765,7 @@ fn test_add_stake_insufficient_liquidity() { // Set the liquidity at lowest possible value so that all staking requests fail let reserve = u64::from(mock::SwapMinimumReserve::get()) - 1; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Check the error assert_noop!( @@ -756,7 +773,7 @@ fn test_add_stake_insufficient_liquidity() { RuntimeOrigin::signed(coldkey), hotkey, netuid, - amount_staked + amount_staked.into() ), Error::::InsufficientLiquidity ); @@ -770,7 +787,7 @@ fn test_remove_stake_insufficient_liquidity() { let subnet_owner_hotkey = U256::from(1002); let hotkey = U256::from(2); let coldkey = U256::from(3); - let amount_staked = DefaultMinStake::::get() * 10; + let amount_staked = DefaultMinStake::::get().to_u64() * 10; let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); @@ -778,21 +795,21 @@ fn test_remove_stake_insufficient_liquidity() { // Simulate stake for hotkey let reserve = u64::MAX / 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); let alpha = SubtensorModule::stake_into_subnet( &hotkey, &coldkey, netuid, - amount_staked, - ::SwapInterface::max_price(), + amount_staked.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); // Set the liquidity at lowest possible value so that all staking requests fail let reserve = u64::from(mock::SwapMinimumReserve::get()) - 1; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Check the error assert_noop!( @@ -801,7 +818,7 @@ fn test_remove_stake_insufficient_liquidity() { ); // Mock provided liquidity - remove becomes successful - SubnetTaoProvided::::insert(netuid, amount_staked + 1); + SubnetTaoProvided::::insert(netuid, TaoCurrency::from(amount_staked + 1)); SubnetAlphaInProvided::::insert(netuid, AlphaCurrency::from(1)); assert_ok!(SubtensorModule::remove_stake( RuntimeOrigin::signed(coldkey), @@ -822,14 +839,14 @@ fn test_remove_stake_total_issuance_no_change() { let subnet_owner_hotkey = U256::from(2); let hotkey_account_id = U256::from(581337); let coldkey_account_id = U256::from(81337); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get().to_u64() * 10; let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); register_ok_neuron(netuid, hotkey_account_id, coldkey_account_id, 192213123); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount); - mock::setup_reserves(netuid, amount * 100, (amount * 100).into()); + mock::setup_reserves(netuid, (amount * 100).into(), (amount * 100).into()); // Some basic assertions assert_eq!( @@ -838,7 +855,7 @@ fn test_remove_stake_total_issuance_no_change() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); assert_eq!( SubtensorModule::get_coldkey_balance(&coldkey_account_id), @@ -849,12 +866,12 @@ fn test_remove_stake_total_issuance_no_change() { let inital_total_issuance = Balances::total_issuance(); // Stake to hotkey account, and check if the result is ok - let (_, fee) = mock::swap_tao_to_alpha(netuid, amount); + let (_, fee) = mock::swap_tao_to_alpha(netuid, amount.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() )); let total_issuance_after_stake = Balances::total_issuance(); @@ -886,12 +903,12 @@ fn test_remove_stake_total_issuance_no_change() { ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); assert_abs_diff_eq!( SubtensorModule::get_total_stake(), - total_fee + SubtensorModule::get_network_min_lock(), - epsilon = fee / 1000 + SubtensorModule::get_network_min_lock() + total_fee.into(), + epsilon = TaoCurrency::from(fee) / 1000.into() ); // Check if total issuance is equal to the added stake, even after remove stake (no fee, @@ -919,7 +936,11 @@ fn test_remove_prev_epoch_stake() { // Test case: (amount_to_stake, AlphaDividendsPerSubnet, TotalHotkeyAlphaLastEpoch, expected_fee) [ // No previous epoch stake and low hotkey stake - (DefaultMinStake::::get() * 10, 0_u64, 1000_u64), + ( + DefaultMinStake::::get().to_u64() * 10, + 0_u64, + 1000_u64, + ), // Same, but larger amount to stake - we get 0.005% for unstake (1_000_000_000, 0_u64, 1000_u64), (100_000_000_000, 0_u64, 1000_u64), @@ -948,15 +969,19 @@ fn test_remove_prev_epoch_stake() { AlphaDividendsPerSubnet::::insert(netuid, hotkey_account_id, alpha_divs); TotalHotkeyAlphaLastEpoch::::insert(hotkey_account_id, netuid, hotkey_alpha); let balance_before = SubtensorModule::get_coldkey_balance(&coldkey_account_id); - mock::setup_reserves(netuid, amount_to_stake * 10, (amount_to_stake * 10).into()); + mock::setup_reserves( + netuid, + (amount_to_stake * 10).into(), + (amount_to_stake * 10).into(), + ); // Stake to hotkey account, and check if the result is ok - let (_, fee) = mock::swap_tao_to_alpha(netuid, amount); + let (_, fee) = mock::swap_tao_to_alpha(netuid, amount.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() )); // Remove all stake @@ -1016,7 +1041,7 @@ fn test_staking_sets_div_variables() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() )); // Verify that divident variables are still clear in the beginning @@ -1110,8 +1135,8 @@ fn test_add_stake_to_hotkey_account_ok() { // The stake that is now in the account, should equal the amount assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - amount, - epsilon = 2 + amount.into(), + epsilon = 2.into() ); }); } @@ -1141,8 +1166,8 @@ fn test_remove_stake_from_hotkey_account() { // Prelimiary checks assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - amount, - epsilon = 10 + amount.into(), + epsilon = 10.into() ); // Remove stake @@ -1154,7 +1179,10 @@ fn test_remove_stake_from_hotkey_account() { ); // The stake on the hotkey account should be 0 - assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), 0); + assert_eq!( + SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), + TaoCurrency::ZERO + ); }); } @@ -1221,8 +1249,8 @@ fn test_remove_stake_from_hotkey_account_registered_in_various_networks() { #[test] fn test_increase_total_stake_ok() { new_test_ext(1).execute_with(|| { - let increment = 10000; - assert_eq!(SubtensorModule::get_total_stake(), 0); + let increment = TaoCurrency::from(10000); + assert_eq!(SubtensorModule::get_total_stake(), TaoCurrency::ZERO); SubtensorModule::increase_total_stake(increment); assert_eq!(SubtensorModule::get_total_stake(), increment); }); @@ -1234,8 +1262,8 @@ fn test_increase_total_stake_ok() { #[test] fn test_decrease_total_stake_ok() { new_test_ext(1).execute_with(|| { - let initial_total_stake = 10000; - let decrement = 5000; + let initial_total_stake = TaoCurrency::from(10000); + let decrement = TaoCurrency::from(5000); SubtensorModule::increase_total_stake(initial_total_stake); SubtensorModule::decrease_total_stake(decrement); @@ -1364,8 +1392,8 @@ fn test_has_enough_stake_yes() { assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - intial_amount, - epsilon = 2 + intial_amount.into(), + epsilon = 2.into() ); assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -1400,8 +1428,8 @@ fn test_has_enough_stake_no() { assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - intial_amount, - epsilon = 2 + intial_amount.into(), + epsilon = 2.into() ); assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -1433,7 +1461,7 @@ fn test_has_enough_stake_no_for_zero() { assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_id), - intial_amount + intial_amount.into() ); assert_eq!( SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -1476,7 +1504,7 @@ fn test_non_existent_account() { // No subnets => no iteration => zero total stake assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&(U256::from(0))), - 0 + TaoCurrency::ZERO ); }); } @@ -1538,8 +1566,8 @@ fn test_clear_small_nominations() { let cold1 = U256::from(3); let cold2 = U256::from(4); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let amount = DefaultMinStake::::get() * 10; - let fee: u64 = DefaultMinStake::::get(); + let amount = DefaultMinStake::::get().to_u64() * 10; + let fee = DefaultMinStake::::get().to_u64(); let init_balance = amount + fee + ExistentialDeposit::get(); // Register hot1. @@ -1558,7 +1586,7 @@ fn test_clear_small_nominations() { RuntimeOrigin::signed(cold1), hot1, netuid, - amount + amount.into() )); let alpha_stake1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold1, netuid); @@ -1582,7 +1610,7 @@ fn test_clear_small_nominations() { RuntimeOrigin::signed(cold2), hot1, netuid, - amount + amount.into() )); let alpha_stake2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hot1, &cold2, netuid); @@ -2037,7 +2065,7 @@ fn test_get_total_delegated_stake_after_unstaking() { let delegate_coldkey = U256::from(1); let delegate_hotkey = U256::from(2); let delegator = U256::from(3); - let initial_stake = DefaultMinStake::::get() * 10; + let initial_stake = DefaultMinStake::::get().to_u64() * 10; let existential_deposit = ExistentialDeposit::get(); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); @@ -2047,24 +2075,24 @@ fn test_get_total_delegated_stake_after_unstaking() { SubtensorModule::add_balance_to_coldkey_account(&delegator, initial_stake); // Delegate stake - let (_, fee) = mock::swap_tao_to_alpha(netuid, initial_stake); + let (_, fee) = mock::swap_tao_to_alpha(netuid, initial_stake.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(delegator), delegate_hotkey, netuid, - initial_stake + initial_stake.into() )); // Check initial delegated stake assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&delegator), - initial_stake - existential_deposit - fee, - epsilon = initial_stake / 100, + (initial_stake - existential_deposit - fee).into(), + epsilon = TaoCurrency::from(initial_stake / 100), ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&delegate_hotkey), - initial_stake - existential_deposit - fee, - epsilon = initial_stake / 100, + (initial_stake - existential_deposit - fee).into(), + epsilon = TaoCurrency::from(initial_stake / 100), ); let delegated_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &delegate_hotkey, @@ -2102,13 +2130,13 @@ fn test_get_total_delegated_stake_after_unstaking() { // Check the total delegated stake after unstaking assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&delegator), - expected_delegated_stake, - epsilon = expected_delegated_stake / 1000, + expected_delegated_stake.into(), + epsilon = TaoCurrency::from(expected_delegated_stake / 1000), ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&delegate_hotkey), - expected_delegated_stake, - epsilon = expected_delegated_stake / 1000, + expected_delegated_stake.into(), + epsilon = TaoCurrency::from(expected_delegated_stake / 1000), ); }); } @@ -2124,7 +2152,10 @@ fn test_get_total_delegated_stake_no_delegations() { register_ok_neuron(netuid, delegate, coldkey, 0); // Check that there's no delegated stake - assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&delegate), 0); + assert_eq!( + SubtensorModule::get_total_stake_for_coldkey(&delegate), + TaoCurrency::ZERO + ); }); } @@ -2136,7 +2167,7 @@ fn test_get_total_delegated_stake_single_delegator() { let delegate_coldkey = U256::from(1); let delegate_hotkey = U256::from(2); let delegator = U256::from(3); - let stake_amount = DefaultMinStake::::get() * 10 - 1; + let stake_amount = DefaultMinStake::::get().to_u64() * 10 - 1; let existential_deposit = ExistentialDeposit::get(); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); @@ -2145,13 +2176,13 @@ fn test_get_total_delegated_stake_single_delegator() { // Add stake from delegator SubtensorModule::add_balance_to_coldkey_account(&delegator, stake_amount); - let (_, fee) = mock::swap_tao_to_alpha(netuid, stake_amount); + let (_, fee) = mock::swap_tao_to_alpha(netuid, stake_amount.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(delegator), delegate_hotkey, netuid, - stake_amount + stake_amount.into() )); // Debug prints @@ -2176,13 +2207,13 @@ fn test_get_total_delegated_stake_single_delegator() { assert_abs_diff_eq!( actual_delegated_stake, - expected_delegated_stake, - epsilon = expected_delegated_stake / 100, + expected_delegated_stake.into(), + epsilon = TaoCurrency::from(expected_delegated_stake / 100), ); assert_abs_diff_eq!( actual_delegator_stake, - expected_delegated_stake, - epsilon = expected_delegated_stake / 100, + expected_delegated_stake.into(), + epsilon = TaoCurrency::from(expected_delegated_stake / 100), ); }); } @@ -2197,15 +2228,18 @@ fn test_get_alpha_share_stake_multiple_delegators() { let coldkey1 = U256::from(3); let coldkey2 = U256::from(4); let existential_deposit = 2; - let stake1 = DefaultMinStake::::get() * 10; - let stake2 = DefaultMinStake::::get() * 10 - 1; + let stake1 = DefaultMinStake::::get() * 10.into(); + let stake2 = DefaultMinStake::::get() * 10.into() - 1.into(); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); register_ok_neuron(netuid, hotkey1, coldkey1, 0); register_ok_neuron(netuid, hotkey2, coldkey2, 0); // Add stake from delegator1 - SubtensorModule::add_balance_to_coldkey_account(&coldkey1, stake1 + existential_deposit); + SubtensorModule::add_balance_to_coldkey_account( + &coldkey1, + stake1.to_u64() + existential_deposit, + ); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey1), hotkey1, @@ -2214,7 +2248,10 @@ fn test_get_alpha_share_stake_multiple_delegators() { )); // Add stake from delegator2 - SubtensorModule::add_balance_to_coldkey_account(&coldkey2, stake2 + existential_deposit); + SubtensorModule::add_balance_to_coldkey_account( + &coldkey2, + stake2.to_u64() + existential_deposit, + ); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey2), hotkey2, @@ -2249,8 +2286,8 @@ fn test_get_total_delegated_stake_exclude_owner_stake() { let delegate_coldkey = U256::from(1); let delegate_hotkey = U256::from(2); let delegator = U256::from(3); - let owner_stake = DefaultMinStake::::get() * 10; - let delegator_stake = DefaultMinStake::::get() * 10 - 1; + let owner_stake = DefaultMinStake::::get().to_u64() * 10; + let delegator_stake = DefaultMinStake::::get().to_u64() * 10 - 1; let netuid = add_dynamic_network(&delegate_hotkey, &delegate_coldkey); @@ -2260,17 +2297,17 @@ fn test_get_total_delegated_stake_exclude_owner_stake() { RuntimeOrigin::signed(delegate_coldkey), delegate_hotkey, netuid, - owner_stake + owner_stake.into() )); // Add delegator stake SubtensorModule::add_balance_to_coldkey_account(&delegator, delegator_stake); - let (_, fee) = mock::swap_tao_to_alpha(netuid, delegator_stake); + let (_, fee) = mock::swap_tao_to_alpha(netuid, delegator_stake.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(delegator), delegate_hotkey, netuid, - delegator_stake + delegator_stake.into() )); // Check the total delegated stake (should exclude owner's stake) @@ -2280,8 +2317,8 @@ fn test_get_total_delegated_stake_exclude_owner_stake() { assert_abs_diff_eq!( actual_delegated_stake, - expected_delegated_stake, - epsilon = expected_delegated_stake / 100 + expected_delegated_stake.into(), + epsilon = TaoCurrency::from(expected_delegated_stake / 100) ); }); } @@ -2332,13 +2369,13 @@ fn test_mining_emission_distribution_validator_valiminer_miner() { RuntimeOrigin::signed(validator_coldkey), validator_hotkey, netuid, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(validator_miner_coldkey), validator_miner_hotkey, netuid, - stake + stake.into() )); // Setup YUMA so that it creates emissions @@ -2375,9 +2412,21 @@ fn test_mining_emission_distribution_validator_valiminer_miner() { SubtensorModule::get_total_stake_for_coldkey(&miner_coldkey) - miner_stake_before; let total_emission = validator_emission + valiminer_emission + miner_emission; - assert_abs_diff_eq!(validator_emission, total_emission / 4, epsilon = 10); - assert_abs_diff_eq!(valiminer_emission, total_emission / 2, epsilon = 10); - assert_abs_diff_eq!(miner_emission, total_emission / 4, epsilon = 10); + assert_abs_diff_eq!( + validator_emission, + total_emission / 4.into(), + epsilon = 10.into() + ); + assert_abs_diff_eq!( + valiminer_emission, + total_emission / 2.into(), + epsilon = 10.into() + ); + assert_abs_diff_eq!( + miner_emission, + total_emission / 4.into(), + epsilon = 10.into() + ); }); } @@ -2401,7 +2450,7 @@ fn test_staking_too_little_fails() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - 1 + 1.into() ), Error::::AmountTooLow ); @@ -2418,15 +2467,14 @@ fn test_add_stake_fee_goes_to_subnet_tao() { let hotkey = U256::from(2); let coldkey = U256::from(3); let existential_deposit = ExistentialDeposit::get(); - let tao_to_stake = DefaultMinStake::::get() * 10; - let fee: u64 = 0; // FIXME: DefaultStakingFee is deprecated + let tao_to_stake = DefaultMinStake::::get() * 10.into(); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); let subnet_tao_before = SubnetTAO::::get(netuid); // Add stake - SubtensorModule::add_balance_to_coldkey_account(&coldkey, tao_to_stake); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, tao_to_stake.to_u64()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -2435,7 +2483,7 @@ fn test_add_stake_fee_goes_to_subnet_tao() { )); // Calculate expected stake - let expected_alpha = AlphaCurrency::from(tao_to_stake - existential_deposit - fee); + let expected_alpha = AlphaCurrency::from(tao_to_stake.to_u64() - existential_deposit); let actual_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid); let subnet_tao_after = SubnetTAO::::get(netuid); @@ -2451,7 +2499,7 @@ fn test_add_stake_fee_goes_to_subnet_tao() { assert_abs_diff_eq!( subnet_tao_before + tao_to_stake, subnet_tao_after, - epsilon = 10 + epsilon = 10.into() ); }); } @@ -2465,14 +2513,14 @@ fn test_remove_stake_fee_goes_to_subnet_tao() { let subnet_owner_hotkey = U256::from(1002); let hotkey = U256::from(2); let coldkey = U256::from(3); - let tao_to_stake = DefaultMinStake::::get() * 10; + let tao_to_stake = DefaultMinStake::::get() * 10.into(); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); let subnet_tao_before = SubnetTAO::::get(netuid); // Add stake - SubtensorModule::add_balance_to_coldkey_account(&coldkey, tao_to_stake); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, tao_to_stake.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey), hotkey, @@ -2495,12 +2543,16 @@ fn test_remove_stake_fee_goes_to_subnet_tao() { assert_abs_diff_eq!( subnet_tao_before, subnet_tao_after, - epsilon = alpha_to_unstake.to_u64() / 1000 + epsilon = (alpha_to_unstake.to_u64() / 1000).into() ); // User balance should decrease by 2x fee as a result of staking + unstaking let balance_after = SubtensorModule::get_coldkey_balance(&coldkey); - assert_abs_diff_eq!(balance_after, tao_to_stake, epsilon = tao_to_stake / 1000); + assert_abs_diff_eq!( + balance_after, + tao_to_stake.to_u64(), + epsilon = tao_to_stake.to_u64() / 1000 + ); }); } @@ -2527,7 +2579,7 @@ fn test_remove_stake_fee_realistic_values() { // This makes fee be equal ~0.0028 Alpha ~= 84000 rao let tao_reserve = 3_896_056_559_708_u64; let alpha_in = 128_011_331_299_964_u64; - mock::setup_reserves(netuid, tao_reserve, alpha_in.into()); + mock::setup_reserves(netuid, tao_reserve.into(), alpha_in.into()); AlphaDividendsPerSubnet::::insert(netuid, hotkey, alpha_divs); TotalHotkeyAlphaLastEpoch::::insert(hotkey, netuid, alpha_to_unstake); @@ -2554,7 +2606,7 @@ fn test_remove_stake_fee_realistic_values() { let balance_after = SubtensorModule::get_coldkey_balance(&coldkey); // FIXME since fee is calculated by SwapInterface and the values here are after fees, the // actual_fee is 0. but it's left here to discuss in review - let actual_fee = expected_tao - (balance_after - balance_before); + let actual_fee = expected_tao.to_u64() - (balance_after - balance_before); log::info!("Actual fee: {:?}", actual_fee); assert_abs_diff_eq!(actual_fee, expected_fee, epsilon = expected_fee / 1000); @@ -2571,9 +2623,11 @@ fn test_stake_below_min_validate() { let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); let amount_staked = { let defaulte_stake = DefaultMinStake::::get(); - let fee = - ::SwapInterface::approx_fee_amount(netuid.into(), defaulte_stake); - let min_valid_stake = defaulte_stake + fee; + let fee = ::SwapInterface::approx_fee_amount( + netuid.into(), + defaulte_stake.into(), + ); + let min_valid_stake = defaulte_stake.to_u64() + fee; min_valid_stake - 1 }; @@ -2585,7 +2639,7 @@ fn test_stake_below_min_validate() { let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake { hotkey, netuid, - amount_staked, + amount_staked: amount_staked.into(), }); let info: DispatchInfo = @@ -2611,7 +2665,7 @@ fn test_stake_below_min_validate() { // Increase the stake to be equal to the minimum, but leave the balance low let amount_staked = { - let min_stake = DefaultMinStake::::get(); + let min_stake = DefaultMinStake::::get().to_u64(); let fee = ::SwapInterface::approx_fee_amount(netuid.into(), min_stake); min_stake + fee * 2 @@ -2619,7 +2673,7 @@ fn test_stake_below_min_validate() { let call_2 = RuntimeCall::SubtensorModule(SubtensorCall::add_stake { hotkey, netuid, - amount_staked, + amount_staked: amount_staked.into(), }); // Submit to the signed extension validate function @@ -2667,7 +2721,7 @@ fn test_stake_below_min_can_unstake() { let coldkey = U256::from(3); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); let amount_staked = { - let defaulte_stake = DefaultMinStake::::get(); + let defaulte_stake = DefaultMinStake::::get().to_u64(); let fee = ::SwapInterface::approx_fee_amount(netuid.into(), defaulte_stake); let min_valid_stake = defaulte_stake + fee; @@ -2682,7 +2736,7 @@ fn test_stake_below_min_can_unstake() { let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake { hotkey, netuid, - amount_staked, + amount_staked: amount_staked.into(), }); let info: DispatchInfo = @@ -2708,7 +2762,7 @@ fn test_stake_below_min_can_unstake() { // Increase the stake to be equal to the minimum, but leave the balance low let amount_staked = { - let min_stake = DefaultMinStake::::get(); + let min_stake = DefaultMinStake::::get().to_u64(); let fee = ::SwapInterface::approx_fee_amount(netuid.into(), min_stake); min_stake + fee * 2 @@ -2716,7 +2770,7 @@ fn test_stake_below_min_can_unstake() { let call_2 = RuntimeCall::SubtensorModule(SubtensorCall::add_stake { hotkey, netuid, - amount_staked, + amount_staked: amount_staked.into(), }); // Submit to the signed extension validate function @@ -2770,7 +2824,7 @@ fn test_add_stake_limit_validate() { let netuid = add_dynamic_network(&hotkey, &coldkey); // Force-set alpha in and tao reserve to make price equal 1.5 - let tao_reserve = 150_000_000_000_u64; + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); @@ -2789,8 +2843,8 @@ fn test_add_stake_limit_validate() { let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake_limit { hotkey, netuid, - amount_staked: amount, - limit_price, + amount_staked: amount.into(), + limit_price: limit_price.into(), allow_partial: false, }); @@ -2841,7 +2895,7 @@ fn test_remove_stake_limit_validate() { ); // Forse-set alpha in and tao reserve to make price equal 1.5 - let tao_reserve = 150_000_000_000_u64; + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); @@ -2857,7 +2911,7 @@ fn test_remove_stake_limit_validate() { hotkey, netuid, amount_unstaked: unstake_amount, - limit_price, + limit_price: limit_price.into(), allow_partial: false, }); @@ -2899,15 +2953,15 @@ fn test_stake_overflow() { SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount); // Setup liquidity with 21M TAO values - mock::setup_reserves(netuid, amount, amount.into()); + mock::setup_reserves(netuid, amount.into(), amount.into()); // Stake and check if the result is ok - let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, amount); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, amount.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() )); // Check if stake has increased properly @@ -2919,8 +2973,8 @@ fn test_stake_overflow() { // Check if total stake has increased accordingly. assert_abs_diff_eq!( SubtensorModule::get_total_stake(), - amount + SubtensorModule::get_network_min_lock(), - epsilon = 1 + SubtensorModule::get_network_min_lock() + amount.into(), + epsilon = 1.into() ); }); } @@ -2935,7 +2989,7 @@ fn test_stake_low_liquidity_validate() { let subnet_owner_hotkey = U256::from(1002); let hotkey = U256::from(2); let coldkey = U256::from(3); - let amount_staked = DefaultMinStake::::get() * 10; + let amount_staked = DefaultMinStake::::get().to_u64() * 10; let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); @@ -2944,13 +2998,13 @@ fn test_stake_low_liquidity_validate() { // Set the liquidity at lowest possible value so that all staking requests fail let reserve = u64::from(mock::SwapMinimumReserve::get()) - 1; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Add stake call let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake { hotkey, netuid, - amount_staked, + amount_staked: amount_staked.into(), }); let info = DispatchInfoOf::<::RuntimeCall>::default(); @@ -2985,29 +3039,29 @@ fn test_unstake_low_liquidity_validate() { let subnet_owner_hotkey = U256::from(1002); let hotkey = U256::from(2); let coldkey = U256::from(3); - let amount_staked = DefaultMinStake::::get() * 10; // FIXME: DefaultStakingFee is deprecated + let amount_staked = DefaultMinStake::::get() * 10.into(); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount_staked); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount_staked.into()); // Simulate stake for hotkey let reserve = u64::MAX / 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); let alpha = SubtensorModule::stake_into_subnet( &hotkey, &coldkey, netuid, amount_staked, - ::SwapInterface::max_price(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); // Set the liquidity at lowest possible value so that all staking requests fail let reserve = u64::from(mock::SwapMinimumReserve::get()) - 1; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Remove stake call let call = RuntimeCall::SubtensorModule(SubtensorCall::remove_stake { @@ -3048,28 +3102,28 @@ fn test_unstake_all_validate() { let subnet_owner_hotkey = U256::from(1002); let hotkey = U256::from(2); let coldkey = U256::from(3); - let amount_staked = DefaultMinStake::::get() * 10; + let amount_staked = DefaultMinStake::::get().to_u64() * 10; let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey); SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount_staked); // Simulate stake for hotkey - SubnetTAO::::insert(netuid, u64::MAX / 1000); + SubnetTAO::::insert(netuid, TaoCurrency::from(u64::MAX / 1000)); SubnetAlphaIn::::insert(netuid, AlphaCurrency::from(u64::MAX / 1000)); SubtensorModule::stake_into_subnet( &hotkey, &coldkey, netuid, - amount_staked, - ::SwapInterface::max_price(), + amount_staked.into(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); // Set the liquidity at lowest possible value so that all staking requests fail let reserve = u64::from(mock::SwapMinimumReserve::get()) - 1; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // unstake_all call let call = RuntimeCall::SubtensorModule(SubtensorCall::unstake_all { hotkey }); @@ -3102,31 +3156,31 @@ fn test_max_amount_add_root() { new_test_ext(0).execute_with(|| { // 0 price on root => max is 0 assert_eq!( - SubtensorModule::get_max_amount_add(NetUid::ROOT, 0), + SubtensorModule::get_max_amount_add(NetUid::ROOT, TaoCurrency::ZERO), Err(Error::::ZeroMaxStakeAmount) ); // 0.999999... price on root => max is 0 assert_eq!( - SubtensorModule::get_max_amount_add(NetUid::ROOT, 999_999_999), + SubtensorModule::get_max_amount_add(NetUid::ROOT, TaoCurrency::from(999_999_999)), Err(Error::::ZeroMaxStakeAmount) ); // 1.0 price on root => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_add(NetUid::ROOT, 1_000_000_000), + SubtensorModule::get_max_amount_add(NetUid::ROOT, TaoCurrency::from(1_000_000_000)), Ok(u64::MAX) ); // 1.000...001 price on root => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_add(NetUid::ROOT, 1_000_000_001), + SubtensorModule::get_max_amount_add(NetUid::ROOT, TaoCurrency::from(1_000_000_001)), Ok(u64::MAX) ); // 2.0 price on root => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_add(NetUid::ROOT, 2_000_000_000), + SubtensorModule::get_max_amount_add(NetUid::ROOT, TaoCurrency::from(2_000_000_000)), Ok(u64::MAX) ); }); @@ -3140,31 +3194,31 @@ fn test_max_amount_add_stable() { // 0 price => max is 0 assert_eq!( - SubtensorModule::get_max_amount_add(netuid, 0), + SubtensorModule::get_max_amount_add(netuid, TaoCurrency::ZERO), Err(Error::::ZeroMaxStakeAmount) ); // 0.999999... price => max is 0 assert_eq!( - SubtensorModule::get_max_amount_add(netuid, 999_999_999), + SubtensorModule::get_max_amount_add(netuid, TaoCurrency::from(999_999_999)), Err(Error::::ZeroMaxStakeAmount) ); // 1.0 price => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_add(netuid, 1_000_000_000), + SubtensorModule::get_max_amount_add(netuid, TaoCurrency::from(1_000_000_000)), Ok(u64::MAX) ); // 1.000...001 price => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_add(netuid, 1_000_000_001), + SubtensorModule::get_max_amount_add(netuid, TaoCurrency::from(1_000_000_001)), Ok(u64::MAX) ); // 2.0 price => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_add(netuid, 2_000_000_000), + SubtensorModule::get_max_amount_add(netuid, TaoCurrency::from(2_000_000_000)), Ok(u64::MAX) ); }); @@ -3241,11 +3295,16 @@ fn test_max_amount_add_dynamic() { let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); // Forse-set alpha in and tao reserve to achieve relative price of subnets - SubnetTAO::::insert(netuid, tao_in); + SubnetTAO::::insert(netuid, TaoCurrency::from(tao_in)); SubnetAlphaIn::::insert(netuid, alpha_in); // Force the swap to initialize - SubtensorModule::swap_tao_for_alpha(netuid, 0, 1_000_000_000_000).unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid, + TaoCurrency::ZERO, + TaoCurrency::from(1_000_000_000_000), + ) + .unwrap(); if !alpha_in.is_zero() { let expected_price = U96F32::from_num(tao_in) / U96F32::from_num(alpha_in); @@ -3258,9 +3317,12 @@ fn test_max_amount_add_dynamic() { } match expected_max_swappable { - Err(e) => assert_err!(SubtensorModule::get_max_amount_add(netuid, limit_price), e), + Err(e) => assert_err!( + SubtensorModule::get_max_amount_add(netuid, limit_price.into()), + e + ), Ok(v) => assert_abs_diff_eq!( - SubtensorModule::get_max_amount_add(netuid, limit_price).unwrap(), + SubtensorModule::get_max_amount_add(netuid, limit_price.into()).unwrap(), v, epsilon = v / 100 ), @@ -3274,37 +3336,37 @@ fn test_max_amount_remove_root() { new_test_ext(0).execute_with(|| { // 0 price on root => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_remove(NetUid::ROOT, 0), + SubtensorModule::get_max_amount_remove(NetUid::ROOT, TaoCurrency::ZERO), Ok(AlphaCurrency::MAX) ); // 0.5 price on root => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_remove(NetUid::ROOT, 500_000_000), + SubtensorModule::get_max_amount_remove(NetUid::ROOT, TaoCurrency::from(500_000_000)), Ok(AlphaCurrency::MAX) ); // 0.999999... price on root => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_remove(NetUid::ROOT, 999_999_999), + SubtensorModule::get_max_amount_remove(NetUid::ROOT, TaoCurrency::from(999_999_999)), Ok(AlphaCurrency::MAX) ); // 1.0 price on root => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_remove(NetUid::ROOT, 1_000_000_000), + SubtensorModule::get_max_amount_remove(NetUid::ROOT, TaoCurrency::from(1_000_000_000)), Ok(AlphaCurrency::MAX) ); // 1.000...001 price on root => max is 0 assert_eq!( - SubtensorModule::get_max_amount_remove(NetUid::ROOT, 1_000_000_001), + SubtensorModule::get_max_amount_remove(NetUid::ROOT, TaoCurrency::from(1_000_000_001)), Err(Error::::ZeroMaxStakeAmount) ); // 2.0 price on root => max is 0 assert_eq!( - SubtensorModule::get_max_amount_remove(NetUid::ROOT, 2_000_000_000), + SubtensorModule::get_max_amount_remove(NetUid::ROOT, TaoCurrency::from(2_000_000_000)), Err(Error::::ZeroMaxStakeAmount) ); }); @@ -3318,31 +3380,31 @@ fn test_max_amount_remove_stable() { // 0 price => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_remove(netuid, 0), + SubtensorModule::get_max_amount_remove(netuid, TaoCurrency::ZERO), Ok(AlphaCurrency::MAX) ); // 0.999999... price => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_remove(netuid, 999_999_999), + SubtensorModule::get_max_amount_remove(netuid, TaoCurrency::from(999_999_999)), Ok(AlphaCurrency::MAX) ); // 1.0 price => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_remove(netuid, 1_000_000_000), + SubtensorModule::get_max_amount_remove(netuid, TaoCurrency::from(1_000_000_000)), Ok(AlphaCurrency::MAX) ); // 1.000...001 price => max is 0 assert_eq!( - SubtensorModule::get_max_amount_remove(netuid, 1_000_000_001), + SubtensorModule::get_max_amount_remove(netuid, TaoCurrency::from(1_000_000_001)), Err(Error::::ZeroMaxStakeAmount) ); // 2.0 price => max is 0 assert_eq!( - SubtensorModule::get_max_amount_remove(netuid, 2_000_000_000), + SubtensorModule::get_max_amount_remove(netuid, TaoCurrency::from(2_000_000_000)), Err(Error::::ZeroMaxStakeAmount) ); }); @@ -3456,7 +3518,7 @@ fn test_max_amount_remove_dynamic() { |&(tao_in, alpha_in, limit_price, ref expected_max_swappable)| { let alpha_in = AlphaCurrency::from(alpha_in); // Forse-set alpha in and tao reserve to achieve relative price of subnets - SubnetTAO::::insert(netuid, tao_in); + SubnetTAO::::insert(netuid, TaoCurrency::from(tao_in)); SubnetAlphaIn::::insert(netuid, alpha_in); if !alpha_in.is_zero() { @@ -3469,13 +3531,14 @@ fn test_max_amount_remove_dynamic() { match expected_max_swappable { Err(_) => assert_err!( - SubtensorModule::get_max_amount_remove(netuid, limit_price), + SubtensorModule::get_max_amount_remove(netuid, limit_price.into()), Error::::ZeroMaxStakeAmount ), Ok(v) => { let v = AlphaCurrency::from(*v); assert_abs_diff_eq!( - SubtensorModule::get_max_amount_remove(netuid, limit_price).unwrap(), + SubtensorModule::get_max_amount_remove(netuid, limit_price.into()) + .unwrap(), v, epsilon = v / 100.into() ); @@ -3492,37 +3555,57 @@ fn test_max_amount_move_root_root() { new_test_ext(0).execute_with(|| { // 0 price on (root, root) exchange => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, NetUid::ROOT, 0), + SubtensorModule::get_max_amount_move(NetUid::ROOT, NetUid::ROOT, TaoCurrency::ZERO), Ok(AlphaCurrency::MAX) ); // 0.5 price on (root, root) => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, NetUid::ROOT, 500_000_000), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + NetUid::ROOT, + TaoCurrency::from(500_000_000) + ), Ok(AlphaCurrency::MAX) ); // 0.999999... price on (root, root) => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, NetUid::ROOT, 999_999_999), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + NetUid::ROOT, + TaoCurrency::from(999_999_999) + ), Ok(AlphaCurrency::MAX) ); // 1.0 price on (root, root) => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, NetUid::ROOT, 1_000_000_000), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + NetUid::ROOT, + TaoCurrency::from(1_000_000_000) + ), Ok(AlphaCurrency::MAX) ); // 1.000...001 price on (root, root) => max is 0 assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, NetUid::ROOT, 1_000_000_001), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + NetUid::ROOT, + TaoCurrency::from(1_000_000_001) + ), Err(Error::::ZeroMaxStakeAmount) ); // 2.0 price on (root, root) => max is 0 assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, NetUid::ROOT, 2_000_000_000), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + NetUid::ROOT, + TaoCurrency::from(2_000_000_000) + ), Err(Error::::ZeroMaxStakeAmount) ); }); @@ -3537,37 +3620,57 @@ fn test_max_amount_move_root_stable() { // 0 price on (root, stable) exchange => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, netuid, 0), + SubtensorModule::get_max_amount_move(NetUid::ROOT, netuid, TaoCurrency::ZERO), Ok(AlphaCurrency::MAX) ); // 0.5 price on (root, stable) => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, netuid, 500_000_000), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + netuid, + TaoCurrency::from(500_000_000) + ), Ok(AlphaCurrency::MAX) ); // 0.999999... price on (root, stable) => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, netuid, 999_999_999), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + netuid, + TaoCurrency::from(999_999_999) + ), Ok(AlphaCurrency::MAX) ); // 1.0 price on (root, stable) => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, netuid, 1_000_000_000), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + netuid, + TaoCurrency::from(1_000_000_000) + ), Ok(AlphaCurrency::MAX) ); // 1.000...001 price on (root, stable) => max is 0 assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, netuid, 1_000_000_001), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + netuid, + TaoCurrency::from(1_000_000_001) + ), Err(Error::::ZeroMaxStakeAmount) ); // 2.0 price on (root, stable) => max is 0 assert_eq!( - SubtensorModule::get_max_amount_move(NetUid::ROOT, netuid, 2_000_000_000), + SubtensorModule::get_max_amount_move( + NetUid::ROOT, + netuid, + TaoCurrency::from(2_000_000_000) + ), Err(Error::::ZeroMaxStakeAmount) ); }); @@ -3587,7 +3690,7 @@ fn test_max_amount_move_stable_dynamic() { let dynamic_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); // Force-set alpha in and tao reserve to make price equal 0.5 - let tao_reserve = 50_000_000_000_u64; + let tao_reserve = TaoCurrency::from(50_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); SubnetTAO::::insert(dynamic_netuid, tao_reserve); SubnetAlphaIn::::insert(dynamic_netuid, alpha_in); @@ -3599,49 +3702,75 @@ fn test_max_amount_move_stable_dynamic() { // 0 price => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, 0), + SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, TaoCurrency::ZERO), Ok(AlphaCurrency::MAX) ); // 2.0 price => max is 0 assert_eq!( - SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, 2_000_000_000), + SubtensorModule::get_max_amount_move( + stable_netuid, + dynamic_netuid, + TaoCurrency::from(2_000_000_000) + ), Err(Error::::ZeroMaxStakeAmount) ); // 3.0 price => max is 0 assert_eq!( - SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, 3_000_000_000), + SubtensorModule::get_max_amount_move( + stable_netuid, + dynamic_netuid, + TaoCurrency::from(3_000_000_000) + ), Err(Error::::ZeroMaxStakeAmount) ); // 2x price => max is 1x TAO assert_abs_diff_eq!( - SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, 500_000_000) - .unwrap(), - AlphaCurrency::from(tao_reserve + (tao_reserve as f64 * 0.003) as u64), - epsilon = AlphaCurrency::from(tao_reserve / 100), + SubtensorModule::get_max_amount_move( + stable_netuid, + dynamic_netuid, + TaoCurrency::from(500_000_000) + ) + .unwrap(), + AlphaCurrency::from( + tao_reserve.to_u64() + (tao_reserve.to_u64() as f64 * 0.003) as u64 + ), + epsilon = AlphaCurrency::from(tao_reserve.to_u64() / 100), ); // Precision test: // 1.99999..9000 price => max > 0 assert!( - SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, 1_999_999_000) - .unwrap() + SubtensorModule::get_max_amount_move( + stable_netuid, + dynamic_netuid, + TaoCurrency::from(1_999_999_000) + ) + .unwrap() > AlphaCurrency::ZERO ); // Max price doesn't panic and returns something meaningful assert_eq!( - SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, u64::MAX), + SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, TaoCurrency::MAX), Err(Error::::ZeroMaxStakeAmount) ); assert_eq!( - SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, u64::MAX - 1), + SubtensorModule::get_max_amount_move( + stable_netuid, + dynamic_netuid, + TaoCurrency::MAX - 1.into() + ), Err(Error::::ZeroMaxStakeAmount) ); assert_eq!( - SubtensorModule::get_max_amount_move(stable_netuid, dynamic_netuid, u64::MAX / 2), + SubtensorModule::get_max_amount_move( + stable_netuid, + dynamic_netuid, + TaoCurrency::MAX / 2.into() + ), Err(Error::::ZeroMaxStakeAmount) ); }); @@ -3661,7 +3790,7 @@ fn test_max_amount_move_dynamic_stable() { let dynamic_netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); // Forse-set alpha in and tao reserve to make price equal 1.5 - let tao_reserve = 150_000_000_000_u64; + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); SubnetTAO::::insert(dynamic_netuid, tao_reserve); SubnetAlphaIn::::insert(dynamic_netuid, alpha_in); @@ -3673,41 +3802,49 @@ fn test_max_amount_move_dynamic_stable() { // 0 price => max is u64::MAX assert_eq!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 0), + SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, TaoCurrency::ZERO), Ok(AlphaCurrency::MAX) ); // Low price values don't blow things up assert!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 1).unwrap() + SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 1.into()).unwrap() > AlphaCurrency::ZERO ); assert!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 2).unwrap() + SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 2.into()).unwrap() > AlphaCurrency::ZERO ); assert!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 3).unwrap() + SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 3.into()).unwrap() > AlphaCurrency::ZERO ); // 1.5000...1 price => max is 0 assert_eq!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 1_500_000_001), + SubtensorModule::get_max_amount_move( + dynamic_netuid, + stable_netuid, + 1_500_000_001.into() + ), Err(Error::::ZeroMaxStakeAmount) ); // 1.5 price => max is 0 because of non-zero slippage assert_abs_diff_eq!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 1_500_000_000) - .unwrap_or(AlphaCurrency::ZERO), + SubtensorModule::get_max_amount_move( + dynamic_netuid, + stable_netuid, + 1_500_000_000.into() + ) + .unwrap_or(AlphaCurrency::ZERO), AlphaCurrency::ZERO, epsilon = 10_000.into() ); // 1/4 price => max is 1x Alpha assert_abs_diff_eq!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 375_000_000) + SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 375_000_000.into()) .unwrap(), alpha_in, epsilon = alpha_in / 1000.into(), @@ -3716,25 +3853,37 @@ fn test_max_amount_move_dynamic_stable() { // Precision test: // 1.499999.. price => max > 0 assert!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, 1_499_999_999) - .unwrap() + SubtensorModule::get_max_amount_move( + dynamic_netuid, + stable_netuid, + 1_499_999_999.into() + ) + .unwrap() > AlphaCurrency::ZERO ); // Max price doesn't panic and returns something meaningful assert!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, u64::MAX) + SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, TaoCurrency::MAX) .unwrap_or(AlphaCurrency::ZERO) < 21_000_000_000_000_000.into() ); assert!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, u64::MAX - 1) - .unwrap_or(AlphaCurrency::ZERO) + SubtensorModule::get_max_amount_move( + dynamic_netuid, + stable_netuid, + TaoCurrency::MAX - 1.into() + ) + .unwrap_or(AlphaCurrency::ZERO) < 21_000_000_000_000_000.into() ); assert!( - SubtensorModule::get_max_amount_move(dynamic_netuid, stable_netuid, u64::MAX / 2) - .unwrap_or(AlphaCurrency::ZERO) + SubtensorModule::get_max_amount_move( + dynamic_netuid, + stable_netuid, + TaoCurrency::MAX / 2.into() + ) + .unwrap_or(AlphaCurrency::ZERO) < 21_000_000_000_000_000.into() ); }); @@ -3938,9 +4087,9 @@ fn test_max_amount_move_dynamic_dynamic() { let alpha_in_2 = AlphaCurrency::from(alpha_in_2); let expected_max_swappable = AlphaCurrency::from(expected_max_swappable); // Forse-set alpha in and tao reserve to achieve relative price of subnets - SubnetTAO::::insert(origin_netuid, tao_in_1); + SubnetTAO::::insert(origin_netuid, TaoCurrency::from(tao_in_1)); SubnetAlphaIn::::insert(origin_netuid, alpha_in_1); - SubnetTAO::::insert(destination_netuid, tao_in_2); + SubnetTAO::::insert(destination_netuid, TaoCurrency::from(tao_in_2)); SubnetAlphaIn::::insert(destination_netuid, alpha_in_2); if !alpha_in_1.is_zero() && !alpha_in_2.is_zero() { @@ -3965,7 +4114,7 @@ fn test_max_amount_move_dynamic_dynamic() { SubtensorModule::get_max_amount_move( origin_netuid, destination_netuid, - limit_price + limit_price.into() ) .unwrap_or(AlphaCurrency::ZERO), expected_max_swappable, @@ -3987,9 +4136,9 @@ fn test_add_stake_limit_ok() { let netuid = add_dynamic_network(&hotkey_account_id, &coldkey_account_id); // Forse-set alpha in and tao reserve to make price equal 1.5 - let tao_reserve = U96F32::from_num(150_000_000_000_u64); - let alpha_in = AlphaCurrency::from(100_000_000_000_u64); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + let tao_reserve = TaoCurrency::from(150_000_000_000); + let alpha_in = AlphaCurrency::from(100_000_000_000); + mock::setup_reserves(netuid, tao_reserve, alpha_in); let current_price = ::SwapInterface::current_alpha_price(netuid.into()); assert_eq!(current_price, U96F32::from_num(1.5)); @@ -4000,7 +4149,7 @@ fn test_add_stake_limit_ok() { // Setup limit price so that it doesn't peak above 4x of current price // The amount that can be executed at this price is 450 TAO only // Alpha produced will be equal to 75 = 450*100/(450+150) - let limit_price = 24_000_000_000; + let limit_price = TaoCurrency::from(24_000_000_000); let expected_executed_stake = AlphaCurrency::from(75_000_000_000); // Add stake with slippage safety and check if the result is ok @@ -4008,7 +4157,7 @@ fn test_add_stake_limit_ok() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount, + amount.into(), limit_price, true )); @@ -4058,9 +4207,9 @@ fn test_add_stake_limit_fill_or_kill() { let netuid = add_dynamic_network(&hotkey_account_id, &coldkey_account_id); // Force-set alpha in and tao reserve to make price equal 1.5 - let tao_reserve: U96F32 = U96F32::from_num(150_000_000_000_u64); - let alpha_in = AlphaCurrency::from(100_000_000_000_u64); - SubnetTAO::::insert(netuid, tao_reserve.to_num::()); + let tao_reserve = TaoCurrency::from(150_000_000_000); + let alpha_in = AlphaCurrency::from(100_000_000_000); + SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); let current_price = ::SwapInterface::current_alpha_price(netuid.into()); @@ -4074,7 +4223,7 @@ fn test_add_stake_limit_fill_or_kill() { // Setup limit price so that it doesn't peak above 4x of current price // The amount that can be executed at this price is 450 TAO only // Alpha produced will be equal to 25 = 100 - 450*100/(150+450) - let limit_price = 24_000_000_000; + let limit_price = TaoCurrency::from(24_000_000_000); // Add stake with slippage safety and check if it fails assert_noop!( @@ -4082,7 +4231,7 @@ fn test_add_stake_limit_fill_or_kill() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount, + amount.into(), limit_price, false ), @@ -4090,7 +4239,7 @@ fn test_add_stake_limit_fill_or_kill() { ); // Lower the amount and it should succeed now - let amount_ok = 450_000_000_000; // fits the maximum + let amount_ok = TaoCurrency::from(450_000_000_000); // fits the maximum assert_ok!(SubtensorModule::add_stake_limit( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, @@ -4111,12 +4260,12 @@ fn test_add_stake_limit_partial_zero_max_stake_amount_error() { // Exact values from the error: // https://taostats.io/extrinsic/5338471-0009?network=finney let amount = 19980000000; - let limit_price = 26953618; - let tao_reserve: U96F32 = U96F32::from_num(5_032_494_439_940_u64); + let limit_price = TaoCurrency::from(26953618); + let tao_reserve = TaoCurrency::from(5_032_494_439_940); let alpha_in = AlphaCurrency::from(186_268_425_402_874); let netuid = add_dynamic_network(&hotkey_account_id, &coldkey_account_id); - SubnetTAO::::insert(netuid, tao_reserve.to_num::()); + SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount); @@ -4126,7 +4275,7 @@ fn test_add_stake_limit_partial_zero_max_stake_amount_error() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount, + amount.into(), limit_price, true ), @@ -4150,9 +4299,9 @@ fn test_remove_stake_limit_ok() { ); // Forse-set sufficient reserves - let tao_reserve: U96F32 = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); - SubnetTAO::::insert(netuid, tao_reserve.to_num::()); + SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); // Stake to hotkey account, and check if the result is ok @@ -4160,7 +4309,7 @@ fn test_remove_stake_limit_ok() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - stake_amount + stake_amount.into() )); let alpha_before = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey_account_id, @@ -4184,7 +4333,7 @@ fn test_remove_stake_limit_ok() { hotkey_account_id, netuid, alpha_before / 2.into(), - limit_price, + limit_price.into(), true )); let alpha_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -4222,16 +4371,16 @@ fn test_remove_stake_limit_fill_or_kill() { ); // Forse-set alpha in and tao reserve to make price equal 1.5 - let tao_reserve: U96F32 = U96F32::from_num(150_000_000_000_u64); + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); - SubnetTAO::::insert(netuid, tao_reserve.to_num::()); + SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); let current_price = ::SwapInterface::current_alpha_price(netuid.into()); assert_eq!(current_price, U96F32::from_num(1.5)); // Setup limit price so that it doesn't drop by more than 10% from current price - let limit_price = 1_350_000_000; + let limit_price = TaoCurrency::from(1_350_000_000); // Remove stake with slippage safety - fails assert_noop!( @@ -4252,7 +4401,7 @@ fn test_remove_stake_limit_fill_or_kill() { hotkey_account_id, netuid, unstake_amount / 100.into(), - limit_price, + limit_price.into(), false ),); }); @@ -4272,10 +4421,10 @@ fn test_add_stake_specific_stake_into_subnet_fail() { U64F64::from_num(161_986_254).saturating_div(U64F64::from_num(u64::MAX)); let existing_stake = AlphaCurrency::from(36_711_495_953); - let tao_in = 2_409_892_148_947; + let tao_in = TaoCurrency::from(2_409_892_148_947); let alpha_in = AlphaCurrency::from(15_358_708_513_716); - let tao_staked = 200_000_000; + let tao_staked = TaoCurrency::from(200_000_000); //add network let netuid = add_dynamic_network(&sn_owner_coldkey, &sn_owner_coldkey); @@ -4285,7 +4434,7 @@ fn test_add_stake_specific_stake_into_subnet_fail() { // Check we have zero staked assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); // Set a hotkey pool for the hotkey @@ -4306,7 +4455,7 @@ fn test_add_stake_specific_stake_into_subnet_fail() { // Give TAO balance to coldkey SubtensorModule::add_balance_to_coldkey_account( &coldkey_account_id, - tao_staked + 1_000_000_000, + tao_staked.to_u64() + 1_000_000_000, ); // Add stake as new hotkey @@ -4314,7 +4463,7 @@ fn test_add_stake_specific_stake_into_subnet_fail() { ::SwapInterface::swap( netuid.into(), OrderType::Buy, - tao_staked, + tao_staked.into(), ::SwapInterface::max_price(), false, true, @@ -4363,7 +4512,7 @@ fn test_remove_99_9991_per_cent_stake_removes_all() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() )); // Remove 99.9991% stake @@ -4388,12 +4537,12 @@ fn test_remove_99_9991_per_cent_stake_removes_all() { // Check that all alpha was unstaked and all TAO balance was returned (less fees) assert_abs_diff_eq!( SubtensorModule::get_coldkey_balance(&coldkey_account_id), - expected_balance, + expected_balance.to_u64(), epsilon = 10, ); assert_eq!( SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), - 0 + TaoCurrency::ZERO ); let new_alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey_account_id, @@ -4420,12 +4569,12 @@ fn test_remove_99_9989_per_cent_stake_leaves_a_little() { SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, amount); // Stake to hotkey account, and check if the result is ok - let (_, fee) = mock::swap_tao_to_alpha(netuid, amount); + let (_, fee) = mock::swap_tao_to_alpha(netuid, amount.into()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() )); // Remove 99.9989% stake @@ -4454,7 +4603,7 @@ fn test_remove_99_9989_per_cent_stake_leaves_a_little() { epsilon = amount / 1000, ); assert_abs_diff_eq!( - SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), + SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id).to_u64(), (amount as f64 * 0.01) as u64, epsilon = amount / 1000, ); @@ -4497,11 +4646,11 @@ fn test_move_stake_limit_partial() { // Forse-set alpha in and tao reserve to make price equal 1.5 on both origin and destination, // but there's much more liquidity on destination, so its price wouldn't go up when restaked - let tao_reserve: U96F32 = U96F32::from_num(150_000_000_000_u64); + let tao_reserve = TaoCurrency::from(150_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); - SubnetTAO::::insert(origin_netuid, tao_reserve.to_num::()); + SubnetTAO::::insert(origin_netuid, tao_reserve); SubnetAlphaIn::::insert(origin_netuid, alpha_in); - SubnetTAO::::insert(destination_netuid, (tao_reserve * 100_000).to_num::()); + SubnetTAO::::insert(destination_netuid, tao_reserve * 100_000.into()); SubnetAlphaIn::::insert(destination_netuid, alpha_in * 100_000.into()); let current_price = ::SwapInterface::current_alpha_price(origin_netuid.into()); @@ -4509,7 +4658,7 @@ fn test_move_stake_limit_partial() { // The relative price between origin and destination subnets is 1. // Setup limit relative price so that it doesn't drop by more than 1% from current price - let limit_price = 990_000_000; + let limit_price = TaoCurrency::from(990_000_000); // Move stake with slippage safety - executes partially assert_ok!(SubtensorModule::swap_stake_limit( @@ -4558,13 +4707,9 @@ fn test_unstake_all_hits_liquidity_min() { ); // Setup the Alpha pool so that removing all the Alpha will bring liqudity below the minimum - let remaining_tao = I96F32::from_num(u64::from(mock::SwapMinimumReserve::get()) - 1); - let alpha_reserves = I110F18::from(stake_amount.to_u64() + 10_000_000); - mock::setup_reserves( - netuid, - remaining_tao.to_num(), - alpha_reserves.to_num::().into(), - ); + let remaining_tao = TaoCurrency::from(u64::from(mock::SwapMinimumReserve::get()) - 1); + let alpha_reserves = AlphaCurrency::from(stake_amount.to_u64() + 10_000_000); + mock::setup_reserves(netuid, remaining_tao, alpha_reserves); // Try to unstake, but we reduce liquidity too far @@ -4601,7 +4746,7 @@ fn test_unstake_all_alpha_hits_liquidity_min() { RuntimeOrigin::signed(coldkey), hotkey, netuid, - stake_amount + stake_amount.into() )); // Setup the pool so that removing all the TAO will bring liqudity below the minimum @@ -4617,7 +4762,7 @@ fn test_unstake_all_alpha_hits_liquidity_min() { mock::setup_reserves( netuid, - tao_reserves.to_num::() / 100_u64, + (tao_reserves.to_num::() / 100_u64).into(), alpha_reserves.to_num::().into(), ); @@ -4657,13 +4802,17 @@ fn test_unstake_all_alpha_works() { RuntimeOrigin::signed(coldkey), hotkey, netuid, - stake_amount + stake_amount.into() )); remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); // Setup the pool so that removing all the TAO will keep liq above min - mock::setup_reserves(netuid, stake_amount * 10, (stake_amount * 100).into()); + mock::setup_reserves( + netuid, + (stake_amount * 10).into(), + (stake_amount * 100).into(), + ); // Unstake all alpha to root assert_ok!(SubtensorModule::unstake_all_alpha( @@ -4705,14 +4854,14 @@ fn test_unstake_all_works() { RuntimeOrigin::signed(coldkey), hotkey, netuid, - stake_amount + stake_amount.into() )); // Setup the pool so that removing all the TAO will keep liq above min mock::setup_reserves( netuid, - stake_amount * 10, - AlphaCurrency::from(stake_amount * 100), + (stake_amount * 10).into(), + (stake_amount * 100).into(), ); remove_stake_rate_limit_for_tests(&hotkey, &coldkey, netuid); @@ -4743,9 +4892,9 @@ fn test_stake_into_subnet_ok() { let netuid = add_dynamic_network(&owner_hotkey, &owner_coldkey); // Forse-set alpha in and tao reserve to make price equal 0.01 - let tao_reserve = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(1_000_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); let current_price = ::SwapInterface::current_alpha_price(netuid.into()) .to_num::(); @@ -4765,8 +4914,8 @@ fn test_stake_into_subnet_ok() { &hotkey, &coldkey, netuid, - amount, - u64::MAX, + amount.into(), + TaoCurrency::MAX, false, )); let fee_rate = pallet_subtensor_swap::FeeRate::::get(NetUid::from(netuid)) as f64 @@ -4796,9 +4945,9 @@ fn test_stake_into_subnet_low_amount() { let netuid = add_dynamic_network(&owner_hotkey, &owner_coldkey); // Forse-set alpha in and tao reserve to make price equal 0.01 - let tao_reserve = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(1_000_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); let current_price = ::SwapInterface::current_alpha_price(netuid.into()) .to_num::(); @@ -4818,8 +4967,8 @@ fn test_stake_into_subnet_low_amount() { &hotkey, &coldkey, netuid, - amount, - u64::MAX, + amount.into(), + TaoCurrency::MAX, false, )); let expected_stake = AlphaCurrency::from(((amount as f64) * 0.997 / current_price) as u64); @@ -4846,9 +4995,9 @@ fn test_unstake_from_subnet_low_amount() { let netuid = add_dynamic_network(&owner_hotkey, &owner_coldkey); // Forse-set alpha in and tao reserve to make price equal 0.01 - let tao_reserve = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(1_000_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); // Initialize swap v3 assert_ok!(::SwapInterface::swap( @@ -4865,8 +5014,8 @@ fn test_unstake_from_subnet_low_amount() { &hotkey, &coldkey, netuid, - amount, - u64::MAX, + amount.into(), + TaoCurrency::MAX, false, )); @@ -4878,7 +5027,7 @@ fn test_unstake_from_subnet_low_amount() { &coldkey, netuid, alpha, - u64::MIN, + TaoCurrency::ZERO, false, )); @@ -4903,9 +5052,9 @@ fn test_stake_into_subnet_prohibitive_limit() { SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount); // Forse-set alpha in and tao reserve to make price equal 0.01 - let tao_reserve = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(1_000_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); // Initialize swap v3 assert_ok!(::SwapInterface::swap( @@ -4924,8 +5073,8 @@ fn test_stake_into_subnet_prohibitive_limit() { RuntimeOrigin::signed(coldkey), owner_hotkey, netuid, - amount, - 0, + amount.into(), + TaoCurrency::ZERO, true, ), Error::::ZeroMaxStakeAmount @@ -4959,9 +5108,9 @@ fn test_unstake_from_subnet_prohibitive_limit() { SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount); // Forse-set alpha in and tao reserve to make price equal 0.01 - let tao_reserve = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(1_000_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); // Initialize swap v3 assert_ok!(::SwapInterface::swap( @@ -4978,8 +5127,8 @@ fn test_unstake_from_subnet_prohibitive_limit() { &owner_hotkey, &coldkey, netuid, - amount, - u64::MAX, + amount.into(), + TaoCurrency::MAX, false, )); @@ -4997,7 +5146,7 @@ fn test_unstake_from_subnet_prohibitive_limit() { owner_hotkey, netuid, alpha, - u64::MAX, + TaoCurrency::MAX, true, ), Error::::ZeroMaxStakeAmount @@ -5034,9 +5183,9 @@ fn test_unstake_full_amount() { SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount); // Forse-set alpha in and tao reserve to make price equal 0.01 - let tao_reserve = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(1_000_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); // Initialize swap v3 assert_ok!(::SwapInterface::swap( @@ -5053,8 +5202,8 @@ fn test_unstake_full_amount() { &owner_hotkey, &coldkey, netuid, - amount, - u64::MAX, + amount.into(), + TaoCurrency::MAX, false, )); @@ -5144,20 +5293,20 @@ fn test_swap_fees_tao_correctness() { pallet_subtensor_swap::EnabledUserLiquidity::::insert(NetUid::from(netuid), true); // Forse-set alpha in and tao reserve to make price equal 0.25 - let tao_reserve = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(400_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); // Check starting "total TAO" let total_tao_before = - user_balance_before + owner_balance_before + SubnetTAO::::get(netuid); + user_balance_before + owner_balance_before + SubnetTAO::::get(netuid).to_u64(); // Get alpha for owner assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(owner_coldkey), owner_hotkey, netuid, - amount, + amount.into(), )); let mut fees = (fee_rate * amount as f64) as u64; @@ -5186,8 +5335,8 @@ fn test_swap_fees_tao_correctness() { RuntimeOrigin::signed(coldkey), owner_hotkey, netuid, - amount, - (limit_price * u64::MAX as f64) as u64, + amount.into(), + ((limit_price * u64::MAX as f64) as u64).into(), true )); fees += (fee_rate * amount as f64) as u64; @@ -5209,11 +5358,13 @@ fn test_swap_fees_tao_correctness() { // Check ending "total TAO" let owner_balance_after = SubtensorModule::get_coldkey_balance(&owner_coldkey); let user_balance_after = SubtensorModule::get_coldkey_balance(&coldkey); - let total_tao_after = - user_balance_after + owner_balance_after + SubnetTAO::::get(netuid) + fees; + let total_tao_after = user_balance_after + + owner_balance_after + + SubnetTAO::::get(netuid).to_u64() + + fees; // Total TAO does not change, leave some epsilon for rounding - assert_abs_diff_eq!(total_tao_before, total_tao_after, epsilon = 2,); + assert_abs_diff_eq!(total_tao_before, total_tao_after, epsilon = 2); }); } @@ -5282,12 +5433,12 @@ fn test_remove_stake_full_limit_ok() { stake_amount, ); - let tao_reserve: U96F32 = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000_u64); let alpha_in = AlphaCurrency::from(100_000_000_000); - SubnetTAO::::insert(netuid, tao_reserve.to_num::()); + SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); - let limit_price = 90_000_000; + let limit_price = TaoCurrency::from(90_000_000); // Remove stake with slippage safety assert_ok!(SubtensorModule::remove_stake_full_limit( @@ -5330,12 +5481,12 @@ fn test_remove_stake_full_limit_fails_slippage_too_high() { stake_amount, ); - let tao_reserve: U96F32 = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); - SubnetTAO::::insert(netuid, tao_reserve.to_num::()); + SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); - let invalid_limit_price = 910_000_000; + let invalid_limit_price = TaoCurrency::from(910_000_000); // Remove stake with slippage safety assert_err!( @@ -5368,9 +5519,9 @@ fn test_remove_stake_full_limit_ok_with_no_limit_price() { stake_amount, ); - let tao_reserve: U96F32 = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(100_000_000_000); - SubnetTAO::::insert(netuid, tao_reserve.to_num::()); + SubnetTAO::::insert(netuid, tao_reserve); SubnetAlphaIn::::insert(netuid, alpha_in); // Remove stake with slippage safety @@ -5403,7 +5554,7 @@ fn test_default_min_stake_sufficiency() { let owner_hotkey = U256::from(1); let owner_coldkey = U256::from(2); let coldkey = U256::from(4); - let min_tao_stake = DefaultMinStake::::get() * 2; + let min_tao_stake = DefaultMinStake::::get().to_u64() * 2; let amount = min_tao_stake; let owner_balance_before = amount * 10; let user_balance_before = amount * 100; @@ -5418,9 +5569,9 @@ fn test_default_min_stake_sufficiency() { // Set some extreme, but realistic TAO and Alpha reserves to minimize slippage // 1% of TAO max supply // 0.01 Alpha price - let tao_reserve = U96F32::from_num(210_000_000_000_000_u64); + let tao_reserve = TaoCurrency::from(210_000_000_000_000); let alpha_in = AlphaCurrency::from(21_000_000_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); let current_price_before = ::SwapInterface::current_alpha_price(netuid.into()); @@ -5429,7 +5580,7 @@ fn test_default_min_stake_sufficiency() { RuntimeOrigin::signed(coldkey), owner_hotkey, netuid, - amount, + amount.into(), )); let fee_stake = (fee_rate * amount as f64) as u64; let current_price_after_stake = @@ -5477,16 +5628,16 @@ fn test_update_position_fees() { pallet_subtensor_swap::EnabledUserLiquidity::::insert(NetUid::from(netuid), true); // Forse-set alpha in and tao reserve to make price equal 0.25 - let tao_reserve = U96F32::from_num(100_000_000_000_u64); + let tao_reserve = TaoCurrency::from(100_000_000_000); let alpha_in = AlphaCurrency::from(400_000_000_000); - mock::setup_reserves(netuid, tao_reserve.to_num(), alpha_in); + mock::setup_reserves(netuid, tao_reserve, alpha_in); // Get alpha for owner assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(owner_coldkey), owner_hotkey, netuid, - amount, + amount.into(), )); // Add owner coldkey Alpha as concentrated liquidity @@ -5515,7 +5666,7 @@ fn test_update_position_fees() { RuntimeOrigin::signed(coldkey), owner_hotkey, netuid, - amount, + amount.into(), )); remove_stake_rate_limit_for_tests(&owner_hotkey, &coldkey, netuid); @@ -5644,11 +5795,16 @@ fn test_large_swap() { pallet_subtensor_swap::EnabledUserLiquidity::::insert(NetUid::from(netuid), true); // Force the swap to initialize - SubtensorModule::swap_tao_for_alpha(netuid, 0, 1_000_000_000_000).unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid, + TaoCurrency::ZERO, + TaoCurrency::from(1_000_000_000_000), + ) + .unwrap(); setup_positions(netuid.into()); - let swap_amount = 100_000_000_000_000; + let swap_amount = TaoCurrency::from(100_000_000_000_000); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey), owner_hotkey, @@ -5667,8 +5823,8 @@ fn test_stake_rate_limits() { let hot1 = U256::from(1); let cold1 = U256::from(3); let netuid = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); - let amount = DefaultMinStake::::get() * 10; - let fee: u64 = DefaultMinStake::::get(); + let amount = DefaultMinStake::::get().to_u64() * 10; + let fee = DefaultMinStake::::get().to_u64(); let init_balance = amount + fee + ExistentialDeposit::get(); register_ok_neuron(netuid, hot1, cold1, 0); @@ -5680,7 +5836,7 @@ fn test_stake_rate_limits() { RuntimeOrigin::signed(cold1), hot1, netuid, - amount + fee + (amount + fee).into() )); assert_err!( diff --git a/pallets/subtensor/src/tests/staking2.rs b/pallets/subtensor/src/tests/staking2.rs index 9e374095de..6fe51b633f 100644 --- a/pallets/subtensor/src/tests/staking2.rs +++ b/pallets/subtensor/src/tests/staking2.rs @@ -6,7 +6,7 @@ use frame_support::{ weights::Weight, }; use sp_core::U256; -use subtensor_runtime_common::{AlphaCurrency, Currency}; +use subtensor_runtime_common::{AlphaCurrency, Currency, TaoCurrency}; use subtensor_swap_interface::SwapHandler; use super::mock; @@ -18,13 +18,13 @@ use crate::*; fn test_stake_base_case() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); - let tao_to_swap = 1_000_000_000; // 1 TAO + let tao_to_swap = TaoCurrency::from(1_000_000_000); // 1 TAO // Set up the subnet with dynamic mechanism SubnetMechanism::::insert(netuid, 1); // Initialize subnet with some existing TAO and Alpha - let initial_subnet_tao = 10_000_000_000; // 10 TAO + let initial_subnet_tao = TaoCurrency::from(10_000_000_000); // 10 TAO let initial_subnet_alpha = AlphaCurrency::from(5_000_000_000); // 5 Alpha mock::setup_reserves(netuid, initial_subnet_tao, initial_subnet_alpha); SubnetAlphaOut::::insert(netuid, initial_subnet_alpha); @@ -38,7 +38,7 @@ fn test_stake_base_case() { SubtensorModule::swap_tao_for_alpha( netuid, tao_to_swap, - ::SwapInterface::max_price(), + ::SwapInterface::max_price().into(), ) .unwrap() .amount_paid_out, @@ -53,7 +53,7 @@ fn test_stake_base_case() { // Check subnet updates assert_eq!( SubnetTAO::::get(netuid), - initial_subnet_tao + tao_to_swap - fee, + initial_subnet_tao + tao_to_swap - fee.into(), "Subnet TAO not updated correctly" ); assert_eq!( @@ -673,7 +673,7 @@ fn test_stake_fee_api() { let alpha_divs = AlphaCurrency::from(100_000_000_000); let total_hotkey_alpha = AlphaCurrency::from(100_000_000_000); - let tao_in = 100_000_000_000; // 100 TAO + let tao_in = TaoCurrency::from(100_000_000_000); // 100 TAO let reciprocal_price = 2; // 1 / price let stake_amount = 100_000_000_000; @@ -681,9 +681,15 @@ fn test_stake_fee_api() { SubnetAlphaOut::::insert(netuid0, AlphaCurrency::from(100_000_000_000)); SubnetAlphaOut::::insert(netuid1, AlphaCurrency::from(100_000_000_000)); // Set pools using price - SubnetAlphaIn::::insert(netuid0, AlphaCurrency::from(tao_in * reciprocal_price)); + SubnetAlphaIn::::insert( + netuid0, + AlphaCurrency::from(tao_in.to_u64() * reciprocal_price), + ); SubnetTAO::::insert(netuid0, tao_in); - SubnetAlphaIn::::insert(netuid1, AlphaCurrency::from(tao_in * reciprocal_price)); + SubnetAlphaIn::::insert( + netuid1, + AlphaCurrency::from(tao_in.to_u64() * reciprocal_price), + ); SubnetTAO::::insert(netuid1, tao_in); // Setup alpha divs for hotkey1 @@ -819,7 +825,7 @@ fn test_stake_fee_calculation() { let alpha_divs = AlphaCurrency::from(100_000_000_000); let total_hotkey_alpha = AlphaCurrency::from(100_000_000_000); - let tao_in = 100_000_000_000; // 100 TAO + let tao_in = TaoCurrency::from(100_000_000_000); // 100 TAO let reciprocal_price = 2; // 1 / price let stake_amount = 100_000_000_000_u64; @@ -832,12 +838,12 @@ fn test_stake_fee_calculation() { mock::setup_reserves( netuid0, tao_in, - AlphaCurrency::from(tao_in * reciprocal_price), + AlphaCurrency::from(tao_in.to_u64() * reciprocal_price), ); mock::setup_reserves( netuid1, tao_in, - AlphaCurrency::from(tao_in * reciprocal_price), + AlphaCurrency::from(tao_in.to_u64() * reciprocal_price), ); // Setup alpha divs for hotkey1 diff --git a/pallets/subtensor/src/tests/subnet.rs b/pallets/subtensor/src/tests/subnet.rs index d2bab08d39..3b93390dc0 100644 --- a/pallets/subtensor/src/tests/subnet.rs +++ b/pallets/subtensor/src/tests/subnet.rs @@ -5,7 +5,7 @@ use crate::*; use frame_support::{assert_err, assert_noop, assert_ok}; use frame_system::Config; use sp_core::U256; -use subtensor_runtime_common::AlphaCurrency; +use subtensor_runtime_common::{AlphaCurrency, TaoCurrency}; use super::mock; @@ -22,7 +22,7 @@ fn test_do_start_call_ok() { add_network_without_emission_block(netuid, tempo, 0); assert_eq!(FirstEmissionBlockNumber::::get(netuid), None); - mock::setup_reserves(netuid, 1_000_000_000, 1_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000.into(), 1_000_000_000.into()); // account 0 is the default owner for any subnet assert_eq!(SubnetOwner::::get(netuid), coldkey_account_id); @@ -64,11 +64,11 @@ fn test_do_start_call_fail_not_owner() { let tempo: u16 = 13; let coldkey_account_id = U256::from(0); let wrong_owner_account_id = U256::from(2); - let burn_cost = 1000; + let burn_cost = TaoCurrency::from(1000); //add network SubtensorModule::set_burn(netuid, burn_cost); add_network_without_emission_block(netuid, tempo, 0); - mock::setup_reserves(netuid, 1_000_000_000, 1_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000.into(), 1_000_000_000.into()); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); @@ -94,11 +94,11 @@ fn test_do_start_call_fail_with_cannot_start_call_now() { let netuid = NetUid::from(1); let tempo: u16 = 13; let coldkey_account_id = U256::from(0); - let burn_cost = 1000; + let burn_cost = TaoCurrency::from(1000); //add network SubtensorModule::set_burn(netuid, burn_cost); add_network_without_emission_block(netuid, tempo, 0); - mock::setup_reserves(netuid, 1_000_000_000, 1_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000.into(), 1_000_000_000.into()); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); @@ -123,13 +123,13 @@ fn test_do_start_call_fail_for_set_again() { let tempo: u16 = 13; let coldkey_account_id = U256::from(0); let hotkey_account_id = U256::from(1); - let burn_cost = 1000; + let burn_cost = TaoCurrency::from(1000); SubtensorModule::set_burn(netuid, burn_cost); add_network_without_emission_block(netuid, tempo, 0); assert_eq!(FirstEmissionBlockNumber::::get(netuid), None); - mock::setup_reserves(netuid, 1_000_000_000, 1_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000.into(), 1_000_000_000.into()); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, 10000); @@ -170,7 +170,7 @@ fn test_do_start_call_ok_with_same_block_number_after_coinbase() { add_network_without_emission_block(netuid, tempo, 0); assert_eq!(FirstEmissionBlockNumber::::get(netuid), None); - mock::setup_reserves(netuid, 1_000_000_000, 1_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000.into(), 1_000_000_000.into()); assert_eq!(SubnetOwner::::get(netuid), coldkey_account_id); @@ -205,7 +205,7 @@ fn test_register_network_min_burn_at_default() { let cost = SubtensorModule::get_network_lock_cost(); // Give coldkey enough for lock - SubtensorModule::add_balance_to_coldkey_account(&sn_owner_coldkey, cost); + SubtensorModule::add_balance_to_coldkey_account(&sn_owner_coldkey, cost.into()); // Register network assert_ok!(SubtensorModule::register_network( @@ -220,7 +220,7 @@ fn test_register_network_min_burn_at_default() { }; // Check min burn is set to default - assert_eq!(MinBurn::::get(netuid), InitialMinBurn::get()); + assert_eq!(MinBurn::::get(netuid), InitialMinBurn::get().into()); // Check registration allowed assert!(NetworkRegistrationAllowed::::get(netuid)); @@ -235,7 +235,7 @@ fn test_register_network_use_symbol_for_subnet_if_available() { let coldkey = U256::from(1_000_000 + i); let hotkey = U256::from(2_000_000 + i); let cost = SubtensorModule::get_network_lock_cost(); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost.into()); assert_ok!(SubtensorModule::register_network( <::RuntimeOrigin>::signed(coldkey), @@ -266,7 +266,7 @@ fn test_register_network_use_next_available_symbol_if_symbol_for_subnet_is_taken let coldkey = U256::from(1_000_000 + i); let hotkey = U256::from(2_000_000 + i); let cost = SubtensorModule::get_network_lock_cost(); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost.into()); assert_ok!(SubtensorModule::register_network( <::RuntimeOrigin>::signed(coldkey), @@ -294,7 +294,7 @@ fn test_register_network_use_next_available_symbol_if_symbol_for_subnet_is_taken let coldkey = U256::from(1_000_000 + 50); let hotkey = U256::from(2_000_000 + 50); let cost = SubtensorModule::get_network_lock_cost(); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost.into()); assert_ok!(SubtensorModule::register_network( <::RuntimeOrigin>::signed(coldkey), @@ -321,7 +321,7 @@ fn test_register_network_use_default_symbol_if_all_symbols_are_taken() { let coldkey = U256::from(1_000_000 + i); let hotkey = U256::from(2_000_000 + i); let cost = SubtensorModule::get_network_lock_cost(); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost.into()); assert_ok!(SubtensorModule::register_network( <::RuntimeOrigin>::signed(coldkey), @@ -333,7 +333,7 @@ fn test_register_network_use_default_symbol_if_all_symbols_are_taken() { let coldkey = U256::from(1_000_000 + 50); let hotkey = U256::from(2_000_000 + 50); let cost = SubtensorModule::get_network_lock_cost(); - SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost); + SubtensorModule::add_balance_to_coldkey_account(&coldkey, cost.into()); assert_ok!(SubtensorModule::register_network( <::RuntimeOrigin>::signed(coldkey), @@ -390,11 +390,11 @@ fn test_subtoken_enable_reject_trading_before_enable() { let hotkey_account_id: U256 = U256::from(1); let coldkey_account_id = U256::from(2); let hotkey_account_2_id: U256 = U256::from(3); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get().to_u64() * 10; let stake_bal = AlphaCurrency::from(10_000_000_000); // 10 Alpha - let limit_price = 1_000_000_000; // not important + let limit_price = TaoCurrency::from(1_000_000_000); // not important add_network_disable_subtoken(netuid, 10, 0); add_network_disable_subtoken(netuid2, 10, 0); @@ -403,7 +403,7 @@ fn test_subtoken_enable_reject_trading_before_enable() { assert!(!SubtokenEnabled::::get(netuid2)); // Set liq high enough to not trigger other errors - SubnetTAO::::set(netuid, 20_000_000_000); + SubnetTAO::::set(netuid, TaoCurrency::from(20_000_000_000)); SubnetAlphaIn::::set(netuid, AlphaCurrency::from(20_000_000_000)); // Register so staking *could* work @@ -428,7 +428,7 @@ fn test_subtoken_enable_reject_trading_before_enable() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount + amount.into() ), Error::::SubtokenDisabled ); @@ -438,7 +438,7 @@ fn test_subtoken_enable_reject_trading_before_enable() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount, + amount.into(), limit_price, false ), @@ -566,16 +566,16 @@ fn test_subtoken_enable_trading_ok_with_enable() { let coldkey_account_id = U256::from(2); let hotkey_account_2_id: U256 = U256::from(3); // stake big enough - let stake_amount = DefaultMinStake::::get() * 10000; + let stake_amount = DefaultMinStake::::get() * 10000.into(); // unstake, transfer, swap just very little - let unstake_amount = AlphaCurrency::from(DefaultMinStake::::get() * 10); + let unstake_amount = AlphaCurrency::from(DefaultMinStake::::get().to_u64() * 10); add_network(netuid, 10, 0); add_network(netuid2, 10, 0); - let reserve = stake_amount * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); - mock::setup_reserves(netuid2, reserve, reserve.into()); + let reserve = stake_amount.to_u64() * 1000; + mock::setup_reserves(netuid, reserve.into(), reserve.into()); + mock::setup_reserves(netuid2, reserve.into(), reserve.into()); SubnetAlphaOut::::insert(netuid, AlphaCurrency::from(reserve)); SubnetAlphaOut::::insert(netuid2, AlphaCurrency::from(reserve)); @@ -585,7 +585,10 @@ fn test_subtoken_enable_trading_ok_with_enable() { register_ok_neuron(netuid, hotkey_account_2_id, coldkey_account_id, 0); register_ok_neuron(netuid2, hotkey_account_2_id, coldkey_account_id, 100); - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, stake_amount * 10); + SubtensorModule::add_balance_to_coldkey_account( + &coldkey_account_id, + stake_amount.to_u64() * 10, + ); // all trading extrinsic should be possible now that subtoken is enabled. assert_ok!(SubtensorModule::add_stake( @@ -685,14 +688,17 @@ fn test_subtoken_enable_ok_for_burn_register_before_enable() { let coldkey_account_id = U256::from(2); let hotkey_account_2_id: U256 = U256::from(3); - let burn_cost = 1000; + let burn_cost = TaoCurrency::from(1000); // Set the burn cost SubtensorModule::set_burn(netuid, burn_cost); // Add the networks with subtoken disabled add_network_disable_subtoken(netuid, 10, 0); add_network_disable_subtoken(netuid2, 10, 0); // Give enough to burned register - SubtensorModule::add_balance_to_coldkey_account(&coldkey_account_id, burn_cost * 2 + 5_000); + SubtensorModule::add_balance_to_coldkey_account( + &coldkey_account_id, + burn_cost.to_u64() * 2 + 5_000, + ); // Should be possible to burned register before enable is activated assert_ok!(SubtensorModule::burned_register( diff --git a/pallets/subtensor/src/tests/swap_coldkey.rs b/pallets/subtensor/src/tests/swap_coldkey.rs index bab264a3c6..1f1b9b113b 100644 --- a/pallets/subtensor/src/tests/swap_coldkey.rs +++ b/pallets/subtensor/src/tests/swap_coldkey.rs @@ -12,7 +12,7 @@ use frame_system::{Config, RawOrigin}; use sp_core::{Get, H256, U256}; use sp_runtime::{DispatchError, traits::TxBaseImplication}; use substrate_fixed::types::U96F32; -use subtensor_runtime_common::{AlphaCurrency, Currency, SubnetInfo}; +use subtensor_runtime_common::{AlphaCurrency, Currency, SubnetInfo, TaoCurrency}; use subtensor_swap_interface::{OrderType, SwapHandler}; use super::mock; @@ -82,7 +82,7 @@ fn test_swap_total_coldkey_stake() { let other_coldkey = U256::from(3); let hotkey = U256::from(4); let other_hotkey = U256::from(5); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get().to_u64() * 10; let netuid = NetUid::from(1u16); add_network(netuid, 1, 0); @@ -91,19 +91,19 @@ fn test_swap_total_coldkey_stake() { register_ok_neuron(netuid, other_hotkey, other_coldkey, 1001000); let reserve = stake * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey, netuid, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), other_hotkey, netuid, - stake + stake.into() )); let total_stake_before_swap = SubtensorModule::get_total_stake_for_coldkey(&old_coldkey); @@ -116,7 +116,7 @@ fn test_swap_total_coldkey_stake() { assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0 + TaoCurrency::ZERO ); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), @@ -208,11 +208,11 @@ fn test_swap_with_no_stake() { assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0 + TaoCurrency::ZERO ); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), - 0 + TaoCurrency::ZERO ); }); } @@ -296,10 +296,10 @@ fn test_swap_idempotency() { let new_coldkey = U256::from(2); let hotkey = U256::from(3); let netuid = NetUid::from(1u16); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get().to_u64() * 10; let reserve = stake * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Add a network add_network(netuid, 1, 0); @@ -310,7 +310,7 @@ fn test_swap_idempotency() { <::RuntimeOrigin>::signed(old_coldkey), hotkey, netuid, - stake + stake.into() )); // Get stake before swap @@ -330,7 +330,7 @@ fn test_swap_idempotency() { assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0 + TaoCurrency::ZERO ); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), @@ -369,15 +369,15 @@ fn test_swap_with_max_values() { SubtensorModule::add_balance_to_coldkey_account(&old_coldkey2, max_stake + 1_000); let reserve = max_stake * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); - mock::setup_reserves(netuid2, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); + mock::setup_reserves(netuid2, reserve.into(), reserve.into()); // Stake to hotkey on each subnet. assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey, netuid, - max_stake + max_stake.into() )); let expected_stake1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, @@ -389,7 +389,7 @@ fn test_swap_with_max_values() { <::RuntimeOrigin>::signed(old_coldkey2), hotkey2, netuid2, - max_stake + max_stake.into() )); let expected_stake2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey2, @@ -411,21 +411,21 @@ fn test_swap_with_max_values() { assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0 + TaoCurrency::ZERO ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), - expected_stake1.to_u64(), - epsilon = expected_stake1.to_u64() / 1000 + expected_stake1.to_u64().into(), + epsilon = TaoCurrency::from(expected_stake1.to_u64()) / 1000.into() ); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey2), - 0 + TaoCurrency::ZERO ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey2), - expected_stake2.to_u64(), - epsilon = expected_stake2.to_u64() / 1000 + expected_stake2.to_u64().into(), + epsilon = TaoCurrency::from(expected_stake2.to_u64()) / 1000.into() ); }); } @@ -437,7 +437,7 @@ fn test_swap_with_non_existent_new_coldkey() { let old_coldkey = U256::from(1); let new_coldkey = U256::from(2); let hotkey = U256::from(3); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get().to_u64() * 10; let netuid = NetUid::from(1); add_network(netuid, 1, 0); @@ -446,14 +446,14 @@ fn test_swap_with_non_existent_new_coldkey() { SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, stake + 1_000); let reserve = stake * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Stake to hotkey. assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey, netuid, - stake + stake.into() )); let expected_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey, @@ -470,7 +470,7 @@ fn test_swap_with_non_existent_new_coldkey() { assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0 + TaoCurrency::ZERO ); let actual_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -533,13 +533,13 @@ fn test_swap_effect_on_delegated_stake() { RuntimeOrigin::signed(old_coldkey), hotkey, netuid, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(delegator), hotkey, netuid, - stake + stake.into() )); let coldkey_stake_before = SubtensorModule::get_total_stake_for_coldkey(&old_coldkey); let delegator_stake_before = SubtensorModule::get_total_stake_for_coldkey(&delegator); @@ -554,17 +554,17 @@ fn test_swap_effect_on_delegated_stake() { assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), coldkey_stake_before, - epsilon = 500 + epsilon = 500.into() ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&delegator), delegator_stake_before, - epsilon = 500 + epsilon = 500.into() ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0, - epsilon = 500 + TaoCurrency::ZERO, + epsilon = 500.into() ); }); } @@ -581,7 +581,7 @@ fn test_swap_concurrent_modifications() { let additional_stake = 500_000_000_000; let reserve = (initial_stake + additional_stake) * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Setup initial state add_network(netuid, 1, 1); @@ -595,7 +595,7 @@ fn test_swap_concurrent_modifications() { <::RuntimeOrigin>::signed(new_coldkey), hotkey, netuid, - initial_stake + initial_stake.into() )); // Verify initial stake @@ -613,7 +613,7 @@ fn test_swap_concurrent_modifications() { <::RuntimeOrigin>::signed(new_coldkey), hotkey, netuid, - additional_stake + additional_stake.into() )); let stake_with_additional = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -676,13 +676,13 @@ fn test_do_swap_coldkey_success() { let hotkey1 = U256::from(3); let hotkey2 = U256::from(4); let netuid = NetUid::from(1u16); - let stake_amount1 = DefaultMinStake::::get() * 10; - let stake_amount2 = DefaultMinStake::::get() * 20; + let stake_amount1 = DefaultMinStake::::get().to_u64() * 10; + let stake_amount2 = DefaultMinStake::::get().to_u64() * 20; let swap_cost = SubtensorModule::get_key_swap_cost(); - let free_balance_old = 12345u64 + swap_cost; + let free_balance_old = 12345 + swap_cost.to_u64(); let reserve = (stake_amount1 + stake_amount2) * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Setup initial state add_network(netuid, 13, 0); @@ -714,13 +714,13 @@ fn test_do_swap_coldkey_success() { <::RuntimeOrigin>::signed(old_coldkey), hotkey1, netuid, - stake_amount1 + stake_amount1.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey2, netuid, - stake_amount2 + stake_amount2.into() )); // Insert an Identity @@ -792,7 +792,7 @@ fn test_do_swap_coldkey_success() { ); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0 + TaoCurrency::ZERO ); assert_eq!( Alpha::::get((hotkey1, new_coldkey, netuid)), @@ -815,7 +815,7 @@ fn test_do_swap_coldkey_success() { // Verify balance transfer assert_eq!( SubtensorModule::get_coldkey_balance(&new_coldkey), - free_balance_old - swap_cost + free_balance_old - swap_cost.to_u64() ); assert_eq!(SubtensorModule::get_coldkey_balance(&old_coldkey), 0); @@ -854,9 +854,9 @@ fn test_swap_stake_for_coldkey() { let new_coldkey = U256::from(2); let hotkey1 = U256::from(3); let hotkey2 = U256::from(4); - let stake_amount1 = DefaultMinStake::::get() * 10; - let stake_amount2 = DefaultMinStake::::get() * 20; - let stake_amount3 = DefaultMinStake::::get() * 30; + let stake_amount1 = DefaultMinStake::::get().to_u64() * 10; + let stake_amount2 = DefaultMinStake::::get().to_u64() * 20; + let stake_amount3 = DefaultMinStake::::get().to_u64() * 30; let mut weight = Weight::zero(); // Setup initial state @@ -874,14 +874,14 @@ fn test_swap_stake_for_coldkey() { ); let reserve = (stake_amount1 + stake_amount2 + stake_amount3) * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Stake to hotkeys assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey1, netuid, - stake_amount1 + stake_amount1.into() )); let expected_stake_alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey1, @@ -893,7 +893,7 @@ fn test_swap_stake_for_coldkey() { <::RuntimeOrigin>::signed(old_coldkey), hotkey2, netuid, - stake_amount2 + stake_amount2.into() )); let expected_stake_alpha2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey2, @@ -909,7 +909,7 @@ fn test_swap_stake_for_coldkey() { <::RuntimeOrigin>::signed(new_coldkey), hotkey1, netuid, - stake_amount3 + stake_amount3.into() )); let expected_stake_alpha3 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey1, @@ -934,7 +934,7 @@ fn test_swap_stake_for_coldkey() { assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), initial_total_stake_for_old_coldkey + initial_total_stake_for_new_coldkey, - epsilon = 2 + epsilon = 2.into() ); // Verify ownership transfer @@ -1011,8 +1011,8 @@ fn test_swap_staking_hotkeys_for_coldkey() { let other_coldkey = U256::from(3); let hotkey1 = U256::from(4); let hotkey2 = U256::from(5); - let stake_amount1 = DefaultMinStake::::get() * 10; - let stake_amount2 = DefaultMinStake::::get() * 20; + let stake_amount1 = DefaultMinStake::::get().to_u64() * 10; + let stake_amount2 = DefaultMinStake::::get().to_u64() * 20; let mut weight = Weight::zero(); // Setup initial state @@ -1028,14 +1028,14 @@ fn test_swap_staking_hotkeys_for_coldkey() { register_ok_neuron(netuid, hotkey2, other_coldkey, 0); let reserve = (stake_amount1 + stake_amount2) * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Stake to hotkeys assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey1, netuid, - stake_amount1 + stake_amount1.into() )); let expected_stake_alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey1, @@ -1047,7 +1047,7 @@ fn test_swap_staking_hotkeys_for_coldkey() { <::RuntimeOrigin>::signed(old_coldkey), hotkey2, netuid, - stake_amount2 + stake_amount2.into() )); let expected_stake_alpha2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey2, @@ -1076,8 +1076,8 @@ fn test_swap_delegated_stake_for_coldkey() { let other_coldkey = U256::from(3); let hotkey1 = U256::from(4); let hotkey2 = U256::from(5); - let stake_amount1 = DefaultMinStake::::get() * 10; - let stake_amount2 = DefaultMinStake::::get() * 20; + let stake_amount1 = DefaultMinStake::::get().to_u64() * 10; + let stake_amount2 = DefaultMinStake::::get().to_u64() * 20; let mut weight = Weight::zero(); let netuid = NetUid::from(1); @@ -1087,7 +1087,7 @@ fn test_swap_delegated_stake_for_coldkey() { register_ok_neuron(netuid, hotkey2, other_coldkey, 0); let reserve = (stake_amount1 + stake_amount2) * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Notice hotkey1 and hotkey2 are Owned by other_coldkey // old_coldkey and new_coldkey therefore delegates stake to them @@ -1102,7 +1102,7 @@ fn test_swap_delegated_stake_for_coldkey() { <::RuntimeOrigin>::signed(old_coldkey), hotkey1, netuid, - stake_amount1 + stake_amount1.into() )); let expected_stake_alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey1, @@ -1110,12 +1110,12 @@ fn test_swap_delegated_stake_for_coldkey() { netuid, ); - let (expected_stake_alpha2, fee) = mock::swap_tao_to_alpha(netuid, stake_amount2); + let (expected_stake_alpha2, fee) = mock::swap_tao_to_alpha(netuid, stake_amount2.into()); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(old_coldkey), hotkey2, netuid, - stake_amount2 + stake_amount2.into() )); let expected_stake_alpha2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( &hotkey2, @@ -1185,7 +1185,7 @@ fn test_swap_delegated_stake_for_coldkey() { ); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0 + TaoCurrency::ZERO ); // Verify TotalHotkeyStake remains unchanged @@ -1248,8 +1248,8 @@ fn test_do_swap_coldkey_with_subnet_ownership() { let new_coldkey = U256::from(2); let hotkey = U256::from(3); let netuid = NetUid::from(1u16); - let stake_amount: u64 = 1000u64; - let swap_cost = SubtensorModule::get_key_swap_cost(); + let stake_amount = 1000; + let swap_cost = SubtensorModule::get_key_swap_cost().to_u64(); // Setup initial state add_network(netuid, 13, 0); @@ -1268,7 +1268,7 @@ fn test_do_swap_coldkey_with_subnet_ownership() { assert_ok!(SubtensorModule::do_swap_coldkey( &old_coldkey, &new_coldkey, - swap_cost + swap_cost.into() )); // Verify subnet ownership transfer @@ -1307,7 +1307,7 @@ fn test_coldkey_swap_total() { let netuid1 = NetUid::from(1); let netuid2 = NetUid::from(2); let netuid3 = NetUid::from(3); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get().to_u64() * 10; SubtensorModule::add_balance_to_coldkey_account(&coldkey, stake * 6); SubtensorModule::add_balance_to_coldkey_account(&delegate1, stake * 2); SubtensorModule::add_balance_to_coldkey_account(&delegate2, stake * 2); @@ -1317,9 +1317,9 @@ fn test_coldkey_swap_total() { SubtensorModule::add_balance_to_coldkey_account(&nominator3, stake * 2); let reserve = stake * 10; - mock::setup_reserves(netuid1, reserve, reserve.into()); - mock::setup_reserves(netuid2, reserve, reserve.into()); - mock::setup_reserves(netuid3, reserve, reserve.into()); + mock::setup_reserves(netuid1, reserve.into(), reserve.into()); + mock::setup_reserves(netuid2, reserve.into(), reserve.into()); + mock::setup_reserves(netuid3, reserve.into(), reserve.into()); // Setup initial state add_network(netuid1, 13, 0); @@ -1342,113 +1342,113 @@ fn test_coldkey_swap_total() { <::RuntimeOrigin>::signed(coldkey), hotkey1, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(coldkey), hotkey2, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(coldkey), hotkey3, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(coldkey), delegate1, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(coldkey), delegate2, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(coldkey), delegate3, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(delegate1), hotkey1, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(delegate2), hotkey2, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(delegate3), hotkey3, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(delegate1), delegate1, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(delegate2), delegate2, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(delegate3), delegate3, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(nominator1), hotkey1, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(nominator2), hotkey2, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(nominator3), hotkey3, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(nominator1), delegate1, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(nominator2), delegate2, netuid1, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(nominator3), delegate3, netuid1, - stake + stake.into() )); assert_eq!( @@ -1525,7 +1525,10 @@ fn test_coldkey_swap_total() { SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), ck_stake ); - assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&coldkey), 0); + assert_eq!( + SubtensorModule::get_total_stake_for_coldkey(&coldkey), + TaoCurrency::ZERO + ); // Check everything is swapped. assert_eq!( @@ -1661,11 +1664,11 @@ fn test_coldkey_delegations() { let delegate = U256::from(2); let netuid = NetUid::from(0); // Stake to 0 let netuid2 = NetUid::from(1); // Stake to 1 - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get().to_u64() * 10; let reserve = stake * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); - mock::setup_reserves(netuid2, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); + mock::setup_reserves(netuid2, reserve.into(), reserve.into()); add_network(netuid, 13, 0); // root add_network(netuid2, 13, 0); @@ -1680,13 +1683,13 @@ fn test_coldkey_delegations() { // since the reserves are equal and we stake the same amount to both networks, we can reuse // this values for different networks. but you should take it into account in case of tests // changes - let (expected_stake, fee) = mock::swap_tao_to_alpha(netuid, stake); + let (expected_stake, fee) = mock::swap_tao_to_alpha(netuid, stake.into()); assert_ok!(SubtensorModule::add_stake( <::RuntimeOrigin>::signed(coldkey), delegate, netuid, - stake + stake.into() )); // Add stake to netuid2 @@ -1694,7 +1697,7 @@ fn test_coldkey_delegations() { <::RuntimeOrigin>::signed(coldkey), delegate, netuid2, - stake + stake.into() )); // Perform the swap @@ -1706,17 +1709,20 @@ fn test_coldkey_delegations() { )); // Verify stake was moved for the delegate - let approx_total_stake = stake * 2 - fee * 2; + let approx_total_stake = TaoCurrency::from(stake * 2 - fee * 2); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&delegate), approx_total_stake, - epsilon = approx_total_stake / 100 + epsilon = approx_total_stake / 100.into() + ); + assert_eq!( + SubtensorModule::get_total_stake_for_coldkey(&coldkey), + TaoCurrency::ZERO ); - assert_eq!(SubtensorModule::get_total_stake_for_coldkey(&coldkey), 0); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_coldkey(&new_coldkey), approx_total_stake, - epsilon = approx_total_stake / 100 + epsilon = approx_total_stake / 100.into() ); assert_eq!( expected_stake, @@ -1746,7 +1752,7 @@ fn test_schedule_swap_coldkey_success() { let swap_cost = SubtensorModule::get_key_swap_cost(); // Add balance to the old coldkey account - SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost + 1_000); + SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost.to_u64() + 1_000); // Schedule the coldkey swap assert_ok!(SubtensorModule::schedule_swap_coldkey( @@ -1785,7 +1791,7 @@ fn test_schedule_swap_coldkey_duplicate() { let swap_cost = SubtensorModule::get_key_swap_cost(); - SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost + 2_000); + SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost.to_u64() + 2_000); assert_ok!(SubtensorModule::schedule_swap_coldkey( <::RuntimeOrigin>::signed(old_coldkey), @@ -1811,10 +1817,10 @@ fn test_schedule_swap_coldkey_execution() { let new_coldkey = U256::from(2); let hotkey = U256::from(3); let netuid = NetUid::from(1u16); - let stake_amount = DefaultMinStake::::get() * 10; + let stake_amount = DefaultMinStake::::get().to_u64() * 10; let reserve = stake_amount * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); add_network(netuid, 13, 0); register_ok_neuron(netuid, hotkey, old_coldkey, 0); @@ -1823,7 +1829,7 @@ fn test_schedule_swap_coldkey_execution() { <::RuntimeOrigin>::signed(old_coldkey), hotkey, netuid, - stake_amount + stake_amount.into() )); // Check initial ownership @@ -1883,7 +1889,7 @@ fn test_schedule_swap_coldkey_execution() { ); assert_eq!( SubtensorModule::get_total_stake_for_coldkey(&old_coldkey), - 0, + TaoCurrency::ZERO, "Old coldkey still has stake" ); @@ -1911,7 +1917,7 @@ fn test_direct_swap_coldkey_call_fails() { <::RuntimeOrigin>::signed(old_coldkey), old_coldkey, new_coldkey, - 0 + TaoCurrency::ZERO ), BadOrigin ); @@ -1928,7 +1934,7 @@ fn test_schedule_swap_coldkey_with_pending_swap() { let swap_cost = SubtensorModule::get_key_swap_cost(); - SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost + 1_000); + SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost.to_u64() + 1_000); assert_ok!(SubtensorModule::schedule_swap_coldkey( <::RuntimeOrigin>::signed(old_coldkey), @@ -1957,7 +1963,10 @@ fn test_schedule_swap_coldkey_failure_and_reschedule() { let swap_cost = SubtensorModule::get_key_swap_cost(); // Two swaps - SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, swap_cost + 1_000 * 2); + SubtensorModule::add_balance_to_coldkey_account( + &old_coldkey, + swap_cost.to_u64() + 1_000 * 2, + ); assert_ok!(SubtensorModule::schedule_swap_coldkey( <::RuntimeOrigin>::signed(old_coldkey), @@ -2018,14 +2027,14 @@ fn test_coldkey_swap_delegate_identity_updated() { let new_coldkey = U256::from(2); let netuid = NetUid::from(1); - let burn_cost = 10; + let burn_cost = TaoCurrency::from(10); let tempo = 1; SubtensorModule::set_burn(netuid, burn_cost); add_network(netuid, tempo, 0); SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, 100_000_000_000); - mock::setup_reserves(netuid, 1_000_000_000_000, 1_000_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000_000.into(), 1_000_000_000_000.into()); assert_ok!(SubtensorModule::burned_register( <::RuntimeOrigin>::signed(old_coldkey), @@ -2071,14 +2080,14 @@ fn test_coldkey_swap_no_identity_no_changes() { let new_coldkey = U256::from(2); let netuid = NetUid::from(1); - let burn_cost = 10; + let burn_cost = TaoCurrency::from(10); let tempo = 1; SubtensorModule::set_burn(netuid, burn_cost); add_network(netuid, tempo, 0); SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, 100_000_000_000); - mock::setup_reserves(netuid, 1_000_000_000_000, 1_000_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000_000.into(), 1_000_000_000_000.into()); assert_ok!(SubtensorModule::burned_register( <::RuntimeOrigin>::signed(old_coldkey), @@ -2109,13 +2118,13 @@ fn test_coldkey_swap_no_identity_no_changes_newcoldkey_exists() { let new_coldkey = U256::from(4); let netuid = NetUid::from(1); - let burn_cost = 10; + let burn_cost = TaoCurrency::from(10); let tempo = 1; SubtensorModule::set_burn(netuid, burn_cost); add_network(netuid, tempo, 0); SubtensorModule::add_balance_to_coldkey_account(&old_coldkey, 100_000_000_000); - mock::setup_reserves(netuid, 1_000_000_000_000, 1_000_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000_000.into(), 1_000_000_000_000.into()); assert_ok!(SubtensorModule::burned_register( <::RuntimeOrigin>::signed(old_coldkey), @@ -2189,7 +2198,7 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() { let stake = 100_000_000_000; let reserve = stake * 100; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); let who = coldkey; // The coldkey signs this transaction @@ -2224,7 +2233,7 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() { <::RuntimeOrigin>::signed(who), hotkey, netuid, - stake + stake.into() )); // Schedule the coldkey for a swap @@ -2246,7 +2255,7 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() { let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake { hotkey, netuid, - amount_staked: stake, + amount_staked: stake.into(), }); let result = extension.validate( RawOrigin::Signed(who).into(), @@ -2268,8 +2277,8 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() { let call = RuntimeCall::SubtensorModule(SubtensorCall::add_stake_limit { hotkey, netuid, - amount_staked: stake, - limit_price: stake, + amount_staked: stake.into(), + limit_price: stake.into(), allow_partial: false, }); let result = extension.validate( @@ -2317,7 +2326,7 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() { origin_netuid: netuid, destination_netuid: netuid, alpha_amount: stake.into(), - limit_price: stake, + limit_price: stake.into(), allow_partial: false, }); let result = extension.validate( @@ -2471,7 +2480,7 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() { let call = RuntimeCall::SubtensorModule(SubtensorCall::remove_stake { hotkey, netuid, - amount_unstaked: (DefaultMinStake::::get() * 2).into(), + amount_unstaked: (DefaultMinStake::::get().to_u64() * 2).into(), }); let result = extension.validate( RawOrigin::Signed(who).into(), @@ -2489,8 +2498,8 @@ fn test_coldkey_in_swap_schedule_prevents_funds_usage() { let call = RuntimeCall::SubtensorModule(SubtensorCall::remove_stake_limit { hotkey, netuid, - amount_unstaked: (DefaultMinStake::::get() * 2).into(), - limit_price: 123456789, // should be low enough + amount_unstaked: (DefaultMinStake::::get().to_u64() * 2).into(), + limit_price: 123456789.into(), // should be low enough allow_partial: true, }); let result = extension.validate( @@ -2524,7 +2533,7 @@ fn test_coldkey_in_swap_schedule_prevents_critical_calls() { let stake = 100_000_000_000; let reserve = stake * 10; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); let who = coldkey; // The coldkey signs this transaction @@ -2546,7 +2555,7 @@ fn test_coldkey_in_swap_schedule_prevents_critical_calls() { <::RuntimeOrigin>::signed(who), hotkey, netuid, - stake + stake.into() )); // Schedule the coldkey for a swap diff --git a/pallets/subtensor/src/tests/swap_hotkey.rs b/pallets/subtensor/src/tests/swap_hotkey.rs index 506bd77a9d..1fbae1708f 100644 --- a/pallets/subtensor/src/tests/swap_hotkey.rs +++ b/pallets/subtensor/src/tests/swap_hotkey.rs @@ -8,7 +8,7 @@ use frame_system::{Config, RawOrigin}; use sp_core::{Get, H160, H256, U256}; use sp_runtime::SaturatedConversion; use substrate_fixed::types::U64F64; -use subtensor_runtime_common::AlphaCurrency; +use subtensor_runtime_common::{AlphaCurrency, TaoCurrency}; use subtensor_swap_interface::SwapHandler; use super::mock; @@ -67,25 +67,25 @@ fn test_swap_total_hotkey_stake() { let old_hotkey = U256::from(1); let new_hotkey = U256::from(2); let coldkey = U256::from(3); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get().to_u64() * 10; let mut weight = Weight::zero(); //add network let netuid = add_dynamic_network(&old_hotkey, &coldkey); - mock::setup_reserves(netuid, amount * 100, (amount * 100).into()); + mock::setup_reserves(netuid, (amount * 100).into(), (amount * 100).into()); // Give it some $$$ in his coldkey balance SubtensorModule::add_balance_to_coldkey_account(&coldkey, amount); // Add stake - let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, amount); + let (expected_alpha, _) = mock::swap_tao_to_alpha(netuid, amount.into()); assert!(!expected_alpha.is_zero()); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey), old_hotkey, netuid, - amount + amount.into() )); // Check if stake has increased @@ -95,8 +95,8 @@ fn test_swap_total_hotkey_stake() { ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&new_hotkey), - 0, - epsilon = 1, + TaoCurrency::ZERO, + epsilon = 1.into(), ); // Swap hotkey @@ -110,8 +110,8 @@ fn test_swap_total_hotkey_stake() { // Verify that total hotkey stake swapped assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), - 0, - epsilon = 1, + TaoCurrency::ZERO, + epsilon = 1.into(), ); assert_eq!( TotalHotkeyAlpha::::get(new_hotkey, netuid), @@ -439,13 +439,13 @@ fn test_swap_hotkey_with_multiple_coldkeys() { RuntimeOrigin::signed(coldkey1), old_hotkey, netuid, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey2), old_hotkey, netuid, - stake / 2 + (stake / 2).into() )); let stake1_before = SubtensorModule::get_total_stake_for_coldkey(&coldkey1); let stake2_before = SubtensorModule::get_total_stake_for_coldkey(&coldkey2); @@ -514,7 +514,7 @@ fn test_swap_staking_hotkeys_multiple_coldkeys() { let coldkey2 = U256::from(4); let staker5 = U256::from(5); let mut weight = Weight::zero(); - let stake = 1_000_000_000; + let stake = TaoCurrency::from(1_000_000_000); // Set up initial state StakingHotkeys::::insert(coldkey1, vec![old_hotkey]); @@ -525,11 +525,11 @@ fn test_swap_staking_hotkeys_multiple_coldkeys() { SubtensorModule::create_account_if_non_existent(&coldkey1, &old_hotkey); SubtensorModule::add_balance_to_coldkey_account( &coldkey1, - stake + ExistentialDeposit::get(), + stake.to_u64() + ExistentialDeposit::get(), ); SubtensorModule::add_balance_to_coldkey_account( &coldkey2, - stake + ExistentialDeposit::get(), + stake.to_u64() + ExistentialDeposit::get(), ); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey1), @@ -606,7 +606,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { let coldkey2 = U256::from(4); let netuid1 = NetUid::from(1); let netuid2 = NetUid::from(2); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get().to_u64() * 10; let mut weight = Weight::zero(); // Set up initial state @@ -615,8 +615,8 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { register_ok_neuron(netuid1, old_hotkey, coldkey1, 1234); register_ok_neuron(netuid2, old_hotkey, coldkey1, 1234); - mock::setup_reserves(netuid1, stake * 100, (stake * 100).into()); - mock::setup_reserves(netuid2, stake * 100, (stake * 100).into()); + mock::setup_reserves(netuid1, (stake * 100).into(), (stake * 100).into()); + mock::setup_reserves(netuid2, (stake * 100).into(), (stake * 100).into()); // Add balance to both coldkeys SubtensorModule::add_balance_to_coldkey_account(&coldkey1, stake + 1_000); @@ -627,7 +627,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { <::RuntimeOrigin>::signed(coldkey1), old_hotkey, netuid1, - stake + stake.into() )); // Stake with coldkey2 also @@ -635,7 +635,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { <::RuntimeOrigin>::signed(coldkey2), old_hotkey, netuid2, - stake + stake.into() )); let ck1_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -651,7 +651,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { assert!(!ck1_stake.is_zero()); assert!(!ck2_stake.is_zero()); let total_hk_stake = SubtensorModule::get_total_stake_for_hotkey(&old_hotkey); - assert!(total_hk_stake > 0); + assert!(!total_hk_stake.is_zero()); assert_ok!(SubtensorModule::perform_hotkey_swap_on_all_subnets( &old_hotkey, @@ -724,7 +724,10 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { SubtensorModule::get_total_stake_for_hotkey(&new_hotkey), total_hk_stake ); - assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), 0); + assert_eq!( + SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), + TaoCurrency::ZERO + ); }); } @@ -894,7 +897,7 @@ fn test_swap_stake_success() { TotalHotkeyShares::::insert(old_hotkey, netuid, shares); Alpha::::insert((old_hotkey, coldkey, netuid), U64F64::from_num(amount)); AlphaDividendsPerSubnet::::insert(netuid, old_hotkey, AlphaCurrency::from(amount)); - TaoDividendsPerSubnet::::insert(netuid, old_hotkey, amount); + TaoDividendsPerSubnet::::insert(netuid, old_hotkey, TaoCurrency::from(amount)); // Perform the swap SubtensorModule::perform_hotkey_swap_on_all_subnets( @@ -945,10 +948,13 @@ fn test_swap_stake_success() { AlphaDividendsPerSubnet::::get(netuid, new_hotkey), amount.into() ); - assert_eq!(TaoDividendsPerSubnet::::get(netuid, old_hotkey), 0); + assert_eq!( + TaoDividendsPerSubnet::::get(netuid, old_hotkey), + TaoCurrency::ZERO + ); assert_eq!( TaoDividendsPerSubnet::::get(netuid, new_hotkey), - amount + amount.into() ); }); } @@ -1041,7 +1047,7 @@ fn test_swap_hotkey_error_cases() { Error::::NotEnoughBalanceToPaySwapHotKey ); - let initial_balance = SubtensorModule::get_key_swap_cost() + 1000; + let initial_balance = SubtensorModule::get_key_swap_cost().to_u64() + 1000; SubtensorModule::add_balance_to_coldkey_account(&coldkey, initial_balance); // Test new hotkey same as old @@ -1088,7 +1094,10 @@ fn test_swap_hotkey_error_cases() { )); // Check balance after swap - assert_eq!(Balances::free_balance(coldkey), initial_balance - swap_cost); + assert_eq!( + Balances::free_balance(coldkey), + initial_balance - swap_cost.to_u64() + ); }); } diff --git a/pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs b/pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs index de012d4074..1acc8b98a6 100644 --- a/pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs +++ b/pallets/subtensor/src/tests/swap_hotkey_with_subnet.rs @@ -5,7 +5,7 @@ use codec::Encode; use frame_support::weights::Weight; use frame_support::{assert_err, assert_noop, assert_ok}; use frame_system::{Config, RawOrigin}; -use subtensor_runtime_common::{AlphaCurrency, Currency}; +use subtensor_runtime_common::{AlphaCurrency, Currency, TaoCurrency}; use super::mock::*; use crate::*; @@ -69,7 +69,7 @@ fn test_swap_total_hotkey_stake() { let old_hotkey = U256::from(1); let new_hotkey = U256::from(2); let coldkey = U256::from(3); - let amount = DefaultMinStake::::get() * 10; + let amount = DefaultMinStake::::get().to_u64() * 10; let fee = (amount as f64 * 0.003) as u64; @@ -84,19 +84,19 @@ fn test_swap_total_hotkey_stake() { RuntimeOrigin::signed(coldkey), old_hotkey, netuid, - amount + amount.into() )); // Check if stake has increased assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), - amount - fee, - epsilon = amount / 100, + (amount - fee).into(), + epsilon = TaoCurrency::from(amount / 100), ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&new_hotkey), - 0, - epsilon = 1, + TaoCurrency::ZERO, + epsilon = 1.into(), ); // Swap hotkey @@ -111,13 +111,13 @@ fn test_swap_total_hotkey_stake() { // Verify that total hotkey stake swapped assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), - 0, - epsilon = 1, + TaoCurrency::ZERO, + epsilon = 1.into(), ); assert_abs_diff_eq!( SubtensorModule::get_total_stake_for_hotkey(&new_hotkey), - amount - fee, - epsilon = amount / 100, + TaoCurrency::from(amount - fee), + epsilon = TaoCurrency::from(amount / 100), ); }); } @@ -450,13 +450,13 @@ fn test_swap_hotkey_with_multiple_coldkeys() { RuntimeOrigin::signed(coldkey1), old_hotkey, netuid, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey2), old_hotkey, netuid, - stake / 2 + TaoCurrency::from(stake / 2) )); let stake1_before = SubtensorModule::get_total_stake_for_coldkey(&coldkey1); let stake2_before = SubtensorModule::get_total_stake_for_coldkey(&coldkey2); @@ -560,13 +560,13 @@ fn test_swap_staking_hotkeys_multiple_coldkeys() { RuntimeOrigin::signed(coldkey1), old_hotkey, netuid, - stake + stake.into() )); assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey2), old_hotkey, netuid, - stake + stake.into() )); System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get()); @@ -635,7 +635,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { let coldkey2 = U256::from(5); let netuid1 = NetUid::from(1); let netuid2 = NetUid::from(2); - let stake = DefaultMinStake::::get() * 10; + let stake = DefaultMinStake::::get().to_u64() * 10; // Set up initial state add_network(netuid1, 1, 1); @@ -652,7 +652,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { <::RuntimeOrigin>::signed(coldkey1), old_hotkey, netuid1, - stake + stake.into() )); // Stake with coldkey2 also @@ -660,7 +660,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { <::RuntimeOrigin>::signed(coldkey2), old_hotkey, netuid2, - stake + stake.into() )); let ck1_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet( @@ -676,7 +676,7 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { assert!(!ck1_stake.is_zero()); assert!(!ck2_stake.is_zero()); let total_hk_stake = SubtensorModule::get_total_stake_for_hotkey(&old_hotkey); - assert!(total_hk_stake > 0); + assert!(!total_hk_stake.is_zero()); System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get()); assert_ok!(SubtensorModule::do_swap_hotkey( @@ -764,7 +764,10 @@ fn test_swap_hotkey_with_multiple_coldkeys_and_subnets() { + SubtensorModule::get_total_stake_for_hotkey(&new_hotkey_2), total_hk_stake ); - assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), 0); + assert_eq!( + SubtensorModule::get_total_stake_for_hotkey(&old_hotkey), + TaoCurrency::ZERO + ); }); } @@ -947,7 +950,7 @@ fn test_swap_stake_success() { TotalHotkeyShares::::insert(old_hotkey, netuid, U64F64::from_num(shares)); Alpha::::insert((old_hotkey, coldkey, netuid), U64F64::from_num(amount)); AlphaDividendsPerSubnet::::insert(netuid, old_hotkey, AlphaCurrency::from(amount)); - TaoDividendsPerSubnet::::insert(netuid, old_hotkey, amount); + TaoDividendsPerSubnet::::insert(netuid, old_hotkey, TaoCurrency::from(amount)); // Perform the swap System::set_block_number(System::block_number() + HotkeySwapOnSubnetInterval::get()); @@ -999,10 +1002,13 @@ fn test_swap_stake_success() { AlphaDividendsPerSubnet::::get(netuid, new_hotkey), AlphaCurrency::from(amount) ); - assert_eq!(TaoDividendsPerSubnet::::get(netuid, old_hotkey), 0); + assert_eq!( + TaoDividendsPerSubnet::::get(netuid, old_hotkey), + TaoCurrency::ZERO + ); assert_eq!( TaoDividendsPerSubnet::::get(netuid, new_hotkey), - amount + amount.into() ); }); } @@ -1035,7 +1041,7 @@ fn test_swap_hotkey_error_cases() { Error::::NotEnoughBalanceToPaySwapHotKey ); - let initial_balance = SubtensorModule::get_key_swap_cost() + 1000; + let initial_balance = SubtensorModule::get_key_swap_cost().to_u64() + 1000; SubtensorModule::add_balance_to_coldkey_account(&coldkey, initial_balance); // Test new hotkey same as old @@ -1553,7 +1559,7 @@ fn test_swap_hotkey_registered_on_other_subnet() { TotalNetworks::::put(1); LastTxBlock::::insert(coldkey, 0); - let initial_balance = SubtensorModule::get_key_swap_cost() + 1000; + let initial_balance = SubtensorModule::get_key_swap_cost().to_u64() + 1000; SubtensorModule::add_balance_to_coldkey_account(&coldkey, initial_balance); // Test new hotkey already registered on other subnet diff --git a/pallets/subtensor/src/tests/weights.rs b/pallets/subtensor/src/tests/weights.rs index cec33a940c..1669ce2b09 100644 --- a/pallets/subtensor/src/tests/weights.rs +++ b/pallets/subtensor/src/tests/weights.rs @@ -18,6 +18,7 @@ use sp_runtime::{ }; use sp_std::collections::vec_deque::VecDeque; use substrate_fixed::types::I32F32; +use subtensor_runtime_common::TaoCurrency; use subtensor_swap_interface::SwapHandler; use tle::{ curves::drand::TinyBLS381, @@ -92,9 +93,9 @@ fn test_set_rootweights_validate() { SubtensorModule::add_balance_to_coldkey_account(&hotkey, u64::MAX); - let min_stake = 500_000_000_000; + let min_stake = TaoCurrency::from(500_000_000_000); // Set the minimum stake - SubtensorModule::set_stake_threshold(min_stake); + SubtensorModule::set_stake_threshold(min_stake.into()); // Verify stake is less than minimum assert!(SubtensorModule::get_total_stake_for_hotkey(&hotkey) < min_stake); @@ -124,7 +125,7 @@ fn test_set_rootweights_validate() { RuntimeOrigin::signed(hotkey), hotkey, netuid, - min_stake + fee + min_stake + fee.into() )); // Verify stake is equal to minimum @@ -151,7 +152,7 @@ fn test_set_rootweights_validate() { RuntimeOrigin::signed(hotkey), hotkey, netuid, - DefaultMinStake::::get() * 10 + DefaultMinStake::::get() * 10.into() )); // Verify stake is more than minimum @@ -232,7 +233,7 @@ fn test_commit_weights_validate() { let min_stake = 500_000_000_000; let reserve = min_stake * 1000; - mock::setup_reserves(netuid, reserve, reserve.into()); + mock::setup_reserves(netuid, reserve.into(), reserve.into()); // Stake some TAO and read what get_total_stake_for_hotkey it gets // It will be a different value due to the slippage @@ -240,12 +241,12 @@ fn test_commit_weights_validate() { RuntimeOrigin::signed(hotkey), hotkey, netuid, - min_stake + min_stake.into() )); let min_stake_with_slippage = SubtensorModule::get_total_stake_for_hotkey(&hotkey); // Set the minimum stake above what hotkey has - SubtensorModule::set_stake_threshold(min_stake_with_slippage + 1); + SubtensorModule::set_stake_threshold(min_stake_with_slippage.to_u64() + 1); // Submit to the signed extension validate function let info = crate::DispatchInfoOf::<::RuntimeCall>::default(); @@ -268,7 +269,7 @@ fn test_commit_weights_validate() { ); // Set the minimum stake equal to what hotkey has - SubtensorModule::set_stake_threshold(min_stake_with_slippage); + SubtensorModule::set_stake_threshold(min_stake_with_slippage.into()); // Submit to the signed extension validate function let result_min_stake = extension.validate( @@ -288,7 +289,7 @@ fn test_commit_weights_validate() { RuntimeOrigin::signed(hotkey), hotkey, netuid, - DefaultMinStake::::get() * 10 + DefaultMinStake::::get() * 10.into() )); // Verify stake is more than minimum @@ -355,17 +356,17 @@ fn test_set_weights_validate() { // Create netuid add_network(netuid, 1, 0); - mock::setup_reserves(netuid, 1_000_000_000_000, 1_000_000_000_000.into()); + mock::setup_reserves(netuid, 1_000_000_000_000.into(), 1_000_000_000_000.into()); // Register the hotkey SubtensorModule::append_neuron(netuid, &hotkey, 0); crate::Owner::::insert(hotkey, coldkey); SubtensorModule::add_balance_to_coldkey_account(&hotkey, u64::MAX); - let min_stake = 500_000_000_000; + let min_stake = TaoCurrency::from(500_000_000_000); // Set the minimum stake - SubtensorModule::set_stake_threshold(min_stake); + SubtensorModule::set_stake_threshold(min_stake.into()); // Verify stake is less than minimum assert!(SubtensorModule::get_total_stake_for_hotkey(&hotkey) < min_stake); @@ -390,18 +391,20 @@ fn test_set_weights_validate() { ); // Increase the stake and make it to be equal to the minimum threshold - let fee = - ::SwapInterface::approx_fee_amount(netuid.into(), min_stake); + let fee = ::SwapInterface::approx_fee_amount( + netuid.into(), + min_stake.into(), + ); assert_ok!(SubtensorModule::do_add_stake( RuntimeOrigin::signed(hotkey), hotkey, netuid, - min_stake + fee + min_stake + fee.into() )); let min_stake_with_slippage = SubtensorModule::get_total_stake_for_hotkey(&hotkey); // Set the minimum stake to what the hotkey has - SubtensorModule::set_stake_threshold(min_stake_with_slippage); + SubtensorModule::set_stake_threshold(min_stake_with_slippage.into()); // Submit to the signed extension validate function let result_min_stake = extension.validate( @@ -452,9 +455,9 @@ fn test_reveal_weights_validate() { crate::Owner::::insert(hotkey, coldkey); SubtensorModule::add_balance_to_coldkey_account(&hotkey, u64::MAX); - let min_stake = 500_000_000_000; + let min_stake = TaoCurrency::from(500_000_000_000); // Set the minimum stake - SubtensorModule::set_stake_threshold(min_stake); + SubtensorModule::set_stake_threshold(min_stake.into()); // Verify stake is less than minimum assert!(SubtensorModule::get_total_stake_for_hotkey(&hotkey) < min_stake); @@ -484,7 +487,7 @@ fn test_reveal_weights_validate() { RuntimeOrigin::signed(hotkey), hotkey, netuid, - min_stake + fee + min_stake + fee.into() )); // Verify stake is equal to minimum @@ -511,7 +514,7 @@ fn test_reveal_weights_validate() { RuntimeOrigin::signed(hotkey), hotkey, netuid, - DefaultMinStake::::get() * 10 + DefaultMinStake::::get() * 10.into() )); // Verify stake is more than minimum @@ -621,14 +624,14 @@ fn test_set_stake_threshold_failed() { RuntimeOrigin::signed(hotkey), hotkey, netuid, - 19_000_000_000_000 + 19_000_000_000_000.into() )); assert!(!SubtensorModule::check_weights_min_stake(&hotkey, netuid)); assert_ok!(SubtensorModule::do_add_stake( RuntimeOrigin::signed(hotkey), hotkey, netuid, - 20_000_000_000_000 + 20_000_000_000_000.into() )); assert!(SubtensorModule::check_weights_min_stake(&hotkey, netuid)); @@ -649,7 +652,7 @@ fn test_set_stake_threshold_failed() { RuntimeOrigin::signed(hotkey), hotkey, netuid, - 100_000_000_000_000 + 100_000_000_000_000.into() )); assert_ok!(SubtensorModule::set_weights( RuntimeOrigin::signed(hotkey), diff --git a/pallets/subtensor/src/utils/misc.rs b/pallets/subtensor/src/utils/misc.rs index 2481d56a6c..0cd708ffbe 100644 --- a/pallets/subtensor/src/utils/misc.rs +++ b/pallets/subtensor/src/utils/misc.rs @@ -8,7 +8,7 @@ use sp_core::Get; use sp_core::U256; use sp_runtime::Saturating; use substrate_fixed::types::{I32F32, U96F32}; -use subtensor_runtime_common::{AlphaCurrency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; impl Pallet { pub fn ensure_subnet_owner_or_root( @@ -71,7 +71,7 @@ impl Pallet { // ======================== // ==== Global Getters ==== // ======================== - pub fn get_total_issuance() -> u64 { + pub fn get_total_issuance() -> TaoCurrency { TotalIssuance::::get() } pub fn get_current_block_as_u64() -> u64 { @@ -270,25 +270,25 @@ impl Pallet { // ======================== // === Token Management === // ======================== - pub fn burn_tokens(amount: u64) { + pub fn burn_tokens(amount: TaoCurrency) { TotalIssuance::::put(TotalIssuance::::get().saturating_sub(amount)); } - pub fn coinbase(amount: u64) { + pub fn coinbase(amount: TaoCurrency) { TotalIssuance::::put(TotalIssuance::::get().saturating_add(amount)); } - pub fn set_subnet_locked_balance(netuid: NetUid, amount: u64) { + pub fn set_subnet_locked_balance(netuid: NetUid, amount: TaoCurrency) { SubnetLocked::::insert(netuid, amount); } - pub fn get_subnet_locked_balance(netuid: NetUid) -> u64 { + pub fn get_subnet_locked_balance(netuid: NetUid) -> TaoCurrency { SubnetLocked::::get(netuid) } - pub fn get_total_subnet_locked() -> u64 { + pub fn get_total_subnet_locked() -> TaoCurrency { let mut total_subnet_locked: u64 = 0; for (_, locked) in SubnetLocked::::iter() { - total_subnet_locked.saturating_accrue(locked); + total_subnet_locked.saturating_accrue(locked.into()); } - total_subnet_locked + total_subnet_locked.into() } // ======================== @@ -528,25 +528,25 @@ impl Pallet { )); } - pub fn get_burn_as_u64(netuid: NetUid) -> u64 { + pub fn get_burn(netuid: NetUid) -> TaoCurrency { Burn::::get(netuid) } - pub fn set_burn(netuid: NetUid, burn: u64) { + pub fn set_burn(netuid: NetUid, burn: TaoCurrency) { Burn::::insert(netuid, burn); } - pub fn get_min_burn_as_u64(netuid: NetUid) -> u64 { + pub fn get_min_burn(netuid: NetUid) -> TaoCurrency { MinBurn::::get(netuid) } - pub fn set_min_burn(netuid: NetUid, min_burn: u64) { + pub fn set_min_burn(netuid: NetUid, min_burn: TaoCurrency) { MinBurn::::insert(netuid, min_burn); Self::deposit_event(Event::MinBurnSet(netuid, min_burn)); } - pub fn get_max_burn_as_u64(netuid: NetUid) -> u64 { + pub fn get_max_burn(netuid: NetUid) -> TaoCurrency { MaxBurn::::get(netuid) } - pub fn set_max_burn(netuid: NetUid, max_burn: u64) { + pub fn set_max_burn(netuid: NetUid, max_burn: TaoCurrency) { MaxBurn::::insert(netuid, max_burn); Self::deposit_event(Event::MaxBurnSet(netuid, max_burn)); } @@ -627,18 +627,18 @@ impl Pallet { StakingHotkeys::::get(coldkey) } - pub fn set_total_issuance(total_issuance: u64) { + pub fn set_total_issuance(total_issuance: TaoCurrency) { TotalIssuance::::put(total_issuance); } - pub fn get_rao_recycled(netuid: NetUid) -> u64 { + pub fn get_rao_recycled(netuid: NetUid) -> TaoCurrency { RAORecycledForRegistration::::get(netuid) } - pub fn set_rao_recycled(netuid: NetUid, rao_recycled: u64) { + pub fn set_rao_recycled(netuid: NetUid, rao_recycled: TaoCurrency) { RAORecycledForRegistration::::insert(netuid, rao_recycled); Self::deposit_event(Event::RAORecycledForRegistrationSet(netuid, rao_recycled)); } - pub fn increase_rao_recycled(netuid: NetUid, inc_rao_recycled: u64) { + pub fn increase_rao_recycled(netuid: NetUid, inc_rao_recycled: TaoCurrency) { let curr_rao_recycled = Self::get_rao_recycled(netuid); let rao_recycled = curr_rao_recycled.saturating_add(inc_rao_recycled); Self::set_rao_recycled(netuid, rao_recycled); @@ -678,6 +678,7 @@ impl Pallet { // Return the default minimum stake multiplied by factor // 21M * 10^9 * 10^6 fits u64, hence no need for fixed type usage here DefaultMinStake::::get() + .to_u64() .saturating_mul(factor) .safe_div(1_000_000) } @@ -686,8 +687,8 @@ impl Pallet { NominatorMinRequiredStake::::put(min_stake); } - pub fn get_key_swap_cost() -> u64 { - T::KeySwapCost::get() + pub fn get_key_swap_cost() -> TaoCurrency { + T::KeySwapCost::get().into() } pub fn get_alpha_values(netuid: NetUid) -> (u16, u16) { diff --git a/pallets/subtensor/src/utils/try_state.rs b/pallets/subtensor/src/utils/try_state.rs index 6136d7bd50..4ade47eeef 100644 --- a/pallets/subtensor/src/utils/try_state.rs +++ b/pallets/subtensor/src/utils/try_state.rs @@ -10,14 +10,15 @@ impl Pallet { let currency_issuance = ::Currency::total_issuance(); // Calculate the expected total issuance - let expected_total_issuance = currency_issuance.saturating_add(TotalStake::::get()); + let expected_total_issuance = + currency_issuance.saturating_add(TotalStake::::get().into()); // Verify the diff between calculated TI and actual TI is less than delta // // These values can be off slightly due to float rounding errors. // They are corrected every runtime upgrade. let delta = 1000; - let total_issuance = TotalIssuance::::get(); + let total_issuance = TotalIssuance::::get().to_u64(); let diff = if total_issuance > expected_total_issuance { total_issuance.checked_sub(expected_total_issuance) @@ -38,16 +39,17 @@ impl Pallet { #[allow(dead_code)] pub(crate) fn check_total_stake() -> Result<(), sp_runtime::TryRuntimeError> { // Calculate the total staked amount - let total_staked = SubnetTAO::::iter().fold(0u64, |acc, (netuid, stake)| { - let acc = acc.saturating_add(stake); + let total_staked = + SubnetTAO::::iter().fold(TaoCurrency::ZERO, |acc, (netuid, stake)| { + let acc = acc.saturating_add(stake); - if netuid.is_root() { - // root network doesn't have initial pool TAO - acc - } else { - acc.saturating_sub(Self::get_network_min_lock()) - } - }); + if netuid.is_root() { + // root network doesn't have initial pool TAO + acc + } else { + acc.saturating_sub(Self::get_network_min_lock()) + } + }); log::warn!( "total_staked: {}, TotalStake: {}", diff --git a/pallets/swap-interface/src/lib.rs b/pallets/swap-interface/src/lib.rs index fce3d53f18..a0b39e151f 100644 --- a/pallets/swap-interface/src/lib.rs +++ b/pallets/swap-interface/src/lib.rs @@ -2,7 +2,7 @@ use frame_support::pallet_prelude::*; use substrate_fixed::types::U96F32; -use subtensor_runtime_common::{AlphaCurrency, NetUid}; +use subtensor_runtime_common::{AlphaCurrency, NetUid, TaoCurrency}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum OrderType { @@ -28,7 +28,11 @@ pub trait SwapHandler { fn current_alpha_price(netuid: NetUid) -> U96F32; fn max_price() -> u64; fn min_price() -> u64; - fn adjust_protocol_liquidity(netuid: NetUid, tao_delta: u64, alpha_delta: AlphaCurrency); + fn adjust_protocol_liquidity( + netuid: NetUid, + tao_delta: TaoCurrency, + alpha_delta: AlphaCurrency, + ); fn is_user_liquidity_enabled(netuid: NetUid) -> bool; } diff --git a/pallets/swap/src/mock.rs b/pallets/swap/src/mock.rs index 014c6ce4fe..78a8f925c8 100644 --- a/pallets/swap/src/mock.rs +++ b/pallets/swap/src/mock.rs @@ -14,7 +14,7 @@ use sp_runtime::{ BuildStorage, traits::{BlakeTwo256, IdentityLookup}, }; -use subtensor_runtime_common::{AlphaCurrency, BalanceOps, NetUid, SubnetInfo}; +use subtensor_runtime_common::{AlphaCurrency, BalanceOps, NetUid, SubnetInfo, TaoCurrency}; use crate::pallet::EnabledUserLiquidity; @@ -87,12 +87,13 @@ parameter_types! { pub struct MockLiquidityProvider; impl SubnetInfo for MockLiquidityProvider { - fn tao_reserve(netuid: NetUid) -> u64 { + fn tao_reserve(netuid: NetUid) -> TaoCurrency { match netuid.into() { 123u16 => 10_000, WRAPPING_FEES_NETUID => 100_000_000_000, _ => 1_000_000_000_000, } + .into() } fn alpha_reserve(netuid: NetUid) -> AlphaCurrency { @@ -119,13 +120,14 @@ impl SubnetInfo for MockLiquidityProvider { pub struct MockBalanceOps; impl BalanceOps for MockBalanceOps { - fn tao_balance(account_id: &AccountId) -> u64 { + fn tao_balance(account_id: &AccountId) -> TaoCurrency { match *account_id { OK_COLDKEY_ACCOUNT_ID => 100_000_000_000_000, OK_COLDKEY_ACCOUNT_ID_2 => 100_000_000_000_000, OK_COLDKEY_ACCOUNT_ID_RICH => 900_000_000_000_000_000_u64, _ => 1_000_000_000, } + .into() } fn alpha_balance( @@ -144,9 +146,12 @@ impl BalanceOps for MockBalanceOps { .into() } - fn increase_balance(_coldkey: &AccountId, _tao: u64) {} + fn increase_balance(_coldkey: &AccountId, _tao: TaoCurrency) {} - fn decrease_balance(_coldkey: &AccountId, tao: u64) -> Result { + fn decrease_balance( + _coldkey: &AccountId, + tao: TaoCurrency, + ) -> Result { Ok(tao) } @@ -168,8 +173,8 @@ impl BalanceOps for MockBalanceOps { Ok(alpha) } - fn increase_provided_tao_reserve(_netuid: NetUid, _tao: u64) {} - fn decrease_provided_tao_reserve(_netuid: NetUid, _tao: u64) {} + fn increase_provided_tao_reserve(_netuid: NetUid, _tao: TaoCurrency) {} + fn decrease_provided_tao_reserve(_netuid: NetUid, _tao: TaoCurrency) {} fn increase_provided_alpha_reserve(_netuid: NetUid, _alpha: AlphaCurrency) {} fn decrease_provided_alpha_reserve(_netuid: NetUid, _alpha: AlphaCurrency) {} } diff --git a/pallets/swap/src/pallet/impls.rs b/pallets/swap/src/pallet/impls.rs index 2e0a28c978..39ad1ea9ae 100644 --- a/pallets/swap/src/pallet/impls.rs +++ b/pallets/swap/src/pallet/impls.rs @@ -7,7 +7,9 @@ use safe_math::*; use sp_arithmetic::helpers_128bit; use sp_runtime::traits::AccountIdConversion; use substrate_fixed::types::{I64F64, U64F64, U96F32}; -use subtensor_runtime_common::{AlphaCurrency, BalanceOps, Currency, NetUid, SubnetInfo}; +use subtensor_runtime_common::{ + AlphaCurrency, BalanceOps, Currency, NetUid, SubnetInfo, TaoCurrency, +}; use subtensor_swap_interface::{SwapHandler, SwapResult}; use super::pallet::*; @@ -21,9 +23,9 @@ const MAX_SWAP_ITERATIONS: u16 = 1000; #[derive(Debug, PartialEq)] pub struct UpdateLiquidityResult { - pub tao: u64, + pub tao: TaoCurrency, pub alpha: AlphaCurrency, - pub fee_tao: u64, + pub fee_tao: TaoCurrency, pub fee_alpha: AlphaCurrency, pub removed: bool, pub tick_low: TickIndex, @@ -32,9 +34,9 @@ pub struct UpdateLiquidityResult { #[derive(Debug, PartialEq)] pub struct RemoveLiquidityResult { - pub tao: u64, + pub tao: TaoCurrency, pub alpha: AlphaCurrency, - pub fee_tao: u64, + pub fee_tao: TaoCurrency, pub fee_alpha: AlphaCurrency, pub tick_low: TickIndex, pub tick_high: TickIndex, @@ -336,7 +338,7 @@ impl Pallet { // Protocol liquidity makes one position from TickIndex::MIN to TickIndex::MAX // We are using the sp_arithmetic sqrt here, which works for u128 let liquidity = helpers_128bit::sqrt( - (tao_reserve as u128).saturating_mul(u64::from(alpha_reserve) as u128), + (tao_reserve.to_u64() as u128).saturating_mul(alpha_reserve.to_u64() as u128), ) as u64; let protocol_account_id = Self::protocol_account_id(); @@ -356,7 +358,7 @@ impl Pallet { /// Adjusts protocol liquidity with new values of TAO and Alpha reserve pub(super) fn adjust_protocol_liquidity( netuid: NetUid, - tao_delta: u64, + tao_delta: TaoCurrency, alpha_delta: AlphaCurrency, ) { // Update protocol position with new liquidity @@ -370,7 +372,7 @@ impl Pallet { let maybe_token_amounts = position.to_token_amounts(current_sqrt_price); if let Ok((tao, alpha)) = maybe_token_amounts { // Get updated reserves, calculate liquidity - let new_tao_reserve = tao.saturating_add(tao_delta); + let new_tao_reserve = tao.saturating_add(tao_delta.to_u64()); let new_alpha_reserve = alpha.saturating_add(alpha_delta.to_u64()); let new_liquidity = helpers_128bit::sqrt( (new_tao_reserve as u128).saturating_mul(new_alpha_reserve as u128), @@ -444,8 +446,8 @@ impl Pallet { // Check if reserves are overused if let Ok(ref swap_result) = result { let checked_reserve = match order_type { - OrderType::Buy => u64::from(alpha_reserve), - OrderType::Sell => tao_reserve, + OrderType::Buy => alpha_reserve.to_u64(), + OrderType::Sell => tao_reserve.to_u64(), }; if checked_reserve < swap_result.amount_paid_out { @@ -466,8 +468,8 @@ impl Pallet { drop_fees: bool, ) -> Result> { ensure!( - T::SubnetInfo::tao_reserve(netuid.into()) >= T::MinimumReserve::get().get() - && u64::from(T::SubnetInfo::alpha_reserve(netuid.into())) + T::SubnetInfo::tao_reserve(netuid.into()).to_u64() >= T::MinimumReserve::get().get() + && T::SubnetInfo::alpha_reserve(netuid.into()).to_u64() >= T::MinimumReserve::get().get(), Error::::ReservesTooLow ); @@ -837,7 +839,7 @@ impl Pallet { let position_id = position.id; ensure!( - T::BalanceOps::tao_balance(coldkey_account_id) >= tao + T::BalanceOps::tao_balance(coldkey_account_id) >= TaoCurrency::from(tao) && T::BalanceOps::alpha_balance( netuid.into(), coldkey_account_id, @@ -933,9 +935,9 @@ impl Pallet { Positions::::remove((netuid, coldkey_account_id, position_id)); Ok(RemoveLiquidityResult { - tao, + tao: tao.into(), alpha: alpha.into(), - fee_tao, + fee_tao: fee_tao.into(), fee_alpha: fee_alpha.into(), tick_low: position.tick_low, tick_high: position.tick_high, @@ -1005,7 +1007,8 @@ impl Pallet { if liquidity_delta > 0 { // Check that user has enough balances ensure!( - T::BalanceOps::tao_balance(coldkey_account_id) >= tao + T::BalanceOps::tao_balance(coldkey_account_id) + >= TaoCurrency::from(tao.saturating_to_num::()) && T::BalanceOps::alpha_balance(netuid, coldkey_account_id, hotkey_account_id) >= AlphaCurrency::from(alpha.saturating_to_num::()), Error::::InsufficientBalance @@ -1056,9 +1059,9 @@ impl Pallet { } Ok(UpdateLiquidityResult { - tao: tao.saturating_to_num::(), + tao: tao.saturating_to_num::().into(), alpha: alpha.saturating_to_num::().into(), - fee_tao, + fee_tao: fee_tao.into(), fee_alpha: fee_alpha.into(), removed: remove, tick_low: position.tick_low, @@ -1269,7 +1272,11 @@ impl SwapHandler for Pallet { .saturating_to_num() } - fn adjust_protocol_liquidity(netuid: NetUid, tao_delta: u64, alpha_delta: AlphaCurrency) { + fn adjust_protocol_liquidity( + netuid: NetUid, + tao_delta: TaoCurrency, + alpha_delta: AlphaCurrency, + ) { Self::adjust_protocol_liquidity(netuid, tao_delta, alpha_delta); } diff --git a/pallets/swap/src/pallet/mod.rs b/pallets/swap/src/pallet/mod.rs index 1630a44c07..894099de7f 100644 --- a/pallets/swap/src/pallet/mod.rs +++ b/pallets/swap/src/pallet/mod.rs @@ -4,7 +4,9 @@ use core::ops::Neg; use frame_support::{PalletId, pallet_prelude::*, traits::Get}; use frame_system::pallet_prelude::*; use substrate_fixed::types::U64F64; -use subtensor_runtime_common::{AlphaCurrency, BalanceOps, Currency, NetUid, SubnetInfo}; +use subtensor_runtime_common::{ + AlphaCurrency, BalanceOps, Currency, NetUid, SubnetInfo, TaoCurrency, +}; use crate::{ position::{Position, PositionId}, @@ -163,7 +165,7 @@ mod pallet { /// The amount of liquidity added to the position liquidity: u64, /// The amount of TAO tokens committed to the position - tao: u64, + tao: TaoCurrency, /// The amount of Alpha tokens committed to the position alpha: AlphaCurrency, /// the lower tick @@ -185,11 +187,11 @@ mod pallet { /// The amount of liquidity removed from the position liquidity: u64, /// The amount of TAO tokens returned to the user - tao: u64, + tao: TaoCurrency, /// The amount of Alpha tokens returned to the user alpha: AlphaCurrency, /// The amount of TAO fees earned from the position - fee_tao: u64, + fee_tao: TaoCurrency, /// The amount of Alpha fees earned from the position fee_alpha: AlphaCurrency, /// the lower tick @@ -216,7 +218,7 @@ mod pallet { /// The amount of Alpha tokens returned to the user alpha: i64, /// The amount of TAO fees earned from the position - fee_tao: u64, + fee_tao: TaoCurrency, /// The amount of Alpha fees earned from the position fee_alpha: AlphaCurrency, /// the lower tick @@ -373,6 +375,7 @@ mod pallet { liquidity, )?; let alpha = AlphaCurrency::from(alpha); + let tao = TaoCurrency::from(tao); // Remove TAO and Alpha balances or fail transaction if they can't be removed exactly let tao_provided = T::BalanceOps::decrease_balance(&coldkey, tao)?; @@ -509,7 +512,7 @@ mod pallet { netuid, position_id, liquidity: liquidity_delta, - tao: result.tao as i64, + tao: result.tao.to_u64() as i64, alpha: result.alpha.to_u64() as i64, fee_tao: result.fee_tao, fee_alpha: result.fee_alpha, @@ -543,7 +546,7 @@ mod pallet { netuid, position_id, liquidity: liquidity_delta, - tao: (result.tao as i64).neg(), + tao: (result.tao.to_u64() as i64).neg(), alpha: (result.alpha.to_u64() as i64).neg(), fee_tao: result.fee_tao, fee_alpha: result.fee_alpha, @@ -554,7 +557,7 @@ mod pallet { } // Credit accrued fees to user account (no matter if liquidity is added or removed) - if result.fee_tao > 0 { + if result.fee_tao > TaoCurrency::ZERO { T::BalanceOps::increase_balance(&coldkey, result.fee_tao); } if !result.fee_alpha.is_zero() { diff --git a/pallets/swap/src/pallet/tests.rs b/pallets/swap/src/pallet/tests.rs index 12bd19813a..ec0c3641f8 100644 --- a/pallets/swap/src/pallet/tests.rs +++ b/pallets/swap/src/pallet/tests.rs @@ -176,7 +176,8 @@ fn test_swap_initialization() { // Calculate expected liquidity let expected_liquidity = - helpers_128bit::sqrt((tao as u128).saturating_mul(u64::from(alpha) as u128)) as u64; + helpers_128bit::sqrt((tao.to_u64() as u128).saturating_mul(alpha.to_u64() as u128)) + as u64; // Get the protocol account let protocol_account_id = Pallet::::protocol_account_id(); @@ -515,14 +516,14 @@ fn test_remove_liquidity_basic() { let remove_result = Pallet::::do_remove_liquidity(netuid, &OK_COLDKEY_ACCOUNT_ID, position_id) .unwrap(); - assert_abs_diff_eq!(remove_result.tao, tao, epsilon = tao / 1000); + assert_abs_diff_eq!(remove_result.tao.to_u64(), tao, epsilon = tao / 1000); assert_abs_diff_eq!( u64::from(remove_result.alpha), alpha, epsilon = alpha / 1000 ); - assert_eq!(remove_result.fee_tao, 0); - assert_eq!(remove_result.fee_alpha, 0.into()); + assert_eq!(remove_result.fee_tao, TaoCurrency::ZERO); + assert_eq!(remove_result.fee_alpha, AlphaCurrency::ZERO); // Liquidity position is removed assert_eq!( @@ -654,8 +655,8 @@ fn test_modify_position_basic() { alpha / 10, epsilon = alpha / 1000 ); - assert!(modify_result.fee_tao > 0); - assert_eq!(modify_result.fee_alpha, 0.into()); + assert!(modify_result.fee_tao > TaoCurrency::ZERO); + assert_eq!(modify_result.fee_alpha, AlphaCurrency::ZERO); // Liquidity position is reduced assert_eq!( @@ -709,8 +710,8 @@ fn test_modify_position_basic() { alpha / 100, epsilon = alpha / 1000 ); - assert_eq!(modify_result.fee_tao, 0); - assert_eq!(modify_result.fee_alpha, 0.into()); + assert_eq!(modify_result.fee_tao, TaoCurrency::ZERO); + assert_eq!(modify_result.fee_alpha, AlphaCurrency::ZERO); }); }); } @@ -821,9 +822,8 @@ fn test_swap_basic() { assert_eq!( position.liquidity, helpers_128bit::sqrt( - MockLiquidityProvider::tao_reserve(netuid.into()) as u128 - * u64::from(MockLiquidityProvider::alpha_reserve(netuid.into())) - as u128 + MockLiquidityProvider::tao_reserve(netuid.into()).to_u64() as u128 + * MockLiquidityProvider::alpha_reserve(netuid.into()).to_u64() as u128 ) as u64 ); assert_eq!(position.tick_low, tick_low); @@ -931,9 +931,9 @@ fn test_swap_single_position() { ////////////////////////////////////////////// // Initialize pool and add the user position assert_ok!(Pallet::::maybe_initialize_v3(netuid)); - let tao_reserve = MockLiquidityProvider::tao_reserve(netuid.into()); + let tao_reserve = MockLiquidityProvider::tao_reserve(netuid.into()).to_u64(); let alpha_reserve = - u64::from(MockLiquidityProvider::alpha_reserve(netuid.into())); + MockLiquidityProvider::alpha_reserve(netuid.into()).to_u64(); let protocol_liquidity = (tao_reserve as f64 * alpha_reserve as f64).sqrt(); // Add liquidity @@ -1236,8 +1236,8 @@ fn test_swap_multiple_positions() { epsilon = output_amount / 10. ); - let tao_reserve = MockLiquidityProvider::tao_reserve(netuid.into()); - let alpha_reserve = u64::from(MockLiquidityProvider::alpha_reserve(netuid.into())); + let tao_reserve = MockLiquidityProvider::tao_reserve(netuid.into()).to_u64(); + let alpha_reserve = MockLiquidityProvider::alpha_reserve(netuid.into()).to_u64(); let output_amount = output_amount as u64; assert!(output_amount > 0); diff --git a/precompiles/src/alpha.rs b/precompiles/src/alpha.rs index 9be98b3b8a..42261674d9 100644 --- a/precompiles/src/alpha.rs +++ b/precompiles/src/alpha.rs @@ -62,7 +62,7 @@ where #[precompile::public("getTaoInPool(uint16)")] #[precompile::view] fn get_tao_in_pool(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { - Ok(pallet_subtensor::SubnetTAO::::get(NetUid::from(netuid))) + Ok(pallet_subtensor::SubnetTAO::::get(NetUid::from(netuid)).to_u64()) } #[precompile::public("getAlphaInPool(uint16)")] @@ -164,9 +164,9 @@ where #[precompile::public("getTaoInEmission(uint16)")] #[precompile::view] fn get_tao_in_emission(_handle: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { - Ok(U256::from(pallet_subtensor::SubnetTaoInEmission::::get( - NetUid::from(netuid), - ))) + Ok(U256::from( + pallet_subtensor::SubnetTaoInEmission::::get(NetUid::from(netuid)).to_u64(), + )) } #[precompile::public("getAlphaInEmission(uint16)")] diff --git a/precompiles/src/metagraph.rs b/precompiles/src/metagraph.rs index a508dd7c02..cc9652a74b 100644 --- a/precompiles/src/metagraph.rs +++ b/precompiles/src/metagraph.rs @@ -5,7 +5,7 @@ use fp_evm::{ExitError, PrecompileFailure, PrecompileHandle}; use pallet_subtensor::AxonInfo as SubtensorModuleAxonInfo; use precompile_utils::{EvmResult, solidity::Codec}; use sp_core::{ByteArray, H256}; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{Currency, NetUid}; use crate::PrecompileExt; @@ -41,9 +41,7 @@ where exit_status: ExitError::InvalidRange, })?; - Ok(pallet_subtensor::Pallet::::get_total_stake_for_hotkey( - &hotkey, - )) + Ok(pallet_subtensor::Pallet::::get_total_stake_for_hotkey(&hotkey).to_u64()) } #[precompile::public("getRank(uint16,uint16)")] diff --git a/precompiles/src/staking.rs b/precompiles/src/staking.rs index 22756c2a9a..8fe064bcaf 100644 --- a/precompiles/src/staking.rs +++ b/precompiles/src/staking.rs @@ -37,7 +37,7 @@ use precompile_utils::EvmResult; use sp_core::{H256, U256}; use sp_runtime::traits::{Dispatchable, StaticLookup, UniqueSaturatedInto}; use sp_std::vec; -use subtensor_runtime_common::{NetUid, ProxyType}; +use subtensor_runtime_common::{Currency, NetUid, ProxyType}; use crate::{PrecompileExt, PrecompileHandleExt}; @@ -89,13 +89,13 @@ where netuid: U256, ) -> EvmResult<()> { let account_id = handle.caller_account_id::(); - let amount_staked = amount_rao.unique_saturated_into(); + let amount_staked: u64 = amount_rao.unique_saturated_into(); let hotkey = R::AccountId::from(address.0); let netuid = try_u16_from_u256(netuid)?; let call = pallet_subtensor::Call::::add_stake { hotkey, netuid: netuid.into(), - amount_staked, + amount_staked: amount_staked.into(), }; handle.try_dispatch_runtime_call::(call, RawOrigin::Signed(account_id)) @@ -133,7 +133,7 @@ where let call = pallet_subtensor::Call::::remove_stake_full_limit { hotkey, netuid: netuid.into(), - limit_price, + limit_price: limit_price.map(Into::into), }; handle.try_dispatch_runtime_call::(call, RawOrigin::Signed(account_id)) @@ -220,7 +220,7 @@ where let coldkey = R::AccountId::from(coldkey.0); let stake = pallet_subtensor::Pallet::::get_total_stake_for_coldkey(&coldkey); - Ok(stake.into()) + Ok(stake.to_u64().into()) } #[precompile::public("getTotalHotkeyStake(bytes32)")] @@ -232,7 +232,7 @@ where let hotkey = R::AccountId::from(hotkey.0); let stake = pallet_subtensor::Pallet::::get_total_stake_for_hotkey(&hotkey); - Ok(stake.into()) + Ok(stake.to_u64().into()) } #[precompile::public("getStake(bytes32,bytes32,uint256)")] @@ -337,15 +337,15 @@ where netuid: U256, ) -> EvmResult<()> { let account_id = handle.caller_account_id::(); - let amount_staked = amount_rao.unique_saturated_into(); - let limit_price = limit_price_rao.unique_saturated_into(); + let amount_staked: u64 = amount_rao.unique_saturated_into(); + let limit_price: u64 = limit_price_rao.unique_saturated_into(); let hotkey = R::AccountId::from(address.0); let netuid = try_u16_from_u256(netuid)?; let call = pallet_subtensor::Call::::add_stake_limit { hotkey, netuid: netuid.into(), - amount_staked, - limit_price, + amount_staked: amount_staked.into(), + limit_price: limit_price.into(), allow_partial, }; @@ -365,12 +365,12 @@ where let hotkey = R::AccountId::from(address.0); let netuid = try_u16_from_u256(netuid)?; let amount_unstaked: u64 = amount_alpha.unique_saturated_into(); - let limit_price = limit_price_rao.unique_saturated_into(); + let limit_price: u64 = limit_price_rao.unique_saturated_into(); let call = pallet_subtensor::Call::::remove_stake_limit { hotkey, netuid: netuid.into(), amount_unstaked: amount_unstaked.into(), - limit_price, + limit_price: limit_price.into(), allow_partial, }; @@ -432,10 +432,11 @@ where let amount_sub = handle.try_convert_apparent_value::()?; let hotkey = R::AccountId::from(address.0); let netuid = try_u16_from_u256(netuid)?; + let amount_staked: u64 = amount_sub.unique_saturated_into(); let call = pallet_subtensor::Call::::add_stake { hotkey, netuid: netuid.into(), - amount_staked: amount_sub.unique_saturated_into(), + amount_staked: amount_staked.into(), }; handle.try_dispatch_runtime_call::(call, RawOrigin::Signed(account_id)) @@ -474,7 +475,8 @@ where let coldkey = R::AccountId::from(coldkey.0); // get total stake of coldkey - let total_stake = pallet_subtensor::Pallet::::get_total_stake_for_coldkey(&coldkey); + let total_stake = + pallet_subtensor::Pallet::::get_total_stake_for_coldkey(&coldkey).to_u64(); // Convert to EVM decimals let stake_u256: SubstrateBalance = total_stake.into(); let stake_eth = ::BalanceConverter::into_evm_balance(stake_u256) @@ -493,7 +495,8 @@ where let hotkey = R::AccountId::from(hotkey.0); // get total stake of hotkey - let total_stake = pallet_subtensor::Pallet::::get_total_stake_for_hotkey(&hotkey); + let total_stake = + pallet_subtensor::Pallet::::get_total_stake_for_hotkey(&hotkey).to_u64(); // Convert to EVM decimals let stake_u256: SubstrateBalance = total_stake.into(); let stake_eth = ::BalanceConverter::into_evm_balance(stake_u256) diff --git a/precompiles/src/subnet.rs b/precompiles/src/subnet.rs index 3a598db61c..8b4d0eff88 100644 --- a/precompiles/src/subnet.rs +++ b/precompiles/src/subnet.rs @@ -8,7 +8,7 @@ use precompile_utils::{EvmResult, prelude::BoundedString}; use sp_core::H256; use sp_runtime::traits::Dispatchable; use sp_std::vec; -use subtensor_runtime_common::NetUid; +use subtensor_runtime_common::{Currency, NetUid}; use crate::{PrecompileExt, PrecompileHandleExt}; @@ -516,7 +516,7 @@ where #[precompile::public("getMinBurn(uint16)")] #[precompile::view] fn get_min_burn(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { - Ok(pallet_subtensor::MinBurn::::get(NetUid::from(netuid))) + Ok(pallet_subtensor::MinBurn::::get(NetUid::from(netuid)).to_u64()) } #[precompile::public("setMinBurn(uint16,uint64)")] @@ -533,7 +533,7 @@ where #[precompile::public("getMaxBurn(uint16)")] #[precompile::view] fn get_max_burn(_: &mut impl PrecompileHandle, netuid: u16) -> EvmResult { - Ok(pallet_subtensor::MaxBurn::::get(NetUid::from(netuid))) + Ok(pallet_subtensor::MaxBurn::::get(NetUid::from(netuid)).to_u64()) } #[precompile::public("setMaxBurn(uint16,uint64)")] diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 8e0f16cff6..c557c9cc5c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -68,7 +68,7 @@ use sp_std::prelude::*; use sp_version::NativeVersion; use sp_version::RuntimeVersion; use subtensor_precompiles::Precompiles; -use subtensor_runtime_common::{AlphaCurrency, time::*, *}; +use subtensor_runtime_common::{AlphaCurrency, TaoCurrency, time::*, *}; // A few exports that help ease life for downstream crates. pub use frame_support::{ @@ -487,7 +487,9 @@ impl >, ) { let ti_before = pallet_subtensor::TotalIssuance::::get(); - pallet_subtensor::TotalIssuance::::put(ti_before.saturating_sub(credit.peek())); + pallet_subtensor::TotalIssuance::::put( + ti_before.saturating_sub(credit.peek().into()), + ); drop(credit); } } @@ -2408,7 +2410,7 @@ impl_runtime_apis! { } impl subtensor_custom_rpc_runtime_api::SubnetRegistrationRuntimeApi for Runtime { - fn get_network_registration_cost() -> u64 { + fn get_network_registration_cost() -> TaoCurrency { SubtensorModule::get_network_lock_cost() } } diff --git a/runtime/tests/pallet_proxy.rs b/runtime/tests/pallet_proxy.rs index 5c6c9cd821..c545d5c09f 100644 --- a/runtime/tests/pallet_proxy.rs +++ b/runtime/tests/pallet_proxy.rs @@ -142,7 +142,7 @@ fn call_add_stake() -> RuntimeCall { RuntimeCall::SubtensorModule(pallet_subtensor::Call::add_stake { hotkey: AccountId::from(DELEGATE), netuid, - amount_staked, + amount_staked: amount_staked.into(), }) } From 608108d36544575aa7a24f4e37152101e66122f1 Mon Sep 17 00:00:00 2001 From: Aliaksandr Tsurko Date: Tue, 5 Aug 2025 21:15:19 +0300 Subject: [PATCH 3/3] Fix typos --- pallets/subtensor/src/lib.rs | 4 +- pallets/subtensor/src/rpc_info/metagraph.rs | 74 ++++++++++----------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index ac4892b2b3..0bc19f9217 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -313,12 +313,12 @@ pub mod pallet { pub fn DefaultZeroU64() -> u64 { 0 } - /// Default value for Alpha cyrrency. + /// Default value for Alpha currency. #[pallet::type_value] pub fn DefaultZeroAlpha() -> AlphaCurrency { AlphaCurrency::ZERO } - /// Default value for Tao cyrrency. + /// Default value for Tao currency. #[pallet::type_value] pub fn DefaultZeroTao() -> TaoCurrency { TaoCurrency::ZERO diff --git a/pallets/subtensor/src/rpc_info/metagraph.rs b/pallets/subtensor/src/rpc_info/metagraph.rs index 18fd9c6d4b..7f9dc46bee 100644 --- a/pallets/subtensor/src/rpc_info/metagraph.rs +++ b/pallets/subtensor/src/rpc_info/metagraph.rs @@ -40,7 +40,7 @@ pub struct Metagraph { alpha_in_emission: Compact, // amount injected outstanding per block tao_in_emission: Compact, // amount of tao injected per block pending_alpha_emission: Compact, // pending alpha to be distributed - pending_root_emission: Compact, // panding tao for root divs to be distributed + pending_root_emission: Compact, // pending tao for root divs to be distributed subnet_volume: Compact, // volume of the subnet in TAO moving_price: I96F32, // subnet moving price. @@ -52,34 +52,34 @@ pub struct Metagraph { min_allowed_weights: Compact, // min allowed weights per val max_weights_limit: Compact, // max allowed weights per val weights_version: Compact, // allowed weights version - weights_rate_limit: Compact, // rate limit on weights. + weights_rate_limit: Compact, // rate limit on weights activity_cutoff: Compact, // validator weights cut off period in blocks - max_validators: Compact, // max allowed validators. + max_validators: Compact, // max allowed validators // Registration num_uids: Compact, max_uids: Compact, - burn: Compact, // current burn cost.. - difficulty: Compact, // current difficulty. - registration_allowed: bool, // allows registrations. - pow_registration_allowed: bool, // pow registration enabled. + burn: Compact, // current burn cost + difficulty: Compact, // current difficulty + registration_allowed: bool, // allows registrations + pow_registration_allowed: bool, // pow registration enabled immunity_period: Compact, // subnet miner immunity period min_difficulty: Compact, // min pow difficulty max_difficulty: Compact, // max pow difficulty min_burn: Compact, // min tao burn max_burn: Compact, // max tao burn - adjustment_alpha: Compact, // adjustment speed for registration params. + adjustment_alpha: Compact, // adjustment speed for registration params adjustment_interval: Compact, // pow and burn adjustment interval target_regs_per_interval: Compact, // target registrations per interval - max_regs_per_block: Compact, // max registrations per block. + max_regs_per_block: Compact, // max registrations per block serving_rate_limit: Compact, // axon serving rate limit // CR - commit_reveal_weights_enabled: bool, // Is CR enabled. + commit_reveal_weights_enabled: bool, // Is CR enabled commit_reveal_period: Compact, // Commit reveal interval // Bonds - liquid_alpha_enabled: bool, // Bonds liquid enabled. + liquid_alpha_enabled: bool, // Bonds liquid enabled alpha_high: Compact, // Alpha param high alpha_low: Compact, // Alpha param low bonds_moving_avg: Compact, // Bonds moving avg @@ -88,7 +88,7 @@ pub struct Metagraph { hotkeys: Vec, // hotkey per UID coldkeys: Vec, // coldkey per UID identities: Vec>, // coldkeys identities - axons: Vec, // UID axons. + axons: Vec, // UID axons active: Vec, // Avtive per UID validator_permit: Vec, // Val permit per UID pruning_score: Vec>, // Pruning per UID @@ -118,18 +118,18 @@ pub struct SelectiveMetagraph { // Name and symbol name: Option>>, // name symbol: Option>>, // token symbol - identity: Option>, // identity information. + identity: Option>, // identity information network_registered_at: Option>, // block at registration // Keys for owner. owner_hotkey: Option, // hotkey - owner_coldkey: Option, // coldkey. + owner_coldkey: Option, // coldkey - // Tempo terms. - block: Option>, // block at call. + // Tempo terms + block: Option>, // block at call tempo: Option>, // epoch tempo last_step: Option>, // last epoch - blocks_since_last_step: Option>, // blocks since last epoch. + blocks_since_last_step: Option>, // blocks since last epoch // Subnet emission terms subnet_emission: Option>, // subnet emission via stao @@ -152,36 +152,36 @@ pub struct SelectiveMetagraph { min_allowed_weights: Option>, // min allowed weights per val max_weights_limit: Option>, // max allowed weights per val weights_version: Option>, // allowed weights version - weights_rate_limit: Option>, // rate limit on weights. + weights_rate_limit: Option>, // rate limit on weights activity_cutoff: Option>, // validator weights cut off period in blocks - max_validators: Option>, // max allowed validators. + max_validators: Option>, // max allowed validators // Registration num_uids: Option>, max_uids: Option>, - burn: Option>, // current burn cost.. - difficulty: Option>, // current difficulty. - registration_allowed: Option, // allows registrations. - pow_registration_allowed: Option, // pow registration enabled. - immunity_period: Option>, // subnet miner immunity period - min_difficulty: Option>, // min pow difficulty - max_difficulty: Option>, // max pow difficulty - min_burn: Option>, // min tao burn - max_burn: Option>, // max tao burn - adjustment_alpha: Option>, // adjustment speed for registration params. + burn: Option>, // current burn cost + difficulty: Option>, // current difficulty + registration_allowed: Option, // allows registrations + pow_registration_allowed: Option, // pow registration enabled + immunity_period: Option>, // subnet miner immunity period + min_difficulty: Option>, // min pow difficulty + max_difficulty: Option>, // max pow difficulty + min_burn: Option>, // min tao burn + max_burn: Option>, // max tao burn + adjustment_alpha: Option>, // adjustment speed for registration params adjustment_interval: Option>, // pow and burn adjustment interval target_regs_per_interval: Option>, // target registrations per interval - max_regs_per_block: Option>, // max registrations per block. - serving_rate_limit: Option>, // axon serving rate limit + max_regs_per_block: Option>, // max registrations per block + serving_rate_limit: Option>, // axon serving rate limit // CR - commit_reveal_weights_enabled: Option, // Is CR enabled. + commit_reveal_weights_enabled: Option, // Is CR enabled commit_reveal_period: Option>, // Commit reveal interval // Bonds - liquid_alpha_enabled: Option, // Bonds liquid enabled. - alpha_high: Option>, // Alpha param high - alpha_low: Option>, // Alpha param low + liquid_alpha_enabled: Option, // Bonds liquid enabled + alpha_high: Option>, // Alpha param high + alpha_low: Option>, // Alpha param low bonds_moving_avg: Option>, // Bonds moving avg // Metagraph info. @@ -205,8 +205,8 @@ pub struct SelectiveMetagraph { total_stake: Option>>, // Total stake per UID // Dividend break down. - tao_dividends_per_hotkey: Option)>>, // List of dividend payouts in tao via root. - alpha_dividends_per_hotkey: Option)>>, // List of dividend payout in alpha via subnet. + tao_dividends_per_hotkey: Option)>>, // List of dividend payouts in tao via root + alpha_dividends_per_hotkey: Option)>>, // List of dividend payout in alpha via subnet // validators validators: Option>>, // List of validators