-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlib.rs
1329 lines (1191 loc) · 39.6 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
// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
// This file is part of Unique Network.
// Unique Network 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.
// Unique Network 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 Unique Network. If not, see <http://www.gnu.org/licenses/>.
//! # Nonfungible Pallet
//!
//! The Nonfungible pallet provides functionality for handling nonfungible collections and tokens.
//!
//! - [`Config`]
//! - [`NonfungibleHandle`]
//! - [`Pallet`]
//! - [`CommonWeights`](common::CommonWeights)
//!
//! ## Overview
//!
//! The Nonfungible pallet provides functions for:
//!
//! - NFT collection creation and removal
//! - Minting and burning of NFT tokens
//! - Retrieving account balances
//! - Transfering NFT tokens
//! - Setting and checking allowance for NFT tokens
//! - Setting properties and permissions for NFT collections and tokens
//! - Nesting and unnesting tokens
//!
//! ### Terminology
//!
//! - **NFT token:** Non fungible token.
//!
//! - **NFT Collection:** A collection of NFT tokens. All NFT tokens are part of a collection.
//! Each collection can define it's own properties, properties for it's tokens and set of permissions.
//!
//! - **Balance:** Number of NFT tokens owned by an account
//!
//! - **Allowance:** NFT tokens owned by one account that another account is allowed to make operations on
//!
//! - **Burning:** The process of “deleting” a token from a collection and from
//! an account balance of the owner.
//!
//! - **Nesting:** Setting up parent-child relationship between tokens. Nested tokens are inhereting
//! owner from their parent. There could be multiple levels of nesting. Token couldn't be nested in
//! it's child token i.e. parent-child relationship graph shouldn't have cycles.
//!
//! - **Properties:** Key-Values pairs. Token properties are attached to a token. Collection properties are
//! attached to a collection. Set of permissions could be defined for each property.
//!
//! ### Implementations
//!
//! The Nonfungible pallet provides implementations for the following traits. If these traits provide
//! the functionality that you need, then you can avoid coupling with the Nonfungible pallet.
//!
//! - [`CommonWeightInfo`](pallet_common::CommonWeightInfo): Functions for retrieval of transaction weight
//! - [`CommonCollectionOperations`](pallet_common::CommonCollectionOperations): Functions for dealing
//! with collections
//!
//! ## Interface
//!
//! ### Dispatchable Functions
//!
//! - `init_collection` - Create NFT collection. NFT collection can be configured to allow or deny access for
//! some accounts.
//! - `destroy_collection` - Destroy exising NFT collection. There should be no tokens in the collection.
//! - `burn` - Burn NFT token owned by account.
//! - `transfer` - Transfer NFT token. Transfers should be enabled for NFT collection.
//! Nests the NFT token if it is sent to another token.
//! - `create_item` - Mint NFT token in collection. Sender should have permission to mint tokens.
//! - `set_allowance` - Set allowance for another account.
//! - `set_token_property` - Set token property value.
//! - `delete_token_property` - Remove property from the token.
//! - `set_collection_properties` - Set collection properties.
//! - `delete_collection_properties` - Remove properties from the collection.
//! - `set_property_permission` - Set collection property permission.
//! - `set_token_property_permissions` - Set token property permissions.
//!
//! ## Assumptions
//!
//! * To perform operations on tokens sender should be in collection's allow list if collection access mode is `AllowList`.
#![cfg_attr(not(feature = "std"), no_std)]
use core::ops::Deref;
use erc::ERC721Events;
use evm_coder::ToLog;
use frame_support::{
dispatch::{Pays, PostDispatchInfo},
ensure, fail,
pallet_prelude::*,
storage::with_transaction,
transactional, BoundedVec,
};
pub use pallet::*;
use pallet_common::{
eth::collection_id_to_address, helpers::add_weight_to_post_info,
weights::WeightInfo as CommonWeightInfo, CollectionHandle, Error as CommonError,
Event as CommonEvent, Pallet as PalletCommon, SelfWeightOf as PalletCommonWeightOf,
};
use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder};
use pallet_structure::Pallet as PalletStructure;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_core::{Get, H160};
use sp_runtime::{ArithmeticError, DispatchError, DispatchResult, TransactionOutcome};
use sp_std::{collections::btree_map::BTreeMap, vec, vec::Vec};
use up_data_structs::{
budget::Budget, mapping::TokenAddressMapping, AccessMode, AuxPropertyValue, CollectionId,
CreateNftExData, CustomDataLimit, PropertiesPermissionMap, Property, PropertyKey,
PropertyKeyPermission, PropertyScope, PropertyValue, TokenChild, TokenId,
TokenProperties as TokenPropertiesT,
};
use weights::WeightInfo;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
pub mod common;
pub mod erc;
pub mod weights;
pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::Config>::CrossAccountId>;
pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
/// Token data, stored independently from other data used to describe it
/// for the convenience of database access. Notably contains the owner account address.
#[struct_versioning::versioned(version = 2, upper)]
#[derive(Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct ItemData<CrossAccountId> {
#[version(..2)]
pub const_data: BoundedVec<u8, CustomDataLimit>,
#[version(..2)]
pub variable_data: BoundedVec<u8, CustomDataLimit>,
pub owner: CrossAccountId,
}
#[frame_support::pallet]
pub mod pallet {
use frame_support::{
pallet_prelude::*, storage::Key, traits::StorageVersion, Blake2_128Concat, Twox64Concat,
};
use up_data_structs::{CollectionId, TokenId};
use super::{weights::WeightInfo, *};
#[pallet::error]
pub enum Error<T> {
/// Not Nonfungible item data used to mint in Nonfungible collection.
NotNonfungibleDataUsedToMintFungibleCollectionToken,
/// Used amount > 1 with NFT
NonfungibleItemsHaveNoAmount,
/// Unable to burn NFT with children
CantBurnNftWithChildren,
}
#[pallet::config]
pub trait Config:
frame_system::Config + pallet_common::Config + pallet_structure::Config + pallet_evm::Config
{
type WeightInfo: WeightInfo;
}
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);
/// Total amount of minted tokens in a collection.
#[pallet::storage]
pub type TokensMinted<T: Config> =
StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;
/// Amount of burnt tokens in a collection.
#[pallet::storage]
pub type TokensBurnt<T: Config> =
StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;
/// Token data, used to partially describe a token.
#[pallet::storage]
pub type TokenData<T: Config> = StorageNMap<
Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
Value = ItemData<T::CrossAccountId>,
QueryKind = OptionQuery,
>;
/// Map of key-value pairs, describing the metadata of a token.
#[pallet::storage]
#[pallet::getter(fn token_properties)]
pub type TokenProperties<T: Config> = StorageNMap<
Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
Value = TokenPropertiesT,
QueryKind = OptionQuery,
>;
/// Custom data of a token that is serialized to bytes,
/// primarily reserved for on-chain operations,
/// normally obscured from the external users.
///
/// Auxiliary properties are slightly different from
/// usual [`TokenProperties`] due to an unlimited number
/// and separately stored and written-to key-value pairs.
///
/// Currently unused.
#[pallet::storage]
#[pallet::getter(fn token_aux_property)]
pub type TokenAuxProperties<T: Config> = StorageNMap<
Key = (
Key<Twox64Concat, CollectionId>,
Key<Twox64Concat, TokenId>,
Key<Twox64Concat, PropertyScope>,
Key<Twox64Concat, PropertyKey>,
),
Value = AuxPropertyValue,
QueryKind = OptionQuery,
>;
/// Used to enumerate tokens owned by account.
#[pallet::storage]
pub type Owned<T: Config> = StorageNMap<
Key = (
Key<Twox64Concat, CollectionId>,
Key<Blake2_128Concat, T::CrossAccountId>,
Key<Twox64Concat, TokenId>,
),
Value = bool,
QueryKind = ValueQuery,
>;
/// Used to enumerate token's children.
#[pallet::storage]
#[pallet::getter(fn token_children)]
pub type TokenChildren<T: Config> = StorageNMap<
Key = (
Key<Twox64Concat, CollectionId>,
Key<Twox64Concat, TokenId>,
Key<Twox64Concat, (CollectionId, TokenId)>,
),
Value = bool,
QueryKind = ValueQuery,
>;
/// Amount of tokens owned by an account in a collection.
#[pallet::storage]
pub type AccountBalance<T: Config> = StorageNMap<
Key = (
Key<Twox64Concat, CollectionId>,
Key<Blake2_128Concat, T::CrossAccountId>,
),
Value = u32,
QueryKind = ValueQuery,
>;
/// Allowance set by a token owner for another user to perform one of certain transactions on a token.
#[pallet::storage]
pub type Allowance<T: Config> = StorageNMap<
Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),
Value = T::CrossAccountId,
QueryKind = OptionQuery,
>;
/// Operator set by a wallet owner that could perform certain transactions on all tokens in the wallet.
#[pallet::storage]
pub type CollectionAllowance<T: Config> = StorageNMap<
Key = (
Key<Twox64Concat, CollectionId>,
Key<Blake2_128Concat, T::CrossAccountId>,
Key<Blake2_128Concat, T::CrossAccountId>,
),
Value = bool,
QueryKind = ValueQuery,
>;
#[pallet::genesis_config]
pub struct GenesisConfig<T>(PhantomData<T>);
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
Self(Default::default())
}
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
StorageVersion::new(1).put::<Pallet<T>>();
}
}
}
pub struct NonfungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);
impl<T: Config> NonfungibleHandle<T> {
pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {
Self(inner)
}
pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {
self.0
}
pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {
&mut self.0
}
}
impl<T: Config> WithRecorder<T> for NonfungibleHandle<T> {
fn recorder(&self) -> &SubstrateRecorder<T> {
self.0.recorder()
}
fn into_recorder(self) -> SubstrateRecorder<T> {
self.0.into_recorder()
}
}
impl<T: Config> Deref for NonfungibleHandle<T> {
type Target = pallet_common::CollectionHandle<T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: Config> Pallet<T> {
/// Get number of NFT tokens in collection.
pub fn total_supply(collection: &NonfungibleHandle<T>) -> u32 {
<TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)
}
/// Check that NFT token exists.
///
/// - `token`: Token ID.
pub fn token_exists(collection: &NonfungibleHandle<T>, token: TokenId) -> bool {
<TokenData<T>>::contains_key((collection.id, token))
}
/// Add or edit auxiliary data for the property.
///
/// - `f`: function that adds or edits auxiliary data.
pub fn try_mutate_token_aux_property<R, E>(
collection_id: CollectionId,
token_id: TokenId,
scope: PropertyScope,
key: PropertyKey,
f: impl FnOnce(&mut Option<AuxPropertyValue>) -> Result<R, E>,
) -> Result<R, E> {
<TokenAuxProperties<T>>::try_mutate((collection_id, token_id, scope, key), f)
}
/// Remove auxiliary data for the property.
pub fn remove_token_aux_property(
collection_id: CollectionId,
token_id: TokenId,
scope: PropertyScope,
key: PropertyKey,
) {
<TokenAuxProperties<T>>::remove((collection_id, token_id, scope, key));
}
/// Get all auxiliary data in a given scope.
///
/// Returns iterator over Property Key - Data pairs.
pub fn iterate_token_aux_properties(
collection_id: CollectionId,
token_id: TokenId,
scope: PropertyScope,
) -> impl Iterator<Item = (PropertyKey, AuxPropertyValue)> {
<TokenAuxProperties<T>>::iter_prefix((collection_id, token_id, scope))
}
/// Get ID of the last minted token
pub fn current_token_id(collection_id: CollectionId) -> TokenId {
TokenId(<TokensMinted<T>>::get(collection_id))
}
}
// unchecked calls skips any permission checks
impl<T: Config> Pallet<T> {
/// Destroy NFT collection
///
/// `destroy_collection` will throw error if collection contains any tokens.
/// Only owner can destroy collection.
pub fn destroy_collection(
collection: NonfungibleHandle<T>,
sender: &T::CrossAccountId,
) -> DispatchResult {
let id = collection.id;
if Self::collection_has_tokens(id) {
return Err(<CommonError<T>>::CantDestroyNotEmptyCollection.into());
}
// =========
PalletCommon::destroy_collection(collection.0, sender)?;
let _ = <TokenData<T>>::clear_prefix((id,), u32::MAX, None);
let _ = <TokenChildren<T>>::clear_prefix((id,), u32::MAX, None);
let _ = <Owned<T>>::clear_prefix((id,), u32::MAX, None);
<TokensMinted<T>>::remove(id);
<TokensBurnt<T>>::remove(id);
let _ = <Allowance<T>>::clear_prefix((id,), u32::MAX, None);
let _ = <AccountBalance<T>>::clear_prefix((id,), u32::MAX, None);
let _ = <CollectionAllowance<T>>::clear_prefix((id,), u32::MAX, None);
Ok(())
}
/// Burn NFT token
///
/// `burn` removes `token` from the `collection`, from it's owner and from the parent token
/// if the token is nested.
/// Only the owner can `burn` the token. The `token` shouldn't have any nested tokens.
/// Also removes all corresponding properties and auxiliary properties.
///
/// - `token`: Token that should be burned
/// - `collection`: Collection that contains the token
pub fn burn(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token: TokenId,
) -> DispatchResult {
let token_data =
<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
ensure!(&token_data.owner == sender, <CommonError<T>>::NoPermission);
if collection.permissions.access() == AccessMode::AllowList {
collection.check_allowlist(sender)?;
}
if Self::token_has_children(collection.id, token) {
return Err(<Error<T>>::CantBurnNftWithChildren.into());
}
let burnt = <TokensBurnt<T>>::get(collection.id)
.checked_add(1)
.ok_or(ArithmeticError::Overflow)?;
let balance = <AccountBalance<T>>::get((collection.id, token_data.owner.clone()))
.checked_sub(1)
.ok_or(ArithmeticError::Overflow)?;
// =========
if balance == 0 {
<AccountBalance<T>>::remove((collection.id, token_data.owner.clone()));
} else {
<AccountBalance<T>>::insert((collection.id, token_data.owner.clone()), balance);
}
<PalletStructure<T>>::unnest_if_nested(&token_data.owner, collection.id, token);
<Owned<T>>::remove((collection.id, &token_data.owner, token));
<TokensBurnt<T>>::insert(collection.id, burnt);
<TokenData<T>>::remove((collection.id, token));
<TokenProperties<T>>::remove((collection.id, token));
let _ = <TokenAuxProperties<T>>::clear_prefix((collection.id, token), u32::MAX, None);
let old_spender = <Allowance<T>>::take((collection.id, token));
if let Some(old_spender) = old_spender {
<PalletCommon<T>>::deposit_event(CommonEvent::Approved(
collection.id,
token,
token_data.owner.clone(),
old_spender,
0,
));
}
<PalletEvm<T>>::deposit_log(
ERC721Events::Transfer {
from: *token_data.owner.as_eth(),
to: H160::default(),
token_id: token.into(),
}
.to_log(collection_id_to_address(collection.id)),
);
<PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
collection.id,
token,
token_data.owner,
1,
));
Ok(())
}
/// A batch operation to add, edit or remove properties for a token.
///
/// - `nesting_budget`: Limit for searching parents in-depth to check ownership.
///
/// All affected properties should have `mutable` permission
/// to be **deleted** or to be **set more than once**,
/// and the sender should have permission to edit those properties.
///
/// This function fires an event for each property change.
/// In case of an error, all the changes (including the events) will be reverted
/// since the function is transactional.
#[transactional]
fn modify_token_properties(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token_id: TokenId,
properties_updates: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,
nesting_budget: &dyn Budget,
) -> DispatchResult {
let mut property_writer =
pallet_common::ExistingTokenPropertyWriter::new(collection, sender);
property_writer.write_token_properties(
sender,
token_id,
properties_updates,
nesting_budget,
erc::ERC721TokenEvent::TokenChanged {
token_id: token_id.into(),
}
.to_log(T::ContractAddress::get()),
)
}
pub fn next_token_id(collection: &NonfungibleHandle<T>) -> Result<TokenId, DispatchError> {
let next_token_id = <TokensMinted<T>>::get(collection.id)
.checked_add(1)
.ok_or(<CommonError<T>>::CollectionTokenLimitExceeded)?;
ensure!(
collection.limits.token_limit() >= next_token_id,
<CommonError<T>>::CollectionTokenLimitExceeded
);
Ok(TokenId(next_token_id))
}
/// Batch operation to add or edit properties for the token
///
/// Same as [`modify_token_properties`] but doesn't allow to remove properties
///
/// [`modify_token_properties`]: struct.Pallet.html#method.modify_token_properties
pub fn set_token_properties(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token_id: TokenId,
properties: impl Iterator<Item = Property>,
nesting_budget: &dyn Budget,
) -> DispatchResult {
Self::modify_token_properties(
collection,
sender,
token_id,
properties.map(|p| (p.key, Some(p.value))),
nesting_budget,
)
}
/// Add or edit single property for the token
///
/// Calls [`set_token_properties`] internally
///
/// [`set_token_properties`]: struct.Pallet.html#method.set_token_properties
pub fn set_token_property(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token_id: TokenId,
property: Property,
nesting_budget: &dyn Budget,
) -> DispatchResult {
Self::set_token_properties(
collection,
sender,
token_id,
[property].into_iter(),
nesting_budget,
)
}
/// Batch operation to remove properties from the token
///
/// Same as [`modify_token_properties`] but doesn't allow to add or edit properties
///
/// [`modify_token_properties`]: struct.Pallet.html#method.modify_token_properties
pub fn delete_token_properties(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token_id: TokenId,
property_keys: impl Iterator<Item = PropertyKey>,
nesting_budget: &dyn Budget,
) -> DispatchResult {
Self::modify_token_properties(
collection,
sender,
token_id,
property_keys.into_iter().map(|key| (key, None)),
nesting_budget,
)
}
/// Remove single property from the token
///
/// Calls [`delete_token_properties`] internally
///
/// [`delete_token_properties`]: struct.Pallet.html#method.delete_token_properties
pub fn delete_token_property(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token_id: TokenId,
property_key: PropertyKey,
nesting_budget: &dyn Budget,
) -> DispatchResult {
Self::delete_token_properties(
collection,
sender,
token_id,
[property_key].into_iter(),
nesting_budget,
)
}
/// Add or edit properties for the collection
pub fn set_collection_properties(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
properties: Vec<Property>,
) -> DispatchResult {
<PalletCommon<T>>::set_collection_properties(collection, sender, properties.into_iter())
}
/// Remove properties from the collection
pub fn delete_collection_properties(
collection: &CollectionHandle<T>,
sender: &T::CrossAccountId,
property_keys: Vec<PropertyKey>,
) -> DispatchResult {
<PalletCommon<T>>::delete_collection_properties(
collection,
sender,
property_keys.into_iter(),
)
}
/// Set property permissions for the token.
///
/// Sender should be the owner or admin of token's collection.
pub fn set_token_property_permissions(
collection: &CollectionHandle<T>,
sender: &T::CrossAccountId,
property_permissions: Vec<PropertyKeyPermission>,
) -> DispatchResult {
<PalletCommon<T>>::set_token_property_permissions(collection, sender, property_permissions)
}
/// Set property permissions for the token with scope.
///
/// Sender should be the owner or admin of token's collection.
pub fn set_scoped_token_property_permissions(
collection: &CollectionHandle<T>,
sender: &T::CrossAccountId,
scope: PropertyScope,
property_permissions: Vec<PropertyKeyPermission>,
) -> DispatchResult {
<PalletCommon<T>>::set_scoped_token_property_permissions(
collection,
sender,
scope,
property_permissions,
)
}
pub fn token_property_permission(collection_id: CollectionId) -> PropertiesPermissionMap {
<PalletCommon<T>>::property_permissions(collection_id)
}
pub fn check_token_immediate_ownership(
collection: &NonfungibleHandle<T>,
token: TokenId,
possible_owner: &T::CrossAccountId,
) -> DispatchResult {
let token_data =
<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
ensure!(
&token_data.owner == possible_owner,
<CommonError<T>>::NoPermission
);
Ok(())
}
/// Transfer NFT token from one account to another.
///
/// `from` account stops being the owner and `to` account becomes the owner of the token.
/// If `to` is token than `to` becomes owner of the token and the token become nested.
/// Unnests token from previous parent if it was nested before.
/// Removes allowance for the token if there was any.
/// Throws if transfers aren't allowed for collection or if receiver reached token ownership limit.
///
/// - `nesting_budget`: Limit for token nesting depth
pub fn transfer(
collection: &NonfungibleHandle<T>,
from: &T::CrossAccountId,
to: &T::CrossAccountId,
token: TokenId,
nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
let depositor = from;
Self::transfer_internal(collection, depositor, from, to, token, nesting_budget)
}
/// Transfers an NFT from the `from` account to the `to` account.
/// The `depositor` is the account who deposits the NFT.
/// For instance, the nesting rules will be checked against the `depositor`'s permissions.
pub fn transfer_internal(
collection: &NonfungibleHandle<T>,
depositor: &T::CrossAccountId,
from: &T::CrossAccountId,
to: &T::CrossAccountId,
token: TokenId,
nesting_budget: &dyn Budget,
) -> DispatchResultWithPostInfo {
ensure!(
collection.limits.transfers_enabled(),
<CommonError<T>>::TransferNotAllowed
);
let mut actual_weight = <SelfWeightOf<T>>::transfer_raw();
let token_data =
<TokenData<T>>::get((collection.id, token)).ok_or(<CommonError<T>>::TokenNotFound)?;
ensure!(&token_data.owner == from, <CommonError<T>>::NoPermission);
if collection.permissions.access() == AccessMode::AllowList {
collection.check_allowlist(from)?;
collection.check_allowlist(to)?;
actual_weight += <PalletCommonWeightOf<T>>::check_accesslist() * 2;
}
<PalletCommon<T>>::ensure_correct_receiver(to)?;
let balance_from = <AccountBalance<T>>::get((collection.id, from))
.checked_sub(1)
.ok_or(<CommonError<T>>::TokenValueTooLow)?;
let balance_to = if from != to {
let balance_to = <AccountBalance<T>>::get((collection.id, to))
.checked_add(1)
.ok_or(ArithmeticError::Overflow)?;
ensure!(
balance_to < collection.limits.account_token_ownership_limit(),
<CommonError<T>>::AccountTokenLimitExceeded,
);
Some(balance_to)
} else {
None
};
<PalletStructure<T>>::nest_if_sent_to_token(
depositor,
to,
collection.id,
token,
nesting_budget,
)?;
// =========
<PalletStructure<T>>::unnest_if_nested(&token_data.owner, collection.id, token);
<TokenData<T>>::insert((collection.id, token), ItemData { owner: to.clone() });
if let Some(balance_to) = balance_to {
// from != to
if balance_from == 0 {
<AccountBalance<T>>::remove((collection.id, from));
} else {
<AccountBalance<T>>::insert((collection.id, from), balance_from);
}
<AccountBalance<T>>::insert((collection.id, to), balance_to);
<Owned<T>>::remove((collection.id, from, token));
<Owned<T>>::insert((collection.id, to, token), true);
}
Self::set_allowance_unchecked(collection, from, token, None, true);
<PalletEvm<T>>::deposit_log(
ERC721Events::Transfer {
from: *from.as_eth(),
to: *to.as_eth(),
token_id: token.into(),
}
.to_log(collection_id_to_address(collection.id)),
);
<PalletCommon<T>>::deposit_event(CommonEvent::Transfer(
collection.id,
token,
from.clone(),
to.clone(),
1,
));
Ok(PostDispatchInfo {
actual_weight: Some(actual_weight),
pays_fee: Pays::Yes,
})
}
/// Batch operation to mint multiple NFT tokens.
///
/// The sender should be the owner/admin of the collection or collection should be configured
/// to allow public minting.
/// Throws if amount of tokens reached it's limit for the collection or if caller reached
/// token ownership limit.
///
/// - `data`: Contains list of token properties and users who will become the owners of the
/// corresponging tokens.
/// - `nesting_budget`: Limit for token nesting depth
pub fn create_multiple_items(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
data: Vec<CreateItemData<T>>,
nesting_budget: &dyn Budget,
) -> DispatchResult {
if !collection.is_owner_or_admin(sender) {
ensure!(
collection.permissions.mint_mode(),
<CommonError<T>>::PublicMintingNotAllowed
);
collection.check_allowlist(sender)?;
for item in data.iter() {
collection.check_allowlist(&item.owner)?;
}
}
for data in data.iter() {
<PalletCommon<T>>::ensure_correct_receiver(&data.owner)?;
}
let first_token = <TokensMinted<T>>::get(collection.id);
let tokens_minted = first_token
.checked_add(data.len() as u32)
.ok_or(ArithmeticError::Overflow)?;
ensure!(
tokens_minted <= collection.limits.token_limit(),
<CommonError<T>>::CollectionTokenLimitExceeded
);
let mut balances = BTreeMap::new();
for data in &data {
let balance = balances
.entry(&data.owner)
.or_insert_with(|| <AccountBalance<T>>::get((collection.id, &data.owner)));
*balance = balance.checked_add(1).ok_or(ArithmeticError::Overflow)?;
ensure!(
*balance <= collection.limits.account_token_ownership_limit(),
<CommonError<T>>::AccountTokenLimitExceeded,
);
}
for (i, data) in data.iter().enumerate() {
let token = TokenId(first_token + i as u32 + 1);
<PalletStructure<T>>::check_nesting(
sender,
&data.owner,
collection.id,
token,
nesting_budget,
)?;
}
// =========
let mut property_writer = pallet_common::NewTokenPropertyWriter::new(collection, sender);
with_transaction(|| {
for (i, data) in data.iter().enumerate() {
let token = first_token + i as u32 + 1;
<TokenData<T>>::insert(
(collection.id, token),
ItemData {
// const_data: data.const_data.clone(),
owner: data.owner.clone(),
},
);
let token = TokenId(token);
<PalletStructure<T>>::nest_if_sent_to_token_unchecked(
&data.owner,
collection.id,
token,
);
if let Err(e) = property_writer.write_token_properties(
sender.conv_eq(&data.owner),
token,
data.properties.clone().into_iter(),
erc::ERC721TokenEvent::TokenChanged {
token_id: token.into(),
}
.to_log(T::ContractAddress::get()),
) {
return TransactionOutcome::Rollback(Err(e));
}
}
TransactionOutcome::Commit(Ok(()))
})?;
<TokensMinted<T>>::insert(collection.id, tokens_minted);
for (account, balance) in balances {
<AccountBalance<T>>::insert((collection.id, account), balance);
}
for (i, data) in data.into_iter().enumerate() {
let token = first_token + i as u32 + 1;
<Owned<T>>::insert((collection.id, &data.owner, token), true);
<PalletEvm<T>>::deposit_log(
ERC721Events::Transfer {
from: H160::default(),
to: *data.owner.as_eth(),
token_id: token.into(),
}
.to_log(collection_id_to_address(collection.id)),
);
<PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(
collection.id,
TokenId(token),
data.owner.clone(),
1,
));
}
Ok(())
}
pub fn set_allowance_unchecked(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token: TokenId,
spender: Option<&T::CrossAccountId>,
assume_implicit_eth: bool,
) {
if let Some(spender) = spender {
let old_spender = <Allowance<T>>::get((collection.id, token));
<Allowance<T>>::insert((collection.id, token), spender);
// In ERC721 there is only one possible approved user of token, so we set
// approved user to spender
<PalletEvm<T>>::deposit_log(
ERC721Events::Approval {
owner: *sender.as_eth(),
approved: *spender.as_eth(),
token_id: token.into(),
}
.to_log(collection_id_to_address(collection.id)),
);
// In Unique chain, any token can have any amount of approved users, so we need to
// set allowance of old owner to 0, and allowance of new owner to 1
if old_spender.as_ref() != Some(spender) {
if let Some(old_owner) = old_spender {
<PalletCommon<T>>::deposit_event(CommonEvent::Approved(
collection.id,
token,
sender.clone(),
old_owner,
0,
));
}
<PalletCommon<T>>::deposit_event(CommonEvent::Approved(
collection.id,
token,
sender.clone(),
spender.clone(),
1,
));
}
} else {
let old_spender = <Allowance<T>>::take((collection.id, token));
if !assume_implicit_eth {
// In ERC721 there is only one possible approved user of token, so we set
// approved user to zero address
<PalletEvm<T>>::deposit_log(
ERC721Events::Approval {
owner: *sender.as_eth(),
approved: H160::default(),
token_id: token.into(),
}
.to_log(collection_id_to_address(collection.id)),
);
}
// In Unique chain, any token can have any amount of approved users, so we need to
// set allowance of old owner to 0
if let Some(old_spender) = old_spender {
<PalletCommon<T>>::deposit_event(CommonEvent::Approved(
collection.id,
token,
sender.clone(),
old_spender,
0,