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.
This commit is contained in:
Alexander Koz
2019-06-12 11:48:27 +03:00
committed by Bastian Köcher
parent f0f32f9250
commit e53e9e9bda
+17 -11
View File
@@ -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<T: Trait> for enum Call where origin: T::Origin {
@@ -145,7 +147,7 @@ decl_module! {
let origin = ensure_signed(origin)?;
let id = Self::next_asset_id();
<NextAssetId<T>>::mutate(|id| *id += 1);
<NextAssetId<T>>::mutate(|id| *id += One::one());
<Balances<T>>::insert((id, origin.clone()), total);
<TotalSupply<T>>::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: <T::Lookup as StaticLookup>::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 = <Balances<T>>::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<T> where <T as system::Trait>::AccountId, <T as Trait>::Balance {
pub enum Event<T>
where <T as system::Trait>::AccountId,
<T as Trait>::Balance,
<T as Trait>::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<T: Trait> 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<T: Trait> Module<T> {
// 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 {
<Balances<T>>::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 {
<TotalSupply<T>>::get(id)
}
}
@@ -260,6 +265,7 @@ mod tests {
impl Trait for Test {
type Event = ();
type Balance = u64;
type AssetId = u32;
}
type Assets = Module<Test>;