-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmod.rs
More file actions
172 lines (148 loc) · 5.24 KB
/
Copy pathmod.rs
File metadata and controls
172 lines (148 loc) · 5.24 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::{migrations, BalanceOf, PropIndex, Proposal};
use frame_support::{
pallet_prelude::*,
traits::{Currency, Imbalance, OnRuntimeUpgrade, ReservableCurrency, StorageVersion},
};
use frame_system::pallet_prelude::*;
use impl_serde::serialize::to_hex;
use sp_runtime::traits::Zero;
use sp_std::prelude::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);
/// Pallet for bfc utility
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
/// Configuration trait of this pallet
#[pallet::config]
pub trait Config: frame_system::Config {
/// Overarching event type.
/// The currency type.
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
/// The origin which may forcibly mint native tokens.
type MintableOrigin: EnsureOrigin<Self::RuntimeOrigin>;
}
#[pallet::error]
pub enum Error<T> {
/// The given amount is too low to process.
AmountTooLow,
/// The account is already blocked.
AccountAlreadyBlocked,
/// The account is not blocked.
AccountNotBlocked,
}
#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// A motion has been proposed by a public account.
Proposed { proposal_index: PropIndex },
/// Minted native tokens and deposit.
MintNative { beneficiary: T::AccountId, minted: BalanceOf<T> },
}
#[pallet::storage]
#[pallet::unbounded]
/// Storage for accepted proposals. Proposal passed by governance will be stored here.
pub type AcceptedProposals<T: Config> = StorageValue<_, Vec<Proposal>, ValueQuery>;
#[pallet::storage]
/// Storage for proposal index. Whenever proposal is accepted, index will be increased.
pub type ProposalIndex<T: Config> = StorageValue<_, PropIndex, ValueQuery>;
#[pallet::storage]
#[pallet::unbounded]
pub type BlockedAccounts<T: Config> = StorageValue<_, Vec<T::AccountId>, ValueQuery>;
impl<T: Config> Pallet<T> {
pub fn is_blocked_account(account: &T::AccountId) -> bool {
BlockedAccounts::<T>::get().contains(account)
}
}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migrations::v3_update::MigrateToV3Update::<T>::on_runtime_upgrade()
}
}
#[pallet::genesis_config]
pub struct GenesisConfig<T> {
pub proposal_index: PropIndex,
#[serde(skip)]
pub _config: PhantomData<T>,
}
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self { proposal_index: 0, _config: Default::default() }
}
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
ProposalIndex::<T>::put(self.proposal_index);
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight((T::DbWeight::get().reads(2).saturating_add(T::DbWeight::get().writes(2)), DispatchClass::Operational,))]
/// General Proposal
/// ####
/// General community proposal without changes on codes.
pub fn community_proposal(
origin: OriginFor<T>,
proposal: Vec<u8>,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
let mut proposal_index = ProposalIndex::<T>::get();
let proposal = Proposal { proposal_hex: to_hex(&proposal[..], true), proposal_index };
let mut proposals = AcceptedProposals::<T>::get();
proposals.push(proposal);
AcceptedProposals::<T>::put(proposals);
proposal_index += 1;
ProposalIndex::<T>::put(proposal_index);
Self::deposit_event(Event::Proposed { proposal_index });
Ok(().into())
}
#[pallet::call_index(1)]
#[pallet::weight((T::DbWeight::get().writes(1), DispatchClass::Operational,))]
/// Mint the exact amount of native tokens and deposit to the target address.
pub fn mint_native(
origin: OriginFor<T>,
beneficiary: T::AccountId,
mint: BalanceOf<T>,
) -> DispatchResultWithPostInfo {
T::MintableOrigin::ensure_origin(origin)?;
ensure!(!mint.is_zero(), Error::<T>::AmountTooLow);
let minted = T::Currency::deposit_creating(&beneficiary, mint);
Self::deposit_event(Event::MintNative { beneficiary, minted: minted.peek() });
Ok(().into())
}
#[pallet::call_index(2)]
#[pallet::weight((T::DbWeight::get().writes(1), DispatchClass::Operational,))]
/// Add an account to the blocked accounts list.
pub fn add_blocked_account(
origin: OriginFor<T>,
account: T::AccountId,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
ensure!(!Self::is_blocked_account(&account), Error::<T>::AccountAlreadyBlocked);
let mut blocked_accounts = BlockedAccounts::<T>::get();
blocked_accounts.push(account);
BlockedAccounts::<T>::put(blocked_accounts);
Ok(().into())
}
#[pallet::call_index(3)]
#[pallet::weight((T::DbWeight::get().writes(1), DispatchClass::Operational,))]
/// Remove an account from the blocked accounts list.
pub fn remove_blocked_account(
origin: OriginFor<T>,
account: T::AccountId,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
ensure!(Self::is_blocked_account(&account), Error::<T>::AccountNotBlocked);
let mut blocked_accounts = BlockedAccounts::<T>::get();
blocked_accounts.retain(|a| a != &account);
BlockedAccounts::<T>::put(blocked_accounts);
Ok(().into())
}
}
}