This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
lib.rs
1259 lines (1139 loc) · 42.5 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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of Substrate.
// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! # Unique (Assets) Module
//!
//! A simple, secure module for dealing with non-fungible assets.
//!
//! ## Related Modules
//!
//! * [`System`](../frame_system/index.html)
//! * [`Support`](../frame_support/index.html)
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
pub mod mock;
#[cfg(test)]
mod tests;
mod functions;
mod impl_nonfungibles;
mod types;
pub mod migration;
pub mod weights;
use codec::{Decode, Encode, HasCompact};
use frame_support::traits::{BalanceStatus::Reserved, Currency, ReservableCurrency};
use frame_system::Config as SystemConfig;
use sp_runtime::{
traits::{Saturating, StaticLookup, Zero},
ArithmeticError, RuntimeDebug,
};
use sp_std::prelude::*;
pub use pallet::*;
pub use types::*;
pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
pub struct Pallet<T, I = ()>(_);
#[pallet::config]
/// The module configuration trait.
pub trait Config<I: 'static = ()>: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::Event>;
/// Identifier for the class of asset.
type ClassId: Member + Parameter + Default + Copy + HasCompact;
/// The type used to identify a unique asset within an asset class.
type InstanceId: Member + Parameter + Default + Copy + HasCompact + From<u16>;
/// The currency mechanism, used for paying for reserves.
type Currency: ReservableCurrency<Self::AccountId>;
/// The origin which may forcibly create or destroy an asset or otherwise alter privileged
/// attributes.
type ForceOrigin: EnsureOrigin<Self::Origin>;
/// The basic amount of funds that must be reserved for an asset class.
#[pallet::constant]
type ClassDeposit: Get<DepositBalanceOf<Self, I>>;
/// The basic amount of funds that must be reserved for an asset instance.
#[pallet::constant]
type InstanceDeposit: Get<DepositBalanceOf<Self, I>>;
/// The basic amount of funds that must be reserved when adding metadata to your asset.
#[pallet::constant]
type MetadataDepositBase: Get<DepositBalanceOf<Self, I>>;
/// The basic amount of funds that must be reserved when adding an attribute to an asset.
#[pallet::constant]
type AttributeDepositBase: Get<DepositBalanceOf<Self, I>>;
/// The additional funds that must be reserved for the number of bytes store in metadata,
/// either "normal" metadata or attribute metadata.
#[pallet::constant]
type DepositPerByte: Get<DepositBalanceOf<Self, I>>;
/// The maximum length of data stored on-chain.
#[pallet::constant]
type StringLimit: Get<u32>;
/// The maximum length of an attribute key.
#[pallet::constant]
type KeyLimit: Get<u32>;
/// The maximum length of an attribute value.
#[pallet::constant]
type ValueLimit: Get<u32>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
#[pallet::storage]
/// Details of an asset class.
pub(super) type Class<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
T::ClassId,
ClassDetails<T::AccountId, DepositBalanceOf<T, I>>,
>;
#[pallet::storage]
/// The assets held by any given account; set out this way so that assets owned by a single
/// account can be enumerated.
pub(super) type Account<T: Config<I>, I: 'static = ()> = StorageNMap<
_,
(
NMapKey<Blake2_128Concat, T::AccountId>, // owner
NMapKey<Blake2_128Concat, T::ClassId>,
NMapKey<Blake2_128Concat, T::InstanceId>,
),
(),
OptionQuery,
>;
#[pallet::storage]
/// The classes owned by any given account; set out this way so that classes owned by a single
/// account can be enumerated.
pub(super) type ClassAccount<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
_,
Blake2_128Concat,
T::AccountId,
Blake2_128Concat,
T::ClassId,
(),
OptionQuery,
>;
#[pallet::storage]
/// The assets in existence and their ownership details.
pub(super) type Asset<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
_,
Blake2_128Concat,
T::ClassId,
Blake2_128Concat,
T::InstanceId,
InstanceDetails<T::AccountId, DepositBalanceOf<T, I>>,
OptionQuery,
>;
#[pallet::storage]
/// Metadata of an asset class.
pub(super) type ClassMetadataOf<T: Config<I>, I: 'static = ()> = StorageMap<
_,
Blake2_128Concat,
T::ClassId,
ClassMetadata<DepositBalanceOf<T, I>, T::StringLimit>,
OptionQuery,
>;
#[pallet::storage]
/// Metadata of an asset instance.
pub(super) type InstanceMetadataOf<T: Config<I>, I: 'static = ()> = StorageDoubleMap<
_,
Blake2_128Concat,
T::ClassId,
Blake2_128Concat,
T::InstanceId,
InstanceMetadata<DepositBalanceOf<T, I>, T::StringLimit>,
OptionQuery,
>;
#[pallet::storage]
/// Metadata of an asset class.
pub(super) type Attribute<T: Config<I>, I: 'static = ()> = StorageNMap<
_,
(
NMapKey<Blake2_128Concat, T::ClassId>,
NMapKey<Blake2_128Concat, Option<T::InstanceId>>,
NMapKey<Blake2_128Concat, BoundedVec<u8, T::KeyLimit>>,
),
(BoundedVec<u8, T::ValueLimit>, DepositBalanceOf<T, I>),
OptionQuery,
>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config<I>, I: 'static = ()> {
/// An asset class was created.
Created { class: T::ClassId, creator: T::AccountId, owner: T::AccountId },
/// An asset class was force-created.
ForceCreated { class: T::ClassId, owner: T::AccountId },
/// An asset `class` was destroyed.
Destroyed { class: T::ClassId },
/// An asset `instance` was issued.
Issued { class: T::ClassId, instance: T::InstanceId, owner: T::AccountId },
/// An asset `instance` was transferred.
Transferred {
class: T::ClassId,
instance: T::InstanceId,
from: T::AccountId,
to: T::AccountId,
},
/// An asset `instance` was destroyed.
Burned { class: T::ClassId, instance: T::InstanceId, owner: T::AccountId },
/// Some asset `instance` was frozen.
Frozen { class: T::ClassId, instance: T::InstanceId },
/// Some asset `instance` was thawed.
Thawed { class: T::ClassId, instance: T::InstanceId },
/// Some asset `class` was frozen.
ClassFrozen { class: T::ClassId },
/// Some asset `class` was thawed.
ClassThawed { class: T::ClassId },
/// The owner changed.
OwnerChanged { class: T::ClassId, new_owner: T::AccountId },
/// The management team changed.
TeamChanged {
class: T::ClassId,
issuer: T::AccountId,
admin: T::AccountId,
freezer: T::AccountId,
},
/// An `instance` of an asset `class` has been approved by the `owner` for transfer by a
/// `delegate`.
ApprovedTransfer {
class: T::ClassId,
instance: T::InstanceId,
owner: T::AccountId,
delegate: T::AccountId,
},
/// An approval for a `delegate` account to transfer the `instance` of an asset `class` was
/// cancelled by its `owner`.
ApprovalCancelled {
class: T::ClassId,
instance: T::InstanceId,
owner: T::AccountId,
delegate: T::AccountId,
},
/// An asset `class` has had its attributes changed by the `Force` origin.
AssetStatusChanged { class: T::ClassId },
/// New metadata has been set for an asset class.
ClassMetadataSet {
class: T::ClassId,
data: BoundedVec<u8, T::StringLimit>,
is_frozen: bool,
},
/// Metadata has been cleared for an asset class.
ClassMetadataCleared { class: T::ClassId },
/// New metadata has been set for an asset instance.
MetadataSet {
class: T::ClassId,
instance: T::InstanceId,
data: BoundedVec<u8, T::StringLimit>,
is_frozen: bool,
},
/// Metadata has been cleared for an asset instance.
MetadataCleared { class: T::ClassId, instance: T::InstanceId },
/// Metadata has been cleared for an asset instance.
Redeposited { class: T::ClassId, successful_instances: Vec<T::InstanceId> },
/// New attribute metadata has been set for an asset class or instance.
AttributeSet {
class: T::ClassId,
maybe_instance: Option<T::InstanceId>,
key: BoundedVec<u8, T::KeyLimit>,
value: BoundedVec<u8, T::ValueLimit>,
},
/// Attribute metadata has been cleared for an asset class or instance.
AttributeCleared {
class: T::ClassId,
maybe_instance: Option<T::InstanceId>,
key: BoundedVec<u8, T::KeyLimit>,
},
}
#[pallet::error]
pub enum Error<T, I = ()> {
/// The signing account has no permission to do the operation.
NoPermission,
/// The given asset ID is unknown.
Unknown,
/// The asset instance ID has already been used for an asset.
AlreadyExists,
/// The owner turned out to be different to what was expected.
WrongOwner,
/// Invalid witness data given.
BadWitness,
/// The asset ID is already taken.
InUse,
/// The asset instance or class is frozen.
Frozen,
/// The delegate turned out to be different to what was expected.
WrongDelegate,
/// There is no delegate approved.
NoDelegate,
/// No approval exists that would allow the transfer.
Unapproved,
}
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Get the owner of the asset instance, if the asset exists.
pub fn owner(class: T::ClassId, instance: T::InstanceId) -> Option<T::AccountId> {
Asset::<T, I>::get(class, instance).map(|i| i.owner)
}
}
#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Issue a new class of non-fungible assets from a public origin.
///
/// This new asset class has no assets initially and its owner is the origin.
///
/// The origin must be Signed and the sender must have sufficient funds free.
///
/// `AssetDeposit` funds of sender are reserved.
///
/// Parameters:
/// - `class`: The identifier of the new asset class. This must not be currently in use.
/// - `admin`: The admin of this class of assets. The admin is the initial address of each
/// member of the asset class's admin team.
///
/// Emits `Created` event when successful.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::create())]
pub fn create(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
admin: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let owner = ensure_signed(origin)?;
let admin = T::Lookup::lookup(admin)?;
Self::do_create_class(
class,
owner.clone(),
admin.clone(),
T::ClassDeposit::get(),
false,
Event::Created { class, creator: owner, owner: admin },
)
}
/// Issue a new class of non-fungible assets from a privileged origin.
///
/// This new asset class has no assets initially.
///
/// The origin must conform to `ForceOrigin`.
///
/// Unlike `create`, no funds are reserved.
///
/// - `class`: The identifier of the new asset. This must not be currently in use.
/// - `owner`: The owner of this class of assets. The owner has full superuser permissions
/// over this asset, but may later change and configure the permissions using
/// `transfer_ownership` and `set_team`.
///
/// Emits `ForceCreated` event when successful.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::force_create())]
pub fn force_create(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
owner: <T::Lookup as StaticLookup>::Source,
free_holding: bool,
) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;
let owner = T::Lookup::lookup(owner)?;
Self::do_create_class(
class,
owner.clone(),
owner.clone(),
Zero::zero(),
free_holding,
Event::ForceCreated { class, owner },
)
}
/// Destroy a class of fungible assets.
///
/// The origin must conform to `ForceOrigin` or must be `Signed` and the sender must be the
/// owner of the asset `class`.
///
/// - `class`: The identifier of the asset class to be destroyed.
/// - `witness`: Information on the instances minted in the asset class. This must be
/// correct.
///
/// Emits `Destroyed` event when successful.
///
/// Weight: `O(n + m)` where:
/// - `n = witness.instances`
/// - `m = witness.instance_metadatas`
/// - `a = witness.attributes`
#[pallet::weight(T::WeightInfo::destroy(
witness.instances,
witness.instance_metadatas,
witness.attributes,
))]
pub fn destroy(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
witness: DestroyWitness,
) -> DispatchResultWithPostInfo {
let maybe_check_owner = match T::ForceOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let details = Self::do_destroy_class(class, witness, maybe_check_owner)?;
Ok(Some(T::WeightInfo::destroy(
details.instances,
details.instance_metadatas,
details.attributes,
))
.into())
}
/// Mint an asset instance of a particular class.
///
/// The origin must be Signed and the sender must be the Issuer of the asset `class`.
///
/// - `class`: The class of the asset to be minted.
/// - `instance`: The instance value of the asset to be minted.
/// - `beneficiary`: The initial owner of the minted asset.
///
/// Emits `Issued` event when successful.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::mint())]
pub fn mint(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
#[pallet::compact] instance: T::InstanceId,
owner: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
let owner = T::Lookup::lookup(owner)?;
Self::do_mint(class, instance, owner, |class_details| {
ensure!(class_details.issuer == origin, Error::<T, I>::NoPermission);
Ok(())
})
}
/// Destroy a single asset instance.
///
/// Origin must be Signed and the sender should be the Admin of the asset `class`.
///
/// - `class`: The class of the asset to be burned.
/// - `instance`: The instance of the asset to be burned.
/// - `check_owner`: If `Some` then the operation will fail with `WrongOwner` unless the
/// asset is owned by this value.
///
/// Emits `Burned` with the actual amount burned.
///
/// Weight: `O(1)`
/// Modes: `check_owner.is_some()`.
#[pallet::weight(T::WeightInfo::burn())]
pub fn burn(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
#[pallet::compact] instance: T::InstanceId,
check_owner: Option<<T::Lookup as StaticLookup>::Source>,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
let check_owner = check_owner.map(T::Lookup::lookup).transpose()?;
Self::do_burn(class, instance, |class_details, details| {
let is_permitted = class_details.admin == origin || details.owner == origin;
ensure!(is_permitted, Error::<T, I>::NoPermission);
ensure!(
check_owner.map_or(true, |o| o == details.owner),
Error::<T, I>::WrongOwner
);
Ok(())
})
}
/// Move an asset from the sender account to another.
///
/// Origin must be Signed and the signing account must be either:
/// - the Admin of the asset `class`;
/// - the Owner of the asset `instance`;
/// - the approved delegate for the asset `instance` (in this case, the approval is reset).
///
/// Arguments:
/// - `class`: The class of the asset to be transferred.
/// - `instance`: The instance of the asset to be transferred.
/// - `dest`: The account to receive ownership of the asset.
///
/// Emits `Transferred`.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::transfer())]
pub fn transfer(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
#[pallet::compact] instance: T::InstanceId,
dest: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
let dest = T::Lookup::lookup(dest)?;
Self::do_transfer(class, instance, dest, |class_details, details| {
if details.owner != origin && class_details.admin != origin {
let approved = details.approved.take().map_or(false, |i| i == origin);
ensure!(approved, Error::<T, I>::NoPermission);
}
Ok(())
})
}
/// Reevaluate the deposits on some assets.
///
/// Origin must be Signed and the sender should be the Owner of the asset `class`.
///
/// - `class`: The class of the asset to be frozen.
/// - `instances`: The instances of the asset class whose deposits will be reevaluated.
///
/// NOTE: This exists as a best-effort function. Any asset instances which are unknown or
/// in the case that the owner account does not have reservable funds to pay for a
/// deposit increase are ignored. Generally the owner isn't going to call this on instances
/// whose existing deposit is less than the refreshed deposit as it would only cost them,
/// so it's of little consequence.
///
/// It will still return an error in the case that the class is unknown of the signer is
/// not permitted to call it.
///
/// Weight: `O(instances.len())`
#[pallet::weight(T::WeightInfo::redeposit(instances.len() as u32))]
pub fn redeposit(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
instances: Vec<T::InstanceId>,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
let mut class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
ensure!(class_details.owner == origin, Error::<T, I>::NoPermission);
let deposit = match class_details.free_holding {
true => Zero::zero(),
false => T::InstanceDeposit::get(),
};
let mut successful = Vec::with_capacity(instances.len());
for instance in instances.into_iter() {
let mut details = match Asset::<T, I>::get(&class, &instance) {
Some(x) => x,
None => continue,
};
let old = details.deposit;
if old > deposit {
T::Currency::unreserve(&class_details.owner, old - deposit);
} else if deposit > old {
if T::Currency::reserve(&class_details.owner, deposit - old).is_err() {
// NOTE: No alterations made to class_details in this iteration so far, so
// this is OK to do.
continue
}
} else {
continue
}
class_details.total_deposit.saturating_accrue(deposit);
class_details.total_deposit.saturating_reduce(old);
details.deposit = deposit;
Asset::<T, I>::insert(&class, &instance, &details);
successful.push(instance);
}
Class::<T, I>::insert(&class, &class_details);
Self::deposit_event(Event::<T, I>::Redeposited {
class,
successful_instances: successful,
});
Ok(())
}
/// Disallow further unprivileged transfer of an asset instance.
///
/// Origin must be Signed and the sender should be the Freezer of the asset `class`.
///
/// - `class`: The class of the asset to be frozen.
/// - `instance`: The instance of the asset to be frozen.
///
/// Emits `Frozen`.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::freeze())]
pub fn freeze(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
#[pallet::compact] instance: T::InstanceId,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
let mut details =
Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::Unknown)?;
let class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
ensure!(class_details.freezer == origin, Error::<T, I>::NoPermission);
details.is_frozen = true;
Asset::<T, I>::insert(&class, &instance, &details);
Self::deposit_event(Event::<T, I>::Frozen { class, instance });
Ok(())
}
/// Re-allow unprivileged transfer of an asset instance.
///
/// Origin must be Signed and the sender should be the Freezer of the asset `class`.
///
/// - `class`: The class of the asset to be thawed.
/// - `instance`: The instance of the asset to be thawed.
///
/// Emits `Thawed`.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::thaw())]
pub fn thaw(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
#[pallet::compact] instance: T::InstanceId,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
let mut details =
Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::Unknown)?;
let class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
ensure!(class_details.admin == origin, Error::<T, I>::NoPermission);
details.is_frozen = false;
Asset::<T, I>::insert(&class, &instance, &details);
Self::deposit_event(Event::<T, I>::Thawed { class, instance });
Ok(())
}
/// Disallow further unprivileged transfers for a whole asset class.
///
/// Origin must be Signed and the sender should be the Freezer of the asset `class`.
///
/// - `class`: The asset class to be frozen.
///
/// Emits `ClassFrozen`.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::freeze_class())]
pub fn freeze_class(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
Class::<T, I>::try_mutate(class, |maybe_details| {
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
ensure!(&origin == &details.freezer, Error::<T, I>::NoPermission);
details.is_frozen = true;
Self::deposit_event(Event::<T, I>::ClassFrozen { class });
Ok(())
})
}
/// Re-allow unprivileged transfers for a whole asset class.
///
/// Origin must be Signed and the sender should be the Admin of the asset `class`.
///
/// - `class`: The class to be thawed.
///
/// Emits `ClassThawed`.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::thaw_class())]
pub fn thaw_class(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
Class::<T, I>::try_mutate(class, |maybe_details| {
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
ensure!(&origin == &details.admin, Error::<T, I>::NoPermission);
details.is_frozen = false;
Self::deposit_event(Event::<T, I>::ClassThawed { class });
Ok(())
})
}
/// Change the Owner of an asset class.
///
/// Origin must be Signed and the sender should be the Owner of the asset `class`.
///
/// - `class`: The asset class whose owner should be changed.
/// - `owner`: The new Owner of this asset class.
///
/// Emits `OwnerChanged`.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::transfer_ownership())]
pub fn transfer_ownership(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
owner: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
let owner = T::Lookup::lookup(owner)?;
Class::<T, I>::try_mutate(class, |maybe_details| {
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
ensure!(&origin == &details.owner, Error::<T, I>::NoPermission);
if details.owner == owner {
return Ok(())
}
// Move the deposit to the new owner.
T::Currency::repatriate_reserved(
&details.owner,
&owner,
details.total_deposit,
Reserved,
)?;
ClassAccount::<T, I>::remove(&details.owner, &class);
ClassAccount::<T, I>::insert(&owner, &class, ());
details.owner = owner.clone();
Self::deposit_event(Event::OwnerChanged { class, new_owner: owner });
Ok(())
})
}
/// Change the Issuer, Admin and Freezer of an asset class.
///
/// Origin must be Signed and the sender should be the Owner of the asset `class`.
///
/// - `class`: The asset class whose team should be changed.
/// - `issuer`: The new Issuer of this asset class.
/// - `admin`: The new Admin of this asset class.
/// - `freezer`: The new Freezer of this asset class.
///
/// Emits `TeamChanged`.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::set_team())]
pub fn set_team(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
issuer: <T::Lookup as StaticLookup>::Source,
admin: <T::Lookup as StaticLookup>::Source,
freezer: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let origin = ensure_signed(origin)?;
let issuer = T::Lookup::lookup(issuer)?;
let admin = T::Lookup::lookup(admin)?;
let freezer = T::Lookup::lookup(freezer)?;
Class::<T, I>::try_mutate(class, |maybe_details| {
let details = maybe_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
ensure!(&origin == &details.owner, Error::<T, I>::NoPermission);
details.issuer = issuer.clone();
details.admin = admin.clone();
details.freezer = freezer.clone();
Self::deposit_event(Event::TeamChanged { class, issuer, admin, freezer });
Ok(())
})
}
/// Approve an instance to be transferred by a delegated third-party account.
///
/// Origin must be Signed and must be the owner of the asset `instance`.
///
/// - `class`: The class of the asset to be approved for delegated transfer.
/// - `instance`: The instance of the asset to be approved for delegated transfer.
/// - `delegate`: The account to delegate permission to transfer the asset.
///
/// Emits `ApprovedTransfer` on success.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::approve_transfer())]
pub fn approve_transfer(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
#[pallet::compact] instance: T::InstanceId,
delegate: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let maybe_check: Option<T::AccountId> = T::ForceOrigin::try_origin(origin)
.map(|_| None)
.or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
let delegate = T::Lookup::lookup(delegate)?;
let class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
let mut details =
Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::Unknown)?;
if let Some(check) = maybe_check {
let permitted = &check == &class_details.admin || &check == &details.owner;
ensure!(permitted, Error::<T, I>::NoPermission);
}
details.approved = Some(delegate);
Asset::<T, I>::insert(&class, &instance, &details);
let delegate = details.approved.expect("set as Some above; qed");
Self::deposit_event(Event::ApprovedTransfer {
class,
instance,
owner: details.owner,
delegate,
});
Ok(())
}
/// Cancel the prior approval for the transfer of an asset by a delegate.
///
/// Origin must be either:
/// - the `Force` origin;
/// - `Signed` with the signer being the Admin of the asset `class`;
/// - `Signed` with the signer being the Owner of the asset `instance`;
///
/// Arguments:
/// - `class`: The class of the asset of whose approval will be cancelled.
/// - `instance`: The instance of the asset of whose approval will be cancelled.
/// - `maybe_check_delegate`: If `Some` will ensure that the given account is the one to
/// which permission of transfer is delegated.
///
/// Emits `ApprovalCancelled` on success.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::cancel_approval())]
pub fn cancel_approval(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
#[pallet::compact] instance: T::InstanceId,
maybe_check_delegate: Option<<T::Lookup as StaticLookup>::Source>,
) -> DispatchResult {
let maybe_check: Option<T::AccountId> = T::ForceOrigin::try_origin(origin)
.map(|_| None)
.or_else(|origin| ensure_signed(origin).map(Some).map_err(DispatchError::from))?;
let class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
let mut details =
Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::Unknown)?;
if let Some(check) = maybe_check {
let permitted = &check == &class_details.admin || &check == &details.owner;
ensure!(permitted, Error::<T, I>::NoPermission);
}
let maybe_check_delegate = maybe_check_delegate.map(T::Lookup::lookup).transpose()?;
let old = details.approved.take().ok_or(Error::<T, I>::NoDelegate)?;
if let Some(check_delegate) = maybe_check_delegate {
ensure!(check_delegate == old, Error::<T, I>::WrongDelegate);
}
Asset::<T, I>::insert(&class, &instance, &details);
Self::deposit_event(Event::ApprovalCancelled {
class,
instance,
owner: details.owner,
delegate: old,
});
Ok(())
}
/// Alter the attributes of a given asset.
///
/// Origin must be `ForceOrigin`.
///
/// - `class`: The identifier of the asset.
/// - `owner`: The new Owner of this asset.
/// - `issuer`: The new Issuer of this asset.
/// - `admin`: The new Admin of this asset.
/// - `freezer`: The new Freezer of this asset.
/// - `free_holding`: Whether a deposit is taken for holding an instance of this asset
/// class.
/// - `is_frozen`: Whether this asset class is frozen except for permissioned/admin
/// instructions.
///
/// Emits `AssetStatusChanged` with the identity of the asset.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::force_asset_status())]
pub fn force_asset_status(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
owner: <T::Lookup as StaticLookup>::Source,
issuer: <T::Lookup as StaticLookup>::Source,
admin: <T::Lookup as StaticLookup>::Source,
freezer: <T::Lookup as StaticLookup>::Source,
free_holding: bool,
is_frozen: bool,
) -> DispatchResult {
T::ForceOrigin::ensure_origin(origin)?;
Class::<T, I>::try_mutate(class, |maybe_asset| {
let mut asset = maybe_asset.take().ok_or(Error::<T, I>::Unknown)?;
let old_owner = asset.owner;
let new_owner = T::Lookup::lookup(owner)?;
asset.owner = new_owner.clone();
asset.issuer = T::Lookup::lookup(issuer)?;
asset.admin = T::Lookup::lookup(admin)?;
asset.freezer = T::Lookup::lookup(freezer)?;
asset.free_holding = free_holding;
asset.is_frozen = is_frozen;
*maybe_asset = Some(asset);
ClassAccount::<T, I>::remove(&old_owner, &class);
ClassAccount::<T, I>::insert(&new_owner, &class, ());
Self::deposit_event(Event::AssetStatusChanged { class });
Ok(())
})
}
/// Set an attribute for an asset class or instance.
///
/// Origin must be either `ForceOrigin` or Signed and the sender should be the Owner of the
/// asset `class`.
///
/// If the origin is Signed, then funds of signer are reserved according to the formula:
/// `MetadataDepositBase + DepositPerByte * (key.len + value.len)` taking into
/// account any already reserved funds.
///
/// - `class`: The identifier of the asset class whose instance's metadata to set.
/// - `maybe_instance`: The identifier of the asset instance whose metadata to set.
/// - `key`: The key of the attribute.
/// - `value`: The value to which to set the attribute.
///
/// Emits `AttributeSet`.
///
/// Weight: `O(1)`
#[pallet::weight(T::WeightInfo::set_attribute())]
pub fn set_attribute(
origin: OriginFor<T>,
#[pallet::compact] class: T::ClassId,
maybe_instance: Option<T::InstanceId>,
key: BoundedVec<u8, T::KeyLimit>,
value: BoundedVec<u8, T::ValueLimit>,
) -> DispatchResult {
let maybe_check_owner = T::ForceOrigin::try_origin(origin)
.map(|_| None)
.or_else(|origin| ensure_signed(origin).map(Some))?;
let mut class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
if let Some(check_owner) = &maybe_check_owner {
ensure!(check_owner == &class_details.owner, Error::<T, I>::NoPermission);
}
let maybe_is_frozen = match maybe_instance {
None => ClassMetadataOf::<T, I>::get(class).map(|v| v.is_frozen),
Some(instance) =>
InstanceMetadataOf::<T, I>::get(class, instance).map(|v| v.is_frozen),
};
ensure!(!maybe_is_frozen.unwrap_or(false), Error::<T, I>::Frozen);
let attribute = Attribute::<T, I>::get((class, maybe_instance, &key));
if attribute.is_none() {
class_details.attributes.saturating_inc();
}
let old_deposit = attribute.map_or(Zero::zero(), |m| m.1);
class_details.total_deposit.saturating_reduce(old_deposit);
let mut deposit = Zero::zero();
if !class_details.free_holding && maybe_check_owner.is_some() {
deposit = T::DepositPerByte::get()
.saturating_mul(((key.len() + value.len()) as u32).into())
.saturating_add(T::AttributeDepositBase::get());
}
class_details.total_deposit.saturating_accrue(deposit);
if deposit > old_deposit {
T::Currency::reserve(&class_details.owner, deposit - old_deposit)?;
} else if deposit < old_deposit {
T::Currency::unreserve(&class_details.owner, old_deposit - deposit);
}
Attribute::<T, I>::insert((&class, maybe_instance, &key), (&value, deposit));