#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(test)]
pub(crate) mod mock;
#[cfg(test)]
mod tests;
extern crate alloc;
use frame_support::{
defensive,
pallet_prelude::*,
traits::{
fungible::{Balanced, Credit, Inspect, Mutate, Unbalanced},
Imbalance, OnUnbalanced,
},
PalletId,
};
pub use pallet::*;
const LOG_TARGET: &str = "runtime::dap";
pub type BalanceOf<T> =
<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{sp_runtime::traits::AccountIdConversion, traits::StorageVersion};
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
#[pallet::config]
pub trait Config: frame_system::Config {
type Currency: Inspect<Self::AccountId>
+ Mutate<Self::AccountId>
+ Balanced<Self::AccountId>;
#[pallet::constant]
type PalletId: Get<PalletId>;
}
impl<T: Config> Pallet<T> {
pub fn buffer_account() -> T::AccountId {
T::PalletId::get().into_account_truncating()
}
}
}
pub type CreditOf<T> = Credit<<T as frame_system::Config>::AccountId, <T as Config>::Currency>;
impl<T: Config> OnUnbalanced<CreditOf<T>> for Pallet<T> {
fn on_nonzero_unbalanced(amount: CreditOf<T>) {
let buffer = Self::buffer_account();
let numeric_amount = amount.peek();
let _ = T::Currency::resolve(&buffer, amount)
.inspect_err(|_| {
defensive!("🚨 Failed to deposit slash to DAP buffer - funds burned, it should never happen!");
})
.inspect(|_| {
<T::Currency as Unbalanced<T::AccountId>>::deactivate(numeric_amount);
log::debug!(
target: LOG_TARGET,
"💸 Deposited slash of {numeric_amount:?} to DAP buffer"
);
});
}
}