Skip to content

Commit f8b50d3

Browse files
author
dio-will
committed
Feat: better dummy pallet
1 parent 24e795c commit f8b50d3

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

pallets/dummy/src/lib.rs

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@ use frame_support::traits::IsSubType;
66
pub use pallet::*;
77
use scale_info::TypeInfo;
88
use sp_runtime::{traits::SignedExtension, transaction_validity::ValidTransactionBuilder};
9-
109
#[frame_support::pallet]
1110
pub mod pallet {
1211
use frame_support::storage::{storage_prefix, unhashed};
12+
use frame_support::traits::ReservableCurrency;
1313
use frame_support::{pallet_prelude::*, traits::Currency};
1414
use frame_system::{pallet_prelude::*, RawOrigin};
1515
use pallet_balances::{self as balances};
1616
use sp_runtime::traits::UniqueSaturatedInto;
17-
1817
#[pallet::pallet]
1918
// #[pallet::generate_store(pub(super) trait Store)]
2019
#[pallet::without_storage_info]
@@ -27,6 +26,12 @@ pub mod pallet {
2726
SudoMigrated(T::AccountId),
2827
// Sudo key balance has been updated
2928
SudoBalanceDeposited(T::AccountId, T::Balance),
29+
// Sudo key proxy has been removed
30+
SudoProxyRemoved(T::AccountId),
31+
// Sudo key reserved balances have been reset
32+
SudoReservedBalanceReset(T::AccountId, T::Balance),
33+
// Sudo key frozen balances have been reset
34+
SudoFrozenBalancesReset(T::AccountId),
3035
}
3136

3237
#[pallet::config]
@@ -67,8 +72,8 @@ pub mod pallet {
6772
}
6873
}
6974

70-
let sudo_balance = balances::Pallet::<T>::free_balance(&sudo_account);
71-
if sudo_balance < amount_to_add {
75+
let sudo_free_balance = balances::Pallet::<T>::free_balance(&sudo_account);
76+
if sudo_free_balance < amount_to_add {
7277
let imbalance =
7378
balances::Pallet::<T>::deposit_creating(&sudo_account, amount_to_add);
7479
drop(imbalance);
@@ -77,17 +82,46 @@ pub mod pallet {
7782
amount_to_add,
7883
));
7984

80-
let _ = balances::Pallet::<T>::mutate_account(&sudo_account, |data| {
85+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
86+
}
87+
88+
// Unregister all proxy accounts for the sudo account.
89+
match pallet_proxy::Pallet::<T>::proxies(&sudo_account) {
90+
(proxies, _) if !proxies.is_empty() => {
91+
let _ = pallet_proxy::Pallet::<T>::remove_proxies(
92+
RawOrigin::Signed(sudo_account.clone()).into(),
93+
);
94+
Self::deposit_event(Event::SudoProxyRemoved(sudo_account.clone()));
95+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
96+
}
97+
_ => {}
98+
}
99+
100+
match pallet_balances::Pallet::<T>::reserved_balance(&sudo_account) {
101+
reserved_balance if reserved_balance > 0u32.into() => {
102+
let _ = <balances::Pallet<T> as ReservableCurrency<T::AccountId>>::unreserve(
103+
&sudo_account,
104+
reserved_balance,
105+
);
106+
Self::deposit_event(Event::SudoReservedBalanceReset(
107+
sudo_account.clone(),
108+
reserved_balance,
109+
));
110+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
111+
}
112+
_ => {}
113+
}
114+
115+
let _ = balances::Pallet::<T>::mutate_account(&sudo_account, |data| {
116+
if data.misc_frozen > 0u32.into() || data.fee_frozen > 0u32.into() {
81117
data.misc_frozen = 0u32.into();
82118
data.fee_frozen = 0u32.into();
83-
});
119+
Self::deposit_event(Event::SudoFrozenBalancesReset(sudo_account.clone()));
120+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
121+
}
122+
});
84123

85-
let _ = pallet_proxy::Pallet::<T>::remove_proxies(
86-
RawOrigin::Signed(sudo_account).into(),
87-
);
88-
weight = weight.saturating_add(T::DbWeight::get().writes(1));
89-
}
90-
weight.saturating_add(T::DbWeight::get().reads(2))
124+
weight.saturating_add(T::DbWeight::get().reads(5))
91125
}
92126
}
93127
}

0 commit comments

Comments
 (0)