Add CallbackHandle to pallet-assets (#12307)

* Add CallbackHandle to pallet-assets

* Address review comments

* Add use for sp_io::storage

* Rebase & review comments

* Fix UT

Co-authored-by: Hoon Kim <mail@hoonkim.me>
This commit is contained in:
Dino Pačandi
2022-12-21 14:03:49 +01:00
committed by GitHub
parent c237b82690
commit 9cdb920462
6 changed files with 81 additions and 3 deletions
+28 -1
View File
@@ -116,6 +116,11 @@
//!
//! Please refer to the [`Pallet`] struct for details on publicly available functions.
//!
//! ### Callbacks
//!
//! Using `CallbackHandle` associated type, user can configure custom callback functions which are
//! executed when new asset is created or an existing asset is destroyed.
//!
//! ## Related Modules
//!
//! * [`System`](../frame_system/index.html)
@@ -171,6 +176,18 @@ pub use weights::WeightInfo;
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
/// Trait with callbacks that are executed after successfull asset creation or destruction.
pub trait AssetsCallback<AssetId, AccountId> {
/// Indicates that asset with `id` was successfully created by the `owner`
fn created(_id: &AssetId, _owner: &AccountId) {}
/// Indicates that asset with `id` has just been destroyed
fn destroyed(_id: &AssetId) {}
}
/// Empty implementation in case no callbacks are required.
impl<AssetId, AccountId> AssetsCallback<AssetId, AccountId> for () {}
#[frame_support::pallet]
pub mod pallet {
use super::*;
@@ -283,6 +300,9 @@ pub mod pallet {
/// Additional data to be stored with an account's asset balance.
type Extra: Member + Parameter + Default + MaxEncodedLen;
/// Callback methods for asset state change (e.g. asset created or destroyed)
type CallbackHandle: AssetsCallback<Self::AssetId, Self::AccountId>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
@@ -598,7 +618,14 @@ pub mod pallet {
status: AssetStatus::Live,
},
);
Self::deposit_event(Event::Created { asset_id: id, creator: owner, owner: admin });
Self::deposit_event(Event::Created {
asset_id: id,
creator: owner.clone(),
owner: admin,
});
T::CallbackHandle::created(&id, &owner);
Ok(())
}