diff --git a/pallets/emission0/src/benchmarks.rs b/pallets/emission0/src/benchmarks.rs index 2f009a9..9e3f0a3 100644 --- a/pallets/emission0/src/benchmarks.rs +++ b/pallets/emission0/src/benchmarks.rs @@ -28,15 +28,4 @@ benchmarks! { ::force_register_agent(&module_key2, vec![], vec![], vec![])?; }: _(RawOrigin::Signed(module_key), module_key2.clone()) - - regain_weight_control { - let module_key: T::AccountId = account("ModuleKey", 0, 2); - let module_key2: T::AccountId = account("ModuleKey2", 0, 3); - - ::force_register_agent(&module_key, vec![], vec![], vec![])?; - ::force_register_agent(&module_key2, vec![], vec![], vec![])?; - - Pallet::::delegate_weight_control(RawOrigin::Signed(module_key.clone()).into(), module_key2.clone())?; - - }: _(RawOrigin::Signed(module_key)) } diff --git a/pallets/emission0/src/lib.rs b/pallets/emission0/src/lib.rs index 1289354..1988beb 100644 --- a/pallets/emission0/src/lib.rs +++ b/pallets/emission0/src/lib.rs @@ -121,6 +121,9 @@ pub mod pallet { /// Tried setting weights for itself. CannotSetWeightsForSelf, + /// Tried setting weights while delegating weight control. + CannotSetWeightsWhileDelegating, + /// Tried delegating weight control to itself. CannotDelegateWeightControlToSelf, @@ -160,12 +163,6 @@ pub mod pallet { ) -> DispatchResult { weight_control::delegate_weight_control::(origin, target) } - - #[pallet::call_index(3)] - #[pallet::weight((T::WeightInfo::regain_weight_control(), DispatchClass::Normal, Pays::Yes))] - pub fn regain_weight_control(origin: OriginFor) -> DispatchResult { - weight_control::regain_weight_control::(origin) - } } } diff --git a/pallets/emission0/src/weight_control.rs b/pallets/emission0/src/weight_control.rs index f435efd..4086802 100644 --- a/pallets/emission0/src/weight_control.rs +++ b/pallets/emission0/src/weight_control.rs @@ -94,12 +94,3 @@ pub fn delegate_weight_control( Ok(()) } - -pub fn regain_weight_control(origin: OriginFor) -> DispatchResult { - let acc_id = ensure_signed(origin)?; - - crate::WeightControlDelegation::::mutate(acc_id, |val| match val.take() { - Some(_) => Ok(()), - None => Err(crate::Error::::AgentIsNotDelegating.into()), - }) -} diff --git a/pallets/emission0/src/weights.rs b/pallets/emission0/src/weights.rs index e34e05d..6d0f345 100644 --- a/pallets/emission0/src/weights.rs +++ b/pallets/emission0/src/weights.rs @@ -40,7 +40,6 @@ use core::marker::PhantomData; pub trait WeightInfo { fn set_weights() -> Weight; fn delegate_weight_control() -> Weight; - fn regain_weight_control() -> Weight; } /// Weights for `pallet_emission0` using the Substrate node and recommended hardware. @@ -78,17 +77,6 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(2_u64)) .saturating_add(T::DbWeight::get().writes(1_u64)) } - /// Storage: `Emission0::WeightControlDelegation` (r:1 w:1) - /// Proof: `Emission0::WeightControlDelegation` (`max_values`: None, `max_size`: Some(64), added: 2539, mode: `MaxEncodedLen`) - fn regain_weight_control() -> Weight { - // Proof Size summary in bytes: - // Measured: `199` - // Estimated: `3529` - // Minimum execution time: 7_000_000 picoseconds. - Weight::from_parts(8_000_000, 3529) - .saturating_add(T::DbWeight::get().reads(1_u64)) - .saturating_add(T::DbWeight::get().writes(1_u64)) - } } // For backwards compatibility and tests. @@ -125,15 +113,4 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(2_u64)) .saturating_add(RocksDbWeight::get().writes(1_u64)) } - /// Storage: `Emission0::WeightControlDelegation` (r:1 w:1) - /// Proof: `Emission0::WeightControlDelegation` (`max_values`: None, `max_size`: Some(64), added: 2539, mode: `MaxEncodedLen`) - fn regain_weight_control() -> Weight { - // Proof Size summary in bytes: - // Measured: `199` - // Estimated: `3529` - // Minimum execution time: 7_000_000 picoseconds. - Weight::from_parts(8_000_000, 3529) - .saturating_add(RocksDbWeight::get().reads(1_u64)) - .saturating_add(RocksDbWeight::get().writes(1_u64)) - } } diff --git a/pallets/emission0/tests/weights.rs b/pallets/emission0/tests/weights.rs index e4bcde5..5928e4e 100644 --- a/pallets/emission0/tests/weights.rs +++ b/pallets/emission0/tests/weights.rs @@ -1,9 +1,11 @@ use pallet_emission0::{ - weight_control::{delegate_weight_control, regain_weight_control, set_weights}, + weight_control::{delegate_weight_control, set_weights}, ConsensusMembers, Error, MaxAllowedWeights, MinStakePerWeight, WeightControlDelegation, Weights, }; -use test_utils::{add_stake, get_origin, register_empty_agent, Test}; +use test_utils::{ + add_stake, get_origin, pallet_governance::Allocators, register_empty_agent, Test, +}; #[test] fn delegates_and_regains_weight_control() { @@ -21,11 +23,6 @@ fn delegates_and_regains_weight_control() { Err(Error::::AgentIsNotRegistered.into()) ); - assert_eq!( - regain_weight_control::(get_origin(delegator)), - Err(Error::::AgentIsNotDelegating.into()) - ); - register_empty_agent(delegator); assert_eq!( @@ -41,10 +38,6 @@ fn delegates_and_regains_weight_control() { ); assert!(WeightControlDelegation::::contains_key(delegator)); - - assert_eq!(regain_weight_control::(get_origin(delegator)), Ok(())); - - assert!(!WeightControlDelegation::::contains_key(delegator)); }); } @@ -56,10 +49,7 @@ fn sets_weights_correctly() { let validator = 0; - assert_eq!( - set_weights::(get_origin(validator), vec![(0, 0); 6]), - Err(Error::::WeightSetTooLarge.into()), - ); + Allocators::::insert(0, ()); assert_eq!( set_weights::(get_origin(validator), vec![(0, 0); 5]), @@ -68,6 +58,11 @@ fn sets_weights_correctly() { register_empty_agent(validator); + assert_eq!( + set_weights::(get_origin(validator), vec![(0, 0); 6]), + Err(Error::::WeightSetTooLarge.into()), + ); + assert_eq!( set_weights::(get_origin(validator), vec![(0, 0); 5]), Err(Error::::NotEnoughStakeToSetWeights.into()), diff --git a/pallets/governance/api/src/lib.rs b/pallets/governance/api/src/lib.rs index a29c8af..ad2b669 100644 --- a/pallets/governance/api/src/lib.rs +++ b/pallets/governance/api/src/lib.rs @@ -1,6 +1,6 @@ #![no_std] -use polkadot_sdk::sp_runtime::Percent; +use polkadot_sdk::{frame_support::dispatch::DispatchResult, sp_runtime::Percent}; pub trait GovernanceApi { fn dao_treasury_address() -> AccountId; @@ -8,4 +8,6 @@ pub trait GovernanceApi { fn treasury_emission_fee() -> Percent; fn is_whitelisted(key: &AccountId) -> bool; + + fn ensure_allocator(key: &AccountId) -> DispatchResult; } diff --git a/pallets/governance/src/curator.rs b/pallets/governance/src/curator.rs deleted file mode 100644 index 3f991fe..0000000 --- a/pallets/governance/src/curator.rs +++ /dev/null @@ -1,55 +0,0 @@ -use crate::AccountIdOf; -use polkadot_sdk::frame_election_provider_support::Get; -use polkadot_sdk::sp_runtime::{DispatchError, Percent}; -use polkadot_sdk::{ - frame_support::dispatch::DispatchResult, frame_system::ensure_signed, - polkadot_sdk_frame::prelude::OriginFor, -}; - -pub fn add_curator(key: AccountIdOf) -> DispatchResult { - if crate::Curators::::contains_key(&key) { - return Err(crate::Error::::AlreadyCurator.into()); - } - - crate::Curators::::insert(key, ()); - Ok(()) -} - -pub fn remove_curator(key: AccountIdOf) -> DispatchResult { - if !crate::Curators::::contains_key(&key) { - return Err(crate::Error::::NotCurator.into()); - } - - crate::Curators::::remove(&key); - Ok(()) -} - -pub fn penalize_agent( - agent_key: AccountIdOf, - percentage: u8, -) -> DispatchResult { - if percentage > T::MaxPenaltyPercentage::get() { - return Err(crate::Error::::InvalidPenaltyPercentage.into()); - } - - pallet_torus0::Agents::::try_mutate(&agent_key, |agent| { - let Some(agent) = agent else { - return Err(crate::Error::::AgentNotFound.into()); - }; - - agent.weight_penalty_factor = Percent::from_percent(100u8.saturating_sub(percentage)); - - Ok::<(), DispatchError>(()) - })?; - - Ok(()) -} - -pub fn ensure_curator(origin: OriginFor) -> DispatchResult { - let key: AccountIdOf = ensure_signed(origin)?; - if !crate::Curators::::contains_key(key) { - return Err(crate::Error::::NotCurator.into()); - } - - Ok(()) -} diff --git a/pallets/governance/src/lib.rs b/pallets/governance/src/lib.rs index 7d37802..fb8a374 100644 --- a/pallets/governance/src/lib.rs +++ b/pallets/governance/src/lib.rs @@ -2,9 +2,9 @@ pub mod application; pub mod config; -pub mod curator; pub mod ext; pub mod proposal; +pub mod roles; pub mod voting; pub mod whitelist; @@ -31,7 +31,6 @@ use polkadot_sdk::sp_std::vec::Vec; #[frame::pallet] pub mod pallet { #![allow(clippy::too_many_arguments)] - use proposal::GlobalParamsData; use super::*; @@ -73,6 +72,9 @@ pub mod pallet { #[pallet::storage] pub type Curators = StorageMap<_, Identity, AccountIdOf, ()>; + #[pallet::storage] + pub type Allocators = StorageMap<_, Identity, AccountIdOf, ()>; + #[pallet::storage] pub type TreasuryEmissionFee = StorageValue<_, Percent, ValueQuery, T::DefaultTreasuryEmissionFee>; @@ -135,56 +137,70 @@ pub mod pallet { #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn add_curator(origin: OriginFor, key: AccountIdOf) -> DispatchResult { ensure_root(origin)?; - curator::add_curator::(key) + roles::manage_role::>(key, true, Error::::AlreadyCurator) } #[pallet::call_index(1)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn remove_curator(origin: OriginFor, key: AccountIdOf) -> DispatchResult { ensure_root(origin)?; - curator::remove_curator::(key) + roles::manage_role::>(key, false, Error::::NotAllocator) } #[pallet::call_index(2)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] + pub fn add_allocator(origin: OriginFor, key: AccountIdOf) -> DispatchResult { + ensure_root(origin)?; + roles::manage_role::>(key, true, Error::::AlreadyAllocator) + } + + #[pallet::call_index(3)] + #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] + pub fn remove_allocator(origin: OriginFor, key: AccountIdOf) -> DispatchResult { + ensure_root(origin)?; + roles::manage_role::>(key, false, Error::::NotAllocator) + } + + #[pallet::call_index(4)] + #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn add_to_whitelist(origin: OriginFor, key: AccountIdOf) -> DispatchResult { - curator::ensure_curator::(origin)?; + roles::ensure_curator::(origin)?; whitelist::add_to_whitelist::(key) } - #[pallet::call_index(3)] + #[pallet::call_index(5)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn remove_from_whitelist(origin: OriginFor, key: AccountIdOf) -> DispatchResult { - curator::ensure_curator::(origin)?; + roles::ensure_curator::(origin)?; whitelist::remove_from_whitelist::(key) } - #[pallet::call_index(4)] + #[pallet::call_index(6)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn accept_application(origin: OriginFor, application_id: u32) -> DispatchResult { - curator::ensure_curator::(origin)?; + roles::ensure_curator::(origin)?; application::accept_application::(application_id) } - #[pallet::call_index(5)] + #[pallet::call_index(7)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn deny_application(origin: OriginFor, application_id: u32) -> DispatchResult { - curator::ensure_curator::(origin)?; + roles::ensure_curator::(origin)?; application::deny_application::(application_id) } - #[pallet::call_index(6)] + #[pallet::call_index(8)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn penalize_agent( origin: OriginFor, agent_key: AccountIdOf, percentage: u8, ) -> DispatchResult { - curator::ensure_curator::(origin)?; - curator::penalize_agent::(agent_key, percentage) + roles::ensure_curator::(origin)?; + roles::penalize_agent::(agent_key, percentage) } - #[pallet::call_index(7)] + #[pallet::call_index(9)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn submit_application( origin: OriginFor, @@ -195,7 +211,7 @@ pub mod pallet { application::submit_application::(payer, agent_key, metadata) } - #[pallet::call_index(8)] + #[pallet::call_index(10)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn add_global_params_proposal( origin: OriginFor, @@ -206,7 +222,7 @@ pub mod pallet { proposal::add_global_params_proposal::(proposer, data, metadata) } - #[pallet::call_index(9)] + #[pallet::call_index(11)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn add_global_custom_proposal( origin: OriginFor, @@ -216,7 +232,7 @@ pub mod pallet { proposal::add_global_custom_proposal::(proposer, metadata) } - #[pallet::call_index(10)] + #[pallet::call_index(12)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn add_dao_treasury_transfer_proposal( origin: OriginFor, @@ -233,7 +249,7 @@ pub mod pallet { ) } - #[pallet::call_index(11)] + #[pallet::call_index(13)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn vote_proposal( origin: OriginFor, @@ -244,21 +260,21 @@ pub mod pallet { voting::add_vote::(voter, proposal_id, agree) } - #[pallet::call_index(12)] + #[pallet::call_index(14)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn remove_vote_proposal(origin: OriginFor, proposal_id: u64) -> DispatchResult { let voter = ensure_signed(origin)?; voting::remove_vote::(voter, proposal_id) } - #[pallet::call_index(13)] + #[pallet::call_index(15)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn enable_vote_delegation(origin: OriginFor) -> DispatchResult { let delegator = ensure_signed(origin)?; voting::enable_delegation::(delegator) } - #[pallet::call_index(14)] + #[pallet::call_index(16)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn disable_vote_delegation(origin: OriginFor) -> DispatchResult { let delegator = ensure_signed(origin)?; @@ -364,6 +380,10 @@ pub mod pallet { InvalidAgentPenaltyPercentage, /// The key is already a curator. AlreadyCurator, + /// The key is already an allocator. + AlreadyAllocator, + /// The key is not an allocator. + NotAllocator, /// Agent not found AgentNotFound, /// Invalid agent penalty percentage @@ -393,4 +413,8 @@ impl pallet_governance_api::GovernanceApi for Pallet fn is_whitelisted(key: &T::AccountId) -> bool { whitelist::is_whitelisted::(key) } + + fn ensure_allocator(key: &T::AccountId) -> DispatchResult { + crate::roles::ensure_allocator::(key) + } } diff --git a/pallets/governance/src/proposal.rs b/pallets/governance/src/proposal.rs index 10c44cd..8a61def 100644 --- a/pallets/governance/src/proposal.rs +++ b/pallets/governance/src/proposal.rs @@ -78,7 +78,7 @@ impl Proposal { max_name_length, max_allowed_agents, max_allowed_weights, - min_weight_stake, + min_stake_per_weight, min_weight_control_fee, min_staking_fee, dividends_participation_weight, @@ -97,7 +97,7 @@ impl Proposal { constraints.min_staking_fee = Percent::from_percent(min_staking_fee); }); pallet_emission0::MaxAllowedWeights::::set(max_allowed_weights); - pallet_emission0::MinStakePerWeight::::set(min_weight_stake); + pallet_emission0::MinStakePerWeight::::set(min_stake_per_weight); crate::GlobalGovernanceConfig::::mutate(|config| { config.proposal_cost = proposal_cost; }); @@ -185,7 +185,7 @@ pub struct GlobalParamsData { pub max_name_length: u16, pub max_allowed_agents: u16, pub max_allowed_weights: u16, - pub min_weight_stake: BalanceOf, + pub min_stake_per_weight: BalanceOf, pub min_weight_control_fee: u8, pub min_staking_fee: u8, pub dividends_participation_weight: Percent, @@ -199,7 +199,7 @@ impl Default for GlobalParamsData { max_name_length: T::DefaultMaxNameLength::get(), max_allowed_agents: T::DefaultMaxAllowedAgents::get(), max_allowed_weights: T::DefaultMaxAllowedWeights::get(), - min_weight_stake: 0, + min_stake_per_weight: 0, min_weight_control_fee: T::DefaultMinWeightControlFee::get(), min_staking_fee: T::DefaultMinStakingFee::get(), dividends_participation_weight: diff --git a/pallets/governance/src/roles.rs b/pallets/governance/src/roles.rs new file mode 100644 index 0000000..178c5a9 --- /dev/null +++ b/pallets/governance/src/roles.rs @@ -0,0 +1,57 @@ +use crate::AccountIdOf; +use crate::{ensure, storage::StorageMap, Config, Error}; +use polkadot_sdk::frame_election_provider_support::Get; +use polkadot_sdk::sp_runtime::{DispatchError, Percent}; +use polkadot_sdk::{ + frame_support::dispatch::DispatchResult, frame_system::ensure_signed, + polkadot_sdk_frame::prelude::OriginFor, +}; + +pub(super) fn manage_role, ()>>( + key: AccountIdOf, + is_add: bool, + error: Error, +) -> DispatchResult { + ensure!(M::contains_key(&key) != is_add, error); + if is_add { + M::insert(key, ()) + } else { + M::remove(&key) + } + Ok(()) +} + +pub fn penalize_agent(agent_key: AccountIdOf, percentage: u8) -> DispatchResult { + if percentage > T::MaxPenaltyPercentage::get() { + return Err(Error::::InvalidPenaltyPercentage.into()); + } + + pallet_torus0::Agents::::try_mutate(&agent_key, |agent| { + let Some(agent) = agent else { + return Err(Error::::AgentNotFound.into()); + }; + + agent.weight_penalty_factor = Percent::from_percent(percentage); + + Ok::<(), DispatchError>(()) + })?; + + Ok(()) +} + +pub fn ensure_curator(origin: OriginFor) -> DispatchResult { + let key: AccountIdOf = ensure_signed(origin)?; + if !crate::Curators::::contains_key(key) { + return Err(Error::::NotCurator.into()); + } + + Ok(()) +} + +pub fn ensure_allocator(key: &AccountIdOf) -> DispatchResult { + if !crate::Allocators::::contains_key(key) { + return Err(Error::::NotAllocator.into()); + } + + Ok(()) +} diff --git a/pallets/governance/src/whitelist.rs b/pallets/governance/src/whitelist.rs index fe0353c..7610d8b 100644 --- a/pallets/governance/src/whitelist.rs +++ b/pallets/governance/src/whitelist.rs @@ -18,6 +18,7 @@ pub fn remove_from_whitelist(key: AccountIdOf) -> DispatchR } crate::Whitelist::::remove(&key); + let _ = pallet_torus0::agent::unregister::(key.clone()); crate::Pallet::::deposit_event(crate::Event::::WhitelistRemoved(key)); Ok(()) } diff --git a/pallets/torus0/src/agent.rs b/pallets/torus0/src/agent.rs index 39adaaa..26a3a90 100644 --- a/pallets/torus0/src/agent.rs +++ b/pallets/torus0/src/agent.rs @@ -36,6 +36,11 @@ pub fn register( let span = debug_span!("register", agent.key = ?agent_key); let _guard = span.enter(); + ensure!( + ::is_whitelisted(&agent_key), + crate::Error::::AgentKeyNotWhitelisted + ); + ensure!( !exists::(&agent_key), crate::Error::::AgentAlreadyRegistered @@ -58,11 +63,6 @@ pub fn register( crate::Error::::TooManyAgentRegistrationsThisInterval ); - ensure!( - ::is_whitelisted(&agent_key), - crate::Error::::AgentKeyNotWhitelisted - ); - let agents_count = crate::Agents::::iter().count(); let max_allowed_agents = crate::MaxAllowedAgents::::get() as usize; diff --git a/pallets/torus0/src/balance.rs b/pallets/torus0/src/balance.rs deleted file mode 100644 index 5dd789c..0000000 --- a/pallets/torus0/src/balance.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::{AccountIdOf, BalanceOf}; -use polkadot_sdk::frame_support::traits::{Currency, ExistenceRequirement}; -use polkadot_sdk::frame_support::{dispatch::DispatchResult, ensure}; - -pub fn transfer_balance( - key: AccountIdOf, - destination: AccountIdOf, - amount: BalanceOf, -) -> DispatchResult { - ensure!(amount > 0, crate::Error::::InvalidAmount); - - ::Currency::transfer( - &key, - &destination, - amount, - ExistenceRequirement::AllowDeath, - ) - .map_err(|_| crate::Error::::NotEnoughBalanceToTransfer)?; - - Ok(()) -} diff --git a/pallets/torus0/src/lib.rs b/pallets/torus0/src/lib.rs index 9530094..d9a547e 100644 --- a/pallets/torus0/src/lib.rs +++ b/pallets/torus0/src/lib.rs @@ -1,7 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] pub mod agent; -pub mod balance; pub mod burn; mod ext; pub mod fee; @@ -254,17 +253,6 @@ pub mod pallet { #[pallet::call_index(3)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] - pub fn transfer_balance( - origin: OriginFor, - destination: AccountIdOf, - amount: BalanceOf, - ) -> DispatchResult { - let key = ensure_signed(origin)?; - balance::transfer_balance::(key, destination, amount) - } - - #[pallet::call_index(4)] - #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn register_agent( origin: OriginFor, agent_key: T::AccountId, @@ -276,14 +264,14 @@ pub mod pallet { agent::register::(agent_key, name, url, metadata) } - #[pallet::call_index(5)] + #[pallet::call_index(4)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn unregister_agent(origin: OriginFor) -> DispatchResult { let agent_key = ensure_signed(origin)?; agent::unregister::(agent_key) } - #[pallet::call_index(6)] + #[pallet::call_index(5)] #[pallet::weight((Weight::zero(), DispatchClass::Normal, Pays::Yes))] pub fn update_agent( origin: OriginFor, diff --git a/pallets/torus0/tests/balance.rs b/pallets/torus0/tests/balance.rs deleted file mode 100644 index 666651b..0000000 --- a/pallets/torus0/tests/balance.rs +++ /dev/null @@ -1,82 +0,0 @@ -use pallet_torus0::Error; -use polkadot_sdk::frame_support::{assert_err, traits::Currency}; -use test_utils::{assert_ok, Balances, Test}; - -#[test] -fn balance_with_amount_greater_than_0() { - test_utils::new_test_ext().execute_with(|| { - let from = 0; - let to = 1; - - let _ = Balances::deposit_creating(&from, 2000); - - assert_eq!(Balances::total_balance(&from), 2000); - assert_eq!(Balances::total_balance(&to), 0); - - assert_ok!(pallet_torus0::balance::transfer_balance::( - from, to, 1000 - )); - - assert_eq!(Balances::total_balance(&from), 1000); - assert_eq!(Balances::total_balance(&to), 1000); - }); -} - -#[test] -fn balance_with_amount_0() { - test_utils::new_test_ext().execute_with(|| { - let from = 0; - let to = 1; - - let _ = Balances::deposit_creating(&from, 2000); - - assert_eq!(Balances::total_balance(&from), 2000); - assert_eq!(Balances::total_balance(&to), 0); - - assert_err!( - pallet_torus0::balance::transfer_balance::(from, to, 0), - Error::::InvalidAmount, - ); - - assert_eq!(Balances::total_balance(&from), 2000); - assert_eq!(Balances::total_balance(&to), 0); - }); -} - -#[test] -fn balance_with_amount_0_without_balance() { - test_utils::new_test_ext().execute_with(|| { - let from = 0; - let to = 1; - - assert_eq!(Balances::total_balance(&from), 0); - assert_eq!(Balances::total_balance(&to), 0); - - assert_err!( - pallet_torus0::balance::transfer_balance::(from, to, 0), - Error::::InvalidAmount, - ); - - assert_eq!(Balances::total_balance(&from), 0); - assert_eq!(Balances::total_balance(&to), 0); - }); -} - -#[test] -fn balance_with_amount_greater_than_0_without_balance() { - test_utils::new_test_ext().execute_with(|| { - let from = 0; - let to = 1; - - assert_eq!(Balances::total_balance(&from), 0); - assert_eq!(Balances::total_balance(&to), 0); - - assert_err!( - pallet_torus0::balance::transfer_balance::(from, to, 1000), - Error::::NotEnoughBalanceToTransfer, - ); - - assert_eq!(Balances::total_balance(&from), 0); - assert_eq!(Balances::total_balance(&to), 0); - }); -} diff --git a/test-utils/src/lib.rs b/test-utils/src/lib.rs index ae32fe8..3c0bf37 100644 --- a/test-utils/src/lib.rs +++ b/test-utils/src/lib.rs @@ -5,7 +5,9 @@ use std::{cell::RefCell, num::NonZeroU128}; use pallet_torus0::MinAllowedStake; use polkadot_sdk::{ frame_support::{ - self, parameter_types, + self, + pallet_prelude::DispatchResult, + parameter_types, traits::{Currency, Everything, Hooks}, PalletId, }, @@ -95,6 +97,10 @@ impl pallet_governance_api::GovernanceApi for Test { fn is_whitelisted(key: &AccountId) -> bool { pallet_governance::Whitelist::::contains_key(key) } + + fn ensure_allocator(key: &AccountId) -> DispatchResult { + pallet_governance::roles::ensure_allocator::(key) + } } impl pallet_torus0::Config for Test {