36
36
37
37
#![ cfg_attr( not( feature = "std" ) , no_std) ]
38
38
39
- use codec:: { Decode , Encode , FullCodec } ;
39
+ use codec:: FullCodec ;
40
40
use frame_support:: {
41
41
decl_error, decl_event, decl_module, decl_storage, dispatch, ensure,
42
42
traits:: { EnsureOrigin , Get } ,
43
43
Hashable ,
44
44
} ;
45
45
use frame_system:: ensure_signed;
46
- use sp_runtime:: {
47
- traits:: { Hash , Member } ,
48
- RuntimeDebug ,
49
- } ;
50
- use sp_std:: {
51
- cmp:: { Eq , Ordering } ,
52
- fmt:: Debug ,
53
- vec:: Vec ,
54
- } ;
46
+ use sp_runtime:: traits:: { Hash , Member } ;
47
+ use sp_std:: { cmp:: Eq , fmt:: Debug , vec:: Vec } ;
55
48
56
49
pub mod nft;
57
- pub use crate :: nft:: { UniqueAssets , NFT } ;
50
+ pub use crate :: nft:: UniqueAssets ;
58
51
59
52
#[ cfg( test) ]
60
53
mod mock;
@@ -66,7 +59,7 @@ pub trait Trait<I = DefaultInstance>: frame_system::Trait {
66
59
/// The dispatch origin that is able to mint new instances of this type of commodity.
67
60
type CommodityAdmin : EnsureOrigin < Self :: Origin > ;
68
61
/// The data type that is used to describe this type of commodity.
69
- type CommodityInfo : Hashable + Member + Debug + Default + FullCodec ;
62
+ type CommodityInfo : Hashable + Member + Debug + Default + FullCodec + Ord ;
70
63
/// The maximum number of this type of commodity that may exist (minted - burned).
71
64
type CommodityLimit : Get < u128 > ;
72
65
/// The maximum number of this type of commodity that any single account may own.
@@ -77,39 +70,8 @@ pub trait Trait<I = DefaultInstance>: frame_system::Trait {
77
70
/// The runtime system's hashing algorithm is used to uniquely identify commodities.
78
71
pub type CommodityId < T > = <T as frame_system:: Trait >:: Hash ;
79
72
80
- /// A generic definition of an NFT that will be used by this pallet.
81
- #[ derive( Encode , Decode , Clone , Eq , RuntimeDebug ) ]
82
- pub struct Commodity < Hash , CommodityInfo > {
83
- pub id : Hash ,
84
- pub commodity : CommodityInfo ,
85
- }
86
-
87
- /// An alias for this pallet's NFT implementation.
88
- pub type CommodityFor < T , I > = Commodity < CommodityId < T > , <T as Trait < I > >:: CommodityInfo > ;
89
-
90
- impl < CommodityId , CommodityInfo > NFT for Commodity < CommodityId , CommodityInfo > {
91
- type Id = CommodityId ;
92
- type Info = CommodityInfo ;
93
- }
94
-
95
- // Needed to maintain a sorted list.
96
- impl < CommodityId : Ord , CommodityInfo : Eq > Ord for Commodity < CommodityId , CommodityInfo > {
97
- fn cmp ( & self , other : & Self ) -> Ordering {
98
- self . id . cmp ( & other. id )
99
- }
100
- }
101
-
102
- impl < CommodityId : Ord , CommodityInfo > PartialOrd for Commodity < CommodityId , CommodityInfo > {
103
- fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
104
- Some ( self . id . cmp ( & other. id ) )
105
- }
106
- }
107
-
108
- impl < CommodityId : Eq , CommodityInfo > PartialEq for Commodity < CommodityId , CommodityInfo > {
109
- fn eq ( & self , other : & Self ) -> bool {
110
- self . id == other. id
111
- }
112
- }
73
+ /// Associates a commodity with its ID.
74
+ pub type Commodity < T , I > = ( CommodityId < T > , <T as Trait < I > >:: CommodityInfo ) ;
113
75
114
76
decl_storage ! {
115
77
trait Store for Module <T : Trait <I >, I : Instance = DefaultInstance > as Commodity {
@@ -120,7 +82,7 @@ decl_storage! {
120
82
/// The total number of this type of commodity owned by an account.
121
83
TotalForAccount get( fn total_for_account) : map hasher( blake2_128_concat) T :: AccountId => u64 = 0 ;
122
84
/// A mapping from an account to a list of all of the commodities of this type that are owned by it.
123
- CommoditiesForAccount get( fn commodities_for_account) : map hasher( blake2_128_concat) T :: AccountId => Vec <CommodityFor <T , I >>;
85
+ CommoditiesForAccount get( fn commodities_for_account) : map hasher( blake2_128_concat) T :: AccountId => Vec <Commodity <T , I >>;
124
86
/// A mapping from a commodity ID to the account that owns it.
125
87
AccountForCommodity get( fn account_for_commodity) : map hasher( identity) CommodityId <T > => T :: AccountId ;
126
88
}
@@ -130,7 +92,7 @@ decl_storage! {
130
92
build( |config: & GenesisConfig <T , I >| {
131
93
for ( who, assets) in config. balances. iter( ) {
132
94
for asset in assets {
133
- match <Module :: <T , I > as UniqueAssets :: <Commodity < CommodityId < T > , < T as Trait < I >> :: CommodityInfo > >>:: mint( who, asset. clone( ) ) {
95
+ match <Module :: <T , I > as UniqueAssets :: <T :: AccountId >>:: mint( who, asset. clone( ) ) {
134
96
Ok ( _) => { }
135
97
Err ( err) => { panic!( err) } ,
136
98
}
@@ -237,10 +199,9 @@ decl_module! {
237
199
}
238
200
}
239
201
240
- impl < T : Trait < I > , I : Instance >
241
- UniqueAssets < Commodity < CommodityId < T > , <T as Trait < I > >:: CommodityInfo > > for Module < T , I >
242
- {
243
- type AccountId = <T as frame_system:: Trait >:: AccountId ;
202
+ impl < T : Trait < I > , I : Instance > UniqueAssets < T :: AccountId > for Module < T , I > {
203
+ type AssetId = CommodityId < T > ;
204
+ type AssetInfo = T :: CommodityInfo ;
244
205
type AssetLimit = T :: CommodityLimit ;
245
206
type UserAssetLimit = T :: UserCommodityLimit ;
246
207
@@ -256,9 +217,7 @@ impl<T: Trait<I>, I: Instance>
256
217
Self :: total_for_account ( account)
257
218
}
258
219
259
- fn assets_for_account (
260
- account : & T :: AccountId ,
261
- ) -> Vec < Commodity < CommodityId < T > , <T as Trait < I > >:: CommodityInfo > > {
220
+ fn assets_for_account ( account : & T :: AccountId ) -> Vec < Commodity < T , I > > {
262
221
Self :: commodities_for_account ( account)
263
222
}
264
223
@@ -287,10 +246,7 @@ impl<T: Trait<I>, I: Instance>
287
246
Error :: <T , I >:: TooManyCommodities
288
247
) ;
289
248
290
- let new_commodity = Commodity {
291
- id : commodity_id,
292
- commodity : commodity_info,
293
- } ;
249
+ let new_commodity = ( commodity_id, commodity_info) ;
294
250
295
251
Total :: < I > :: mutate ( |total| * total += 1 ) ;
296
252
TotalForAccount :: < T , I > :: mutate ( owner_account, |total| * total += 1 ) ;
@@ -312,10 +268,7 @@ impl<T: Trait<I>, I: Instance>
312
268
Error :: <T , I >:: NonexistentCommodity
313
269
) ;
314
270
315
- let burn_commodity = Commodity :: < CommodityId < T > , <T as Trait < I > >:: CommodityInfo > {
316
- id : * commodity_id,
317
- commodity : <T as Trait < I > >:: CommodityInfo :: default ( ) ,
318
- } ;
271
+ let burn_commodity = ( * commodity_id, <T as Trait < I > >:: CommodityInfo :: default ( ) ) ;
319
272
320
273
Total :: < I > :: mutate ( |total| * total -= 1 ) ;
321
274
Burned :: < I > :: mutate ( |total| * total += 1 ) ;
@@ -346,10 +299,7 @@ impl<T: Trait<I>, I: Instance>
346
299
Error :: <T , I >:: TooManyCommoditiesForAccount
347
300
) ;
348
301
349
- let xfer_commodity = Commodity :: < CommodityId < T > , <T as Trait < I > >:: CommodityInfo > {
350
- id : * commodity_id,
351
- commodity : <T as Trait < I > >:: CommodityInfo :: default ( ) ,
352
- } ;
302
+ let xfer_commodity = ( * commodity_id, <T as Trait < I > >:: CommodityInfo :: default ( ) ) ;
353
303
354
304
TotalForAccount :: < T , I > :: mutate ( & owner, |total| * total -= 1 ) ;
355
305
TotalForAccount :: < T , I > :: mutate ( dest_account, |total| * total += 1 ) ;
0 commit comments