123456#![recursion_limit = "1024"]7#![cfg_attr(not(feature = "std"), no_std)]8#![allow(9 clippy::too_many_arguments,10 clippy::unnecessary_mut_passed,11 clippy::unused_unit12)]1314extern crate alloc;1516pub use serde::{Serialize, Deserialize};1718pub use frame_support::{19 construct_runtime, decl_event, decl_module, decl_storage, decl_error,20 dispatch::DispatchResult,21 ensure, fail, parameter_types,22 traits::{23 Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,24 Randomness, IsSubType, WithdrawReasons,25 },26 weights::{27 constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},28 DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,29 WeightToFeePolynomial, DispatchClass,30 },31 StorageValue, transactional,32};3334use frame_system::{self as system, ensure_signed};35use sp_core::H160;36use sp_std::vec;37use sp_runtime::{DispatchError, sp_std::prelude::Vec};38use core::ops::{Deref, DerefMut};39use nft_data_structs::{40 MAX_DECIMAL_POINTS, MAX_SPONSOR_TIMEOUT, MAX_TOKEN_OWNERSHIP, MAX_REFUNGIBLE_PIECES,41 CUSTOM_DATA_LIMIT, COLLECTION_NUMBER_LIMIT, ACCOUNT_TOKEN_OWNERSHIP_LIMIT,42 VARIABLE_ON_CHAIN_SCHEMA_LIMIT, CONST_ON_CHAIN_SCHEMA_LIMIT, COLLECTION_ADMINS_LIMIT,43 OFFCHAIN_SCHEMA_LIMIT, AccessMode, Collection, CreateItemData, CollectionLimits, CollectionId,44 CollectionMode, TokenId, SchemaVersion, SponsorshipState, Ownership, NftItemType,45 FungibleItemType, ReFungibleItemType,46};4748#[cfg(test)]49mod mock;5051#[cfg(test)]52mod tests;5354mod default_weights;55mod eth;56mod sponsorship;57pub use sponsorship::NftSponsorshipHandler;58pub use eth::sponsoring::NftEthSponsorshipHandler;5960pub use eth::NftErcSupport;61pub use eth::account::*;62use eth::erc::{ERC20Events, ERC721Events};6364#[cfg(feature = "runtime-benchmarks")]65mod benchmarking;6667pub trait WeightInfo {68 fn create_collection() -> Weight;69 fn destroy_collection() -> Weight;70 fn add_to_white_list() -> Weight;71 fn remove_from_white_list() -> Weight;72 fn set_public_access_mode() -> Weight;73 fn set_mint_permission() -> Weight;74 fn change_collection_owner() -> Weight;75 fn add_collection_admin() -> Weight;76 fn remove_collection_admin() -> Weight;77 fn set_collection_sponsor() -> Weight;78 fn confirm_sponsorship() -> Weight;79 fn remove_collection_sponsor() -> Weight;80 fn create_item(s: usize) -> Weight;81 fn burn_item() -> Weight;82 fn transfer() -> Weight;83 fn approve() -> Weight;84 fn transfer_from() -> Weight;85 fn set_offchain_schema() -> Weight;86 fn set_const_on_chain_schema() -> Weight;87 fn set_variable_on_chain_schema() -> Weight;88 fn set_variable_meta_data() -> Weight;89 fn enable_contract_sponsoring() -> Weight;90 fn set_schema_version() -> Weight;91 fn set_contract_sponsoring_rate_limit() -> Weight;92 fn set_variable_meta_data_sponsoring_rate_limit() -> Weight;93 fn toggle_contract_white_list() -> Weight;94 fn add_to_contract_white_list() -> Weight;95 fn remove_from_contract_white_list() -> Weight;96 fn set_collection_limits() -> Weight;97}9899decl_error! {100 101 pub enum Error for Module<T: Config> {102 103 TotalCollectionsLimitExceeded,104 105 CollectionDecimalPointLimitExceeded,106 107 CollectionNameLimitExceeded,108 109 CollectionDescriptionLimitExceeded,110 111 CollectionTokenPrefixLimitExceeded,112 113 CollectionNotFound,114 115 TokenNotFound,116 117 AdminNotFound,118 119 NumOverflow,120 121 AlreadyAdmin,122 123 NoPermission,124 125 ConfirmUnsetSponsorFail,126 127 PublicMintingNotAllowed,128 129 MustBeTokenOwner,130 131 TokenValueTooLow,132 133 NftSizeLimitExceeded,134 135 ApproveNotFound,136 137 TokenValueNotEnough,138 139 ApproveRequired,140 141 AddresNotInWhiteList,142 143 CollectionAdminsLimitExceeded,144 145 AddressOwnershipLimitExceeded,146 147 EmptyArgument,148 149 TokenConstDataLimitExceeded,150 151 TokenVariableDataLimitExceeded,152 153 NotNftDataUsedToMintNftCollectionToken,154 155 NotFungibleDataUsedToMintFungibleCollectionToken,156 157 NotReFungibleDataUsedToMintReFungibleCollectionToken,158 159 UnexpectedCollectionType,160 161 CantStoreMetadataInFungibleTokens,162 163 CollectionTokenLimitExceeded,164 165 AccountTokenLimitExceeded,166 167 CollectionLimitBoundsExceeded,168 169 OwnerPermissionsCantBeReverted,170 171 SchemaDataLimitExceeded,172 173 WrongRefungiblePieces,174 175 BadCreateRefungibleCall,176 177 OutOfGas,178 179 TransferNotAllowed,180 }181}182183#[must_use = "Should call submit_logs or save, otherwise some data will be lost for evm side"]184pub struct CollectionHandle<T: Config> {185 pub id: CollectionId,186 collection: Collection<T>,187 recorder: pallet_evm_coder_substrate::SubstrateRecorder<T>,188}189impl<T: Config> CollectionHandle<T> {190 pub fn get_with_gas_limit(id: CollectionId, gas_limit: u64) -> Option<Self> {191 <CollectionById<T>>::get(id).map(|collection| Self {192 id,193 collection,194 recorder: pallet_evm_coder_substrate::SubstrateRecorder::new(195 eth::collection_id_to_address(id),196 gas_limit,197 ),198 })199 }200 pub fn get(id: CollectionId) -> Option<Self> {201 Self::get_with_gas_limit(id, u64::MAX)202 }203 pub fn log(&self, log: impl evm_coder::ToLog) -> DispatchResult {204 self.recorder.log_sub(log)205 }206 #[allow(dead_code)]207 fn consume_gas(&self, gas: u64) -> DispatchResult {208 self.recorder.consume_gas_sub(gas)209 }210 fn consume_sload(&self) -> DispatchResult {211 self.recorder.consume_sload_sub()212 }213 fn consume_sstore(&self) -> DispatchResult {214 self.recorder.consume_sstore_sub()215 }216 pub fn submit_logs(self) -> DispatchResult {217 self.recorder.submit_logs()218 }219 pub fn save(self) -> DispatchResult {220 self.recorder.submit_logs()?;221 <CollectionById<T>>::insert(self.id, self.collection);222 Ok(())223 }224}225impl<T: Config> Deref for CollectionHandle<T> {226 type Target = Collection<T>;227228 fn deref(&self) -> &Self::Target {229 &self.collection230 }231}232233impl<T: Config> DerefMut for CollectionHandle<T> {234 fn deref_mut(&mut self) -> &mut Self::Target {235 &mut self.collection236 }237}238239pub trait Config: system::Config + pallet_evm_coder_substrate::Config + Sized {240 type Event: From<Event<Self>> + Into<<Self as system::Config>::Event>;241242 243 type WeightInfo: WeightInfo;244245 type EvmAddressMapping: pallet_evm::AddressMapping<Self::AccountId>;246 type EvmBackwardsAddressMapping: EvmBackwardsAddressMapping<Self::AccountId>;247248 type CrossAccountId: CrossAccountId<Self::AccountId>;249 type Currency: Currency<Self::AccountId>;250 type CollectionCreationPrice: Get<251 <<Self as Config>::Currency as Currency<Self::AccountId>>::Balance,252 >;253 type TreasuryAccountId: Get<Self::AccountId>;254}255256257258259260261262263264265266267268269270271272273274275276277278decl_storage! {279 trait Store for Module<T: Config> as Nft {280281 282 283 CreatedCollectionCount: u32;284 285 ChainVersion: u64;286 287 288 ItemListIndex: map hasher(blake2_128_concat) CollectionId => TokenId;289 290291 292 293 294 DestroyedCollectionCount: u32;295 296 297 pub AccountItemCount get(fn account_item_count): map hasher(twox_64_concat) T::AccountId => u32;298 299300 301 302 303 pub CollectionById get(fn collection_id) config(): map hasher(blake2_128_concat) CollectionId => Option<Collection<T>> = None;304 305 306 pub AdminList get(fn admin_list_collection): map hasher(blake2_128_concat) CollectionId => Vec<T::CrossAccountId>;307 308 309 pub WhiteList get(fn white_list): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) T::AccountId => bool;310 311312 313 314 pub Balance get(fn balance_count): double_map hasher(blake2_128_concat) CollectionId, hasher(twox_64_concat) T::AccountId => u128;315316 317 318 319 pub Allowances get(fn approved): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) (TokenId, T::AccountId, T::AccountId) => u128;320321 322 323 pub NftItemList get(fn nft_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<NftItemType<T::CrossAccountId>>;324 325 pub FungibleItemList get(fn fungible_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) T::AccountId => FungibleItemType;326 327 pub ReFungibleItemList get(fn refungible_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<ReFungibleItemType<T::CrossAccountId>>;328 329330 331 332 pub AddressTokens get(fn address_tokens): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) T::AccountId => Vec<TokenId>;333 334335 336 337 338 pub CreateItemBasket get(fn create_item_basket): map hasher(blake2_128_concat) (CollectionId, T::AccountId) => T::BlockNumber;339 340 pub NftTransferBasket get(fn nft_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => T::BlockNumber;341 342 pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(twox_64_concat) T::AccountId => T::BlockNumber;343 344 pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => T::BlockNumber;345 346347 348 349 pub VariableMetaDataBasket get(fn variable_meta_data_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<T::BlockNumber> = None;350 }351 add_extra_genesis {352 build(|config: &GenesisConfig<T>| {353 354 for (_num, _c) in &config.collection_id {355 <Module<T>>::init_collection(_c);356 }357358 for (_num, _c, _i) in &config.nft_item_id {359 <Module<T>>::init_nft_token(*_c, _i);360 }361362 for (collection_id, account_id, fungible_item) in &config.fungible_item_id {363 <Module<T>>::init_fungible_token(*collection_id, &T::CrossAccountId::from_sub(account_id.clone()), fungible_item);364 }365366 for (_num, _c, _i) in &config.refungible_item_id {367 <Module<T>>::init_refungible_token(*_c, _i);368 }369 })370 }371}372373decl_event!(374 pub enum Event<T>375 where376 AccountId = <T as frame_system::Config>::AccountId,377 CrossAccountId = <T as Config>::CrossAccountId,378 {379 380 381 382 383 384 385 386 387 388 CollectionCreated(CollectionId, u8, AccountId),389390 391 392 393 394 395 396 397 398 399 ItemCreated(CollectionId, TokenId, CrossAccountId),400401 402 403 404 405 406 407 408 ItemDestroyed(CollectionId, TokenId),409410 411 412 413 414 415 416 417 418 419 420 421 Transfer(CollectionId, TokenId, CrossAccountId, CrossAccountId, u128),422423 424 425 426 427 428 429 430 431 432 Approved(CollectionId, TokenId, CrossAccountId, CrossAccountId, u128),433 }434);435436decl_module! {437 pub struct Module<T: Config> for enum Call438 where439 origin: T::Origin440 {441 fn deposit_event() = default;442 const CollectionAdminsLimit: u64 = COLLECTION_ADMINS_LIMIT;443 type Error = Error<T>;444445 fn on_initialize(_now: T::BlockNumber) -> Weight {446 0447 }448449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 #[weight = <T as Config>::WeightInfo::create_collection()]466 #[transactional]467 pub fn create_collection(origin,468 collection_name: Vec<u16>,469 collection_description: Vec<u16>,470 token_prefix: Vec<u8>,471 mode: CollectionMode) -> DispatchResult {472473 474 let who = ensure_signed(origin)?;475476 477 let mut imbalance = <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();478 imbalance.subsume(<<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(479 &T::TreasuryAccountId::get(),480 T::CollectionCreationPrice::get(),481 ));482 <T as Config>::Currency::settle(483 &who,484 imbalance,485 WithdrawReasons::TRANSFER,486 ExistenceRequirement::KeepAlive,487 ).map_err(|_| Error::<T>::NoPermission)?;488489 let decimal_points = match mode {490 CollectionMode::Fungible(points) => points,491 _ => 0492 };493494 let created_count = CreatedCollectionCount::get();495 let destroyed_count = DestroyedCollectionCount::get();496497 498 ensure!(created_count - destroyed_count < COLLECTION_NUMBER_LIMIT, Error::<T>::TotalCollectionsLimitExceeded);499500 501 ensure!(decimal_points <= MAX_DECIMAL_POINTS, Error::<T>::CollectionDecimalPointLimitExceeded);502 ensure!(collection_name.len() <= 64, Error::<T>::CollectionNameLimitExceeded);503 ensure!(collection_description.len() <= 256, Error::<T>::CollectionDescriptionLimitExceeded);504 ensure!(token_prefix.len() <= 16, Error::<T>::CollectionTokenPrefixLimitExceeded);505506 507 let next_id = created_count508 .checked_add(1)509 .ok_or(Error::<T>::NumOverflow)?;510511 CreatedCollectionCount::put(next_id);512513 let limits = CollectionLimits {514 sponsored_data_size: CUSTOM_DATA_LIMIT,515 ..Default::default()516 };517518 519 let new_collection = Collection {520 owner: who.clone(),521 name: collection_name,522 mode: mode.clone(),523 mint_mode: false,524 access: AccessMode::Normal,525 description: collection_description,526 decimal_points,527 token_prefix,528 offchain_schema: Vec::new(),529 schema_version: SchemaVersion::ImageURL,530 sponsorship: SponsorshipState::Disabled,531 variable_on_chain_schema: Vec::new(),532 const_on_chain_schema: Vec::new(),533 limits,534 transfers_enabled: true,535 };536537 538 <CollectionById<T>>::insert(next_id, new_collection);539540 541 Self::deposit_event(RawEvent::CollectionCreated(next_id, mode.id(), who));542543 Ok(())544 }545546 547 548 549 550 551 552 553 554 555 #[weight = <T as Config>::WeightInfo::destroy_collection()]556 #[transactional]557 pub fn destroy_collection(origin, collection_id: CollectionId) -> DispatchResult {558559 let sender = ensure_signed(origin)?;560 let collection = Self::get_collection(collection_id)?;561 Self::check_owner_permissions(&collection, &sender)?;562 if !collection.limits.owner_can_destroy {563 fail!(Error::<T>::NoPermission);564 }565566 <AddressTokens<T>>::remove_prefix(collection_id, None);567 <Allowances<T>>::remove_prefix(collection_id, None);568 <Balance<T>>::remove_prefix(collection_id, None);569 <ItemListIndex>::remove(collection_id);570 <AdminList<T>>::remove(collection_id);571 <CollectionById<T>>::remove(collection_id);572 <WhiteList<T>>::remove_prefix(collection_id, None);573574 <NftItemList<T>>::remove_prefix(collection_id, None);575 <FungibleItemList<T>>::remove_prefix(collection_id, None);576 <ReFungibleItemList<T>>::remove_prefix(collection_id, None);577578 <NftTransferBasket<T>>::remove_prefix(collection_id, None);579 <FungibleTransferBasket<T>>::remove_prefix(collection_id, None);580 <ReFungibleTransferBasket<T>>::remove_prefix(collection_id, None);581582 <VariableMetaDataBasket<T>>::remove_prefix(collection_id, None);583584 DestroyedCollectionCount::put(DestroyedCollectionCount::get()585 .checked_add(1)586 .ok_or(Error::<T>::NumOverflow)?);587588 Ok(())589 }590591 592 593 594 595 596 597 598 599 600 601 602 603 #[weight = <T as Config>::WeightInfo::add_to_white_list()]604 #[transactional]605 pub fn add_to_white_list(origin, collection_id: CollectionId, address: T::CrossAccountId) -> DispatchResult{606607 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);608 let collection = Self::get_collection(collection_id)?;609610 Self::toggle_white_list_internal(611 &sender,612 &collection,613 &address,614 true,615 )?;616617 Ok(())618 }619620 621 622 623 624 625 626 627 628 629 630 631 632 #[weight = <T as Config>::WeightInfo::remove_from_white_list()]633 #[transactional]634 pub fn remove_from_white_list(origin, collection_id: CollectionId, address: T::CrossAccountId) -> DispatchResult{635636 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);637 let collection = Self::get_collection(collection_id)?;638639 Self::toggle_white_list_internal(640 &sender,641 &collection,642 &address,643 false,644 )?;645646 Ok(())647 }648649 650 651 652 653 654 655 656 657 658 659 660 #[weight = <T as Config>::WeightInfo::set_public_access_mode()]661 #[transactional]662 pub fn set_public_access_mode(origin, collection_id: CollectionId, mode: AccessMode) -> DispatchResult663 {664 let sender = ensure_signed(origin)?;665666 let mut target_collection = Self::get_collection(collection_id)?;667 Self::check_owner_permissions(&target_collection, &sender)?;668 target_collection.access = mode;669 target_collection.save()670 }671672 673 674 675 676 677 678 679 680 681 682 683 684 685 #[weight = <T as Config>::WeightInfo::set_mint_permission()]686 #[transactional]687 pub fn set_mint_permission(origin, collection_id: CollectionId, mint_permission: bool) -> DispatchResult688 {689 let sender = ensure_signed(origin)?;690691 let mut target_collection = Self::get_collection(collection_id)?;692 Self::check_owner_permissions(&target_collection, &sender)?;693 target_collection.mint_mode = mint_permission;694 target_collection.save()695 }696697 698 699 700 701 702 703 704 705 706 707 708 #[weight = <T as Config>::WeightInfo::change_collection_owner()]709 #[transactional]710 pub fn change_collection_owner(origin, collection_id: CollectionId, new_owner: T::AccountId) -> DispatchResult {711712 let sender = ensure_signed(origin)?;713 let mut target_collection = Self::get_collection(collection_id)?;714 Self::check_owner_permissions(&target_collection, &sender)?;715 target_collection.owner = new_owner;716 target_collection.save()717 }718719 720 721 722 723 724 725 726 727 728 729 730 731 732 #[weight = <T as Config>::WeightInfo::add_collection_admin()]733 #[transactional]734 pub fn add_collection_admin(origin, collection_id: CollectionId, new_admin_id: T::CrossAccountId) -> DispatchResult {735 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);736 let collection = Self::get_collection(collection_id)?;737 Self::check_owner_or_admin_permissions(&collection, &sender)?;738 let mut admin_arr = <AdminList<T>>::get(collection_id);739740 match admin_arr.binary_search(&new_admin_id) {741 Ok(_) => {},742 Err(idx) => {743 ensure!(admin_arr.len() < COLLECTION_ADMINS_LIMIT as usize, Error::<T>::CollectionAdminsLimitExceeded);744 admin_arr.insert(idx, new_admin_id);745 <AdminList<T>>::insert(collection_id, admin_arr);746 }747 }748 Ok(())749 }750751 752 753 754 755 756 757 758 759 760 761 762 763 #[weight = <T as Config>::WeightInfo::remove_collection_admin()]764 #[transactional]765 pub fn remove_collection_admin(origin, collection_id: CollectionId, account_id: T::CrossAccountId) -> DispatchResult {766 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);767 let collection = Self::get_collection(collection_id)?;768 Self::check_owner_or_admin_permissions(&collection, &sender)?;769 let mut admin_arr = <AdminList<T>>::get(collection_id);770771 if let Ok(idx) = admin_arr.binary_search(&account_id) {772 admin_arr.remove(idx);773 <AdminList<T>>::insert(collection_id, admin_arr);774 }775 Ok(())776 }777778 779 780 781 782 783 784 785 786 787 #[weight = <T as Config>::WeightInfo::set_collection_sponsor()]788 #[transactional]789 pub fn set_collection_sponsor(origin, collection_id: CollectionId, new_sponsor: T::AccountId) -> DispatchResult {790 let sender = ensure_signed(origin)?;791 let mut target_collection = Self::get_collection(collection_id)?;792 Self::check_owner_permissions(&target_collection, &sender)?;793794 target_collection.sponsorship = SponsorshipState::Unconfirmed(new_sponsor);795 target_collection.save()796 }797798 799 800 801 802 803 804 805 #[weight = <T as Config>::WeightInfo::confirm_sponsorship()]806 #[transactional]807 pub fn confirm_sponsorship(origin, collection_id: CollectionId) -> DispatchResult {808 let sender = ensure_signed(origin)?;809810 let mut target_collection = Self::get_collection(collection_id)?;811 ensure!(812 target_collection.sponsorship.pending_sponsor() == Some(&sender),813 Error::<T>::ConfirmUnsetSponsorFail814 );815816 target_collection.sponsorship = SponsorshipState::Confirmed(sender);817 target_collection.save()818 }819820 821 822 823 824 825 826 827 828 829 #[weight = <T as Config>::WeightInfo::remove_collection_sponsor()]830 #[transactional]831 pub fn remove_collection_sponsor(origin, collection_id: CollectionId) -> DispatchResult {832 let sender = ensure_signed(origin)?;833834 let mut target_collection = Self::get_collection(collection_id)?;835 Self::check_owner_permissions(&target_collection, &sender)?;836837 target_collection.sponsorship = SponsorshipState::Disabled;838 target_collection.save()839 }840841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864865 #[weight = <T as Config>::WeightInfo::create_item(data.data_size())]866 #[transactional]867 pub fn create_item(origin, collection_id: CollectionId, owner: T::CrossAccountId, data: CreateItemData) -> DispatchResult {868 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);869 let collection = Self::get_collection(collection_id)?;870871 Self::create_item_internal(&sender, &collection, &owner, data)?;872873 collection.submit_logs()874 }875876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 #[weight = <T as Config>::WeightInfo::create_item(items_data.iter()895 .map(|data| { data.data_size() })896 .sum())]897 #[transactional]898 pub fn create_multiple_items(origin, collection_id: CollectionId, owner: T::CrossAccountId, items_data: Vec<CreateItemData>) -> DispatchResult {899900 ensure!(!items_data.is_empty(), Error::<T>::EmptyArgument);901 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);902 let collection = Self::get_collection(collection_id)?;903904 Self::create_multiple_items_internal(&sender, &collection, &owner, items_data)?;905906 collection.submit_logs()907 }908909 910911 912 913 914 915 916 917 918 919 920 921 922 #[weight = <T as Config>::WeightInfo::burn_item()]923 #[transactional]924 pub fn set_transfers_enabled_flag(origin, collection_id: CollectionId, value: bool) -> DispatchResult {925926 let sender = ensure_signed(origin)?;927 let mut target_collection = Self::get_collection(collection_id)?;928929 Self::check_owner_permissions(&target_collection, &sender)?;930931 target_collection.transfers_enabled = value;932 target_collection.save()933 }934935 936 937 938 939 940 941 942 943 944 945 946 947 948 #[weight = <T as Config>::WeightInfo::burn_item()]949 #[transactional]950 pub fn burn_item(origin, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResult {951952 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);953 let target_collection = Self::get_collection(collection_id)?;954955 Self::burn_item_internal(&sender, &target_collection, item_id, value)?;956957 target_collection.submit_logs()958 }959960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 #[weight = <T as Config>::WeightInfo::transfer()]984 #[transactional]985 pub fn transfer(origin, recipient: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResult {986 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);987 let collection = Self::get_collection(collection_id)?;988989 Self::transfer_internal(&sender, &recipient, &collection, item_id, value)?;990991 collection.submit_logs()992 }993994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 #[weight = <T as Config>::WeightInfo::approve()]1010 #[transactional]1011 pub fn approve(origin, spender: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, amount: u128) -> DispatchResult {1012 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1013 let collection = Self::get_collection(collection_id)?;10141015 Self::approve_internal(&sender, &spender, &collection, item_id, amount)?;10161017 collection.submit_logs()1018 }10191020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 #[weight = <T as Config>::WeightInfo::transfer_from()]1040 #[transactional]1041 pub fn transfer_from(origin, from: T::CrossAccountId, recipient: T::CrossAccountId, collection_id: CollectionId, item_id: TokenId, value: u128 ) -> DispatchResult {1042 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1043 let collection = Self::get_collection(collection_id)?;10441045 Self::transfer_from_internal(&sender, &from, &recipient, &collection, item_id, value)?;10461047 collection.submit_logs()1048 }1049 1050 1051 1052 1053 10541055 10561057 10581059 1060 10611062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 #[weight = <T as Config>::WeightInfo::set_variable_meta_data()]1075 #[transactional]1076 pub fn set_variable_meta_data (1077 origin,1078 collection_id: CollectionId,1079 item_id: TokenId,1080 data: Vec<u8>1081 ) -> DispatchResult {1082 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);10831084 let collection = Self::get_collection(collection_id)?;10851086 Self::set_variable_meta_data_internal(&sender, &collection, item_id, data)?;10871088 Ok(())1089 }10901091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 #[weight = <T as Config>::WeightInfo::set_schema_version()]1106 #[transactional]1107 pub fn set_schema_version(1108 origin,1109 collection_id: CollectionId,1110 version: SchemaVersion1111 ) -> DispatchResult {1112 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1113 let mut target_collection = Self::get_collection(collection_id)?;1114 Self::check_owner_or_admin_permissions(&target_collection, &sender)?;1115 target_collection.schema_version = version;1116 target_collection.save()1117 }11181119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 #[weight = <T as Config>::WeightInfo::set_offchain_schema()]1132 #[transactional]1133 pub fn set_offchain_schema(1134 origin,1135 collection_id: CollectionId,1136 schema: Vec<u8>1137 ) -> DispatchResult {1138 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1139 let mut target_collection = Self::get_collection(collection_id)?;1140 Self::check_owner_or_admin_permissions(&target_collection, &sender)?;11411142 1143 ensure!(schema.len() as u32 <= OFFCHAIN_SCHEMA_LIMIT, "");11441145 target_collection.offchain_schema = schema;1146 target_collection.save()1147 }11481149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 #[weight = <T as Config>::WeightInfo::set_const_on_chain_schema()]1162 #[transactional]1163 pub fn set_const_on_chain_schema (1164 origin,1165 collection_id: CollectionId,1166 schema: Vec<u8>1167 ) -> DispatchResult {1168 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1169 let mut target_collection = Self::get_collection(collection_id)?;1170 Self::check_owner_or_admin_permissions(&target_collection, &sender)?;11711172 1173 ensure!(schema.len() as u32 <= CONST_ON_CHAIN_SCHEMA_LIMIT, "");11741175 target_collection.const_on_chain_schema = schema;1176 target_collection.save()1177 }11781179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 #[weight = <T as Config>::WeightInfo::set_const_on_chain_schema()]1192 #[transactional]1193 pub fn set_variable_on_chain_schema (1194 origin,1195 collection_id: CollectionId,1196 schema: Vec<u8>1197 ) -> DispatchResult {1198 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1199 let mut target_collection = Self::get_collection(collection_id)?;1200 Self::check_owner_or_admin_permissions(&target_collection, &sender)?;12011202 1203 ensure!(schema.len() as u32 <= VARIABLE_ON_CHAIN_SCHEMA_LIMIT, "");12041205 target_collection.variable_on_chain_schema = schema;1206 target_collection.save()1207 }12081209 #[weight = <T as Config>::WeightInfo::set_collection_limits()]1210 #[transactional]1211 pub fn set_collection_limits(1212 origin,1213 collection_id: u32,1214 new_limits: CollectionLimits<T::BlockNumber>,1215 ) -> DispatchResult {1216 let sender = T::CrossAccountId::from_sub(ensure_signed(origin)?);1217 let mut target_collection = Self::get_collection(collection_id)?;1218 Self::check_owner_permissions(&target_collection, sender.as_sub())?;1219 let old_limits = &target_collection.limits;12201221 1222 ensure!(new_limits.sponsor_transfer_timeout <= MAX_SPONSOR_TIMEOUT &&1223 new_limits.account_token_ownership_limit <= MAX_TOKEN_OWNERSHIP &&1224 new_limits.sponsored_data_size <= CUSTOM_DATA_LIMIT,1225 Error::<T>::CollectionLimitBoundsExceeded);12261227 1228 ensure!(old_limits.token_limit >= new_limits.token_limit, Error::<T>::CollectionTokenLimitExceeded);1229 ensure!(new_limits.token_limit > 0, Error::<T>::CollectionTokenLimitExceeded);12301231 ensure!(1232 (old_limits.owner_can_transfer || !new_limits.owner_can_transfer) &&1233 (old_limits.owner_can_destroy || !new_limits.owner_can_destroy),1234 Error::<T>::OwnerPermissionsCantBeReverted,1235 );12361237 target_collection.limits = new_limits;12381239 target_collection.save()1240 }1241 }1242}12431244impl<T: Config> Module<T> {1245 pub fn create_item_internal(1246 sender: &T::CrossAccountId,1247 collection: &CollectionHandle<T>,1248 owner: &T::CrossAccountId,1249 data: CreateItemData,1250 ) -> DispatchResult {1251 Self::can_create_items_in_collection(collection, sender, owner, 1)?;1252 Self::validate_create_item_args(collection, &data)?;1253 Self::create_item_no_validation(collection, owner, data)?;12541255 Ok(())1256 }12571258 pub fn transfer_internal(1259 sender: &T::CrossAccountId,1260 recipient: &T::CrossAccountId,1261 target_collection: &CollectionHandle<T>,1262 item_id: TokenId,1263 value: u128,1264 ) -> DispatchResult {1265 1266 Self::is_correct_transfer(target_collection, recipient)?;12671268 1269 ensure!(1270 Self::is_item_owner(sender, target_collection, item_id)?1271 || Self::is_owner_or_admin_permissions(target_collection, sender)?,1272 Error::<T>::NoPermission1273 );12741275 if target_collection.access == AccessMode::WhiteList {1276 Self::check_white_list(target_collection, sender)?;1277 Self::check_white_list(target_collection, recipient)?;1278 }12791280 match target_collection.mode {1281 CollectionMode::NFT => Self::transfer_nft(1282 target_collection,1283 item_id,1284 sender.clone(),1285 recipient.clone(),1286 )?,1287 CollectionMode::Fungible(_) => {1288 Self::transfer_fungible(target_collection, value, sender, recipient)?1289 }1290 CollectionMode::ReFungible => Self::transfer_refungible(1291 target_collection,1292 item_id,1293 value,1294 sender.clone(),1295 recipient.clone(),1296 )?,1297 _ => (),1298 };12991300 Self::deposit_event(RawEvent::Transfer(1301 target_collection.id,1302 item_id,1303 sender.clone(),1304 recipient.clone(),1305 value,1306 ));13071308 Ok(())1309 }13101311 pub fn approve_internal(1312 sender: &T::CrossAccountId,1313 spender: &T::CrossAccountId,1314 collection: &CollectionHandle<T>,1315 item_id: TokenId,1316 amount: u128,1317 ) -> DispatchResult {1318 Self::token_exists(collection, item_id)?;13191320 1321 let bypasses_limits = collection.limits.owner_can_transfer1322 && Self::is_owner_or_admin_permissions(collection, sender)?;13231324 let allowance_limit = if bypasses_limits {1325 None1326 } else if let Some(amount) = Self::owned_amount(sender, collection, item_id)? {1327 Some(amount)1328 } else {1329 fail!(Error::<T>::NoPermission);1330 };13311332 if collection.access == AccessMode::WhiteList {1333 Self::check_white_list(collection, sender)?;1334 Self::check_white_list(collection, spender)?;1335 }13361337 collection.consume_sload()?;1338 let allowance: u128 = amount1339 .checked_add(<Allowances<T>>::get(1340 collection.id,1341 (item_id, sender.as_sub(), spender.as_sub()),1342 ))1343 .ok_or(Error::<T>::NumOverflow)?;1344 if let Some(limit) = allowance_limit {1345 ensure!(limit >= allowance, Error::<T>::TokenValueTooLow);1346 }1347 collection.consume_sstore()?;1348 <Allowances<T>>::insert(1349 collection.id,1350 (item_id, sender.as_sub(), spender.as_sub()),1351 allowance,1352 );13531354 if matches!(collection.mode, CollectionMode::NFT) {1355 1356 collection.log(ERC721Events::Approval {1357 owner: *sender.as_eth(),1358 approved: *spender.as_eth(),1359 token_id: item_id.into(),1360 })?;1361 }13621363 if matches!(collection.mode, CollectionMode::Fungible(_)) {1364 1365 collection.log(ERC20Events::Approval {1366 owner: *sender.as_eth(),1367 spender: *spender.as_eth(),1368 value: allowance.into(),1369 })?;1370 }13711372 Self::deposit_event(RawEvent::Approved(1373 collection.id,1374 item_id,1375 sender.clone(),1376 spender.clone(),1377 allowance,1378 ));1379 Ok(())1380 }13811382 pub fn transfer_from_internal(1383 sender: &T::CrossAccountId,1384 from: &T::CrossAccountId,1385 recipient: &T::CrossAccountId,1386 collection: &CollectionHandle<T>,1387 item_id: TokenId,1388 amount: u128,1389 ) -> DispatchResult {1390 1391 collection.consume_sload()?;1392 let approval: u128 =1393 <Allowances<T>>::get(collection.id, (item_id, from.as_sub(), sender.as_sub()));13941395 1396 Self::is_correct_transfer(collection, recipient)?;13971398 1399 ensure!(1400 approval >= amount1401 || (collection.limits.owner_can_transfer1402 && Self::is_owner_or_admin_permissions(collection, sender)?),1403 Error::<T>::NoPermission1404 );14051406 if collection.access == AccessMode::WhiteList {1407 Self::check_white_list(collection, sender)?;1408 Self::check_white_list(collection, recipient)?;1409 }14101411 1412 let allowance = approval.saturating_sub(amount);1413 collection.consume_sstore()?;1414 if allowance > 0 {1415 <Allowances<T>>::insert(1416 collection.id,1417 (item_id, from.as_sub(), sender.as_sub()),1418 allowance,1419 );1420 } else {1421 <Allowances<T>>::remove(collection.id, (item_id, from.as_sub(), sender.as_sub()));1422 }14231424 match collection.mode {1425 CollectionMode::NFT => {1426 Self::transfer_nft(collection, item_id, from.clone(), recipient.clone())?1427 }1428 CollectionMode::Fungible(_) => {1429 Self::transfer_fungible(collection, amount, from, recipient)?1430 }1431 CollectionMode::ReFungible => Self::transfer_refungible(1432 collection,1433 item_id,1434 amount,1435 from.clone(),1436 recipient.clone(),1437 )?,1438 _ => (),1439 };14401441 if matches!(collection.mode, CollectionMode::Fungible(_)) {1442 collection.log(ERC20Events::Approval {1443 owner: *from.as_eth(),1444 spender: *sender.as_eth(),1445 value: allowance.into(),1446 })?;1447 }14481449 Ok(())1450 }14511452 pub fn set_variable_meta_data_internal(1453 sender: &T::CrossAccountId,1454 collection: &CollectionHandle<T>,1455 item_id: TokenId,1456 data: Vec<u8>,1457 ) -> DispatchResult {1458 Self::token_exists(collection, item_id)?;14591460 ensure!(1461 CUSTOM_DATA_LIMIT >= data.len() as u32,1462 Error::<T>::TokenVariableDataLimitExceeded1463 );14641465 1466 ensure!(1467 Self::is_item_owner(sender, collection, item_id)?1468 || Self::is_owner_or_admin_permissions(collection, sender)?,1469 Error::<T>::NoPermission1470 );14711472 match collection.mode {1473 CollectionMode::NFT => Self::set_nft_variable_data(collection, item_id, data)?,1474 CollectionMode::ReFungible => {1475 Self::set_re_fungible_variable_data(collection, item_id, data)?1476 }1477 CollectionMode::Fungible(_) => fail!(Error::<T>::CantStoreMetadataInFungibleTokens),1478 _ => fail!(Error::<T>::UnexpectedCollectionType),1479 };14801481 Ok(())1482 }14831484 pub fn create_multiple_items_internal(1485 sender: &T::CrossAccountId,1486 collection: &CollectionHandle<T>,1487 owner: &T::CrossAccountId,1488 items_data: Vec<CreateItemData>,1489 ) -> DispatchResult {1490 Self::can_create_items_in_collection(collection, sender, owner, items_data.len() as u32)?;14911492 for data in &items_data {1493 Self::validate_create_item_args(collection, data)?;1494 }1495 for data in &items_data {1496 Self::create_item_no_validation(collection, owner, data.clone())?;1497 }14981499 Ok(())1500 }15011502 pub fn burn_item_internal(1503 sender: &T::CrossAccountId,1504 collection: &CollectionHandle<T>,1505 item_id: TokenId,1506 value: u128,1507 ) -> DispatchResult {1508 ensure!(1509 Self::is_item_owner(sender, collection, item_id)?1510 || (collection.limits.owner_can_transfer1511 && Self::is_owner_or_admin_permissions(collection, sender)?),1512 Error::<T>::NoPermission1513 );15141515 if collection.access == AccessMode::WhiteList {1516 Self::check_white_list(collection, sender)?;1517 }15181519 match collection.mode {1520 CollectionMode::NFT => Self::burn_nft_item(collection, item_id)?,1521 CollectionMode::Fungible(_) => Self::burn_fungible_item(sender, collection, value)?,1522 CollectionMode::ReFungible => Self::burn_refungible_item(collection, item_id, sender)?,1523 _ => (),1524 };15251526 Ok(())1527 }15281529 pub fn toggle_white_list_internal(1530 sender: &T::CrossAccountId,1531 collection: &CollectionHandle<T>,1532 address: &T::CrossAccountId,1533 whitelisted: bool,1534 ) -> DispatchResult {1535 Self::check_owner_or_admin_permissions(collection, sender)?;15361537 if whitelisted {1538 <WhiteList<T>>::insert(collection.id, address.as_sub(), true);1539 } else {1540 <WhiteList<T>>::remove(collection.id, address.as_sub());1541 }15421543 Ok(())1544 }15451546 fn is_correct_transfer(1547 collection: &CollectionHandle<T>,1548 recipient: &T::CrossAccountId,1549 ) -> DispatchResult {1550 let collection_id = collection.id;15511552 1553 collection.consume_sload()?;1554 let account_items: u32 =1555 <AddressTokens<T>>::get(collection_id, recipient.as_sub()).len() as u32;1556 ensure!(1557 collection.limits.account_token_ownership_limit > account_items,1558 Error::<T>::AccountTokenLimitExceeded1559 );15601561 1562 ensure!(collection.transfers_enabled, Error::<T>::TransferNotAllowed);15631564 Ok(())1565 }15661567 fn can_create_items_in_collection(1568 collection: &CollectionHandle<T>,1569 sender: &T::CrossAccountId,1570 owner: &T::CrossAccountId,1571 amount: u32,1572 ) -> DispatchResult {1573 let collection_id = collection.id;15741575 1576 let total_items: u32 = ItemListIndex::get(collection_id)1577 .checked_add(amount)1578 .ok_or(Error::<T>::CollectionTokenLimitExceeded)?;1579 let account_items: u32 = (<AddressTokens<T>>::get(collection_id, owner.as_sub()).len()1580 as u32)1581 .checked_add(amount)1582 .ok_or(Error::<T>::AccountTokenLimitExceeded)?;1583 ensure!(1584 collection.limits.token_limit >= total_items,1585 Error::<T>::CollectionTokenLimitExceeded1586 );1587 ensure!(1588 collection.limits.account_token_ownership_limit >= account_items,1589 Error::<T>::AccountTokenLimitExceeded1590 );15911592 if !Self::is_owner_or_admin_permissions(collection, sender)? {1593 ensure!(collection.mint_mode, Error::<T>::PublicMintingNotAllowed);1594 Self::check_white_list(collection, owner)?;1595 Self::check_white_list(collection, sender)?;1596 }15971598 Ok(())1599 }16001601 fn validate_create_item_args(1602 target_collection: &CollectionHandle<T>,1603 data: &CreateItemData,1604 ) -> DispatchResult {1605 match target_collection.mode {1606 CollectionMode::NFT => {1607 if !matches!(data, CreateItemData::NFT(_)) {1608 fail!(Error::<T>::NotNftDataUsedToMintNftCollectionToken);1609 }1610 }1611 CollectionMode::Fungible(_) => {1612 if !matches!(data, CreateItemData::Fungible(_)) {1613 fail!(Error::<T>::NotFungibleDataUsedToMintFungibleCollectionToken);1614 }1615 }1616 CollectionMode::ReFungible => {1617 if let CreateItemData::ReFungible(data) = data {1618 1619 ensure!(1620 data.pieces <= MAX_REFUNGIBLE_PIECES,1621 Error::<T>::WrongRefungiblePieces1622 );1623 ensure!(data.pieces > 0, Error::<T>::WrongRefungiblePieces);1624 } else {1625 fail!(Error::<T>::NotReFungibleDataUsedToMintReFungibleCollectionToken);1626 }1627 }1628 _ => {1629 fail!(Error::<T>::UnexpectedCollectionType);1630 }1631 };16321633 Ok(())1634 }16351636 fn create_item_no_validation(1637 collection: &CollectionHandle<T>,1638 owner: &T::CrossAccountId,1639 data: CreateItemData,1640 ) -> DispatchResult {1641 match data {1642 CreateItemData::NFT(data) => {1643 let item = NftItemType {1644 owner: owner.clone(),1645 const_data: data.const_data.into_inner(),1646 variable_data: data.variable_data.into_inner(),1647 };16481649 Self::add_nft_item(collection, item)?;1650 }1651 CreateItemData::Fungible(data) => {1652 Self::add_fungible_item(collection, owner, data.value)?;1653 }1654 CreateItemData::ReFungible(data) => {1655 let owner_list = vec![Ownership {1656 owner: owner.clone(),1657 fraction: data.pieces,1658 }];16591660 let item = ReFungibleItemType {1661 owner: owner_list,1662 const_data: data.const_data.into_inner(),1663 variable_data: data.variable_data.into_inner(),1664 };16651666 Self::add_refungible_item(collection, item)?;1667 }1668 };16691670 Ok(())1671 }16721673 fn add_fungible_item(1674 collection: &CollectionHandle<T>,1675 owner: &T::CrossAccountId,1676 value: u128,1677 ) -> DispatchResult {1678 let collection_id = collection.id;16791680 1681 collection.consume_sload()?;1682 let balance: u128 = <FungibleItemList<T>>::get(collection_id, owner.as_sub()).value;16831684 1685 let item = FungibleItemType {1686 value: balance.checked_add(value).ok_or(Error::<T>::NumOverflow)?,1687 };1688 collection.consume_sstore()?;1689 <FungibleItemList<T>>::insert(collection_id, owner.as_sub(), item);16901691 1692 collection.consume_sload()?;1693 let new_balance = <Balance<T>>::get(collection_id, owner.as_sub())1694 .checked_add(value)1695 .ok_or(Error::<T>::NumOverflow)?;1696 collection.consume_sstore()?;1697 <Balance<T>>::insert(collection_id, owner.as_sub(), new_balance);16981699 collection.log(ERC20Events::Transfer {1700 from: H160::default(),1701 to: *owner.as_eth(),1702 value: value.into(),1703 })?;1704 Self::deposit_event(RawEvent::ItemCreated(collection_id, 0, owner.clone()));1705 Ok(())1706 }17071708 fn add_refungible_item(1709 collection: &CollectionHandle<T>,1710 item: ReFungibleItemType<T::CrossAccountId>,1711 ) -> DispatchResult {1712 let collection_id = collection.id;17131714 let current_index = <ItemListIndex>::get(collection_id)1715 .checked_add(1)1716 .ok_or(Error::<T>::NumOverflow)?;1717 let itemcopy = item.clone();17181719 ensure!(item.owner.len() == 1, Error::<T>::BadCreateRefungibleCall,);1720 let item_owner = item.owner.first().expect("only one owner is defined");17211722 let value = item_owner.fraction;1723 let owner = item_owner.owner.clone();17241725 Self::add_token_index(collection, current_index, &owner)?;17261727 <ItemListIndex>::insert(collection_id, current_index);1728 <ReFungibleItemList<T>>::insert(collection_id, current_index, itemcopy);17291730 1731 let new_balance = <Balance<T>>::get(collection_id, owner.as_sub())1732 .checked_add(value)1733 .ok_or(Error::<T>::NumOverflow)?;1734 <Balance<T>>::insert(collection_id, owner.as_sub(), new_balance);17351736 Self::deposit_event(RawEvent::ItemCreated(collection_id, current_index, owner));1737 Ok(())1738 }17391740 fn add_nft_item(1741 collection: &CollectionHandle<T>,1742 item: NftItemType<T::CrossAccountId>,1743 ) -> DispatchResult {1744 let collection_id = collection.id;17451746 let current_index = <ItemListIndex>::get(collection_id)1747 .checked_add(1)1748 .ok_or(Error::<T>::NumOverflow)?;17491750 let item_owner = item.owner.clone();1751 Self::add_token_index(collection, current_index, &item.owner)?;17521753 <ItemListIndex>::insert(collection_id, current_index);1754 <NftItemList<T>>::insert(collection_id, current_index, item);17551756 1757 let new_balance = <Balance<T>>::get(collection_id, item_owner.as_sub())1758 .checked_add(1)1759 .ok_or(Error::<T>::NumOverflow)?;1760 <Balance<T>>::insert(collection_id, item_owner.as_sub(), new_balance);17611762 collection.log(ERC721Events::Transfer {1763 from: H160::default(),1764 to: *item_owner.as_eth(),1765 token_id: current_index.into(),1766 })?;1767 Self::deposit_event(RawEvent::ItemCreated(1768 collection_id,1769 current_index,1770 item_owner,1771 ));1772 Ok(())1773 }17741775 fn burn_refungible_item(1776 collection: &CollectionHandle<T>,1777 item_id: TokenId,1778 owner: &T::CrossAccountId,1779 ) -> DispatchResult {1780 let collection_id = collection.id;17811782 let mut token = <ReFungibleItemList<T>>::get(collection_id, item_id)1783 .ok_or(Error::<T>::TokenNotFound)?;1784 let rft_balance = token1785 .owner1786 .iter()1787 .find(|&i| i.owner == *owner)1788 .ok_or(Error::<T>::TokenNotFound)?;1789 Self::remove_token_index(collection, item_id, owner)?;17901791 1792 let new_balance = <Balance<T>>::get(collection_id, rft_balance.owner.as_sub())1793 .checked_sub(rft_balance.fraction)1794 .ok_or(Error::<T>::NumOverflow)?;1795 <Balance<T>>::insert(collection_id, rft_balance.owner.as_sub(), new_balance);17961797 1798 let index = token1799 .owner1800 .iter()1801 .position(|i| i.owner == *owner)1802 .expect("owned item is exists");1803 token.owner.remove(index);1804 let owner_count = token.owner.len();18051806 1807 if owner_count == 0 {1808 <ReFungibleItemList<T>>::remove(collection_id, item_id);1809 <VariableMetaDataBasket<T>>::remove(collection_id, item_id);1810 } else {1811 <ReFungibleItemList<T>>::insert(collection_id, item_id, token);1812 }18131814 Ok(())1815 }18161817 fn burn_nft_item(collection: &CollectionHandle<T>, item_id: TokenId) -> DispatchResult {1818 let collection_id = collection.id;18191820 let item =1821 <NftItemList<T>>::get(collection_id, item_id).ok_or(Error::<T>::TokenNotFound)?;1822 Self::remove_token_index(collection, item_id, &item.owner)?;18231824 1825 let new_balance = <Balance<T>>::get(collection_id, item.owner.as_sub())1826 .checked_sub(1)1827 .ok_or(Error::<T>::NumOverflow)?;1828 <Balance<T>>::insert(collection_id, item.owner.as_sub(), new_balance);1829 <NftItemList<T>>::remove(collection_id, item_id);1830 <VariableMetaDataBasket<T>>::remove(collection_id, item_id);18311832 Self::deposit_event(RawEvent::ItemDestroyed(collection.id, item_id));1833 Ok(())1834 }18351836 fn burn_fungible_item(1837 owner: &T::CrossAccountId,1838 collection: &CollectionHandle<T>,1839 value: u128,1840 ) -> DispatchResult {1841 let collection_id = collection.id;18421843 let mut balance = <FungibleItemList<T>>::get(collection_id, owner.as_sub());1844 ensure!(balance.value >= value, Error::<T>::TokenValueNotEnough);18451846 1847 let new_balance = <Balance<T>>::get(collection_id, owner.as_sub())1848 .checked_sub(value)1849 .ok_or(Error::<T>::NumOverflow)?;1850 <Balance<T>>::insert(collection_id, owner.as_sub(), new_balance);18511852 if balance.value - value > 0 {1853 balance.value -= value;1854 <FungibleItemList<T>>::insert(collection_id, owner.as_sub(), balance);1855 } else {1856 <FungibleItemList<T>>::remove(collection_id, owner.as_sub());1857 }18581859 collection.log(ERC20Events::Transfer {1860 from: *owner.as_eth(),1861 to: H160::default(),1862 value: value.into(),1863 })?;1864 Ok(())1865 }18661867 pub fn get_collection(1868 collection_id: CollectionId,1869 ) -> Result<CollectionHandle<T>, sp_runtime::DispatchError> {1870 Ok(<CollectionHandle<T>>::get(collection_id).ok_or(Error::<T>::CollectionNotFound)?)1871 }18721873 fn check_owner_permissions(1874 target_collection: &CollectionHandle<T>,1875 subject: &T::AccountId,1876 ) -> DispatchResult {1877 ensure!(1878 *subject == target_collection.owner,1879 Error::<T>::NoPermission1880 );18811882 Ok(())1883 }18841885 fn is_owner_or_admin_permissions(1886 collection: &CollectionHandle<T>,1887 subject: &T::CrossAccountId,1888 ) -> Result<bool, DispatchError> {1889 collection.consume_sload()?;1890 Ok(*subject.as_sub() == collection.owner1891 || <AdminList<T>>::get(collection.id).contains(subject))1892 }18931894 fn check_owner_or_admin_permissions(1895 collection: &CollectionHandle<T>,1896 subject: &T::CrossAccountId,1897 ) -> DispatchResult {1898 ensure!(1899 Self::is_owner_or_admin_permissions(collection, subject)?,1900 Error::<T>::NoPermission1901 );19021903 Ok(())1904 }19051906 fn owned_amount(1907 subject: &T::CrossAccountId,1908 collection: &CollectionHandle<T>,1909 item_id: TokenId,1910 ) -> Result<Option<u128>, DispatchError> {1911 collection.consume_sload()?;1912 Ok(Self::owned_amount_unchecked(subject, collection, item_id))1913 }19141915 fn owned_amount_unchecked(1916 subject: &T::CrossAccountId,1917 target_collection: &CollectionHandle<T>,1918 item_id: TokenId,1919 ) -> Option<u128> {1920 let collection_id = target_collection.id;19211922 match target_collection.mode {1923 CollectionMode::NFT => {1924 (<NftItemList<T>>::get(collection_id, item_id)?.owner == *subject).then(|| 1)1925 }1926 CollectionMode::Fungible(_) => {1927 Some(<FungibleItemList<T>>::get(collection_id, &subject.as_sub()).value)1928 }1929 CollectionMode::ReFungible => <ReFungibleItemList<T>>::get(collection_id, item_id)?1930 .owner1931 .iter()1932 .find(|i| i.owner == *subject)1933 .map(|i| i.fraction),1934 CollectionMode::Invalid => None,1935 }1936 }19371938 fn is_item_owner(1939 subject: &T::CrossAccountId,1940 target_collection: &CollectionHandle<T>,1941 item_id: TokenId,1942 ) -> Result<bool, DispatchError> {1943 Ok(match target_collection.mode {1944 CollectionMode::Fungible(_) => true,1945 _ => Self::owned_amount(subject, target_collection, item_id)?.is_some(),1946 })1947 }19481949 fn check_white_list(1950 collection: &CollectionHandle<T>,1951 address: &T::CrossAccountId,1952 ) -> DispatchResult {1953 collection.consume_sload()?;1954 ensure!(1955 <WhiteList<T>>::contains_key(collection.id, address.as_sub()),1956 Error::<T>::AddresNotInWhiteList,1957 );1958 Ok(())1959 }19601961 1962 1963 fn token_exists(target_collection: &CollectionHandle<T>, item_id: TokenId) -> DispatchResult {1964 let collection_id = target_collection.id;1965 let exists = match target_collection.mode {1966 CollectionMode::NFT => <NftItemList<T>>::contains_key(collection_id, item_id),1967 CollectionMode::Fungible(_) => true,1968 CollectionMode::ReFungible => {1969 <ReFungibleItemList<T>>::contains_key(collection_id, item_id)1970 }1971 _ => false,1972 };19731974 ensure!(exists, Error::<T>::TokenNotFound);1975 Ok(())1976 }19771978 fn transfer_fungible(1979 collection: &CollectionHandle<T>,1980 value: u128,1981 owner: &T::CrossAccountId,1982 recipient: &T::CrossAccountId,1983 ) -> DispatchResult {1984 let collection_id = collection.id;19851986 collection.consume_sload()?;1987 collection.consume_sload()?;1988 let mut recipient_balance = <FungibleItemList<T>>::get(collection_id, recipient.as_sub());1989 let mut balance = <FungibleItemList<T>>::get(collection_id, owner.as_sub());19901991 recipient_balance.value = recipient_balance1992 .value1993 .checked_add(value)1994 .ok_or(Error::<T>::NumOverflow)?;1995 balance.value = balance1996 .value1997 .checked_sub(value)1998 .ok_or(Error::<T>::TokenValueTooLow)?;19992000 2001 collection.consume_sstore()?;2002 collection.consume_sstore()?;2003 if balance.value != 0 {2004 <Balance<T>>::insert(collection_id, owner.as_sub(), balance.value);2005 } else {2006 <Balance<T>>::remove(collection_id, owner.as_sub());2007 }2008 <Balance<T>>::insert(collection_id, recipient.as_sub(), recipient_balance.value);20092010 2011 collection.consume_sstore()?;2012 collection.consume_sstore()?;2013 if balance.value != 0 {2014 <FungibleItemList<T>>::insert(collection_id, owner.as_sub(), balance);2015 } else {2016 <FungibleItemList<T>>::remove(collection_id, owner.as_sub());2017 }2018 <FungibleItemList<T>>::insert(collection_id, recipient.as_sub(), recipient_balance);20192020 collection.log(ERC20Events::Transfer {2021 from: *owner.as_eth(),2022 to: *recipient.as_eth(),2023 value: value.into(),2024 })?;2025 Self::deposit_event(RawEvent::Transfer(2026 collection.id,2027 1,2028 owner.clone(),2029 recipient.clone(),2030 value,2031 ));20322033 Ok(())2034 }20352036 fn transfer_refungible(2037 collection: &CollectionHandle<T>,2038 item_id: TokenId,2039 value: u128,2040 owner: T::CrossAccountId,2041 new_owner: T::CrossAccountId,2042 ) -> DispatchResult {2043 let collection_id = collection.id;2044 collection.consume_sload()?;2045 let full_item = <ReFungibleItemList<T>>::get(collection_id, item_id)2046 .ok_or(Error::<T>::TokenNotFound)?;20472048 let item = full_item2049 .owner2050 .iter()2051 .find(|i| i.owner == owner)2052 .ok_or(Error::<T>::TokenNotFound)?;2053 let amount = item.fraction;20542055 ensure!(amount >= value, Error::<T>::TokenValueTooLow);20562057 collection.consume_sload()?;2058 2059 let balance_old_owner = <Balance<T>>::get(collection_id, item.owner.as_sub())2060 .checked_sub(value)2061 .ok_or(Error::<T>::NumOverflow)?;2062 collection.consume_sstore()?;2063 <Balance<T>>::insert(collection_id, item.owner.as_sub(), balance_old_owner);20642065 collection.consume_sload()?;2066 let balance_new_owner = <Balance<T>>::get(collection_id, new_owner.as_sub())2067 .checked_add(value)2068 .ok_or(Error::<T>::NumOverflow)?;2069 collection.consume_sstore()?;2070 <Balance<T>>::insert(collection_id, new_owner.as_sub(), balance_new_owner);20712072 let old_owner = item.owner.clone();2073 let new_owner_has_account = full_item.owner.iter().any(|i| i.owner == new_owner);20742075 let mut new_full_item = full_item.clone();2076 2077 if amount == value && !new_owner_has_account {2078 2079 2080 new_full_item2081 .owner2082 .iter_mut()2083 .find(|i| i.owner == owner)2084 .expect("old owner does present in refungible")2085 .owner = new_owner.clone();2086 collection.consume_sstore()?;2087 <ReFungibleItemList<T>>::insert(collection_id, item_id, new_full_item);20882089 2090 Self::move_token_index(collection, item_id, &old_owner, &new_owner)?;2091 } else {2092 new_full_item2093 .owner2094 .iter_mut()2095 .find(|i| i.owner == owner)2096 .expect("old owner does present in refungible")2097 .fraction -= value;20982099 2100 if new_owner_has_account {2101 2102 new_full_item2103 .owner2104 .iter_mut()2105 .find(|i| i.owner == new_owner)2106 .expect("new owner has account")2107 .fraction += value;2108 } else {2109 2110 new_full_item.owner.push(Ownership {2111 owner: new_owner.clone(),2112 fraction: value,2113 });2114 Self::add_token_index(collection, item_id, &new_owner)?;2115 }21162117 collection.consume_sstore()?;2118 <ReFungibleItemList<T>>::insert(collection_id, item_id, new_full_item);2119 }21202121 Self::deposit_event(RawEvent::Transfer(2122 collection.id,2123 item_id,2124 owner,2125 new_owner,2126 amount,2127 ));21282129 Ok(())2130 }21312132 fn transfer_nft(2133 collection: &CollectionHandle<T>,2134 item_id: TokenId,2135 sender: T::CrossAccountId,2136 new_owner: T::CrossAccountId,2137 ) -> DispatchResult {2138 let collection_id = collection.id;2139 collection.consume_sload()?;2140 let mut item =2141 <NftItemList<T>>::get(collection_id, item_id).ok_or(Error::<T>::TokenNotFound)?;21422143 ensure!(sender == item.owner, Error::<T>::MustBeTokenOwner);21442145 collection.consume_sload()?;2146 2147 let balance_old_owner = <Balance<T>>::get(collection_id, item.owner.as_sub())2148 .checked_sub(1)2149 .ok_or(Error::<T>::NumOverflow)?;2150 collection.consume_sstore()?;2151 <Balance<T>>::insert(collection_id, item.owner.as_sub(), balance_old_owner);21522153 collection.consume_sload()?;2154 let balance_new_owner = <Balance<T>>::get(collection_id, new_owner.as_sub())2155 .checked_add(1)2156 .ok_or(Error::<T>::NumOverflow)?;2157 collection.consume_sstore()?;2158 <Balance<T>>::insert(collection_id, new_owner.as_sub(), balance_new_owner);21592160 2161 let old_owner = item.owner.clone();2162 item.owner = new_owner.clone();2163 collection.consume_sstore()?;2164 <NftItemList<T>>::insert(collection_id, item_id, item);21652166 2167 Self::move_token_index(collection, item_id, &old_owner, &new_owner)?;21682169 collection.log(ERC721Events::Transfer {2170 from: *sender.as_eth(),2171 to: *new_owner.as_eth(),2172 token_id: item_id.into(),2173 })?;2174 Self::deposit_event(RawEvent::Transfer(2175 collection.id,2176 item_id,2177 sender,2178 new_owner,2179 1,2180 ));21812182 Ok(())2183 }21842185 fn set_re_fungible_variable_data(2186 collection: &CollectionHandle<T>,2187 item_id: TokenId,2188 data: Vec<u8>,2189 ) -> DispatchResult {2190 let collection_id = collection.id;2191 let mut item = <ReFungibleItemList<T>>::get(collection_id, item_id)2192 .ok_or(Error::<T>::TokenNotFound)?;21932194 item.variable_data = data;21952196 <ReFungibleItemList<T>>::insert(collection_id, item_id, item);21972198 Ok(())2199 }22002201 fn set_nft_variable_data(2202 collection: &CollectionHandle<T>,2203 item_id: TokenId,2204 data: Vec<u8>,2205 ) -> DispatchResult {2206 let collection_id = collection.id;2207 let mut item =2208 <NftItemList<T>>::get(collection_id, item_id).ok_or(Error::<T>::TokenNotFound)?;22092210 item.variable_data = data;22112212 <NftItemList<T>>::insert(collection_id, item_id, item);22132214 Ok(())2215 }22162217 #[allow(dead_code)]2218 fn init_collection(item: &Collection<T>) {2219 2220 assert!(2221 item.decimal_points <= MAX_DECIMAL_POINTS,2222 "decimal_points parameter must be lower than MAX_DECIMAL_POINTS"2223 );2224 assert!(2225 item.name.len() <= 64,2226 "Collection name can not be longer than 63 char"2227 );2228 assert!(2229 item.name.len() <= 256,2230 "Collection description can not be longer than 255 char"2231 );2232 assert!(2233 item.token_prefix.len() <= 16,2234 "Token prefix can not be longer than 15 char"2235 );22362237 2238 let next_id = CreatedCollectionCount::get().checked_add(1).unwrap();22392240 CreatedCollectionCount::put(next_id);2241 }22422243 #[allow(dead_code)]2244 fn init_nft_token(collection_id: CollectionId, item: &NftItemType<T::CrossAccountId>) {2245 let current_index = <ItemListIndex>::get(collection_id).checked_add(1).unwrap();22462247 Self::add_token_index(2248 &CollectionHandle::get(collection_id).unwrap(),2249 current_index,2250 &item.owner,2251 )2252 .unwrap();22532254 <ItemListIndex>::insert(collection_id, current_index);22552256 2257 let new_balance = <Balance<T>>::get(collection_id, item.owner.as_sub())2258 .checked_add(1)2259 .unwrap();2260 <Balance<T>>::insert(collection_id, item.owner.as_sub(), new_balance);2261 }22622263 #[allow(dead_code)]2264 fn init_fungible_token(2265 collection_id: CollectionId,2266 owner: &T::CrossAccountId,2267 item: &FungibleItemType,2268 ) {2269 let current_index = <ItemListIndex>::get(collection_id).checked_add(1).unwrap();22702271 Self::add_token_index(2272 &CollectionHandle::get(collection_id).unwrap(),2273 current_index,2274 owner,2275 )2276 .unwrap();22772278 <ItemListIndex>::insert(collection_id, current_index);22792280 2281 let new_balance = <Balance<T>>::get(collection_id, owner.as_sub())2282 .checked_add(item.value)2283 .unwrap();2284 <Balance<T>>::insert(collection_id, owner.as_sub(), new_balance);2285 }22862287 #[allow(dead_code)]2288 fn init_refungible_token(2289 collection_id: CollectionId,2290 item: &ReFungibleItemType<T::CrossAccountId>,2291 ) {2292 let current_index = <ItemListIndex>::get(collection_id).checked_add(1).unwrap();22932294 let value = item.owner.first().unwrap().fraction;2295 let owner = item.owner.first().unwrap().owner.clone();22962297 Self::add_token_index(2298 &CollectionHandle::get(collection_id).unwrap(),2299 current_index,2300 &owner,2301 )2302 .unwrap();23032304 <ItemListIndex>::insert(collection_id, current_index);23052306 2307 let new_balance = <Balance<T>>::get(collection_id, &owner.as_sub())2308 .checked_add(value)2309 .unwrap();2310 <Balance<T>>::insert(collection_id, owner.as_sub(), new_balance);2311 }23122313 fn add_token_index(2314 collection: &CollectionHandle<T>,2315 item_index: TokenId,2316 owner: &T::CrossAccountId,2317 ) -> DispatchResult {2318 2319 collection.consume_sload()?;2320 if <AccountItemCount<T>>::contains_key(owner.as_sub()) {2321 2322 collection.consume_sload()?;2323 let count = <AccountItemCount<T>>::get(owner.as_sub());2324 ensure!(2325 count < ACCOUNT_TOKEN_OWNERSHIP_LIMIT,2326 Error::<T>::AddressOwnershipLimitExceeded2327 );23282329 collection.consume_sstore()?;2330 <AccountItemCount<T>>::insert(2331 owner.as_sub(),2332 count.checked_add(1).ok_or(Error::<T>::NumOverflow)?,2333 );2334 } else {2335 collection.consume_sstore()?;2336 <AccountItemCount<T>>::insert(owner.as_sub(), 1);2337 }23382339 collection.consume_sload()?;2340 let list_exists = <AddressTokens<T>>::contains_key(collection.id, owner.as_sub());2341 if list_exists {2342 collection.consume_sload()?;2343 let mut list = <AddressTokens<T>>::get(collection.id, owner.as_sub());2344 let item_contains = list.contains(&item_index.clone());23452346 if !item_contains {2347 list.push(item_index);2348 }23492350 collection.consume_sstore()?;2351 <AddressTokens<T>>::insert(collection.id, owner.as_sub(), list);2352 } else {2353 let itm = vec![item_index];2354 collection.consume_sstore()?;2355 <AddressTokens<T>>::insert(collection.id, owner.as_sub(), itm);2356 }23572358 Ok(())2359 }23602361 fn remove_token_index(2362 collection: &CollectionHandle<T>,2363 item_index: TokenId,2364 owner: &T::CrossAccountId,2365 ) -> DispatchResult {2366 2367 collection.consume_sload()?;2368 collection.consume_sstore()?;2369 <AccountItemCount<T>>::insert(2370 owner.as_sub(),2371 <AccountItemCount<T>>::get(owner.as_sub())2372 .checked_sub(1)2373 .ok_or(Error::<T>::NumOverflow)?,2374 );23752376 collection.consume_sload()?;2377 let list_exists = <AddressTokens<T>>::contains_key(collection.id, owner.as_sub());2378 if list_exists {2379 collection.consume_sload()?;2380 let mut list = <AddressTokens<T>>::get(collection.id, owner.as_sub());2381 let item_contains = list.contains(&item_index.clone());23822383 if item_contains {2384 list.retain(|&item| item != item_index);2385 collection.consume_sstore()?;2386 <AddressTokens<T>>::insert(collection.id, owner.as_sub(), list);2387 }2388 }23892390 Ok(())2391 }23922393 fn move_token_index(2394 collection: &CollectionHandle<T>,2395 item_index: TokenId,2396 old_owner: &T::CrossAccountId,2397 new_owner: &T::CrossAccountId,2398 ) -> DispatchResult {2399 Self::remove_token_index(collection, item_index, old_owner)?;2400 Self::add_token_index(collection, item_index, new_owner)?;24012402 Ok(())2403 }2404}24052406sp_api::decl_runtime_apis! {2407 pub trait NftApi {2408 2409 fn eth_contract_code(account: H160) -> Option<Vec<u8>>;2410 }2411}