-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathlib.rs
568 lines (489 loc) · 17 KB
/
lib.rs
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// This file is part of Acala.
// Copyright (C) 2020-2022 Acala Foundation.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::unnecessary_cast)]
#![allow(clippy::unused_unit)]
#![allow(clippy::upper_case_acronyms)]
use frame_support::{
pallet_prelude::*,
require_transactional,
traits::{
tokens::nonfungibles::{Inspect, Mutate, Transfer},
Currency,
ExistenceRequirement::{AllowDeath, KeepAlive},
NamedReservableCurrency,
},
transactional, PalletId,
};
use frame_system::pallet_prelude::*;
use orml_traits::InspectExtended;
use primitives::{
nft::{Attributes, ClassProperty, NFTBalance, Properties, CID},
ReserveIdentifier,
};
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};
use sp_runtime::{
traits::{AccountIdConversion, Hash, Saturating, StaticLookup, Zero},
DispatchResult, RuntimeDebug,
};
use sp_std::prelude::*;
pub mod benchmarking;
mod mock;
mod tests;
pub mod weights;
pub use module::*;
pub use weights::WeightInfo;
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct ClassData<Balance> {
/// Deposit reserved to create token class
pub deposit: Balance,
/// Class properties
pub properties: Properties,
/// Class attributes
pub attributes: Attributes,
}
#[derive(Encode, Decode, Clone, RuntimeDebug, PartialEq, Eq, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct TokenData<Balance> {
/// Deposit reserved to create token
pub deposit: Balance,
/// Token attributes
pub attributes: Attributes,
}
pub type TokenIdOf<T> = <T as orml_nft::Config>::TokenId;
pub type ClassIdOf<T> = <T as orml_nft::Config>::ClassId;
pub type BalanceOf<T> =
<<T as pallet_proxy::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
#[frame_support::pallet]
pub mod module {
use super::*;
pub const RESERVE_ID: ReserveIdentifier = ReserveIdentifier::Nft;
#[pallet::config]
pub trait Config:
frame_system::Config
+ orml_nft::Config<ClassData = ClassData<BalanceOf<Self>>, TokenData = TokenData<BalanceOf<Self>>>
+ pallet_proxy::Config
{
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// Currency type for reserve balance.
type Currency: NamedReservableCurrency<
Self::AccountId,
Balance = BalanceOf<Self>,
ReserveIdentifier = ReserveIdentifier,
>;
/// The minimum balance to create class
#[pallet::constant]
type CreateClassDeposit: Get<BalanceOf<Self>>;
/// The minimum balance to create token
#[pallet::constant]
type CreateTokenDeposit: Get<BalanceOf<Self>>;
/// Deposit required for per byte.
#[pallet::constant]
type DataDepositPerByte: Get<BalanceOf<Self>>;
/// The NFT's module id
#[pallet::constant]
type PalletId: Get<PalletId>;
/// Maximum number of bytes in attributes
#[pallet::constant]
type MaxAttributesBytes: Get<u32>;
/// Weight information for the extrinsics in this module.
type WeightInfo: WeightInfo;
}
#[pallet::error]
pub enum Error<T> {
/// ClassId not found
ClassIdNotFound,
/// TokenId not found
TokenIdNotFound,
/// The operator is not the owner of the token and has no permission
NoPermission,
/// Quantity is invalid. need >= 1
InvalidQuantity,
/// Property of class don't support transfer
NonTransferable,
/// Property of class don't support burn
NonBurnable,
/// Property of class don't support mint
NonMintable,
/// Can not destroy class
/// Total issuance is not 0
CannotDestroyClass,
/// Cannot perform mutable action
Immutable,
/// Attributes too large
AttributesTooLarge,
/// The given token ID is not correct
IncorrectTokenId,
}
#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// Created NFT class.
CreatedClass {
owner: T::AccountId,
class_id: ClassIdOf<T>,
},
/// Minted NFT token.
MintedToken {
from: T::AccountId,
to: T::AccountId,
class_id: ClassIdOf<T>,
quantity: u32,
},
/// Transferred NFT token.
TransferredToken {
from: T::AccountId,
to: T::AccountId,
class_id: ClassIdOf<T>,
token_id: TokenIdOf<T>,
},
/// Burned NFT token.
BurnedToken {
owner: T::AccountId,
class_id: ClassIdOf<T>,
token_id: TokenIdOf<T>,
},
/// Burned NFT token with remark.
BurnedTokenWithRemark {
owner: T::AccountId,
class_id: ClassIdOf<T>,
token_id: TokenIdOf<T>,
remark_hash: T::Hash,
},
/// Destroyed NFT class.
DestroyedClass {
owner: T::AccountId,
class_id: ClassIdOf<T>,
},
}
#[pallet::pallet]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Create NFT class, tokens belong to the class.
///
/// - `metadata`: external metadata
/// - `properties`: class property, include `Transferable` `Burnable`
#[pallet::weight(<T as Config>::WeightInfo::create_class())]
#[transactional]
pub fn create_class(
origin: OriginFor<T>,
metadata: CID,
properties: Properties,
attributes: Attributes,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let next_id = orml_nft::Pallet::<T>::next_class_id();
let owner: T::AccountId = T::PalletId::get().into_sub_account(next_id);
let class_deposit = T::CreateClassDeposit::get();
let data_deposit = Self::data_deposit(&metadata, &attributes)?;
let proxy_deposit = <pallet_proxy::Pallet<T>>::deposit(1u32);
let deposit = class_deposit.saturating_add(data_deposit);
let total_deposit = proxy_deposit.saturating_add(deposit);
// ensure enough token for proxy deposit + class deposit + data deposit
<T as module::Config>::Currency::transfer(&who, &owner, total_deposit, KeepAlive)?;
<T as module::Config>::Currency::reserve_named(&RESERVE_ID, &owner, deposit)?;
// owner add proxy delegate to origin
<pallet_proxy::Pallet<T>>::add_proxy_delegate(&owner, who, Default::default(), Zero::zero())?;
let data = ClassData {
deposit,
properties,
attributes,
};
orml_nft::Pallet::<T>::create_class(&owner, metadata, data)?;
Self::deposit_event(Event::CreatedClass {
owner,
class_id: next_id,
});
Ok(().into())
}
/// Mint NFT token
///
/// - `to`: the token owner's account
/// - `class_id`: token belong to the class id
/// - `metadata`: external metadata
/// - `quantity`: token quantity
#[pallet::weight(<T as Config>::WeightInfo::mint(*quantity))]
#[transactional]
pub fn mint(
origin: OriginFor<T>,
to: <T::Lookup as StaticLookup>::Source,
class_id: ClassIdOf<T>,
metadata: CID,
attributes: Attributes,
#[pallet::compact] quantity: u32,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let to = T::Lookup::lookup(to)?;
Self::do_mint(&who, &to, class_id, metadata, attributes, quantity)?;
Ok(())
}
/// Transfer NFT token to another account
///
/// - `to`: the token owner's account
/// - `token`: (class_id, token_id)
#[pallet::weight(<T as Config>::WeightInfo::transfer())]
#[transactional]
pub fn transfer(
origin: OriginFor<T>,
to: <T::Lookup as StaticLookup>::Source,
token: (ClassIdOf<T>, TokenIdOf<T>),
) -> DispatchResult {
let who = ensure_signed(origin)?;
let to = T::Lookup::lookup(to)?;
Self::do_transfer(&who, &to, token)
}
/// Burn NFT token
///
/// - `token`: (class_id, token_id)
#[pallet::weight(<T as Config>::WeightInfo::burn())]
#[transactional]
pub fn burn(origin: OriginFor<T>, token: (ClassIdOf<T>, TokenIdOf<T>)) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_burn(who, token, None)
}
/// Burn NFT token
///
/// - `token`: (class_id, token_id)
/// - `remark`: Vec<u8>
#[pallet::weight(<T as Config>::WeightInfo::burn_with_remark(remark.len() as u32))]
#[transactional]
pub fn burn_with_remark(
origin: OriginFor<T>,
token: (ClassIdOf<T>, TokenIdOf<T>),
remark: Vec<u8>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
Self::do_burn(who, token, Some(remark))
}
/// Destroy NFT class, remove dest from proxy, and send all the free
/// balance to dest
///
/// - `class_id`: The class ID to destroy
/// - `dest`: The proxy account that will receive free balance
#[pallet::weight(<T as Config>::WeightInfo::destroy_class())]
#[transactional]
pub fn destroy_class(
origin: OriginFor<T>,
class_id: ClassIdOf<T>,
dest: <T::Lookup as StaticLookup>::Source,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
let dest = T::Lookup::lookup(dest)?;
let class_info = orml_nft::Pallet::<T>::classes(class_id).ok_or(Error::<T>::ClassIdNotFound)?;
ensure!(who == class_info.owner, Error::<T>::NoPermission);
ensure!(
class_info.total_issuance == Zero::zero(),
Error::<T>::CannotDestroyClass
);
let data = class_info.data;
<T as module::Config>::Currency::unreserve_named(&RESERVE_ID, &who, data.deposit);
orml_nft::Pallet::<T>::destroy_class(&who, class_id)?;
// this should unresere proxy deposit
pallet_proxy::Pallet::<T>::remove_proxy_delegate(&who, dest.clone(), Default::default(), Zero::zero())?;
<T as module::Config>::Currency::transfer(
&who,
&dest,
<T as module::Config>::Currency::free_balance(&who),
AllowDeath,
)?;
Self::deposit_event(Event::DestroyedClass { owner: who, class_id });
Ok(().into())
}
/// Update NFT class properties. The current class properties must contains
/// ClassPropertiesMutable.
///
/// - `class_id`: The class ID to update
/// - `properties`: The new properties
#[pallet::weight(<T as Config>::WeightInfo::update_class_properties())]
#[transactional]
pub fn update_class_properties(
origin: OriginFor<T>,
class_id: ClassIdOf<T>,
properties: Properties,
) -> DispatchResult {
let who = ensure_signed(origin)?;
orml_nft::Classes::<T>::try_mutate(class_id, |class_info| {
let class_info = class_info.as_mut().ok_or(Error::<T>::ClassIdNotFound)?;
ensure!(who == class_info.owner, Error::<T>::NoPermission);
let mut data = &mut class_info.data;
ensure!(
data.properties.0.contains(ClassProperty::ClassPropertiesMutable),
Error::<T>::Immutable
);
data.properties = properties;
Ok(())
})
}
}
}
impl<T: Config> Pallet<T> {
#[require_transactional]
fn do_transfer(from: &T::AccountId, to: &T::AccountId, token: (ClassIdOf<T>, TokenIdOf<T>)) -> DispatchResult {
let class_info = orml_nft::Pallet::<T>::classes(token.0).ok_or(Error::<T>::ClassIdNotFound)?;
let data = class_info.data;
ensure!(
data.properties.0.contains(ClassProperty::Transferable),
Error::<T>::NonTransferable
);
let token_info = orml_nft::Pallet::<T>::tokens(token.0, token.1).ok_or(Error::<T>::TokenIdNotFound)?;
orml_nft::Pallet::<T>::transfer(from, to, token)?;
<T as module::Config>::Currency::unreserve_named(&RESERVE_ID, from, token_info.data.deposit);
<T as module::Config>::Currency::transfer(from, to, token_info.data.deposit, AllowDeath)?;
<T as module::Config>::Currency::reserve_named(&RESERVE_ID, to, token_info.data.deposit)?;
Self::deposit_event(Event::TransferredToken {
from: from.clone(),
to: to.clone(),
class_id: token.0,
token_id: token.1,
});
Ok(())
}
#[require_transactional]
fn do_mint(
who: &T::AccountId,
to: &T::AccountId,
class_id: ClassIdOf<T>,
metadata: CID,
attributes: Attributes,
quantity: u32,
) -> Result<Vec<TokenIdOf<T>>, DispatchError> {
ensure!(quantity >= 1, Error::<T>::InvalidQuantity);
let class_info = orml_nft::Pallet::<T>::classes(class_id).ok_or(Error::<T>::ClassIdNotFound)?;
ensure!(*who == class_info.owner, Error::<T>::NoPermission);
ensure!(
class_info.data.properties.0.contains(ClassProperty::Mintable),
Error::<T>::NonMintable
);
let data_deposit = Self::data_deposit(&metadata, &attributes)?;
let deposit = T::CreateTokenDeposit::get().saturating_add(data_deposit);
let total_deposit = deposit.saturating_mul(quantity.into());
// `repatriate_reserved` will check `to` account exist and may return
// `DeadAccount`.
<T as module::Config>::Currency::transfer(who, to, total_deposit, KeepAlive)?;
<T as module::Config>::Currency::reserve_named(&RESERVE_ID, to, total_deposit)?;
let mut token_ids = Vec::with_capacity(quantity as usize);
let data = TokenData { deposit, attributes };
for _ in 0..quantity {
token_ids.push(orml_nft::Pallet::<T>::mint(
to,
class_id,
metadata.clone(),
data.clone(),
)?);
}
Self::deposit_event(Event::MintedToken {
from: who.clone(),
to: to.clone(),
class_id,
quantity,
});
Ok(token_ids)
}
fn do_burn(who: T::AccountId, token: (ClassIdOf<T>, TokenIdOf<T>), remark: Option<Vec<u8>>) -> DispatchResult {
let class_info = orml_nft::Pallet::<T>::classes(token.0).ok_or(Error::<T>::ClassIdNotFound)?;
let data = class_info.data;
ensure!(
data.properties.0.contains(ClassProperty::Burnable),
Error::<T>::NonBurnable
);
let token_info = orml_nft::Pallet::<T>::tokens(token.0, token.1).ok_or(Error::<T>::TokenIdNotFound)?;
ensure!(who == token_info.owner, Error::<T>::NoPermission);
orml_nft::Pallet::<T>::burn(&who, token)?;
<T as module::Config>::Currency::unreserve_named(&RESERVE_ID, &who, token_info.data.deposit);
if let Some(remark) = remark {
let hash = T::Hashing::hash(&remark[..]);
Self::deposit_event(Event::BurnedTokenWithRemark {
owner: who,
class_id: token.0,
token_id: token.1,
remark_hash: hash,
});
} else {
Self::deposit_event(Event::BurnedToken {
owner: who,
class_id: token.0,
token_id: token.1,
});
}
Ok(())
}
fn data_deposit(metadata: &[u8], attributes: &Attributes) -> Result<BalanceOf<T>, DispatchError> {
// Addition can't overflow because we will be out of memory before that
let attributes_len = attributes.iter().fold(0, |acc, (k, v)| {
acc.saturating_add(v.len().saturating_add(k.len()) as u32)
});
ensure!(
attributes_len <= T::MaxAttributesBytes::get(),
Error::<T>::AttributesTooLarge
);
let total_data_len = attributes_len.saturating_add(metadata.len() as u32);
Ok(T::DataDepositPerByte::get().saturating_mul(total_data_len.into()))
}
}
impl<T: Config> InspectExtended<T::AccountId> for Pallet<T> {
type Balance = NFTBalance;
fn balance(who: &T::AccountId) -> Self::Balance {
orml_nft::TokensByOwner::<T>::iter_prefix((who,)).count() as u128
}
fn next_token_id(class: Self::ClassId) -> Self::InstanceId {
orml_nft::Pallet::<T>::next_token_id(class)
}
}
impl<T: Config> Inspect<T::AccountId> for Pallet<T> {
type InstanceId = TokenIdOf<T>;
type ClassId = ClassIdOf<T>;
fn owner(class: &Self::ClassId, instance: &Self::InstanceId) -> Option<T::AccountId> {
orml_nft::Pallet::<T>::tokens(class, instance).map(|t| t.owner)
}
fn class_owner(class: &Self::ClassId) -> Option<T::AccountId> {
orml_nft::Pallet::<T>::classes(class).map(|c| c.owner)
}
fn can_transfer(class: &Self::ClassId, _: &Self::InstanceId) -> bool {
orml_nft::Pallet::<T>::classes(class).map_or(false, |class_info| {
class_info.data.properties.0.contains(ClassProperty::Transferable)
})
}
}
impl<T: Config> Mutate<T::AccountId> for Pallet<T> {
/// Mint some asset `instance` of `class` to be owned by `who`.
fn mint_into(class: &Self::ClassId, instance: &Self::InstanceId, who: &T::AccountId) -> DispatchResult {
// Ensure the next token ID is correct
ensure!(
orml_nft::Pallet::<T>::next_token_id(class) == *instance,
Error::<T>::IncorrectTokenId
);
let class_owner = <Self as Inspect<T::AccountId>>::class_owner(class).ok_or(Error::<T>::ClassIdNotFound)?;
Self::do_mint(&class_owner, who, *class, Default::default(), Default::default(), 1u32)?;
Ok(())
}
/// Burn some asset `instance` of `class`.
fn burn_from(class: &Self::ClassId, instance: &Self::InstanceId) -> DispatchResult {
let owner = <Self as Inspect<T::AccountId>>::owner(class, instance).ok_or(Error::<T>::TokenIdNotFound)?;
Self::do_burn(owner, (*class, *instance), None)
}
}
impl<T: Config> Transfer<T::AccountId> for Pallet<T> {
/// Transfer asset `instance` of `class` into `destination` account.
fn transfer(class: &Self::ClassId, instance: &Self::InstanceId, destination: &T::AccountId) -> DispatchResult {
let owner = <Self as Inspect<T::AccountId>>::owner(class, instance).ok_or(Error::<T>::TokenIdNotFound)?;
Self::do_transfer(&owner, destination, (*class, *instance))
}
}