From e53e9e9bda47300fb4e2964c72fabc77be4c4d4b Mon Sep 17 00:00:00 2001 From: Alexander Koz Date: Wed, 12 Jun 2019 11:48:27 +0300 Subject: [PATCH] srml-assets: AssetID should be generic parameter (#2838) * improved srml-assets: AssetId is generic type-parameter now. * rem already imported Codec; use primitives::One instead into-convertion. --- substrate/srml/assets/src/lib.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/substrate/srml/assets/src/lib.rs b/substrate/srml/assets/src/lib.rs index 3f7c1b3efc..e0a99b7d16 100644 --- a/substrate/srml/assets/src/lib.rs +++ b/substrate/srml/assets/src/lib.rs @@ -123,6 +123,7 @@ use srml_support::{StorageValue, StorageMap, Parameter, decl_module, decl_event, decl_storage, ensure}; use primitives::traits::{Member, SimpleArithmetic, Zero, StaticLookup}; use system::ensure_signed; +use primitives::traits::One; /// The module configuration trait. pub trait Trait: system::Trait { @@ -131,9 +132,10 @@ pub trait Trait: system::Trait { /// The units in which we record balances. type Balance: Member + Parameter + SimpleArithmetic + Default + Copy; -} -type AssetId = u32; + /// The arithmetic type of asset identifier. + type AssetId: Parameter + SimpleArithmetic + Default + Copy; +} decl_module! { pub struct Module for enum Call where origin: T::Origin { @@ -145,7 +147,7 @@ decl_module! { let origin = ensure_signed(origin)?; let id = Self::next_asset_id(); - >::mutate(|id| *id += 1); + >::mutate(|id| *id += One::one()); >::insert((id, origin.clone()), total); >::insert(id, total); @@ -155,7 +157,7 @@ decl_module! { /// Move some assets from one holder to another. fn transfer(origin, - #[compact] id: AssetId, + #[compact] id: T::AssetId, target: ::Source, #[compact] amount: T::Balance ) { @@ -172,7 +174,7 @@ decl_module! { } /// Destroy any assets of `id` owned by `origin`. - fn destroy(origin, #[compact] id: AssetId) { + fn destroy(origin, #[compact] id: T::AssetId) { let origin = ensure_signed(origin)?; let balance = >::take((id, origin.clone())); ensure!(!balance.is_zero(), "origin balance should be non-zero"); @@ -184,7 +186,10 @@ decl_module! { } decl_event!( - pub enum Event where ::AccountId, ::Balance { + pub enum Event + where ::AccountId, + ::Balance, + ::AssetId { /// Some assets were issued. Issued(AssetId, AccountId, Balance), /// Some assets were transferred. @@ -197,11 +202,11 @@ decl_event!( decl_storage! { trait Store for Module as Assets { /// The number of units of assets held by any given account. - Balances: map (AssetId, T::AccountId) => T::Balance; + Balances: map (T::AssetId, T::AccountId) => T::Balance; /// The next asset identifier up for grabs. - NextAssetId get(next_asset_id): AssetId; + NextAssetId get(next_asset_id): T::AssetId; /// The total unit supply of an asset. - TotalSupply: map AssetId => T::Balance; + TotalSupply: map T::AssetId => T::Balance; } } @@ -210,12 +215,12 @@ impl Module { // Public immutables /// Get the asset `id` balance of `who`. - pub fn balance(id: AssetId, who: T::AccountId) -> T::Balance { + pub fn balance(id: T::AssetId, who: T::AccountId) -> T::Balance { >::get((id, who)) } /// Get the total supply of an asset `id`. - pub fn total_supply(id: AssetId) -> T::Balance { + pub fn total_supply(id: T::AssetId) -> T::Balance { >::get(id) } } @@ -260,6 +265,7 @@ mod tests { impl Trait for Test { type Event = (); type Balance = u64; + type AssetId = u32; } type Assets = Module;