Pallet assets improvements (#13543)

This commit is contained in:
Dino Pačandi
2023-03-13 16:44:47 +01:00
committed by GitHub
parent 2009821cde
commit efd5d98e10
4 changed files with 85 additions and 18 deletions
+36 -4
View File
@@ -91,12 +91,44 @@ impl pallet_balances::Config for Test {
pub struct AssetsCallbackHandle;
impl AssetsCallback<AssetId, AccountId> for AssetsCallbackHandle {
fn created(_id: &AssetId, _owner: &AccountId) {
storage::set(b"asset_created", &().encode());
fn created(_id: &AssetId, _owner: &AccountId) -> Result<(), ()> {
if Self::should_err() {
Err(())
} else {
storage::set(Self::CREATED.as_bytes(), &().encode());
Ok(())
}
}
fn destroyed(_id: &AssetId) {
storage::set(b"asset_destroyed", &().encode());
fn destroyed(_id: &AssetId) -> Result<(), ()> {
if Self::should_err() {
Err(())
} else {
storage::set(Self::DESTROYED.as_bytes(), &().encode());
Ok(())
}
}
}
impl AssetsCallbackHandle {
pub const CREATED: &'static str = "asset_created";
pub const DESTROYED: &'static str = "asset_destroyed";
const RETURN_ERROR: &'static str = "return_error";
// Configures `Self` to return `Ok` when callbacks are invoked
pub fn set_return_ok() {
storage::clear(Self::RETURN_ERROR.as_bytes());
}
// Configures `Self` to return `Err` when callbacks are invoked
pub fn set_return_error() {
storage::set(Self::RETURN_ERROR.as_bytes(), &().encode());
}
// If `true`, callback should return `Err`, `Ok` otherwise.
fn should_err() -> bool {
storage::exists(Self::RETURN_ERROR.as_bytes())
}
}