diff --git a/common/src/currency.rs b/common/src/currency.rs index 75a774bbce..8233383e95 100644 --- a/common/src/currency.rs +++ b/common/src/currency.rs @@ -33,103 +33,101 @@ 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 - } -} +#[freeze_struct("4d1bcb31c40c2594")] +#[repr(transparent)] +#[derive( + Deserialize, + Serialize, + Clone, + Copy, + Decode, + DecodeWithMemTracking, + Default, + Encode, + Eq, + Hash, + MaxEncodedLen, + Ord, + PartialEq, + PartialOrd, + RuntimeDebug, +)] +pub struct TaoCurrency(u64); -impl From for AlphaCurrency { - fn from(value: u64) -> Self { - Self(value) - } -} +// 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 $currency_type { + pub const fn new(inner: u64) -> Self { + Self(inner) + } + } -impl ToFixed for AlphaCurrency { - fn to_fixed(self) -> F { - self.0.to_fixed() - } + impl TypeInfo for $currency_type { + type Identity = ::Identity; + fn type_info() -> scale_info::Type { + ::type_info() + } + } - fn checked_to_fixed(self) -> Option { - self.0.checked_to_fixed() - } + impl Display for $currency_type { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + Display::fmt(&self.0, f) + } + } - fn saturating_to_fixed(self) -> F { - self.0.saturating_to_fixed() - } - fn wrapping_to_fixed(self) -> F { - self.0.wrapping_to_fixed() - } + impl CompactAs for $currency_type { + type As = u64; - fn overflowing_to_fixed(self) -> (F, bool) { - self.0.overflowing_to_fixed() - } -} + fn encode_as(&self) -> &Self::As { + &self.0 + } -impl Currency for AlphaCurrency { - const MAX: Self = Self(u64::MAX); - const ZERO: Self = Self(0); -} + fn decode_from(v: Self::As) -> Result { + Ok(Self(v)) + } + } -pub trait Currency: ToFixed + Into + From + Clone + Copy { - const MAX: Self; - const ZERO: Self; + impl From> for $currency_type { + fn from(c: Compact<$currency_type>) -> Self { + c.0 + } + } - fn is_zero(&self) -> bool { - Into::::into(*self) == 0 - } + impl From<$currency_type> for u64 { + fn from(val: $currency_type) -> Self { + val.0 + } + } - fn to_u64(&self) -> u64 { - (*self).into() - } + impl From for $currency_type { + fn from(value: u64) -> Self { + Self(value) + } + } - fn saturating_add(&self, rhv: Self) -> Self { - Into::::into(*self).saturating_add(rhv.into()).into() - } + impl ToFixed for $currency_type { + fn to_fixed(self) -> F { + self.0.to_fixed() + } - #[allow(clippy::arithmetic_side_effects)] - fn saturating_div(&self, rhv: Self) -> Self { - Into::::into(*self).saturating_div(rhv.into()).into() - } + fn checked_to_fixed(self) -> Option { + self.0.checked_to_fixed() + } - fn saturating_sub(&self, rhv: Self) -> Self { - Into::::into(*self).saturating_sub(rhv.into()).into() - } + fn saturating_to_fixed(self) -> F { + self.0.saturating_to_fixed() + } + fn wrapping_to_fixed(self) -> F { + self.0.wrapping_to_fixed() + } - fn saturating_mul(&self, rhv: Self) -> Self { - Into::::into(*self).saturating_mul(rhv.into()).into() - } + fn overflowing_to_fixed(self) -> (F, bool) { + self.0.overflowing_to_fixed() + } + } + }; } macro_rules! impl_arithmetic_operators { @@ -208,8 +206,6 @@ macro_rules! impl_arithmetic_operators { }; } -impl_arithmetic_operators!(AlphaCurrency); - macro_rules! impl_approx { ($currency_type:ident) => { #[cfg(feature = "approx")] @@ -231,4 +227,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); +} diff --git a/common/src/lib.rs b/common/src/lib.rs index 8a0093f01e..abec6ed7c8 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -168,7 +168,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; @@ -176,10 +176,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, @@ -192,8 +195,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 ba5e155b7c..ba5c12b172 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] @@ -665,7 +665,7 @@ pub mod pallet { pub fn sudo_set_min_burn( origin: OriginFor, netuid: NetUid, - min_burn: u64, + min_burn: TaoCurrency, ) -> DispatchResult { ensure_root(origin)?; @@ -688,7 +688,7 @@ pub mod pallet { pub fn sudo_set_max_burn( origin: OriginFor, netuid: NetUid, - max_burn: u64, + max_burn: TaoCurrency, ) -> DispatchResult { ensure_root(origin)?; @@ -904,7 +904,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)?; @@ -948,7 +948,7 @@ pub mod pallet { ))] pub fn sudo_set_network_min_lock_cost( origin: OriginFor, - lock_cost: u64, + lock_cost: TaoCurrency, ) -> DispatchResult { ensure_root(origin)?; @@ -1005,7 +1005,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 f6495524ee..6d3061fcc5 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 ); }); } @@ -1652,7 +1652,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 @@ -1670,7 +1670,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 @@ -1688,7 +1688,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 6489199092..e3d5d8f1c1 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, @@ -402,7 +402,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 b239adb70e..783b880c43 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 { @@ -71,7 +71,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); @@ -104,7 +104,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); @@ -131,17 +131,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, @@ -173,9 +173,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(), @@ -211,9 +211,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(), @@ -242,7 +242,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); @@ -260,7 +260,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); @@ -424,9 +424,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(), @@ -449,14 +449,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( @@ -469,7 +469,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, @@ -573,9 +573,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(), @@ -616,7 +616,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); @@ -658,8 +658,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()); @@ -703,7 +703,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); @@ -711,11 +711,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); @@ -747,10 +747,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(), @@ -759,7 +759,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( @@ -767,7 +767,7 @@ mod pallet_benchmarks { origin.clone(), netuid, stake_tao, - u64::MAX, + TaoCurrency::MAX, false )); @@ -797,7 +797,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); @@ -808,10 +808,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); @@ -832,7 +832,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); @@ -864,18 +864,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); @@ -925,10 +925,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(), @@ -937,7 +937,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( @@ -945,7 +945,7 @@ mod pallet_benchmarks { hot.clone(), netuid, stake_tao, - u64::MAX, + TaoCurrency::MAX, false )); @@ -980,10 +980,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(), @@ -992,9 +992,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( @@ -1002,7 +1002,7 @@ mod pallet_benchmarks { hot.clone(), netuid1, stake_tao, - u64::MAX, + TaoCurrency::MAX, false )); @@ -1034,8 +1034,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(), @@ -1073,8 +1073,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(), @@ -1103,8 +1103,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(), @@ -1185,9 +1185,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(), @@ -1305,8 +1305,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] _( @@ -1351,9 +1351,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()); @@ -1371,7 +1371,7 @@ mod pallet_benchmarks { RawOrigin::Signed(coldkey.clone()).into(), hotkey.clone(), netuid, - staked_amt + staked_amt.into() )); // Remove stake limit for benchmark @@ -1388,7 +1388,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); @@ -1399,10 +1399,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); @@ -1423,7 +1423,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 50e5f96558..064bab4d2a 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 { @@ -96,9 +96,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 d359bd5034..6a96090b05 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); @@ -52,7 +55,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); @@ -224,10 +227,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), @@ -244,12 +247,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 837fea2767..fe1878f397 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 @@ -409,7 +409,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); @@ -484,8 +484,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. @@ -514,18 +514,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 { @@ -549,17 +551,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 { @@ -575,8 +577,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 d19cb88e9f..7ce065ff41 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 @@ -89,8 +89,8 @@ impl Pallet { // Difference becomes buy. let buy_swap_result = Self::swap_tao_for_alpha( *netuid_i, - tou64!(difference_tao), - T::SwapInterface::max_price(), + tou64!(difference_tao).into(), + T::SwapInterface::max_price().into(), true, ); if let Ok(buy_swap_result_ok) = buy_swap_result { @@ -145,16 +145,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); @@ -217,14 +218,14 @@ 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; // Accumulate root divs for subnet. PendingRootDivs::::mutate(*netuid_i, |total| { - *total = total.saturating_add(root_tao); + *total = total.saturating_add(root_tao.into()); }); } } @@ -264,7 +265,7 @@ impl Pallet { // Get and drain the subnet pending root divs. let pending_tao = PendingRootDivs::::get(netuid); - PendingRootDivs::::insert(netuid, 0); + PendingRootDivs::::insert(netuid, TaoCurrency::ZERO); // Get this amount as alpha that was swapped for pending root divs. let pending_swapped = PendingAlphaSwapped::::get(netuid); @@ -324,7 +325,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, @@ -526,13 +527,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()); }); } } @@ -554,7 +555,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, @@ -584,7 +585,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 f8474bff0a..0bc19f9217 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] @@ -313,10 +313,15 @@ 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 { - 0.into() + AlphaCurrency::ZERO + } + /// Default value for Tao currency. + #[pallet::type_value] + pub fn DefaultZeroTao() -> TaoCurrency { + TaoCurrency::ZERO } #[pallet::type_value] /// Default value for zero. @@ -394,8 +399,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. @@ -443,18 +448,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. @@ -478,8 +483,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. @@ -533,8 +538,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. @@ -809,8 +814,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] @@ -1000,9 +1005,9 @@ pub mod pallet { NetUid, Blake2_128Concat, T::AccountId, - u64, + TaoCurrency, ValueQuery, - DefaultZeroU64, + DefaultZeroTao, >; /// ================== @@ -1037,9 +1042,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. @@ -1050,10 +1055,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>; @@ -1062,7 +1067,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>; @@ -1162,11 +1167,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 = @@ -1202,7 +1208,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>; @@ -1266,7 +1272,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 = @@ -1387,16 +1393,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 = @@ -1419,8 +1427,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>; @@ -1633,6 +1647,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>; @@ -1768,14 +1783,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, } } } @@ -1795,6 +1810,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, @@ -2133,7 +2150,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())) } @@ -2160,10 +2177,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())) } @@ -2182,7 +2199,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())) } @@ -2589,7 +2606,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)) } @@ -2613,8 +2630,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( @@ -2625,12 +2642,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( @@ -2675,11 +2695,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 6f90f02340..169b4e4276 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 c0528d0600..6b6e1e0172 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 19d9b49406..60d300f29d 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 a485d12bd2..042ad0fe77 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 @@ -39,7 +39,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 8237385181..e614827e90 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 c5f1dcc2bf..e52d15fb07 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..7f9dc46bee 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, // 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. + 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 - 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 @@ -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 @@ -118,31 +118,31 @@ 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 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 @@ -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. - 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. + 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. @@ -200,13 +200,13 @@ 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. - 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 @@ -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 f1b06e9843..fe90de270a 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)?; @@ -63,7 +63,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. @@ -71,8 +73,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, )?; @@ -124,8 +126,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. @@ -135,7 +137,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; @@ -147,7 +149,7 @@ impl Pallet { &hotkey, netuid, stake_to_be_added, - max_amount, + max_amount.into(), allow_partial, )?; @@ -157,7 +159,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. @@ -168,12 +171,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); @@ -185,7 +188,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 84a749817c..79f1fcf016 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 { @@ -256,7 +256,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. @@ -303,11 +303,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 { @@ -347,7 +347,7 @@ impl Pallet { origin_coldkey, origin_netuid, move_amount, - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), true, )?; @@ -365,7 +365,7 @@ impl Pallet { destination_coldkey, destination_netuid, tao_unstaked, - T::SwapInterface::max_price(), + T::SwapInterface::max_price().into(), set_limit, )?; } @@ -409,7 +409,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); @@ -419,7 +419,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); @@ -431,7 +431,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 @@ -439,8 +439,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); } } @@ -449,7 +453,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 @@ -457,7 +461,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); @@ -489,7 +493,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 7b9b50959d..17e3b2487e 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. @@ -66,18 +66,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); }) @@ -156,17 +156,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); @@ -222,7 +222,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; @@ -254,7 +254,7 @@ impl Pallet { &coldkey, netuid, alpha_unstaked, - T::SwapInterface::min_price(), + T::SwapInterface::min_price().into(), false, )?; @@ -273,7 +273,7 @@ impl Pallet { &coldkey, NetUid::ROOT, total_tao_unstaked, - T::SwapInterface::max_price(), + T::SwapInterface::max_price().into(), false, // no limit for Root subnet )?; @@ -327,7 +327,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. @@ -364,13 +364,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); }) @@ -383,13 +383,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); @@ -401,7 +401,7 @@ impl Pallet { netuid.into(), OrderType::Sell, u64::MAX, - limit_price, + limit_price.into(), false, true, ) @@ -419,7 +419,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 9acea97113..139ea82c5d 100644 --- a/pallets/subtensor/src/staking/set_children.rs +++ b/pallets/subtensor/src/staking/set_children.rs @@ -103,7 +103,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 ff39d1f3f5..d3e6d69bd2 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)); @@ -290,7 +293,7 @@ impl Pallet { log::trace!("Finalized tao for hotkey {hotkey:?} on subnet {netuid}: {finalized_tao:?}"); // 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( @@ -563,8 +566,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, drop_fees: bool, ) -> Result { // Step 1: Get the mechanism type for the subnet (0 for Stable, 1 for Dynamic) @@ -573,8 +576,8 @@ impl Pallet { let swap_result = T::SwapInterface::swap( netuid.into(), OrderType::Buy, - tao, - price_limit, + tao.into(), + price_limit.into(), drop_fees, false, )?; @@ -593,7 +596,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. @@ -601,7 +604,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. @@ -609,8 +612,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, @@ -624,7 +627,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) @@ -635,7 +638,7 @@ impl Pallet { netuid.into(), OrderType::Sell, alpha.into(), - price_limit, + price_limit.into(), drop_fees, false, )?; @@ -656,12 +659,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. @@ -691,9 +699,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); @@ -727,7 +735,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, @@ -743,7 +751,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. @@ -753,8 +761,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. @@ -831,7 +839,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, @@ -852,9 +860,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!( @@ -910,8 +919,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. @@ -923,13 +932,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 @@ -945,7 +954,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 ); @@ -956,12 +965,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 ); @@ -1010,7 +1019,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 ); } @@ -1151,7 +1160,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 ); @@ -1181,22 +1190,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 de2b5fa689..244b9af2e9 100644 --- a/pallets/subtensor/src/subnets/leasing.rs +++ b/pallets/subtensor/src/subnets/leasing.rs @@ -24,7 +24,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; @@ -310,7 +310,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, @@ -323,19 +323,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 ab2d015dda..bfde68601d 100644 --- a/pallets/subtensor/src/subnets/registration.rs +++ b/pallets/subtensor/src/subnets/registration.rs @@ -110,9 +110,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 ); @@ -133,13 +133,13 @@ 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(), + T::SwapInterface::max_price().into(), false, )? .amount_paid_out; @@ -154,7 +154,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!("NeuronRegistered( netuid:{netuid:?} uid:{neuron_uid:?} hotkey:{hotkey:?} ) "); @@ -369,7 +369,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 13c1bc4b33..17e349c995 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. @@ -185,7 +185,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); @@ -193,11 +194,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 29a49c2834..e233460e39 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 50a6e506a6..64769389bb 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, *}; @@ -2238,7 +2238,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); @@ -2450,13 +2450,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, @@ -2528,11 +2528,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, @@ -2859,7 +2859,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); @@ -2904,13 +2904,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 @@ -2948,9 +2948,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() + ); }); }); } @@ -2973,7 +2981,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 @@ -3005,8 +3013,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(), false, ) .unwrap() @@ -3041,9 +3049,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); @@ -3067,11 +3075,12 @@ 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 = + 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:?}"); @@ -3086,7 +3095,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); @@ -3095,17 +3108,17 @@ 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 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: I96F32 = 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:?}"); @@ -3119,7 +3132,7 @@ fn test_parent_child_chain_emission() { log::info!("rel_stake_inc_c: {rel_stake_inc_c:?}"); // Verify the final stake distribution - let stake_inc_eps: I96F32 = I96F32::from_num(1e-4); // 4 decimal places + let stake_inc_eps = I96F32::from_num(1e-4); // 4 decimal places // Each child has chk_take take let expected_a = I96F32::from_num(2_f64 / 3_f64) @@ -3198,11 +3211,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 @@ -3321,7 +3334,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); @@ -3345,7 +3362,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 @@ -3577,11 +3594,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:?}"); @@ -3810,7 +3827,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 @@ -3828,7 +3845,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 @@ -3853,7 +3870,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); @@ -3870,7 +3887,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 c6cb2e0049..f74565c44e 100644 --- a/pallets/subtensor/src/tests/coinbase.rs +++ b/pallets/subtensor/src/tests/coinbase.rs @@ -50,7 +50,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"); @@ -88,9 +88,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); @@ -103,9 +103,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); @@ -126,17 +126,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); }); @@ -153,7 +153,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 = 100_000_000; add_network(netuid1, 1, 0); add_network(netuid2, 1, 0); @@ -161,12 +161,24 @@ fn test_coinbase_tao_issuance_different_prices() { let initial_tao: u64 = 100_000_u64; let initial_alpha1: u64 = initial_tao * 10; let initial_alpha2: u64 = initial_tao * 5; - mock::setup_reserves(netuid1, initial_tao, initial_alpha1.into()); - mock::setup_reserves(netuid2, initial_tao, initial_alpha2.into()); + mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha1.into()); + mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha2.into()); // Force the swap to initialize - SubtensorModule::swap_tao_for_alpha(netuid1, 0, 1_000_000_000_000, false).unwrap(); - SubtensorModule::swap_tao_for_alpha(netuid2, 0, 1_000_000_000_000, false).unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid1, + TaoCurrency::ZERO, + 1_000_000_000_000.into(), + false, + ) + .unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid2, + TaoCurrency::ZERO, + 1_000_000_000_000.into(), + false, + ) + .unwrap(); // Make subnets dynamic. SubnetMechanism::::insert(netuid1, 1); @@ -177,8 +189,8 @@ fn test_coinbase_tao_issuance_different_prices() { SubnetMovingPrice::::insert(netuid2, I96F32::from_num(2)); // Assert initial TAO reserves. - assert_eq!(SubnetTAO::::get(netuid1), initial_tao); - assert_eq!(SubnetTAO::::get(netuid2), initial_tao); + assert_eq!(SubnetTAO::::get(netuid1), initial_tao.into()); + assert_eq!(SubnetTAO::::get(netuid2), initial_tao.into()); // Run the coinbase with the emission amount. SubtensorModule::run_coinbase(U96F32::from_num(emission)); @@ -186,19 +198,27 @@ fn test_coinbase_tao_issuance_different_prices() { // Assert tao emission is split evenly. assert_abs_diff_eq!( SubnetTAO::::get(netuid1), - initial_tao + emission / 3, - epsilon = 1, + TaoCurrency::from(initial_tao + emission / 3), + epsilon = 1.into(), ); assert_abs_diff_eq!( SubnetTAO::::get(netuid2), - initial_tao + 2 * emission / 3, - epsilon = 1, + TaoCurrency::from(initial_tao + 2 * emission / 3), + epsilon = 1.into(), ); // Prices are low => we limit tao issued (buy alpha with it) - let tao_issued = ((0.1 + 0.2) * emission as f64) as u64; - assert_abs_diff_eq!(TotalIssuance::::get(), tao_issued, epsilon = 10); - assert_abs_diff_eq!(TotalStake::::get(), emission, epsilon = 10); + let tao_issued = TaoCurrency::from(((0.1 + 0.2) * emission as f64) as u64); + assert_abs_diff_eq!( + TotalIssuance::::get(), + tao_issued, + epsilon = 10.into() + ); + assert_abs_diff_eq!( + TotalStake::::get(), + emission.into(), + epsilon = 10.into() + ); }); } @@ -214,7 +234,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)); @@ -270,7 +290,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)); @@ -295,7 +315,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)); @@ -328,9 +348,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)); @@ -366,9 +386,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)); @@ -405,9 +425,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)); @@ -442,8 +462,8 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() { // Setup prices 0.000001 let initial_tao: u64 = 10_000_u64; let initial_alpha: u64 = initial_tao * 100_000_u64; - mock::setup_reserves(netuid1, initial_tao, initial_alpha.into()); - mock::setup_reserves(netuid2, initial_tao, initial_alpha.into()); + mock::setup_reserves(netuid1, initial_tao.into(), initial_alpha.into()); + mock::setup_reserves(netuid2, initial_tao.into(), initial_alpha.into()); // Enable emission FirstEmissionBlockNumber::::insert(netuid1, 0); @@ -452,8 +472,20 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() { SubnetMovingPrice::::insert(netuid2, I96F32::from_num(2)); // Force the swap to initialize - SubtensorModule::swap_tao_for_alpha(netuid1, 0, 1_000_000_000_000, false).unwrap(); - SubtensorModule::swap_tao_for_alpha(netuid2, 0, 1_000_000_000_000, false).unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid1, + TaoCurrency::ZERO, + 1_000_000_000_000.into(), + false, + ) + .unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid2, + TaoCurrency::ZERO, + 1_000_000_000_000.into(), + false, + ) + .unwrap(); // Get the prices before the run_coinbase let price_1_before = ::SwapInterface::current_alpha_price(netuid1); @@ -493,7 +525,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)); @@ -511,10 +543,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) @@ -543,7 +575,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, + ) }); } @@ -553,7 +591,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, + ) }); } @@ -576,9 +620,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); @@ -603,7 +647,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( @@ -639,15 +689,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); @@ -661,7 +711,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); }); } @@ -691,7 +745,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 = @@ -748,15 +808,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); @@ -783,12 +843,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 @@ -835,9 +895,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, @@ -874,14 +934,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(), @@ -890,7 +950,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() ); }); } @@ -935,15 +995,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); @@ -992,7 +1052,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() ); }); } @@ -1020,7 +1080,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); @@ -1142,11 +1208,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. @@ -1242,7 +1308,7 @@ fn test_get_root_children_drain() { SubtensorModule::drain_pending_emission( alpha, pending_alpha, - 0, + TaoCurrency::ZERO, AlphaCurrency::ZERO, AlphaCurrency::ZERO, ); @@ -1258,11 +1324,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, @@ -1274,11 +1340,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. @@ -1289,7 +1355,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, @@ -1303,7 +1369,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), @@ -1388,7 +1457,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( @@ -1469,7 +1544,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( @@ -1550,7 +1631,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( @@ -1718,7 +1805,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)]; @@ -1748,7 +1835,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 ); }); @@ -1761,7 +1848,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)]; @@ -1791,7 +1878,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 ); }); @@ -1804,7 +1891,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)]; @@ -1834,7 +1921,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 ); }); @@ -1847,7 +1934,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)]; @@ -1877,7 +1964,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 ); }); @@ -1904,7 +1991,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% @@ -1955,7 +2042,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 @@ -2135,7 +2222,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. @@ -2144,7 +2231,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); @@ -2152,8 +2245,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() ); }); } @@ -2183,7 +2276,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. @@ -2210,12 +2303,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); @@ -2249,7 +2348,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); @@ -2263,7 +2362,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. @@ -2303,7 +2402,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); @@ -2348,7 +2447,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. @@ -2407,7 +2506,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}"); }); } @@ -2424,7 +2523,13 @@ 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, false).unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid, + TaoCurrency::ZERO, + 1_000_000_000_000.into(), + false, + ) + .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 1a002735ec..c7aabb899e 100644 --- a/pallets/subtensor/src/tests/delegate_info.rs +++ b/pallets/subtensor/src/tests/delegate_info.rs @@ -123,7 +123,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 aa29209118..bdf675648b 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::*; @@ -573,7 +573,7 @@ fn test_1_graph() { RuntimeOrigin::signed(coldkey), hotkey, netuid, - stake_amount + stake_amount.into() )); assert_eq!(SubtensorModule::get_subnetwork_n(netuid), 1); @@ -590,7 +590,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); @@ -652,7 +652,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); @@ -709,7 +709,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); @@ -727,7 +727,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); @@ -1312,13 +1312,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)); @@ -2281,14 +2281,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 611012b2b7..bf5cd5dc1c 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) { @@ -37,20 +38,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) ); }); } @@ -839,7 +839,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" @@ -855,7 +855,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 81bf186eea..fd7dcbab45 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; @@ -842,7 +842,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(), @@ -859,7 +859,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(), @@ -872,8 +872,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); } @@ -942,7 +942,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( @@ -950,7 +950,7 @@ pub fn increase_stake_on_coldkey_hotkey_account( coldkey, netuid, tao_staked, - ::SwapInterface::max_price(), + ::SwapInterface::max_price().into(), false, ) .unwrap(); @@ -962,7 +962,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, @@ -975,20 +975,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, @@ -1008,9 +1008,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!( @@ -1034,10 +1034,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 987a5d20e0..bab483ec68 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,23 @@ 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, false).unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid, + TaoCurrency::ZERO, + 1_000_000_000_000.into(), + false, + ) + .unwrap(); // Get the current price (should be 1.0) let current_price = @@ -581,8 +598,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 +636,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 +661,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 +692,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 +703,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 +720,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 +741,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 +762,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 +770,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 +778,7 @@ fn test_add_stake_insufficient_liquidity() { RuntimeOrigin::signed(coldkey), hotkey, netuid, - amount_staked + amount_staked.into() ), Error::::InsufficientLiquidity ); @@ -771,7 +793,7 @@ fn test_add_stake_insufficient_liquidity_one_side_ok() { 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); @@ -780,14 +802,14 @@ fn test_add_stake_insufficient_liquidity_one_side_ok() { // Set the liquidity at lowest possible value so that all staking requests fail let reserve_alpha = u64::from(mock::SwapMinimumReserve::get()); let reserve_tao = u64::from(mock::SwapMinimumReserve::get()) - 1; - mock::setup_reserves(netuid, reserve_tao, reserve_alpha.into()); + mock::setup_reserves(netuid, reserve_tao.into(), reserve_alpha.into()); // Check the error assert_ok!(SubtensorModule::add_stake( RuntimeOrigin::signed(coldkey), hotkey, netuid, - amount_staked + amount_staked.into() )); }); } @@ -800,7 +822,7 @@ fn test_add_stake_insufficient_liquidity_one_side_fail() { 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); @@ -809,7 +831,7 @@ fn test_add_stake_insufficient_liquidity_one_side_fail() { // Set the liquidity at lowest possible value so that all staking requests fail let reserve_alpha = u64::from(mock::SwapMinimumReserve::get()) - 1; let reserve_tao = u64::from(mock::SwapMinimumReserve::get()); - mock::setup_reserves(netuid, reserve_tao, reserve_alpha.into()); + mock::setup_reserves(netuid, reserve_tao.into(), reserve_alpha.into()); // Check the error assert_noop!( @@ -817,7 +839,7 @@ fn test_add_stake_insufficient_liquidity_one_side_fail() { RuntimeOrigin::signed(coldkey), hotkey, netuid, - amount_staked + amount_staked.into() ), Error::::InsufficientLiquidity ); @@ -831,7 +853,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); @@ -839,21 +861,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!( @@ -862,7 +884,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), @@ -883,14 +905,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!( @@ -899,7 +921,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), @@ -910,12 +932,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(); @@ -947,12 +969,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, @@ -980,7 +1002,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), @@ -1009,15 +1035,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 @@ -1077,7 +1107,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 @@ -1171,8 +1201,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() ); }); } @@ -1202,8 +1232,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 @@ -1215,7 +1245,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 + ); }); } @@ -1282,8 +1315,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); }); @@ -1295,8 +1328,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); @@ -1425,8 +1458,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( @@ -1461,8 +1494,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( @@ -1494,7 +1527,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( @@ -1537,7 +1570,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 ); }); } @@ -1599,8 +1632,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. @@ -1619,7 +1652,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); @@ -1643,7 +1676,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); @@ -2098,7 +2131,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); @@ -2108,24 +2141,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, @@ -2163,13 +2196,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), ); }); } @@ -2185,7 +2218,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 + ); }); } @@ -2197,7 +2233,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); @@ -2206,13 +2242,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 @@ -2237,13 +2273,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), ); }); } @@ -2258,15 +2294,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, @@ -2275,7 +2314,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, @@ -2310,8 +2352,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); @@ -2321,17 +2363,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) @@ -2341,8 +2383,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) ); }); } @@ -2393,13 +2435,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 @@ -2436,9 +2478,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() + ); }); } @@ -2462,7 +2516,7 @@ fn test_staking_too_little_fails() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - 1 + 1.into() ), Error::::AmountTooLow ); @@ -2479,15 +2533,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, @@ -2496,7 +2549,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); @@ -2512,7 +2565,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() ); }); } @@ -2526,14 +2579,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, @@ -2556,12 +2609,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 + ); }); } @@ -2588,7 +2645,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); @@ -2615,7 +2672,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); @@ -2632,9 +2689,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 }; @@ -2646,7 +2705,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 = @@ -2672,7 +2731,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 @@ -2680,7 +2739,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 @@ -2728,7 +2787,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; @@ -2743,7 +2802,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 = @@ -2769,7 +2828,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 @@ -2777,7 +2836,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 @@ -2831,7 +2890,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); @@ -2850,8 +2909,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, }); @@ -2902,7 +2961,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); @@ -2918,7 +2977,7 @@ fn test_remove_stake_limit_validate() { hotkey, netuid, amount_unstaked: unstake_amount, - limit_price, + limit_price: limit_price.into(), allow_partial: false, }); @@ -2960,15 +3019,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 @@ -2980,8 +3039,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() ); }); } @@ -2996,7 +3055,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); @@ -3005,13 +3064,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(); @@ -3046,29 +3105,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 { @@ -3109,28 +3168,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 }); @@ -3163,31 +3222,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) ); }); @@ -3201,31 +3260,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) ); }); @@ -3302,11 +3361,17 @@ 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, false).unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid, + TaoCurrency::ZERO, + 1_000_000_000_000.into(), + false, + ) + .unwrap(); if !alpha_in.is_zero() { let expected_price = U96F32::from_num(tao_in) / U96F32::from_num(alpha_in); @@ -3319,9 +3384,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 ), @@ -3335,37 +3403,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) ); }); @@ -3379,31 +3447,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) ); }); @@ -3517,7 +3585,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() { @@ -3530,13 +3598,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() ); @@ -3553,37 +3622,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) ); }); @@ -3598,37 +3687,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) ); }); @@ -3648,7 +3757,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); @@ -3660,49 +3769,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) ); }); @@ -3722,7 +3857,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); @@ -3734,41 +3869,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(), @@ -3777,25 +3920,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() ); }); @@ -3999,9 +4154,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() { @@ -4026,7 +4181,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, @@ -4048,9 +4203,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)); @@ -4061,7 +4216,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 @@ -4069,7 +4224,7 @@ fn test_add_stake_limit_ok() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount, + amount.into(), limit_price, true )); @@ -4119,9 +4274,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()); @@ -4135,7 +4290,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!( @@ -4143,7 +4298,7 @@ fn test_add_stake_limit_fill_or_kill() { RuntimeOrigin::signed(coldkey_account_id), hotkey_account_id, netuid, - amount, + amount.into(), limit_price, false ), @@ -4151,7 +4306,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, @@ -4172,12 +4327,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); @@ -4187,7 +4342,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 ), @@ -4211,9 +4366,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 @@ -4221,7 +4376,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, @@ -4245,7 +4400,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( @@ -4283,16 +4438,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!( @@ -4313,7 +4468,7 @@ fn test_remove_stake_limit_fill_or_kill() { hotkey_account_id, netuid, unstake_amount / 100.into(), - limit_price, + limit_price.into(), false ),); }); @@ -4333,10 +4488,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); @@ -4346,7 +4501,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 @@ -4367,7 +4522,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 @@ -4375,7 +4530,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, @@ -4424,7 +4579,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 @@ -4449,12 +4604,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, @@ -4481,12 +4636,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 @@ -4515,7 +4670,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, ); @@ -4558,11 +4713,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()); @@ -4570,7 +4725,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( @@ -4619,13 +4774,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 @@ -4662,7 +4813,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 @@ -4678,7 +4829,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(), ); @@ -4718,13 +4869,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( @@ -4766,14 +4921,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); @@ -4804,9 +4959,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::(); @@ -4826,8 +4981,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 @@ -4857,9 +5012,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::(); @@ -4879,8 +5034,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); @@ -4907,9 +5062,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( @@ -4926,8 +5081,8 @@ fn test_unstake_from_subnet_low_amount() { &hotkey, &coldkey, netuid, - amount, - u64::MAX, + amount.into(), + TaoCurrency::MAX, false, )); @@ -4939,7 +5094,7 @@ fn test_unstake_from_subnet_low_amount() { &coldkey, netuid, alpha, - u64::MIN, + TaoCurrency::ZERO, false, )); @@ -4964,9 +5119,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( @@ -4985,8 +5140,8 @@ fn test_stake_into_subnet_prohibitive_limit() { RuntimeOrigin::signed(coldkey), owner_hotkey, netuid, - amount, - 0, + amount.into(), + TaoCurrency::ZERO, true, ), Error::::ZeroMaxStakeAmount @@ -5020,9 +5175,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( @@ -5039,8 +5194,8 @@ fn test_unstake_from_subnet_prohibitive_limit() { &owner_hotkey, &coldkey, netuid, - amount, - u64::MAX, + amount.into(), + TaoCurrency::MAX, false, )); @@ -5058,7 +5213,7 @@ fn test_unstake_from_subnet_prohibitive_limit() { owner_hotkey, netuid, alpha, - u64::MAX, + TaoCurrency::MAX, true, ), Error::::ZeroMaxStakeAmount @@ -5095,9 +5250,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( @@ -5114,8 +5269,8 @@ fn test_unstake_full_amount() { &owner_hotkey, &coldkey, netuid, - amount, - u64::MAX, + amount.into(), + TaoCurrency::MAX, false, )); @@ -5205,20 +5360,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; @@ -5247,8 +5402,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; @@ -5270,11 +5425,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); }); } @@ -5343,12 +5500,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( @@ -5391,12 +5548,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!( @@ -5429,9 +5586,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 @@ -5464,7 +5621,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; @@ -5479,9 +5636,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()); @@ -5490,7 +5647,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 = @@ -5538,16 +5695,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 @@ -5576,7 +5733,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); @@ -5705,11 +5862,17 @@ 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, false).unwrap(); + SubtensorModule::swap_tao_for_alpha( + netuid, + TaoCurrency::ZERO, + 1_000_000_000_000.into(), + false, + ) + .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, @@ -5728,8 +5891,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); @@ -5741,7 +5904,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 9ae58a2fbb..f5d57d02d3 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(), false, ) .unwrap() @@ -54,7 +54,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!( @@ -663,7 +663,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; @@ -671,9 +671,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 @@ -809,7 +815,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; @@ -822,12 +828,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 dffec5b865..b60f3ffa41 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 8f77fef9fd..5a9ebf5127 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 89396061be..314f72c2bd 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 2ff01bc875..88b51a1300 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 655d320c2f..6c6fe5d796 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 9d91f370ea..3760778bfc 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 { @@ -467,12 +469,13 @@ impl Pallet { ) -> Result> { match order_type { OrderType::Buy => ensure!( - u64::from(T::SubnetInfo::alpha_reserve(netuid.into())) + T::SubnetInfo::alpha_reserve(netuid.into()).to_u64() >= T::MinimumReserve::get().get(), Error::::ReservesTooLow ), OrderType::Sell => ensure!( - T::SubnetInfo::tao_reserve(netuid.into()) >= T::MinimumReserve::get().get(), + T::SubnetInfo::tao_reserve(netuid.into()).to_u64() + >= T::MinimumReserve::get().get(), Error::::ReservesTooLow ), } @@ -842,7 +845,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, @@ -938,9 +941,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, @@ -1010,7 +1013,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 @@ -1061,9 +1065,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, @@ -1274,7 +1278,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 9affc5ed98..4c3a890c9b 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 a403a8462f..5f927d62ae 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 db35faf7e0..43714b4662 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); } } @@ -2405,7 +2407,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 bfd003ed17..b81c34e1dc 100644 --- a/runtime/tests/pallet_proxy.rs +++ b/runtime/tests/pallet_proxy.rs @@ -143,7 +143,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(), }) }