1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod tests;
#[allow(clippy::unused_unit)]
#[frame_support::pallet]
pub mod pallet {
use frame_support::{
dispatch::DispatchResult,
pallet_prelude::*,
sp_runtime::traits::AccountIdConversion,
traits::{Currency, ExistenceRequirement::AllowDeath, Get},
PalletId,
};
use frame_system::pallet_prelude::*;
type AccountIdFor<T> = <T as frame_system::Config>::AccountId;
type BalanceFor<T> = <<T as Config>::Currency as Currency<AccountIdFor<T>>>::Balance;
#[pallet::config]
pub trait Config: frame_system::Config {
type AdminOrigin: EnsureOrigin<Self::Origin>;
#[pallet::constant]
type PalletId: Get<PalletId>;
type Currency: Currency<Self::AccountId>;
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type WeightInfo: WeightInfo;
}
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Withdrawn(AccountIdFor<T>, BalanceFor<T>),
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::extra_constants]
impl<T: Config> Pallet<T> {
pub fn treasury_account() -> T::AccountId {
T::PalletId::get().into_account()
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(T::WeightInfo::withdraw())]
pub fn withdraw(origin: OriginFor<T>, amount: BalanceFor<T>, recipient: AccountIdFor<T>) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;
T::Currency::transfer(&Self::treasury_account(), &recipient, amount, AllowDeath)?;
Self::deposit_event(Event::Withdrawn(recipient, amount));
Ok(())
}
}
pub trait WeightInfo {
fn withdraw() -> Weight;
}
impl WeightInfo for () {
fn withdraw() -> Weight {
Default::default()
}
}
}