This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add CallbackHandle to pallet-assets #12307
Merged
ggwpez
merged 6 commits into
paritytech:master
from
AstarNetwork:feature/pallet-assets-with-callback
Dec 21, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe26401
Add CallbackHandle to pallet-assets
Dinonard e18185f
Address review comments
Dinonard d6153c4
Add use for sp_io::storage
Dinonard 0491fd6
Rebase & review comments
Dinonard 0c9ea66
Fix UT
Dinonard e409e8a
Merge branch 'paritytech:master' into feature/pallet-assets-with-call…
hoonsubin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,13 @@ | |
use super::*; | ||
use crate as pallet_assets; | ||
|
||
use codec::Encode; | ||
use frame_support::{ | ||
construct_runtime, parameter_types, | ||
traits::{AsEnsureOriginWithArg, ConstU32, ConstU64, GenesisBuild}, | ||
}; | ||
use sp_core::H256; | ||
use sp_io::storage; | ||
use sp_runtime::{ | ||
testing::Header, | ||
traits::{BlakeTwo256, IdentityLookup}, | ||
|
@@ -45,6 +47,9 @@ construct_runtime!( | |
} | ||
); | ||
|
||
type AccountId = u64; | ||
type AssetId = u32; | ||
|
||
impl frame_system::Config for Test { | ||
type BaseCallFilter = frame_support::traits::Everything; | ||
type BlockWeights = (); | ||
|
@@ -55,7 +60,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; | ||
|
@@ -84,6 +89,17 @@ 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) { | ||
storage::set(b"asset_created", &().encode()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can setup some helpers in your frame_support::parameter_types! {
pub storage AssetCreated: bool = false;
pub storage AssetDestroyed: bool = false;
} And then use them like |
||
} | ||
|
||
fn destroyed(_id: &AssetId) { | ||
storage::set(b"asset_destroyed", &().encode()); | ||
} | ||
} | ||
|
||
impl Config for Test { | ||
type RuntimeEvent = RuntimeEvent; | ||
type Balance = u64; | ||
|
@@ -100,6 +116,7 @@ impl Config for Test { | |
type StringLimit = ConstU32<50>; | ||
type Freezer = TestFreezer; | ||
type WeightInfo = (); | ||
type CallbackHandle = AssetsCallbackHandle; | ||
type Extra = (); | ||
type RemoveItemsLimit = ConstU32<5>; | ||
#[cfg(feature = "runtime-benchmarks")] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ use frame_support::{ | |
traits::{fungibles::InspectEnumerable, Currency}, | ||
}; | ||
use pallet_balances::Error as BalancesError; | ||
use sp_io::storage; | ||
use sp_runtime::{traits::ConvertInto, TokenError}; | ||
|
||
fn asset_ids() -> Vec<u32> { | ||
|
@@ -1194,3 +1195,32 @@ 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!(storage::get(b"asset_created").is_none()); | ||
assert!(storage::get(b"asset_destroyed").is_none()); | ||
|
||
Balances::make_free_balance_be(&1, 100); | ||
assert_ok!(Assets::create(RuntimeOrigin::signed(1), 0, 1, 1)); | ||
assert!(storage::get(b"asset_created").is_some()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe check that is actually true, and not just that it exists? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could do that, the nature of the check is that it's |
||
assert!(storage::get(b"asset_destroyed").is_none()); | ||
|
||
assert_ok!(Assets::start_destroy(RuntimeOrigin::signed(1), 0)); | ||
assert_ok!(Assets::destroy_accounts(RuntimeOrigin::signed(1), 0)); | ||
assert_ok!(Assets::destroy_approvals(RuntimeOrigin::signed(1), 0)); | ||
assert_ok!(Assets::finish_destroy(RuntimeOrigin::signed(1), 0)); | ||
assert!(storage::get(b"asset_destroyed").is_some()); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn root_asset_create_should_work() { | ||
new_test_ext().execute_with(|| { | ||
assert!(storage::get(b"asset_created").is_none()); | ||
assert_ok!(Assets::force_create(RuntimeOrigin::root(), 0, 1, true, 1)); | ||
assert!(storage::get(b"asset_created").is_some()); | ||
assert!(storage::get(b"asset_destroyed").is_none()); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Sorry am late to the party.), was just wondering if
OnCreated = (); OnDestroyed = ()
might make it a bit more similar to other pallet callbacks?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My idea was to keep it simple by only having one type but I do like your suggestion. It's more verbose :)