|
| 1 | +//! # Pool Withdrawal Fix Pallet |
| 2 | +//! |
| 3 | +//! A pallet for handling pool withdrawal operations through delegation pallet connector. |
| 4 | +//! This pallet provides withdrawal functionality that can be called by root or governance. |
| 5 | +
|
| 6 | +#![cfg_attr(not(feature = "std"), no_std)] |
| 7 | + |
| 8 | +use frame_support::pallet_prelude::Weight; |
| 9 | +pub use pallet::*; |
| 10 | + |
| 11 | +#[frame_support::pallet] |
| 12 | +pub mod pallet { |
| 13 | + use crate::WeightInfo; |
| 14 | + use frame_support::pallet_prelude::*; |
| 15 | + use frame_support::traits::Currency; |
| 16 | + use frame_support::traits::EnsureOrigin; |
| 17 | + use frame_support::traits::LockableCurrency; |
| 18 | + use frame_system::pallet_prelude::*; |
| 19 | + use sp_staking::OnStakingUpdate; |
| 20 | + |
| 21 | + pub type BalanceOf<T> = |
| 22 | + <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance; |
| 23 | + |
| 24 | + #[pallet::pallet] |
| 25 | + pub struct Pallet<T>(_); |
| 26 | + |
| 27 | + /// Configure the pallet by specifying the parameters and types on which it depends. |
| 28 | + #[pallet::config] |
| 29 | + pub trait Config: frame_system::Config { |
| 30 | + /// Because this pallet emits events, it depends on the runtime's definition of an event. |
| 31 | + type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; |
| 32 | + type Currency: LockableCurrency<Self::AccountId, Moment = BlockNumberFor<Self>>; |
| 33 | + /// Integrate Delegated Pallet |
| 34 | + type DelegationPalletConnector: OnStakingUpdate<Self::AccountId, BalanceOf<Self>>; |
| 35 | + /// Governance origin for withdrawal calls |
| 36 | + type GovernanceOrigin: EnsureOrigin<Self::RuntimeOrigin>; |
| 37 | + /// Type representing the weight of this pallet |
| 38 | + type WeightInfo: WeightInfo; |
| 39 | + } |
| 40 | + |
| 41 | + /// Pallets use events to inform users when important changes are made. |
| 42 | + /// https://docs.substrate.io/main-docs/build/events-errors/ |
| 43 | + #[pallet::event] |
| 44 | + #[pallet::generate_deposit(pub(super) fn deposit_event)] |
| 45 | + pub enum Event<T: Config> { |
| 46 | + /// Withdrawal was called for a stash account. [stash_account, amount] |
| 47 | + WithdrawCalled { stash_account: T::AccountId, amount: BalanceOf<T> }, |
| 48 | + } |
| 49 | + |
| 50 | + #[pallet::error] |
| 51 | + pub enum Error<T> { |
| 52 | + /// Withdrawal failed due to insufficient balance or other staking constraints. |
| 53 | + WithdrawalFailed, |
| 54 | + } |
| 55 | + |
| 56 | + /// Dispatchable functions allows users to interact with the pallet and invoke state changes. |
| 57 | + /// These functions materialize as "extrinsics", which are often compared to transactions. |
| 58 | + /// Dispatchable functions must be annotated with a weight and must return a DispatchResult. |
| 59 | + #[pallet::call] |
| 60 | + impl<T: Config> Pallet<T> { |
| 61 | + /// Call withdrawal for a stash account through the delegation pallet connector. |
| 62 | + /// This function can be called by root or governance. |
| 63 | + #[pallet::call_index(0)] |
| 64 | + #[pallet::weight(T::WeightInfo::call_withdraw())] |
| 65 | + pub fn call_withdraw( |
| 66 | + origin: OriginFor<T>, |
| 67 | + stash_account: T::AccountId, |
| 68 | + amount: BalanceOf<T>, |
| 69 | + ) -> DispatchResult { |
| 70 | + T::GovernanceOrigin::ensure_origin(origin)?; |
| 71 | + |
| 72 | + // Call the delegation pallet connector to handle withdrawal |
| 73 | + T::DelegationPalletConnector::on_withdraw(&stash_account, amount); |
| 74 | + |
| 75 | + // Emit an event. |
| 76 | + Self::deposit_event(Event::WithdrawCalled { stash_account, amount }); |
| 77 | + |
| 78 | + Ok(()) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// Weight functions needed for pallet_pool_withdrawal_fix. |
| 84 | +pub trait WeightInfo { |
| 85 | + fn call_withdraw() -> Weight; |
| 86 | +} |
| 87 | + |
| 88 | +/// Default weight implementation for the pallet. |
| 89 | +impl WeightInfo for () { |
| 90 | + fn call_withdraw() -> Weight { |
| 91 | + Weight::from_parts(10_000, 0) |
| 92 | + } |
| 93 | +} |
0 commit comments