pallet-staking event

This commit is contained in:
Luke Parker
2023-10-22 03:19:01 -04:00
parent d29d19bdfe
commit 52eb68677a
2 changed files with 45 additions and 6 deletions

View File

@@ -248,7 +248,9 @@ impl coins::Config for Runtime {
impl validator_sets::Config for Runtime { impl validator_sets::Config for Runtime {
type RuntimeEvent = RuntimeEvent; type RuntimeEvent = RuntimeEvent;
} }
impl staking::Config for Runtime {} impl staking::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
}
pub struct IdentityValidatorIdOf; pub struct IdentityValidatorIdOf;
impl Convert<PublicKey, Option<PublicKey>> for IdentityValidatorIdOf { impl Convert<PublicKey, Option<PublicKey>> for IdentityValidatorIdOf {

View File

@@ -24,12 +24,35 @@ pub mod pallet {
NoDeallocation, NoDeallocation,
} }
// TODO: Event #[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Staked {
validator: T::AccountId,
amount: Amount,
},
Unstaked {
validator: T::AccountId,
amount: Amount,
},
ImmediateDeallocation {
validator: T::AccountId,
network: NetworkId,
amount: Amount,
},
DeallocationClaimed {
validator: T::AccountId,
network: NetworkId,
session: Session,
amount: Amount,
},
}
#[pallet::config] #[pallet::config]
pub trait Config: pub trait Config:
frame_system::Config + CoinsConfig + VsConfig + SessionConfig<ValidatorId = PublicKey> frame_system::Config + CoinsConfig + VsConfig + SessionConfig<ValidatorId = PublicKey>
{ {
type RuntimeEvent: IsType<<Self as frame_system::Config>::RuntimeEvent> + From<Event<Self>>;
} }
#[pallet::pallet] #[pallet::pallet]
@@ -52,17 +75,19 @@ pub mod pallet {
T::AccountId::decode(&mut TrailingZeroInput::new(b"staking")).unwrap() T::AccountId::decode(&mut TrailingZeroInput::new(b"staking")).unwrap()
} }
fn add_stake(account: &T::AccountId, amount: u64) { fn add_stake(account: T::AccountId, amount: u64) {
Staked::<T>::mutate(account, |staked| *staked += amount); Staked::<T>::mutate(account, |staked| *staked += amount);
Self::deposit_event(Event::Staked { validator: account, amount: Amount(amount) });
} }
fn remove_stake(account: &T::AccountId, amount: u64) -> Result<(), Error<T>> { fn remove_stake(account: T::AccountId, amount: u64) -> Result<(), Error<T>> {
Staked::<T>::mutate(account, |staked| { Staked::<T>::mutate(account, |staked| {
let available = *staked - Self::allocated(account); let available = *staked - Self::allocated(account);
if available < amount { if available < amount {
Err(Error::<T>::StakeUnavilable)?; Err(Error::<T>::StakeUnavilable)?;
} }
*staked -= amount; *staked -= amount;
Self::deposit_event(Event::Unstaked { validator: account, amount: Amount(amount) });
Ok::<_, Error<T>>(()) Ok::<_, Error<T>>(())
}) })
} }
@@ -98,7 +123,7 @@ pub mod pallet {
let signer = ensure_signed(origin)?; let signer = ensure_signed(origin)?;
let balance = Balance { coin: Coin::Serai, amount: Amount(amount) }; let balance = Balance { coin: Coin::Serai, amount: Amount(amount) };
Coins::<T>::transfer_internal(signer, Self::account(), balance)?; Coins::<T>::transfer_internal(signer, Self::account(), balance)?;
Self::add_stake(&signer, amount); Self::add_stake(signer, amount);
Ok(()) Ok(())
} }
@@ -107,7 +132,7 @@ pub mod pallet {
#[pallet::weight((0, DispatchClass::Operational))] // TODO #[pallet::weight((0, DispatchClass::Operational))] // TODO
pub fn unstake(origin: OriginFor<T>, #[pallet::compact] amount: u64) -> DispatchResult { pub fn unstake(origin: OriginFor<T>, #[pallet::compact] amount: u64) -> DispatchResult {
let signer = ensure_signed(origin)?; let signer = ensure_signed(origin)?;
Self::remove_stake(&signer, amount)?; Self::remove_stake(signer, amount)?;
let balance = Balance { coin: Coin::Serai, amount: Amount(amount) }; let balance = Balance { coin: Coin::Serai, amount: Amount(amount) };
Coins::<T>::transfer_internal(Self::account(), signer, balance)?; Coins::<T>::transfer_internal(Self::account(), signer, balance)?;
Ok(()) Ok(())
@@ -125,6 +150,7 @@ pub mod pallet {
// add to amount allocated // add to amount allocated
Self::allocate_internal(&account, amount)?; Self::allocate_internal(&account, amount)?;
// This does not emit an event as the validator-sets pallet will
// increase allocation for participant in validator set // increase allocation for participant in validator set
VsPallet::<T>::increase_allocation(network, account, Amount(amount))?; VsPallet::<T>::increase_allocation(network, account, Amount(amount))?;
@@ -146,6 +172,11 @@ pub mod pallet {
VsPallet::<T>::decrease_allocation(network, account, Amount(amount))?; VsPallet::<T>::decrease_allocation(network, account, Amount(amount))?;
if can_immediately_deallocate { if can_immediately_deallocate {
Self::deallocate_internal(&account, amount)?; Self::deallocate_internal(&account, amount)?;
Self::deposit_event(Event::ImmediateDeallocation {
validator: account,
network,
amount: Amount(amount),
});
} }
Ok(()) Ok(())
@@ -163,6 +194,12 @@ pub mod pallet {
Err(Error::<T>::NoDeallocation)? Err(Error::<T>::NoDeallocation)?
}; };
Self::deallocate_internal(&account, amount.0)?; Self::deallocate_internal(&account, amount.0)?;
Self::deposit_event(Event::DeallocationClaimed {
validator: account,
network,
session,
amount,
});
Ok(()) Ok(())
} }
} }