git.delta.rocks / unique-network / refs/commits / 885653bf99d3

difftreelog

source

test-pallets/utils/src/lib.rs2.7 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617#![cfg_attr(not(feature = "std"), no_std)]1819pub use pallet::*;2021#[frame_support::pallet]22pub mod pallet {23	use frame_support::pallet_prelude::*;24	use frame_system::pallet_prelude::*;25	use pallet_unique_scheduler::{ScheduledId, Pallet as SchedulerPallet};2627	#[pallet::config]28	pub trait Config: frame_system::Config + pallet_unique_scheduler::Config {29		type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;30	}3132	#[pallet::event]33	#[pallet::generate_deposit(pub(super) fn deposit_event)]34	pub enum Event<T: Config> {35		ValueIsSet,36		ShouldRollback,37	}3839	#[pallet::pallet]40	#[pallet::generate_store(pub(super) trait Store)]41	pub struct Pallet<T>(_);4243	#[pallet::storage]44	#[pallet::getter(fn test_value)]45	pub type TestValue<T> = StorageValue<_, u32, ValueQuery>;4647	#[pallet::error]48	pub enum Error<T> {49		TriggerRollback,50	}5152	#[pallet::call]53	impl<T: Config> Pallet<T> {54		#[pallet::weight(10_000)]55		pub fn set_test_value(origin: OriginFor<T>, value: u32) -> DispatchResult {56			ensure_signed(origin)?;5758			<TestValue<T>>::put(value);5960			Self::deposit_event(Event::ValueIsSet);6162			Ok(())63		}6465		#[pallet::weight(10_000)]66		pub fn set_test_value_and_rollback(origin: OriginFor<T>, value: u32) -> DispatchResult {67			Self::set_test_value(origin, value)?;6869			Self::deposit_event(Event::ShouldRollback);7071			Err(<Error<T>>::TriggerRollback.into())72		}7374		#[pallet::weight(10_000)]75		pub fn inc_test_value(origin: OriginFor<T>) -> DispatchResult {76			Self::set_test_value(origin, <TestValue<T>>::get() + 1)77		}7879		#[pallet::weight(10_000)]80		pub fn self_canceling_inc(81			origin: OriginFor<T>,82			id: ScheduledId,83			max_test_value: u32,84		) -> DispatchResult {85			if <TestValue<T>>::get() < max_test_value {86				Self::inc_test_value(origin)?;87			} else {88				SchedulerPallet::<T>::cancel_named(origin, id)?;89			}9091			Ok(())92		}9394		#[pallet::weight(100_000_000)]95		pub fn just_take_fee(_origin: OriginFor<T>) -> DispatchResult {96			Ok(())97		}98	}99}