-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathlib.rs
More file actions
321 lines (277 loc) · 9.77 KB
/
Copy pathlib.rs
File metadata and controls
321 lines (277 loc) · 9.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//! # Non Fungible Token
//! The module provides implementations for non-fungible-token.
//!
//! - [`Config`](./trait.Config.html)
//! - [`Call`](./enum.Call.html)
//! - [`Module`](./struct.Module.html)
//!
//! ## Overview
//!
//! This module provides basic functions to create and manager
//! NFT(non fungible token) such as `create_class`, `transfer`, `mint`, `burn`.
//! ### Module Functions
//!
//! - `create_class` - Create NFT(non fungible token) class
//! - `transfer` - Transfer NFT(non fungible token) to another account.
//! - `mint` - Mint NFT(non fungible token)
//! - `burn` - Burn NFT(non fungible token)
//! - `destroy_class` - Destroy NFT(non fungible token) class
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::unused_unit)]
use frame_support::{ensure, pallet_prelude::*, traits::Get, BoundedVec, Parameter};
use frame_system::pallet_prelude::*;
use parity_scale_codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_runtime::{
traits::{AtLeast32BitUnsigned, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Member, One, Zero},
ArithmeticError, DispatchError, DispatchResult, RuntimeDebug,
};
use sp_std::vec::Vec;
mod mock;
mod tests;
/// Class info
#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, MaxEncodedLen, RuntimeDebug, TypeInfo)]
pub struct ClassInfo<TokenId, AccountId, Data, ClassMetadataOf> {
/// Class metadata
pub metadata: ClassMetadataOf,
/// Total issuance for the class
pub total_issuance: TokenId,
/// Class owner
pub owner: AccountId,
/// Class Properties
pub data: Data,
}
/// Token info
#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Eq, PartialEq, MaxEncodedLen, RuntimeDebug, TypeInfo)]
pub struct TokenInfo<AccountId, Data, TokenMetadataOf> {
/// Token metadata
pub metadata: TokenMetadataOf,
/// Token owner
pub owner: AccountId,
/// Token Properties
pub data: Data,
}
pub use module::*;
#[frame_support::pallet]
pub mod module {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
/// The class ID type
type ClassId: Parameter + Member + AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen;
/// The token ID type
type TokenId: Parameter + Member + AtLeast32BitUnsigned + Default + Copy + MaxEncodedLen;
/// The class properties type
type ClassData: Parameter + Member + MaybeSerializeDeserialize;
/// The token properties type
type TokenData: Parameter + Member + MaybeSerializeDeserialize;
/// The maximum size of a class's metadata
type MaxClassMetadata: Get<u32>;
/// The maximum size of a token's metadata
type MaxTokenMetadata: Get<u32>;
}
pub type ClassMetadataOf<T> = BoundedVec<u8, <T as Config>::MaxClassMetadata>;
pub type TokenMetadataOf<T> = BoundedVec<u8, <T as Config>::MaxTokenMetadata>;
pub type ClassInfoOf<T> = ClassInfo<
<T as Config>::TokenId,
<T as frame_system::Config>::AccountId,
<T as Config>::ClassData,
ClassMetadataOf<T>,
>;
pub type TokenInfoOf<T> =
TokenInfo<<T as frame_system::Config>::AccountId, <T as Config>::TokenData, TokenMetadataOf<T>>;
pub type GenesisTokenData<T> = (
<T as frame_system::Config>::AccountId, // Token owner
Vec<u8>, // Token metadata
<T as Config>::TokenData,
);
pub type GenesisTokens<T> = (
<T as frame_system::Config>::AccountId, // Token class owner
Vec<u8>, // Token class metadata
<T as Config>::ClassData,
Vec<GenesisTokenData<T>>, // Vector of tokens belonging to this class
);
/// Error for non-fungible-token module.
#[pallet::error]
pub enum Error<T> {
/// No available class ID
NoAvailableClassId,
/// No available token ID
NoAvailableTokenId,
/// Token(ClassId, TokenId) not found
TokenNotFound,
/// Class not found
ClassNotFound,
/// The operator is not the owner of the token and has no permission
NoPermission,
/// Can not destroy class
/// Total issuance is not 0
CannotDestroyClass,
/// Failed because the Maximum amount of metadata was exceeded
MaxMetadataExceeded,
}
/// Next available class ID.
#[pallet::storage]
#[pallet::getter(fn next_class_id)]
pub type NextClassId<T: Config> = StorageValue<_, T::ClassId, ValueQuery>;
/// Next available token ID.
#[pallet::storage]
#[pallet::getter(fn next_token_id)]
pub type NextTokenId<T: Config> = StorageMap<_, Twox64Concat, T::ClassId, T::TokenId, ValueQuery>;
/// Store class info.
///
/// Returns `None` if class info not set or removed.
#[pallet::storage]
#[pallet::getter(fn classes)]
pub type Classes<T: Config> = StorageMap<_, Twox64Concat, T::ClassId, ClassInfoOf<T>>;
/// Store token info.
///
/// Returns `None` if token info not set or removed.
#[pallet::storage]
#[pallet::getter(fn tokens)]
pub type Tokens<T: Config> =
StorageDoubleMap<_, Twox64Concat, T::ClassId, Twox64Concat, T::TokenId, TokenInfoOf<T>>;
/// Token existence check by owner and class ID.
#[pallet::storage]
#[pallet::getter(fn tokens_by_owner)]
pub type TokensByOwner<T: Config> = StorageNMap<
_,
(
NMapKey<Blake2_128Concat, T::AccountId>, // owner
NMapKey<Blake2_128Concat, T::ClassId>,
NMapKey<Blake2_128Concat, T::TokenId>,
),
(),
ValueQuery,
>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub tokens: Vec<GenesisTokens<T>>,
}
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
tokens: Default::default(),
}
}
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
self.tokens.iter().for_each(|token_class| {
let class_id = Pallet::<T>::create_class(&token_class.0, token_class.1.to_vec(), token_class.2.clone())
.expect("Create class cannot fail while building genesis");
for (account_id, token_metadata, token_data) in &token_class.3 {
Pallet::<T>::mint(account_id, class_id, token_metadata.to_vec(), token_data.clone())
.expect("Token mint cannot fail during genesis");
}
})
}
}
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
}
impl<T: Config> Pallet<T> {
/// Create NFT(non fungible token) class
pub fn create_class(
owner: &T::AccountId,
metadata: Vec<u8>,
data: T::ClassData,
) -> Result<T::ClassId, DispatchError> {
let bounded_metadata: BoundedVec<u8, T::MaxClassMetadata> =
metadata.try_into().map_err(|_| Error::<T>::MaxMetadataExceeded)?;
let class_id = NextClassId::<T>::try_mutate(|id| -> Result<T::ClassId, DispatchError> {
let current_id = *id;
*id = id.checked_add(&One::one()).ok_or(Error::<T>::NoAvailableClassId)?;
Ok(current_id)
})?;
let info = ClassInfo {
metadata: bounded_metadata,
total_issuance: Default::default(),
owner: owner.clone(),
data,
};
Classes::<T>::insert(class_id, info);
Ok(class_id)
}
/// Transfer NFT(non fungible token) from `from` account to `to` account
pub fn transfer(from: &T::AccountId, to: &T::AccountId, token: (T::ClassId, T::TokenId)) -> DispatchResult {
Tokens::<T>::try_mutate(token.0, token.1, |token_info| -> DispatchResult {
let info = token_info.as_mut().ok_or(Error::<T>::TokenNotFound)?;
ensure!(info.owner == *from, Error::<T>::NoPermission);
if from == to {
// no change needed
return Ok(());
}
info.owner = to.clone();
TokensByOwner::<T>::remove((from, token.0, token.1));
TokensByOwner::<T>::insert((to, token.0, token.1), ());
Ok(())
})
}
/// Mint NFT(non fungible token) to `owner`
pub fn mint(
owner: &T::AccountId,
class_id: T::ClassId,
metadata: Vec<u8>,
data: T::TokenData,
) -> Result<T::TokenId, DispatchError> {
NextTokenId::<T>::try_mutate(class_id, |id| -> Result<T::TokenId, DispatchError> {
let bounded_metadata: BoundedVec<u8, T::MaxTokenMetadata> =
metadata.try_into().map_err(|_| Error::<T>::MaxMetadataExceeded)?;
let token_id = *id;
*id = id.checked_add(&One::one()).ok_or(Error::<T>::NoAvailableTokenId)?;
Classes::<T>::try_mutate(class_id, |class_info| -> DispatchResult {
let info = class_info.as_mut().ok_or(Error::<T>::ClassNotFound)?;
info.total_issuance = info
.total_issuance
.checked_add(&One::one())
.ok_or(ArithmeticError::Overflow)?;
Ok(())
})?;
let token_info = TokenInfo {
metadata: bounded_metadata,
owner: owner.clone(),
data,
};
Tokens::<T>::insert(class_id, token_id, token_info);
TokensByOwner::<T>::insert((owner, class_id, token_id), ());
Ok(token_id)
})
}
/// Burn NFT(non fungible token) from `owner`
pub fn burn(owner: &T::AccountId, token: (T::ClassId, T::TokenId)) -> DispatchResult {
Tokens::<T>::try_mutate_exists(token.0, token.1, |token_info| -> DispatchResult {
let t = token_info.take().ok_or(Error::<T>::TokenNotFound)?;
ensure!(t.owner == *owner, Error::<T>::NoPermission);
Classes::<T>::try_mutate(token.0, |class_info| -> DispatchResult {
let info = class_info.as_mut().ok_or(Error::<T>::ClassNotFound)?;
info.total_issuance = info
.total_issuance
.checked_sub(&One::one())
.ok_or(ArithmeticError::Overflow)?;
Ok(())
})?;
TokensByOwner::<T>::remove((owner, token.0, token.1));
Ok(())
})
}
/// Destroy NFT(non fungible token) class
pub fn destroy_class(owner: &T::AccountId, class_id: T::ClassId) -> DispatchResult {
Classes::<T>::try_mutate_exists(class_id, |class_info| -> DispatchResult {
let info = class_info.take().ok_or(Error::<T>::ClassNotFound)?;
ensure!(info.owner == *owner, Error::<T>::NoPermission);
ensure!(info.total_issuance == Zero::zero(), Error::<T>::CannotDestroyClass);
NextTokenId::<T>::remove(class_id);
Ok(())
})
}
pub fn is_owner(account: &T::AccountId, token: (T::ClassId, T::TokenId)) -> bool {
TokensByOwner::<T>::contains_key((account, token.0, token.1))
}
}