Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Oct 19, 2022
1 parent 6c3effa commit 8ad9cf5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
4 changes: 2 additions & 2 deletions frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
is_frozen: false,
},
);
Self::deposit_event(Event::ForceCreated { asset_id: id, owner: owner.clone() });
T::CallbackHandle::created(&id, &owner);
Self::deposit_event(Event::ForceCreated { asset_id: id, owner });
Ok(())
}

Expand Down Expand Up @@ -704,8 +704,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
T::Currency::unreserve(&owner, approval.deposit);
}

Self::deposit_event(Event::Destroyed { asset_id: id.clone() });
T::CallbackHandle::destroyed(&id);
Self::deposit_event(Event::Destroyed { asset_id: id });

Ok(DestroyWitness {
accounts: details.accounts,
Expand Down
8 changes: 7 additions & 1 deletion frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,8 +553,14 @@ pub mod pallet {
is_frozen: false,
},
);

Self::deposit_event(Event::Created {
asset_id: id,
creator: owner.clone(),
owner: admin,
});
T::CallbackHandle::created(&id, &owner);
Self::deposit_event(Event::Created { asset_id: id, creator: owner, owner: admin });

Ok(())
}

Expand Down
21 changes: 18 additions & 3 deletions frame/assets/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use super::*;
use crate as pallet_assets;

use codec::Encode;
use frame_support::{
construct_runtime, parameter_types,
traits::{ConstU32, ConstU64, GenesisBuild},
Expand All @@ -45,6 +46,9 @@ construct_runtime!(
}
);

type AccountId = u64;
type AssetId = u32;

impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
Expand All @@ -55,7 +59,7 @@ impl frame_system::Config for Test {
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
Expand Down Expand Up @@ -84,10 +88,21 @@ impl pallet_balances::Config for Test {
type ReserveIdentifier = [u8; 8];
}

pub struct AssetsCallbackHandle;
impl AssetsCallback<AssetId, AccountId> for AssetsCallbackHandle {
fn created(_id: &AssetId, _owner: &AccountId) {
sp_io::storage::set(b"asset_created", &true.encode());
}

fn destroyed(_id: &AssetId) {
sp_io::storage::set(b"asset_destroyed", &true.encode());
}
}

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type Balance = u64;
type AssetId = u32;
type AssetId = AssetId;
type Currency = Balances;
type ForceOrigin = frame_system::EnsureRoot<u64>;
type AssetDeposit = ConstU64<1>;
Expand All @@ -98,7 +113,7 @@ impl Config for Test {
type StringLimit = ConstU32<50>;
type Freezer = TestFreezer;
type WeightInfo = ();
type CallbackHandle = ();
type CallbackHandle = AssetsCallbackHandle;
type Extra = ();
}

Expand Down
27 changes: 27 additions & 0 deletions frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,3 +999,30 @@ fn querying_roles_should_work() {
assert_eq!(Assets::freezer(0), Some(4));
});
}

#[test]
fn normal_asset_create_and_destroy_callbacks_should_work() {
new_test_ext().execute_with(|| {
assert!(sp_io::storage::get(b"asset_created").is_none());
assert!(sp_io::storage::get(b"asset_destroyed").is_none());

Balances::make_free_balance_be(&1, 100);
assert_ok!(Assets::create(Origin::signed(1), 0, 1, 1));
assert!(sp_io::storage::get(b"asset_created").is_some());
assert!(sp_io::storage::get(b"asset_destroyed").is_none());

let w = Asset::<Test>::get(0).unwrap().destroy_witness();
assert_ok!(Assets::destroy(Origin::signed(1), 0, w));
assert!(sp_io::storage::get(b"asset_destroyed").is_some());
});
}

#[test]
fn root_asset_create_should_work() {
new_test_ext().execute_with(|| {
assert!(sp_io::storage::get(b"asset_created").is_none());
assert_ok!(Assets::force_create(Origin::root(), 0, 1, true, 1));
assert!(sp_io::storage::get(b"asset_created").is_some());
assert!(sp_io::storage::get(b"asset_destroyed").is_none());
});
}

0 comments on commit 8ad9cf5

Please sign in to comment.