Skip to content

Commit

Permalink
Add decimals for foreign assets in assets-registry
Browse files Browse the repository at this point in the history
  • Loading branch information
andor0 committed Jan 25, 2022
1 parent ff39c92 commit 12e35bb
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
31 changes: 31 additions & 0 deletions frame/assets-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ pub mod pallet {
ForeignAdminApproved,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode, TypeInfo)]
pub struct Metadata {
pub decimals: u8,
}

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
Expand Down Expand Up @@ -114,6 +119,12 @@ pub mod pallet {
OptionQuery,
>;

#[pallet::storage]
#[pallet::getter(fn asset_metadata)]
/// Mapping local asset to asset metadata.
pub type AssetMetadata<T: Config> =
StorageMap<_, Blake2_128Concat, T::LocalAssetId, Metadata, OptionQuery>;

#[pallet::type_value]
pub fn LocalAdminOnEmpty<T: Config>() -> T::AccountId {
T::AccountId::default()
Expand Down Expand Up @@ -170,13 +181,15 @@ pub mod pallet {
local_asset_id: T::LocalAssetId,
foreign_asset_id: T::ForeignAssetId,
},
AssetMetadataUpdated(T::LocalAssetId),
}

#[pallet::error]
pub enum Error<T> {
OnlyAllowedForAdmins,
LocalAssetIdAlreadyUsed,
ForeignAssetIdAlreadyUsed,
LocalAssetIdNotFound,
}

#[pallet::call]
Expand Down Expand Up @@ -226,6 +239,24 @@ pub mod pallet {
});
Ok(().into())
}

#[pallet::weight(10_000)]
pub fn set_metadata(
origin: OriginFor<T>,
local_asset_id: T::LocalAssetId,
metadata: Metadata,
) -> DispatchResultWithPostInfo {
let _who = ensure_signed(origin.clone())?;
Self::ensure_admins_only(origin)?;
ensure!(
<LocalToForeign<T>>::contains_key(local_asset_id),
Error::<T>::LocalAssetIdNotFound
);

<AssetMetadata<T>>::insert(local_asset_id, metadata);
Self::deposit_event(Event::AssetMetadataUpdated(local_asset_id));
Ok(().into())
}
}

impl<T: Config> RemoteAssetRegistry for Pallet<T> {
Expand Down
34 changes: 34 additions & 0 deletions frame/assets-registry/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,37 @@ fn approve_assets_mapping_candidate_tests() {
);
})
}

#[test]
fn set_metadata_tests() {
new_test_ext().execute_with(|| {
let (local_asset_id, foreign_asset_id) = (0, 100);
assert_ok!(AssetsRegistry::set_local_admin(Origin::signed(ROOT), ALICE));
assert_ok!(AssetsRegistry::set_foreign_admin(Origin::signed(ROOT), BOB));

assert_noop!(
AssetsRegistry::set_metadata(
Origin::signed(ALICE),
local_asset_id,
Metadata { decimals: 12 }
),
Error::<Test>::LocalAssetIdNotFound
);

assert_ok!(AssetsRegistry::approve_assets_mapping_candidate(
Origin::signed(ALICE),
local_asset_id,
foreign_asset_id
));
assert_ok!(AssetsRegistry::approve_assets_mapping_candidate(
Origin::signed(BOB),
local_asset_id,
foreign_asset_id
));
assert_ok!(AssetsRegistry::set_metadata(
Origin::signed(ALICE),
local_asset_id,
Metadata { decimals: 12 }
));
})
}

0 comments on commit 12e35bb

Please sign in to comment.