Add claim_deallocation to the staking pallet

This commit is contained in:
Luke Parker
2023-10-12 00:26:35 -04:00
parent 3da5577950
commit 108e2b57d9
2 changed files with 17 additions and 3 deletions

View File

@@ -13,12 +13,13 @@ pub mod pallet {
use serai_primitives::{NetworkId, Amount, PublicKey}; use serai_primitives::{NetworkId, Amount, PublicKey};
use validator_sets_pallet::{Config as VsConfig, Pallet as VsPallet}; use validator_sets_pallet::{primitives::Session, Config as VsConfig, Pallet as VsPallet};
use pallet_session::{Config as SessionConfig, SessionManager}; use pallet_session::{Config as SessionConfig, SessionManager};
#[pallet::error] #[pallet::error]
pub enum Error<T> { pub enum Error<T> {
StakeUnavilable, StakeUnavilable,
NoDeallocation,
} }
// TODO: Event // TODO: Event
@@ -76,7 +77,6 @@ pub mod pallet {
}) })
} }
#[allow(unused)] // TODO
fn deallocate_internal(account: &T::AccountId, amount: u64) -> Result<(), Error<T>> { fn deallocate_internal(account: &T::AccountId, amount: u64) -> Result<(), Error<T>> {
Allocated::<T>::try_mutate(account, |allocated| { Allocated::<T>::try_mutate(account, |allocated| {
if *allocated < amount { if *allocated < amount {
@@ -150,7 +150,20 @@ pub mod pallet {
Ok(()) Ok(())
} }
// TODO: Add a function to reclaim deallocated funds #[pallet::call_index(4)]
#[pallet::weight((0, DispatchClass::Operational))] // TODO
pub fn claim_deallocation(
origin: OriginFor<T>,
network: NetworkId,
session: Session,
) -> DispatchResult {
let account = ensure_signed(origin)?;
let Some(amount) = VsPallet::<T>::take_deallocatable_amount(network, session, account) else {
Err(Error::<T>::NoDeallocation)?
};
Self::deallocate_internal(&account, amount.0)?;
Ok(())
}
} }
// Call order is end_session(i - 1) -> start_session(i) -> new_session(i + 1) // Call order is end_session(i - 1) -> start_session(i) -> new_session(i + 1)

View File

@@ -163,6 +163,7 @@ pub mod pallet {
#[pallet::getter(fn keys)] #[pallet::getter(fn keys)]
pub type Keys<T: Config> = StorageMap<_, Twox64Concat, ValidatorSet, KeyPair, OptionQuery>; pub type Keys<T: Config> = StorageMap<_, Twox64Concat, ValidatorSet, KeyPair, OptionQuery>;
// TODO: Expand
#[pallet::event] #[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)] #[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> { pub enum Event<T: Config> {