123456#![recursion_limit = "1024"]78#![cfg_attr(not(feature = "std"), no_std)]910#[cfg(feature = "std")]11pub use std::*;1213#[cfg(feature = "std")]14pub use serde::*;1516pub use frame_support::{17 construct_runtime, decl_event, decl_module, decl_storage, decl_error,18 dispatch::DispatchResult,19 ensure, fail, parameter_types,20 traits::{21 Currency, ExistenceRequirement, Get, Imbalance, KeyOwnerProofSystem, OnUnbalanced,22 Randomness, IsSubType, WithdrawReasons,23 },24 weights::{25 constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},26 DispatchInfo, GetDispatchInfo, IdentityFee, Pays, PostDispatchInfo, Weight,27 WeightToFeePolynomial, DispatchClass,28 },29 StorageValue,30 transactional,31};3233use frame_system::{self as system, ensure_signed, ensure_root};34use sp_runtime::sp_std::prelude::Vec;35use core::ops::{Deref, DerefMut};36use nft_data_structs::{37 MAX_DECIMAL_POINTS, MAX_SPONSOR_TIMEOUT, MAX_TOKEN_OWNERSHIP, MAX_REFUNGIBLE_PIECES,38 AccessMode, ChainLimits, Collection, CreateItemData, CollectionLimits,39 CollectionId, CollectionMode, TokenId, 40 SchemaVersion, SponsorshipState, Ownership,41 NftItemType, FungibleItemType, ReFungibleItemType42};4344#[cfg(test)]45mod mock;4647#[cfg(test)]48mod tests;4950mod default_weights;5152#[cfg(feature = "runtime-benchmarks")]53mod benchmarking;5455pub trait WeightInfo {56 fn create_collection() -> Weight;57 fn destroy_collection() -> Weight;58 fn add_to_white_list() -> Weight;59 fn remove_from_white_list() -> Weight;60 fn set_public_access_mode() -> Weight;61 fn set_mint_permission() -> Weight;62 fn change_collection_owner() -> Weight;63 fn add_collection_admin() -> Weight;64 fn remove_collection_admin() -> Weight;65 fn set_collection_sponsor() -> Weight;66 fn confirm_sponsorship() -> Weight;67 fn remove_collection_sponsor() -> Weight;68 fn create_item(s: usize) -> Weight;69 fn burn_item() -> Weight;70 fn transfer() -> Weight;71 fn approve() -> Weight;72 fn transfer_from() -> Weight;73 fn set_offchain_schema() -> Weight;74 fn set_const_on_chain_schema() -> Weight;75 fn set_variable_on_chain_schema() -> Weight;76 fn set_variable_meta_data() -> Weight;77 fn enable_contract_sponsoring() -> Weight;78 fn set_schema_version() -> Weight;79 fn set_chain_limits() -> Weight;80 fn set_contract_sponsoring_rate_limit() -> Weight;81 fn set_variable_meta_data_sponsoring_rate_limit() -> Weight;82 fn toggle_contract_white_list() -> Weight;83 fn add_to_contract_white_list() -> Weight;84 fn remove_from_contract_white_list() -> Weight;85 fn set_collection_limits() -> Weight;86}8788decl_error! {89 90 pub enum Error for Module<T: Config> {91 92 TotalCollectionsLimitExceeded,93 94 CollectionDecimalPointLimitExceeded, 95 96 CollectionNameLimitExceeded, 97 98 CollectionDescriptionLimitExceeded, 99 100 CollectionTokenPrefixLimitExceeded,101 102 CollectionNotFound,103 104 TokenNotFound,105 106 AdminNotFound,107 108 NumOverflow, 109 110 AlreadyAdmin, 111 112 NoPermission,113 114 ConfirmUnsetSponsorFail,115 116 PublicMintingNotAllowed,117 118 MustBeTokenOwner,119 120 TokenValueTooLow,121 122 NftSizeLimitExceeded,123 124 ApproveNotFound,125 126 TokenValueNotEnough,127 128 ApproveRequired,129 130 AddresNotInWhiteList,131 132 CollectionAdminsLimitExceeded,133 134 AddressOwnershipLimitExceeded,135 136 EmptyArgument,137 138 TokenConstDataLimitExceeded,139 140 TokenVariableDataLimitExceeded,141 142 NotNftDataUsedToMintNftCollectionToken,143 144 NotFungibleDataUsedToMintFungibleCollectionToken,145 146 NotReFungibleDataUsedToMintReFungibleCollectionToken,147 148 UnexpectedCollectionType,149 150 CantStoreMetadataInFungibleTokens,151 152 CollectionTokenLimitExceeded,153 154 AccountTokenLimitExceeded,155 156 CollectionLimitBoundsExceeded,157 158 OwnerPermissionsCantBeReverted,159 160 SchemaDataLimitExceeded,161 162 WrongRefungiblePieces,163 164 BadCreateRefungibleCall,165 }166}167168pub struct CollectionHandle<T: frame_system::Config> {169 pub id: CollectionId,170 pub collection: Collection<T>,171}172173impl<T: frame_system::Config> Deref for CollectionHandle<T> {174 type Target = Collection<T>;175176 fn deref(&self) -> &Self::Target {177 &self.collection178 }179}180181impl<T: frame_system::Config> DerefMut for CollectionHandle<T> {182 fn deref_mut(&mut self) -> &mut Self::Target {183 &mut self.collection184 }185}186187188189pub trait Config: system::Config + Sized {190 type Event: From<Event<Self>> + Into<<Self as system::Config>::Event>;191192 193 type WeightInfo: WeightInfo;194195 type Currency: Currency<Self::AccountId>;196 type CollectionCreationPrice: Get<<<Self as Config>::Currency as Currency<Self::AccountId>>::Balance>;197 type TreasuryAccountId: Get<Self::AccountId>;198}199200201202203204205206207208209210211212213214215216217218219220221222decl_storage! {223 trait Store for Module<T: Config> as Nft {224225 226 227 CreatedCollectionCount: u32;228 229 ChainVersion: u64;230 231 232 ItemListIndex: map hasher(blake2_128_concat) CollectionId => TokenId;233 234235 236 pub ChainLimit get(fn chain_limit) config(): ChainLimits;237 238239 240 241 242 DestroyedCollectionCount: u32;243 244 245 pub AccountItemCount get(fn account_item_count): map hasher(twox_64_concat) T::AccountId => u32;246 247248 249 250 251 pub CollectionById get(fn collection_id) config(): map hasher(blake2_128_concat) CollectionId => Option<Collection<T>> = None;252 253 254 pub AdminList get(fn admin_list_collection): map hasher(blake2_128_concat) CollectionId => Vec<T::AccountId>;255 256 257 pub WhiteList get(fn white_list): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) T::AccountId => bool;258 259260 261 262 pub Balance get(fn balance_count): double_map hasher(blake2_128_concat) CollectionId, hasher(twox_64_concat) T::AccountId => u128;263264 265 266 267 pub Allowances get(fn approved): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) (TokenId, T::AccountId, T::AccountId) => u128;268269 270 271 pub NftItemList get(fn nft_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<NftItemType<T::AccountId>>;272 273 pub FungibleItemList get(fn fungible_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) T::AccountId => FungibleItemType;274 275 pub ReFungibleItemList get(fn refungible_item_id) config(): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<ReFungibleItemType<T::AccountId>>;276 277278 279 280 pub AddressTokens get(fn address_tokens): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) T::AccountId => Vec<TokenId>;281 282283 284 285 286 pub CreateItemBasket get(fn create_item_basket): map hasher(blake2_128_concat) (CollectionId, T::AccountId) => T::BlockNumber;287 288 pub NftTransferBasket get(fn nft_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => T::BlockNumber;289 290 pub FungibleTransferBasket get(fn fungible_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(twox_64_concat) T::AccountId => T::BlockNumber;291 292 pub ReFungibleTransferBasket get(fn refungible_transfer_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => T::BlockNumber;293 294295 296 297 pub VariableMetaDataBasket get(fn variable_meta_data_basket): double_map hasher(blake2_128_concat) CollectionId, hasher(blake2_128_concat) TokenId => Option<T::BlockNumber> = None;298 299 300 301 pub ContractOwner get(fn contract_owner): map hasher(twox_64_concat) T::AccountId => Option<T::AccountId>;302 303 pub ContractSelfSponsoring get(fn contract_self_sponsoring): map hasher(twox_64_concat) T::AccountId => bool;304 305 pub ContractSponsorBasket get(fn contract_sponsor_basket): map hasher(twox_64_concat) (T::AccountId, T::AccountId) => T::BlockNumber;306 307 pub ContractSponsoringRateLimit get(fn contract_sponsoring_rate_limit): map hasher(twox_64_concat) T::AccountId => T::BlockNumber;308 309 pub ContractWhiteListEnabled get(fn contract_white_list_enabled): map hasher(twox_64_concat) T::AccountId => bool; 310 311 pub ContractWhiteList get(fn contract_white_list): double_map hasher(twox_64_concat) T::AccountId, hasher(blake2_128_concat) T::AccountId => bool; 312 313 }314 add_extra_genesis {315 build(|config: &GenesisConfig<T>| {316 317 for (_num, _c) in &config.collection_id {318 <Module<T>>::init_collection(_c);319 }320321 for (_num, _c, _i) in &config.nft_item_id {322 <Module<T>>::init_nft_token(*_c, _i);323 }324325 for (collection_id, account_id, fungible_item) in &config.fungible_item_id {326 <Module<T>>::init_fungible_token(*collection_id, account_id, fungible_item);327 }328329 for (_num, _c, _i) in &config.refungible_item_id {330 <Module<T>>::init_refungible_token(*_c, _i);331 }332 })333 }334}335336decl_event!(337 pub enum Event<T>338 where339 AccountId = <T as system::Config>::AccountId,340 {341 342 343 344 345 346 347 348 349 350 CollectionCreated(CollectionId, u8, AccountId),351352 353 354 355 356 357 358 359 360 361 ItemCreated(CollectionId, TokenId, AccountId),362363 364 365 366 367 368 369 370 ItemDestroyed(CollectionId, TokenId),371372 373 374 375 376 377 378 379 380 381 382 383 Transfer(CollectionId, TokenId, AccountId, AccountId, u128),384385 386 387 388 389 390 391 392 393 394 Approved(CollectionId, TokenId, AccountId, AccountId, u128),395 }396);397398decl_module! {399 pub struct Module<T: Config> for enum Call 400 where 401 origin: T::Origin402 {403 fn deposit_event() = default;404 type Error = Error<T>;405406 fn on_initialize(_now: T::BlockNumber) -> Weight {407 0408 }409410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 #[weight = <T as Config>::WeightInfo::create_collection()]427 #[transactional]428 pub fn create_collection(origin,429 collection_name: Vec<u16>,430 collection_description: Vec<u16>,431 token_prefix: Vec<u8>,432 mode: CollectionMode) -> DispatchResult {433434 435 let who = ensure_signed(origin)?;436437 438 let mut imbalance = <<<T as Config>::Currency as Currency<T::AccountId>>::PositiveImbalance>::zero();439 imbalance.subsume(<<T as Config>::Currency as Currency<T::AccountId>>::deposit_creating(440 &T::TreasuryAccountId::get(),441 T::CollectionCreationPrice::get(),442 ));443 <T as Config>::Currency::settle(444 &who,445 imbalance,446 WithdrawReasons::TRANSFER,447 ExistenceRequirement::KeepAlive,448 ).map_err(|_| Error::<T>::NoPermission)?;449450 let decimal_points = match mode {451 CollectionMode::Fungible(points) => points,452 _ => 0453 };454455 let chain_limit = ChainLimit::get();456457 let created_count = CreatedCollectionCount::get();458 let destroyed_count = DestroyedCollectionCount::get();459460 461 ensure!(created_count - destroyed_count < chain_limit.collection_numbers_limit, Error::<T>::TotalCollectionsLimitExceeded);462463 464 ensure!(decimal_points <= MAX_DECIMAL_POINTS, Error::<T>::CollectionDecimalPointLimitExceeded);465 ensure!(collection_name.len() <= 64, Error::<T>::CollectionNameLimitExceeded);466 ensure!(collection_description.len() <= 256, Error::<T>::CollectionDescriptionLimitExceeded);467 ensure!(token_prefix.len() <= 16, Error::<T>::CollectionTokenPrefixLimitExceeded);468469 470 let next_id = created_count471 .checked_add(1)472 .ok_or(Error::<T>::NumOverflow)?;473474 CreatedCollectionCount::put(next_id);475476 let limits = CollectionLimits {477 sponsored_data_size: chain_limit.custom_data_limit,478 ..Default::default()479 };480481 482 let new_collection = Collection {483 owner: who.clone(),484 name: collection_name,485 mode: mode.clone(),486 mint_mode: false,487 access: AccessMode::Normal,488 description: collection_description,489 decimal_points: decimal_points,490 token_prefix: token_prefix,491 offchain_schema: Vec::new(),492 schema_version: SchemaVersion::ImageURL,493 sponsorship: SponsorshipState::Disabled,494 variable_on_chain_schema: Vec::new(),495 const_on_chain_schema: Vec::new(),496 limits,497 };498499 500 <CollectionById<T>>::insert(next_id, new_collection);501502 503 Self::deposit_event(RawEvent::CollectionCreated(next_id, mode.into(), who.clone()));504505 Ok(())506 }507508 509 510 511 512 513 514 515 516 517 #[weight = <T as Config>::WeightInfo::destroy_collection()]518 #[transactional]519 pub fn destroy_collection(origin, collection_id: CollectionId) -> DispatchResult {520521 let sender = ensure_signed(origin)?;522 let collection = Self::get_collection(collection_id)?;523 Self::check_owner_permissions(&collection, sender)?;524 if !collection.limits.owner_can_destroy {525 fail!(Error::<T>::NoPermission);526 }527528 <AddressTokens<T>>::remove_prefix(collection_id);529 <Allowances<T>>::remove_prefix(collection_id);530 <Balance<T>>::remove_prefix(collection_id);531 <ItemListIndex>::remove(collection_id);532 <AdminList<T>>::remove(collection_id);533 <CollectionById<T>>::remove(collection_id);534 <WhiteList<T>>::remove_prefix(collection_id);535536 <NftItemList<T>>::remove_prefix(collection_id);537 <FungibleItemList<T>>::remove_prefix(collection_id);538 <ReFungibleItemList<T>>::remove_prefix(collection_id);539540 <NftTransferBasket<T>>::remove_prefix(collection_id);541 <FungibleTransferBasket<T>>::remove_prefix(collection_id);542 <ReFungibleTransferBasket<T>>::remove_prefix(collection_id);543544 <VariableMetaDataBasket<T>>::remove_prefix(collection_id);545546 DestroyedCollectionCount::put(DestroyedCollectionCount::get()547 .checked_add(1)548 .ok_or(Error::<T>::NumOverflow)?);549550 Ok(())551 }552553 554 555 556 557 558 559 560 561 562 563 564 565 #[weight = <T as Config>::WeightInfo::add_to_white_list()]566 #[transactional]567 pub fn add_to_white_list(origin, collection_id: CollectionId, address: T::AccountId) -> DispatchResult{568569 let sender = ensure_signed(origin)?;570 let collection = Self::get_collection(collection_id)?;571 Self::check_owner_or_admin_permissions(&collection, sender)?;572573 <WhiteList<T>>::insert(collection_id, address, true);574 575 Ok(())576 }577578 579 580 581 582 583 584 585 586 587 588 589 590 #[weight = <T as Config>::WeightInfo::remove_from_white_list()]591 #[transactional]592 pub fn remove_from_white_list(origin, collection_id: CollectionId, address: T::AccountId) -> DispatchResult{593594 let sender = ensure_signed(origin)?;595 let collection = Self::get_collection(collection_id)?;596 Self::check_owner_or_admin_permissions(&collection, sender)?;597598 <WhiteList<T>>::remove(collection_id, address);599600 Ok(())601 }602603 604 605 606 607 608 609 610 611 612 613 614 #[weight = <T as Config>::WeightInfo::set_public_access_mode()]615 #[transactional]616 pub fn set_public_access_mode(origin, collection_id: CollectionId, mode: AccessMode) -> DispatchResult617 {618 let sender = ensure_signed(origin)?;619620 let mut target_collection = Self::get_collection(collection_id)?;621 Self::check_owner_permissions(&target_collection, sender)?;622 target_collection.access = mode;623 Self::save_collection(target_collection);624625 Ok(())626 }627628 629 630 631 632 633 634 635 636 637 638 639 640 641 #[weight = <T as Config>::WeightInfo::set_mint_permission()]642 #[transactional]643 pub fn set_mint_permission(origin, collection_id: CollectionId, mint_permission: bool) -> DispatchResult644 {645 let sender = ensure_signed(origin)?;646647 let mut target_collection = Self::get_collection(collection_id)?;648 Self::check_owner_permissions(&target_collection, sender)?;649 target_collection.mint_mode = mint_permission;650 Self::save_collection(target_collection);651652 Ok(())653 }654655 656 657 658 659 660 661 662 663 664 665 666 #[weight = <T as Config>::WeightInfo::change_collection_owner()]667 #[transactional]668 pub fn change_collection_owner(origin, collection_id: CollectionId, new_owner: T::AccountId) -> DispatchResult {669670 let sender = ensure_signed(origin)?;671 let mut target_collection = Self::get_collection(collection_id)?;672 Self::check_owner_permissions(&target_collection, sender)?;673 target_collection.owner = new_owner;674 Self::save_collection(target_collection);675676 Ok(())677 }678679 680 681 682 683 684 685 686 687 688 689 690 691 692 #[weight = <T as Config>::WeightInfo::add_collection_admin()]693 #[transactional]694 pub fn add_collection_admin(origin, collection_id: CollectionId, new_admin_id: T::AccountId) -> DispatchResult {695696 let sender = ensure_signed(origin)?;697 let collection = Self::get_collection(collection_id)?;698 Self::check_owner_or_admin_permissions(&collection, sender)?;699 let mut admin_arr = <AdminList<T>>::get(collection_id);700701 match admin_arr.binary_search(&new_admin_id) {702 Ok(_) => {},703 Err(idx) => {704 let limits = ChainLimit::get();705 ensure!(admin_arr.len() < limits.collections_admins_limit as usize, Error::<T>::CollectionAdminsLimitExceeded);706 admin_arr.insert(idx, new_admin_id);707 <AdminList<T>>::insert(collection_id, admin_arr);708 }709 }710 Ok(())711 }712713 714 715 716 717 718 719 720 721 722 723 724 725 #[weight = <T as Config>::WeightInfo::remove_collection_admin()]726 #[transactional]727 pub fn remove_collection_admin(origin, collection_id: CollectionId, account_id: T::AccountId) -> DispatchResult {728729 let sender = ensure_signed(origin)?;730 let collection = Self::get_collection(collection_id)?;731 Self::check_owner_or_admin_permissions(&collection, sender)?;732 let mut admin_arr = <AdminList<T>>::get(collection_id);733734 match admin_arr.binary_search(&account_id) {735 Ok(idx) => {736 admin_arr.remove(idx);737 <AdminList<T>>::insert(collection_id, admin_arr);738 },739 Err(_) => {}740 }741 Ok(())742 }743744 745 746 747 748 749 750 751 752 753 #[weight = <T as Config>::WeightInfo::set_collection_sponsor()]754 #[transactional]755 pub fn set_collection_sponsor(origin, collection_id: CollectionId, new_sponsor: T::AccountId) -> DispatchResult {756757 let sender = ensure_signed(origin)?;758 let mut target_collection = Self::get_collection(collection_id)?;759 Self::check_owner_permissions(&target_collection, sender)?;760761 target_collection.sponsorship = SponsorshipState::Unconfirmed(new_sponsor);762 Self::save_collection(target_collection);763764 Ok(())765 }766767 768 769 770 771 772 773 774 #[weight = <T as Config>::WeightInfo::confirm_sponsorship()]775 #[transactional]776 pub fn confirm_sponsorship(origin, collection_id: CollectionId) -> DispatchResult {777778 let sender = ensure_signed(origin)?;779780 let mut target_collection = Self::get_collection(collection_id)?;781 ensure!(782 target_collection.sponsorship.pending_sponsor() == Some(&sender),783 Error::<T>::ConfirmUnsetSponsorFail784 );785786 target_collection.sponsorship = SponsorshipState::Confirmed(sender);787 Self::save_collection(target_collection);788789 Ok(())790 }791792 793 794 795 796 797 798 799 800 801 #[weight = <T as Config>::WeightInfo::remove_collection_sponsor()]802 #[transactional]803 pub fn remove_collection_sponsor(origin, collection_id: CollectionId) -> DispatchResult {804805 let sender = ensure_signed(origin)?;806807 let mut target_collection = Self::get_collection(collection_id)?;808 Self::check_owner_permissions(&target_collection, sender)?;809810 target_collection.sponsorship = SponsorshipState::Disabled;811 Self::save_collection(target_collection);812813 Ok(())814 }815816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839840 #[weight = <T as Config>::WeightInfo::create_item(data.len())]841 #[transactional]842 pub fn create_item(origin, collection_id: CollectionId, owner: T::AccountId, data: CreateItemData) -> DispatchResult {843 let sender = ensure_signed(origin)?;844 Self::create_item_internal(sender, collection_id, owner, data)845 }846847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 #[weight = <T as Config>::WeightInfo::create_item(items_data.into_iter()866 .map(|data| { data.len() })867 .sum())]868 #[transactional]869 pub fn create_multiple_items(origin, collection_id: CollectionId, owner: T::AccountId, items_data: Vec<CreateItemData>) -> DispatchResult {870871 ensure!(items_data.len() > 0, Error::<T>::EmptyArgument);872 let sender = ensure_signed(origin)?;873874 let target_collection = Self::get_collection(collection_id)?;875876 Self::can_create_items_in_collection(&target_collection, &sender, &owner, items_data.len() as u32)?;877878 for data in &items_data {879 Self::validate_create_item_args(&target_collection, data)?;880 }881 for data in &items_data {882 Self::create_item_no_validation(&target_collection, owner.clone(), data.clone())?;883 }884885 Ok(())886 }887888 889 890 891 892 893 894 895 896 897 898 899 900 901 #[weight = <T as Config>::WeightInfo::burn_item()]902 #[transactional]903 pub fn burn_item(origin, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResult {904905 let sender = ensure_signed(origin)?;906907 908 let target_collection = Self::get_collection(collection_id)?;909 ensure!(910 Self::is_item_owner(sender.clone(), &target_collection, item_id) ||911 (912 target_collection.limits.owner_can_transfer &&913 Self::is_owner_or_admin_permissions(&target_collection, sender.clone())914 ),915 Error::<T>::NoPermission916 );917918 if target_collection.access == AccessMode::WhiteList {919 Self::check_white_list(&target_collection, &sender)?;920 }921922 match target_collection.mode923 {924 CollectionMode::NFT => Self::burn_nft_item(&target_collection, item_id)?,925 CollectionMode::Fungible(_) => Self::burn_fungible_item(&sender, &target_collection, value)?,926 CollectionMode::ReFungible => Self::burn_refungible_item(&target_collection, item_id, &sender)?,927 _ => ()928 };929930 931 Self::deposit_event(RawEvent::ItemDestroyed(target_collection.id, item_id));932933 Ok(())934 }935936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 #[weight = <T as Config>::WeightInfo::transfer()]960 #[transactional]961 pub fn transfer(origin, recipient: T::AccountId, collection_id: CollectionId, item_id: TokenId, value: u128) -> DispatchResult {962 let sender = ensure_signed(origin)?;963 let collection = Self::get_collection(collection_id)?;964965 Self::transfer_internal(sender, recipient, &collection, item_id, value)966 }967968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 #[weight = <T as Config>::WeightInfo::approve()]984 #[transactional]985 pub fn approve(origin, spender: T::AccountId, collection_id: CollectionId, item_id: TokenId, amount: u128) -> DispatchResult {986987 let sender = ensure_signed(origin)?;988 let target_collection = Self::get_collection(collection_id)?;989990 Self::token_exists(&target_collection, item_id)?;991992 993 let bypasses_limits = target_collection.limits.owner_can_transfer &&994 Self::is_owner_or_admin_permissions(995 &target_collection,996 sender.clone(),997 );998999 let allowance_limit = if bypasses_limits {1000 None1001 } else if let Some(amount) = Self::owned_amount(1002 sender.clone(),1003 &target_collection,1004 item_id,1005 ) {1006 Some(amount)1007 } else {1008 fail!(Error::<T>::NoPermission);1009 };10101011 if target_collection.access == AccessMode::WhiteList {1012 Self::check_white_list(&target_collection, &sender)?;1013 Self::check_white_list(&target_collection, &spender)?;1014 }10151016 let allowance: u128 = amount1017 .checked_add(<Allowances<T>>::get(collection_id, (item_id, &sender, &spender)))1018 .ok_or(Error::<T>::NumOverflow)?;1019 if let Some(limit) = allowance_limit {1020 ensure!(limit >= allowance, Error::<T>::TokenValueTooLow);1021 }1022 <Allowances<T>>::insert(collection_id, (item_id, sender.clone(), spender.clone()), allowance);10231024 Self::deposit_event(RawEvent::Approved(target_collection.id, item_id, sender, spender, allowance));1025 Ok(())1026 }1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 #[weight = <T as Config>::WeightInfo::transfer_from()]1048 #[transactional]1049 pub fn transfer_from(origin, from: T::AccountId, recipient: T::AccountId, collection_id: CollectionId, item_id: TokenId, value: u128 ) -> DispatchResult {10501051 let sender = ensure_signed(origin)?;1052 let target_collection = Self::get_collection(collection_id)?;10531054 1055 let approval: u128 = <Allowances<T>>::get(collection_id, (item_id, &from, &sender));10561057 1058 Self::is_correct_transfer(&target_collection, &recipient)?;10591060 1061 ensure!(1062 approval >= value || 1063 (1064 target_collection.limits.owner_can_transfer &&1065 Self::is_owner_or_admin_permissions(&target_collection, sender.clone())1066 ),1067 Error::<T>::NoPermission1068 );10691070 if target_collection.access == AccessMode::WhiteList {1071 Self::check_white_list(&target_collection, &sender)?;1072 Self::check_white_list(&target_collection, &recipient)?;1073 }10741075 1076 if approval.saturating_sub(value) > 0 {1077 <Allowances<T>>::insert(collection_id, (item_id, &from, &sender), approval - value);1078 }1079 else {1080 <Allowances<T>>::remove(collection_id, (item_id, &from, &sender));1081 }10821083 match target_collection.mode1084 {1085 CollectionMode::NFT => Self::transfer_nft(&target_collection, item_id, from.clone(), recipient.clone())?,1086 CollectionMode::Fungible(_) => Self::transfer_fungible(&target_collection, value, &from, &recipient)?,1087 CollectionMode::ReFungible => Self::transfer_refungible(&target_collection, item_id, value, from.clone(), recipient.clone())?,1088 _ => ()1089 };10901091 Self::deposit_event(RawEvent::Transfer(target_collection.id, item_id, from, recipient, value));1092 Ok(())1093 }10941095 1096 10971098 1099 1100 1101 11021103 11041105 11061107 1108 11091110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 #[weight = <T as Config>::WeightInfo::set_variable_meta_data()]1123 #[transactional]1124 pub fn set_variable_meta_data (1125 origin,1126 collection_id: CollectionId,1127 item_id: TokenId,1128 data: Vec<u8>1129 ) -> DispatchResult {1130 let sender = ensure_signed(origin)?;1131 1132 let target_collection = Self::get_collection(collection_id)?;1133 Self::token_exists(&target_collection, item_id)?;11341135 ensure!(ChainLimit::get().custom_data_limit >= data.len() as u32, Error::<T>::TokenVariableDataLimitExceeded);11361137 1138 ensure!(Self::is_item_owner(sender.clone(), &target_collection, item_id) ||1139 Self::is_owner_or_admin_permissions(&target_collection, sender.clone()),1140 Error::<T>::NoPermission);11411142 match target_collection.mode1143 {1144 CollectionMode::NFT => Self::set_nft_variable_data(&target_collection, item_id, data)?,1145 CollectionMode::ReFungible => Self::set_re_fungible_variable_data(&target_collection, item_id, data)?,1146 CollectionMode::Fungible(_) => fail!(Error::<T>::CantStoreMetadataInFungibleTokens),1147 _ => fail!(Error::<T>::UnexpectedCollectionType)1148 };11491150 Ok(())1151 }1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 #[weight = <T as Config>::WeightInfo::set_schema_version()]1168 #[transactional]1169 pub fn set_schema_version(1170 origin,1171 collection_id: CollectionId,1172 version: SchemaVersion1173 ) -> DispatchResult {1174 let sender = ensure_signed(origin)?;1175 let mut target_collection = Self::get_collection(collection_id)?;1176 Self::check_owner_or_admin_permissions(&target_collection, sender.clone())?;1177 target_collection.schema_version = version;1178 Self::save_collection(target_collection);11791180 Ok(())1181 }11821183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 #[weight = <T as Config>::WeightInfo::set_offchain_schema()]1196 #[transactional]1197 pub fn set_offchain_schema(1198 origin,1199 collection_id: CollectionId,1200 schema: Vec<u8>1201 ) -> DispatchResult {1202 let sender = ensure_signed(origin)?;1203 let mut target_collection = Self::get_collection(collection_id)?;1204 Self::check_owner_or_admin_permissions(&target_collection, sender.clone())?;12051206 1207 ensure!(schema.len() as u32 <= ChainLimit::get().offchain_schema_limit, "");12081209 target_collection.offchain_schema = schema;1210 Self::save_collection(target_collection);12111212 Ok(())1213 }12141215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 #[weight = <T as Config>::WeightInfo::set_const_on_chain_schema()]1228 #[transactional]1229 pub fn set_const_on_chain_schema (1230 origin,1231 collection_id: CollectionId,1232 schema: Vec<u8>1233 ) -> DispatchResult {1234 let sender = ensure_signed(origin)?;1235 let mut target_collection = Self::get_collection(collection_id)?;1236 Self::check_owner_or_admin_permissions(&target_collection, sender.clone())?;12371238 1239 ensure!(schema.len() as u32 <= ChainLimit::get().const_on_chain_schema_limit, "");12401241 target_collection.const_on_chain_schema = schema;1242 Self::save_collection(target_collection);12431244 Ok(())1245 }12461247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 #[weight = <T as Config>::WeightInfo::set_const_on_chain_schema()]1260 #[transactional]1261 pub fn set_variable_on_chain_schema (1262 origin,1263 collection_id: CollectionId,1264 schema: Vec<u8>1265 ) -> DispatchResult {1266 let sender = ensure_signed(origin)?;1267 let mut target_collection = Self::get_collection(collection_id)?;1268 Self::check_owner_or_admin_permissions(&target_collection, sender.clone())?;12691270 1271 ensure!(schema.len() as u32 <= ChainLimit::get().variable_on_chain_schema_limit, "");12721273 target_collection.variable_on_chain_schema = schema;1274 Self::save_collection(target_collection);12751276 Ok(())1277 }12781279 1280 #[weight = <T as Config>::WeightInfo::set_chain_limits()]1281 #[transactional]1282 pub fn set_chain_limits(1283 origin,1284 limits: ChainLimits1285 ) -> DispatchResult {12861287 #[cfg(not(feature = "runtime-benchmarks"))]1288 ensure_root(origin)?;12891290 <ChainLimit>::put(limits);1291 Ok(())1292 }12931294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 #[weight = <T as Config>::WeightInfo::enable_contract_sponsoring()]1306 #[transactional]1307 pub fn enable_contract_sponsoring(1308 origin,1309 contract_address: T::AccountId,1310 enable: bool1311 ) -> DispatchResult {13121313 let sender = ensure_signed(origin)?;13141315 #[cfg(feature = "runtime-benchmarks")]1316 <ContractOwner<T>>::insert(contract_address.clone(), sender.clone());13171318 Self::ensure_contract_owned(sender, &contract_address)?;13191320 <ContractSelfSponsoring<T>>::insert(contract_address, enable);1321 Ok(())1322 }13231324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 #[weight = <T as Config>::WeightInfo::set_contract_sponsoring_rate_limit()]1342 #[transactional]1343 pub fn set_contract_sponsoring_rate_limit(1344 origin,1345 contract_address: T::AccountId,1346 rate_limit: T::BlockNumber1347 ) -> DispatchResult {1348 let sender = ensure_signed(origin)?;13491350 #[cfg(feature = "runtime-benchmarks")]1351 <ContractOwner<T>>::insert(contract_address.clone(), sender.clone());13521353 Self::ensure_contract_owned(sender, &contract_address)?;1354 <ContractSponsoringRateLimit<T>>::insert(contract_address, rate_limit);1355 Ok(())1356 }13571358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 #[weight = <T as Config>::WeightInfo::toggle_contract_white_list()]1370 #[transactional]1371 pub fn toggle_contract_white_list(1372 origin,1373 contract_address: T::AccountId,1374 enable: bool1375 ) -> DispatchResult {1376 let sender = ensure_signed(origin)?;13771378 #[cfg(feature = "runtime-benchmarks")]1379 <ContractOwner<T>>::insert(contract_address.clone(), sender.clone());13801381 Self::ensure_contract_owned(sender, &contract_address)?;1382 if enable {1383 <ContractWhiteListEnabled<T>>::insert(contract_address, true);1384 } else {1385 <ContractWhiteListEnabled<T>>::remove(contract_address);1386 }1387 Ok(())1388 }1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 #[weight = <T as Config>::WeightInfo::add_to_contract_white_list()]1402 #[transactional]1403 pub fn add_to_contract_white_list(1404 origin,1405 contract_address: T::AccountId,1406 account_address: T::AccountId1407 ) -> DispatchResult {1408 let sender = ensure_signed(origin)?;14091410 #[cfg(feature = "runtime-benchmarks")]1411 <ContractOwner<T>>::insert(contract_address.clone(), sender.clone());1412 1413 Self::ensure_contract_owned(sender, &contract_address)?; 1414 <ContractWhiteList<T>>::insert(contract_address, account_address, true);1415 Ok(())1416 }14171418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 #[weight = <T as Config>::WeightInfo::remove_from_contract_white_list()]1430 #[transactional]1431 pub fn remove_from_contract_white_list(1432 origin,1433 contract_address: T::AccountId,1434 account_address: T::AccountId1435 ) -> DispatchResult {1436 let sender = ensure_signed(origin)?;14371438 #[cfg(feature = "runtime-benchmarks")]1439 <ContractOwner<T>>::insert(contract_address.clone(), sender.clone());14401441 Self::ensure_contract_owned(sender, &contract_address)?;1442 <ContractWhiteList<T>>::remove(contract_address, account_address);1443 Ok(())1444 }14451446 #[weight = <T as Config>::WeightInfo::set_collection_limits()]1447 #[transactional]1448 pub fn set_collection_limits(1449 origin,1450 collection_id: u32,1451 new_limits: CollectionLimits<T::BlockNumber>,1452 ) -> DispatchResult {1453 let sender = ensure_signed(origin)?;1454 let mut target_collection = Self::get_collection(collection_id)?;1455 Self::check_owner_permissions(&target_collection, sender.clone())?;1456 let old_limits = &target_collection.limits;1457 let chain_limits = ChainLimit::get();14581459 1460 ensure!(new_limits.sponsor_transfer_timeout <= MAX_SPONSOR_TIMEOUT &&1461 new_limits.account_token_ownership_limit <= MAX_TOKEN_OWNERSHIP && 1462 new_limits.sponsored_data_size <= chain_limits.custom_data_limit,1463 Error::<T>::CollectionLimitBoundsExceeded);14641465 1466 ensure!(old_limits.token_limit >= new_limits.token_limit, Error::<T>::CollectionTokenLimitExceeded);1467 ensure!(new_limits.token_limit > 0, Error::<T>::CollectionTokenLimitExceeded);14681469 ensure!(1470 (old_limits.owner_can_transfer || !new_limits.owner_can_transfer) &&1471 (old_limits.owner_can_destroy || !new_limits.owner_can_destroy),1472 Error::<T>::OwnerPermissionsCantBeReverted,1473 );14741475 target_collection.limits = new_limits;1476 Self::save_collection(target_collection);14771478 Ok(())1479 } 1480 }1481}14821483impl<T: Config> Module<T> {1484 pub fn create_item_internal(sender: T::AccountId, collection_id: CollectionId, owner: T::AccountId, data: CreateItemData) -> DispatchResult {1485 let target_collection = Self::get_collection(collection_id)?;14861487 Self::can_create_items_in_collection(&target_collection, &sender, &owner, 1)?;1488 Self::validate_create_item_args(&target_collection, &data)?;1489 Self::create_item_no_validation(&target_collection, owner, data)?;14901491 Ok(())1492 }14931494 pub fn transfer_internal(sender: T::AccountId, recipient: T::AccountId, target_collection: &CollectionHandle<T>, item_id: TokenId, value: u128) -> DispatchResult {1495 1496 Self::is_correct_transfer(target_collection, &recipient)?;14971498 1499 ensure!(Self::is_item_owner(sender.clone(), target_collection, item_id) ||1500 Self::is_owner_or_admin_permissions(target_collection, sender.clone()),1501 Error::<T>::NoPermission);15021503 if target_collection.access == AccessMode::WhiteList {1504 Self::check_white_list(target_collection, &sender)?;1505 Self::check_white_list(target_collection, &recipient)?;1506 }15071508 match target_collection.mode1509 {1510 CollectionMode::NFT => Self::transfer_nft(target_collection, item_id, sender.clone(), recipient.clone())?,1511 CollectionMode::Fungible(_) => Self::transfer_fungible(target_collection, value, &sender, &recipient)?,1512 CollectionMode::ReFungible => Self::transfer_refungible(target_collection, item_id, value, sender.clone(), recipient.clone())?,1513 _ => ()1514 };15151516 Self::deposit_event(RawEvent::Transfer(target_collection.id, item_id, sender, recipient, value));15171518 Ok(())1519 }152015211522 fn is_correct_transfer(collection: &CollectionHandle<T>, recipient: &T::AccountId) -> DispatchResult {1523 let collection_id = collection.id;15241525 1526 let account_items: u32 = <AddressTokens<T>>::get(collection_id, recipient).len() as u32;1527 ensure!(collection.limits.account_token_ownership_limit > account_items, Error::<T>::AccountTokenLimitExceeded);1528 1529 Ok(())1530 }15311532 fn can_create_items_in_collection(collection: &CollectionHandle<T>, sender: &T::AccountId, owner: &T::AccountId, amount: u32) -> DispatchResult {1533 let collection_id = collection.id;15341535 1536 let total_items: u32 = ItemListIndex::get(collection_id)1537 .checked_add(amount)1538 .ok_or(Error::<T>::CollectionTokenLimitExceeded)?;1539 let account_items: u32 = (<AddressTokens<T>>::get(collection_id, owner).len() as u32)1540 .checked_add(amount)1541 .ok_or(Error::<T>::AccountTokenLimitExceeded)?;1542 ensure!(collection.limits.token_limit >= total_items, Error::<T>::CollectionTokenLimitExceeded);1543 ensure!(collection.limits.account_token_ownership_limit >= account_items, Error::<T>::AccountTokenLimitExceeded);15441545 if !Self::is_owner_or_admin_permissions(collection, sender.clone()) {1546 ensure!(collection.mint_mode == true, Error::<T>::PublicMintingNotAllowed);1547 Self::check_white_list(collection, owner)?;1548 Self::check_white_list(collection, sender)?;1549 }15501551 Ok(())1552 }15531554 fn validate_create_item_args(target_collection: &CollectionHandle<T>, data: &CreateItemData) -> DispatchResult {1555 match target_collection.mode1556 {1557 CollectionMode::NFT => {1558 if let CreateItemData::NFT(data) = data {1559 1560 ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, Error::<T>::TokenConstDataLimitExceeded);1561 ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, Error::<T>::TokenVariableDataLimitExceeded);1562 } else {1563 fail!(Error::<T>::NotNftDataUsedToMintNftCollectionToken);1564 }1565 },1566 CollectionMode::Fungible(_) => {1567 if let CreateItemData::Fungible(_) = data {1568 } else {1569 fail!(Error::<T>::NotFungibleDataUsedToMintFungibleCollectionToken);1570 }1571 },1572 CollectionMode::ReFungible => {1573 if let CreateItemData::ReFungible(data) = data {15741575 1576 ensure!(ChainLimit::get().custom_data_limit >= data.const_data.len() as u32, Error::<T>::TokenConstDataLimitExceeded);1577 ensure!(ChainLimit::get().custom_data_limit >= data.variable_data.len() as u32, Error::<T>::TokenVariableDataLimitExceeded);15781579 1580 ensure!(data.pieces <= MAX_REFUNGIBLE_PIECES, Error::<T>::WrongRefungiblePieces);1581 ensure!(data.pieces > 0, Error::<T>::WrongRefungiblePieces);1582 } else {1583 fail!(Error::<T>::NotReFungibleDataUsedToMintReFungibleCollectionToken);1584 }1585 },1586 _ => { fail!(Error::<T>::UnexpectedCollectionType); }1587 };15881589 Ok(())1590 }15911592 fn create_item_no_validation(collection: &CollectionHandle<T>, owner: T::AccountId, data: CreateItemData) -> DispatchResult {1593 match data1594 {1595 CreateItemData::NFT(data) => {1596 let item = NftItemType {1597 owner: owner.clone(),1598 const_data: data.const_data,1599 variable_data: data.variable_data1600 };16011602 Self::add_nft_item(collection, item)?;1603 },1604 CreateItemData::Fungible(data) => {1605 Self::add_fungible_item(collection, &owner, data.value)?;1606 },1607 CreateItemData::ReFungible(data) => {1608 let mut owner_list = Vec::new();1609 owner_list.push(Ownership {owner: owner.clone(), fraction: data.pieces});16101611 let item = ReFungibleItemType {1612 owner: owner_list,1613 const_data: data.const_data,1614 variable_data: data.variable_data1615 };16161617 Self::add_refungible_item(collection, item)?;1618 }1619 };16201621 Ok(())1622 }16231624 fn add_fungible_item(collection: &CollectionHandle<T>, owner: &T::AccountId, value: u128) -> DispatchResult {1625 let collection_id = collection.id;16261627 1628 let balance: u128 = <FungibleItemList<T>>::get(collection_id, owner).value;16291630 1631 let item = FungibleItemType {1632 value: balance.checked_add(value).ok_or(Error::<T>::NumOverflow)?,1633 };1634 <FungibleItemList<T>>::insert(collection_id, (*owner).clone(), item);16351636 1637 let new_balance = <Balance<T>>::get(collection_id, owner)1638 .checked_add(value)1639 .ok_or(Error::<T>::NumOverflow)?;1640 <Balance<T>>::insert(collection_id, (*owner).clone(), new_balance);16411642 Self::deposit_event(RawEvent::ItemCreated(collection_id, 0, owner.clone()));1643 Ok(())1644 }16451646 fn add_refungible_item(collection: &CollectionHandle<T>, item: ReFungibleItemType<T::AccountId>) -> DispatchResult {1647 let collection_id = collection.id;16481649 let current_index = <ItemListIndex>::get(collection_id)1650 .checked_add(1)1651 .ok_or(Error::<T>::NumOverflow)?;1652 let itemcopy = item.clone();16531654 ensure!(1655 item.owner.len() == 1,1656 Error::<T>::BadCreateRefungibleCall,1657 );1658 let item_owner = item.owner.first().expect("only one owner is defined");16591660 let value = item_owner.fraction;1661 let owner = item_owner.owner.clone();16621663 Self::add_token_index(collection_id, current_index, &owner)?;16641665 <ItemListIndex>::insert(collection_id, current_index);1666 <ReFungibleItemList<T>>::insert(collection_id, current_index, itemcopy);16671668 1669 let new_balance = <Balance<T>>::get(collection_id, &owner)1670 .checked_add(value)1671 .ok_or(Error::<T>::NumOverflow)?;1672 <Balance<T>>::insert(collection_id, owner.clone(), new_balance);16731674 Self::deposit_event(RawEvent::ItemCreated(collection_id, current_index, owner));1675 Ok(())1676 }16771678 fn add_nft_item(collection: &CollectionHandle<T>, item: NftItemType<T::AccountId>) -> DispatchResult {1679 let collection_id = collection.id;16801681 let current_index = <ItemListIndex>::get(collection_id)1682 .checked_add(1)1683 .ok_or(Error::<T>::NumOverflow)?;16841685 let item_owner = item.owner.clone();1686 Self::add_token_index(collection_id, current_index, &item.owner)?;16871688 <ItemListIndex>::insert(collection_id, current_index);1689 <NftItemList<T>>::insert(collection_id, current_index, item);16901691 1692 let new_balance = <Balance<T>>::get(collection_id, item_owner.clone())1693 .checked_add(1)1694 .ok_or(Error::<T>::NumOverflow)?;1695 <Balance<T>>::insert(collection_id, item_owner.clone(), new_balance);16961697 Self::deposit_event(RawEvent::ItemCreated(collection_id, current_index, item_owner));1698 Ok(())1699 }17001701 fn burn_refungible_item(1702 collection: &CollectionHandle<T>,1703 item_id: TokenId,1704 owner: &T::AccountId,1705 ) -> DispatchResult {1706 let collection_id = collection.id;17071708 let mut token = <ReFungibleItemList<T>>::get(collection_id, item_id)1709 .ok_or(Error::<T>::TokenNotFound)?;1710 let rft_balance = token1711 .owner1712 .iter()1713 .find(|&i| i.owner == *owner)1714 .ok_or(Error::<T>::TokenNotFound)?;1715 Self::remove_token_index(collection_id, item_id, owner)?;17161717 1718 let new_balance = <Balance<T>>::get(collection_id, rft_balance.owner.clone())1719 .checked_sub(rft_balance.fraction)1720 .ok_or(Error::<T>::NumOverflow)?;1721 <Balance<T>>::insert(collection_id, rft_balance.owner.clone(), new_balance);17221723 1724 let index = token1725 .owner1726 .iter()1727 .position(|i| i.owner == *owner)1728 .expect("owned item is exists");1729 token.owner.remove(index);1730 let owner_count = token.owner.len();17311732 1733 if owner_count == 0 {1734 <ReFungibleItemList<T>>::remove(collection_id, item_id);1735 <VariableMetaDataBasket<T>>::remove(collection_id, item_id);1736 }1737 else {1738 <ReFungibleItemList<T>>::insert(collection_id, item_id, token);1739 }17401741 Ok(())1742 }17431744 fn burn_nft_item(collection: &CollectionHandle<T>, item_id: TokenId) -> DispatchResult {1745 let collection_id = collection.id;17461747 let item = <NftItemList<T>>::get(collection_id, item_id)1748 .ok_or(Error::<T>::TokenNotFound)?;1749 Self::remove_token_index(collection_id, item_id, &item.owner)?;17501751 1752 let new_balance = <Balance<T>>::get(collection_id, &item.owner)1753 .checked_sub(1)1754 .ok_or(Error::<T>::NumOverflow)?;1755 <Balance<T>>::insert(collection_id, item.owner.clone(), new_balance);1756 <NftItemList<T>>::remove(collection_id, item_id);1757 <VariableMetaDataBasket<T>>::remove(collection_id, item_id);17581759 Ok(())1760 }17611762 fn burn_fungible_item(owner: &T::AccountId, collection: &CollectionHandle<T>, value: u128) -> DispatchResult {1763 let collection_id = collection.id;17641765 let mut balance = <FungibleItemList<T>>::get(collection_id, owner);1766 ensure!(balance.value >= value, Error::<T>::TokenValueNotEnough);17671768 1769 let new_balance = <Balance<T>>::get(collection_id, owner)1770 .checked_sub(value)1771 .ok_or(Error::<T>::NumOverflow)?;1772 <Balance<T>>::insert(collection_id, (*owner).clone(), new_balance);17731774 if balance.value - value > 0 {1775 balance.value -= value;1776 <FungibleItemList<T>>::insert(collection_id, (*owner).clone(), balance);1777 }1778 else {1779 <FungibleItemList<T>>::remove(collection_id, owner);1780 }17811782 Ok(())1783 }17841785 pub fn get_collection(collection_id: CollectionId) -> Result<CollectionHandle<T>, sp_runtime::DispatchError> {1786 Ok(<CollectionById<T>>::get(collection_id)1787 .map(|collection| CollectionHandle {1788 id: collection_id,1789 collection1790 })1791 .ok_or(Error::<T>::CollectionNotFound)?)1792 }17931794 fn save_collection(collection: CollectionHandle<T>) {1795 <CollectionById<T>>::insert(collection.id, collection.collection);1796 }17971798 fn check_owner_permissions(target_collection: &CollectionHandle<T>, subject: T::AccountId) -> DispatchResult {1799 ensure!(1800 subject == target_collection.owner,1801 Error::<T>::NoPermission1802 );18031804 Ok(())1805 }18061807 fn is_owner_or_admin_permissions(collection: &CollectionHandle<T>, subject: T::AccountId) -> bool {1808 subject == collection.owner || <AdminList<T>>::get(collection.id).contains(&subject)1809 }18101811 fn check_owner_or_admin_permissions(1812 collection: &CollectionHandle<T>,1813 subject: T::AccountId,1814 ) -> DispatchResult {1815 ensure!(Self::is_owner_or_admin_permissions(collection, subject), Error::<T>::NoPermission);18161817 Ok(())1818 }18191820 fn owned_amount(1821 subject: T::AccountId,1822 target_collection: &CollectionHandle<T>,1823 item_id: TokenId,1824 ) -> Option<u128> {1825 let collection_id = target_collection.id;18261827 match target_collection.mode {1828 CollectionMode::NFT => (<NftItemList<T>>::get(collection_id, item_id)?.owner == subject)1829 .then(|| 1),1830 CollectionMode::Fungible(_) => Some(<FungibleItemList<T>>::get(collection_id, &subject)1831 .value),1832 CollectionMode::ReFungible => <ReFungibleItemList<T>>::get(collection_id, item_id)?1833 .owner1834 .iter()1835 .find(|i| i.owner == subject)1836 .map(|i| i.fraction),1837 CollectionMode::Invalid => None,1838 }1839 }18401841 fn is_item_owner(subject: T::AccountId, target_collection: &CollectionHandle<T>, item_id: TokenId) -> bool {1842 match target_collection.mode {1843 CollectionMode::Fungible(_) => true,1844 _ => Self::owned_amount(subject, target_collection, item_id).is_some(),1845 }1846 }18471848 fn check_white_list(collection: &CollectionHandle<T>, address: &T::AccountId) -> DispatchResult {1849 let collection_id = collection.id;18501851 let mes = Error::<T>::AddresNotInWhiteList;1852 ensure!(<WhiteList<T>>::contains_key(collection_id, address), mes);18531854 Ok(())1855 }18561857 1858 1859 fn token_exists(1860 target_collection: &CollectionHandle<T>,1861 item_id: TokenId,1862 ) -> DispatchResult {1863 let collection_id = target_collection.id;1864 let exists = match target_collection.mode1865 {1866 CollectionMode::NFT => <NftItemList<T>>::contains_key(collection_id, item_id),1867 CollectionMode::Fungible(_) => true,1868 CollectionMode::ReFungible => <ReFungibleItemList<T>>::contains_key(collection_id, item_id),1869 _ => false1870 };18711872 ensure!(exists == true, Error::<T>::TokenNotFound);1873 Ok(())1874 }18751876 fn transfer_fungible(1877 collection: &CollectionHandle<T>,1878 value: u128,1879 owner: &T::AccountId,1880 recipient: &T::AccountId,1881 ) -> DispatchResult {1882 let collection_id = collection.id;18831884 let mut balance = <FungibleItemList<T>>::get(collection_id, owner);1885 ensure!(balance.value >= value, Error::<T>::TokenValueTooLow);18861887 1888 Self::add_fungible_item(collection, recipient, value)?;18891890 1891 <Balance<T>>::insert(collection_id, (*owner).clone(), balance.value - value);18921893 1894 if balance.value == value {1895 <FungibleItemList<T>>::remove(collection_id, owner);1896 }1897 else {1898 balance.value -= value;1899 <FungibleItemList<T>>::insert(collection_id, (*owner).clone(), balance);1900 }19011902 Ok(())1903 }19041905 fn transfer_refungible(1906 collection: &CollectionHandle<T>,1907 item_id: TokenId,1908 value: u128,1909 owner: T::AccountId,1910 new_owner: T::AccountId,1911 ) -> DispatchResult {1912 let collection_id = collection.id;1913 let full_item = <ReFungibleItemList<T>>::get(collection_id, item_id)1914 .ok_or(Error::<T>::TokenNotFound)?;19151916 let item = full_item1917 .owner1918 .iter()1919 .filter(|i| i.owner == owner)1920 .next()1921 .ok_or(Error::<T>::TokenNotFound)?;1922 let amount = item.fraction;19231924 ensure!(amount >= value, Error::<T>::TokenValueTooLow);19251926 1927 let balance_old_owner = <Balance<T>>::get(collection_id, item.owner.clone())1928 .checked_sub(value)1929 .ok_or(Error::<T>::NumOverflow)?;1930 <Balance<T>>::insert(collection_id, item.owner.clone(), balance_old_owner);19311932 let balance_new_owner = <Balance<T>>::get(collection_id, new_owner.clone())1933 .checked_add(value)1934 .ok_or(Error::<T>::NumOverflow)?;1935 <Balance<T>>::insert(collection_id, new_owner.clone(), balance_new_owner);19361937 let old_owner = item.owner.clone();1938 let new_owner_has_account = full_item.owner.iter().any(|i| i.owner == new_owner);19391940 1941 if amount == value && !new_owner_has_account {1942 1943 1944 let mut new_full_item = full_item.clone();1945 new_full_item1946 .owner1947 .iter_mut()1948 .find(|i| i.owner == owner)1949 .expect("old owner does present in refungible")1950 .owner = new_owner.clone();1951 <ReFungibleItemList<T>>::insert(collection_id, item_id, new_full_item);19521953 1954 Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?;1955 } else {1956 let mut new_full_item = full_item.clone();1957 new_full_item1958 .owner1959 .iter_mut()1960 .find(|i| i.owner == owner)1961 .expect("old owner does present in refungible")1962 .fraction -= value;19631964 1965 if new_owner_has_account {1966 1967 new_full_item1968 .owner1969 .iter_mut()1970 .find(|i| i.owner == new_owner)1971 .expect("new owner has account")1972 .fraction += value;1973 } else {1974 1975 new_full_item.owner.push(Ownership {1976 owner: new_owner.clone(),1977 fraction: value,1978 });1979 Self::add_token_index(collection_id, item_id, &new_owner)?;1980 }19811982 <ReFungibleItemList<T>>::insert(collection_id, item_id, new_full_item);1983 }19841985 Ok(())1986 }19871988 fn transfer_nft(1989 collection: &CollectionHandle<T>,1990 item_id: TokenId,1991 sender: T::AccountId,1992 new_owner: T::AccountId,1993 ) -> DispatchResult {1994 let collection_id = collection.id;1995 let mut item = <NftItemList<T>>::get(collection_id, item_id)1996 .ok_or(Error::<T>::TokenNotFound)?;19971998 ensure!(1999 sender == item.owner,2000 Error::<T>::MustBeTokenOwner2001 );20022003 2004 let balance_old_owner = <Balance<T>>::get(collection_id, item.owner.clone())2005 .checked_sub(1)2006 .ok_or(Error::<T>::NumOverflow)?;2007 <Balance<T>>::insert(collection_id, item.owner.clone(), balance_old_owner);20082009 let balancenew_owner = <Balance<T>>::get(collection_id, new_owner.clone())2010 .checked_add(1)2011 .ok_or(Error::<T>::NumOverflow)?;2012 <Balance<T>>::insert(collection_id, new_owner.clone(), balancenew_owner);20132014 2015 let old_owner = item.owner.clone();2016 item.owner = new_owner.clone();2017 <NftItemList<T>>::insert(collection_id, item_id, item);20182019 2020 Self::move_token_index(collection_id, item_id, &old_owner, &new_owner)?;20212022 Ok(())2023 }2024 2025 fn set_re_fungible_variable_data(2026 collection: &CollectionHandle<T>,2027 item_id: TokenId,2028 data: Vec<u8>2029 ) -> DispatchResult {2030 let collection_id = collection.id;2031 let mut item = <ReFungibleItemList<T>>::get(collection_id, item_id)2032 .ok_or(Error::<T>::TokenNotFound)?;20332034 item.variable_data = data;20352036 <ReFungibleItemList<T>>::insert(collection_id, item_id, item);20372038 Ok(())2039 }20402041 fn set_nft_variable_data(2042 collection: &CollectionHandle<T>,2043 item_id: TokenId,2044 data: Vec<u8>2045 ) -> DispatchResult {2046 let collection_id = collection.id;2047 let mut item = <NftItemList<T>>::get(collection_id, item_id)2048 .ok_or(Error::<T>::TokenNotFound)?;2049 2050 item.variable_data = data;20512052 <NftItemList<T>>::insert(collection_id, item_id, item);2053 2054 Ok(())2055 }20562057 fn init_collection(item: &Collection<T>) {2058 2059 assert!(2060 item.decimal_points <= MAX_DECIMAL_POINTS,2061 "decimal_points parameter must be lower than MAX_DECIMAL_POINTS"2062 );2063 assert!(2064 item.name.len() <= 64,2065 "Collection name can not be longer than 63 char"2066 );2067 assert!(2068 item.name.len() <= 256,2069 "Collection description can not be longer than 255 char"2070 );2071 assert!(2072 item.token_prefix.len() <= 16,2073 "Token prefix can not be longer than 15 char"2074 );20752076 2077 let next_id = CreatedCollectionCount::get()2078 .checked_add(1)2079 .unwrap();20802081 CreatedCollectionCount::put(next_id);2082 }20832084 fn init_nft_token(collection_id: CollectionId, item: &NftItemType<T::AccountId>) {2085 let current_index = <ItemListIndex>::get(collection_id)2086 .checked_add(1)2087 .unwrap();20882089 let item_owner = item.owner.clone();2090 Self::add_token_index(collection_id, current_index, &item.owner).unwrap();20912092 <ItemListIndex>::insert(collection_id, current_index);20932094 2095 let new_balance = <Balance<T>>::get(collection_id, &item_owner)2096 .checked_add(1)2097 .unwrap();2098 <Balance<T>>::insert(collection_id, item_owner.clone(), new_balance);2099 }21002101 fn init_fungible_token(collection_id: CollectionId, owner: &T::AccountId, item: &FungibleItemType) {2102 let current_index = <ItemListIndex>::get(collection_id)2103 .checked_add(1)2104 .unwrap();21052106 Self::add_token_index(collection_id, current_index, owner).unwrap();21072108 <ItemListIndex>::insert(collection_id, current_index);21092110 2111 let new_balance = <Balance<T>>::get(collection_id, owner)2112 .checked_add(item.value)2113 .unwrap();2114 <Balance<T>>::insert(collection_id, (*owner).clone(), new_balance);2115 }21162117 fn init_refungible_token(collection_id: CollectionId, item: &ReFungibleItemType<T::AccountId>) {2118 let current_index = <ItemListIndex>::get(collection_id)2119 .checked_add(1)2120 .unwrap();21212122 let value = item.owner.first().unwrap().fraction;2123 let owner = item.owner.first().unwrap().owner.clone();21242125 Self::add_token_index(collection_id, current_index, &owner).unwrap();21262127 <ItemListIndex>::insert(collection_id, current_index);21282129 2130 let new_balance = <Balance<T>>::get(collection_id, &owner)2131 .checked_add(value)2132 .unwrap();2133 <Balance<T>>::insert(collection_id, owner.clone(), new_balance);2134 }21352136 fn add_token_index(collection_id: CollectionId, item_index: TokenId, owner: &T::AccountId) -> DispatchResult {2137 2138 if <AccountItemCount<T>>::contains_key(owner) {21392140 2141 let count = <AccountItemCount<T>>::get(owner);2142 ensure!(count < ChainLimit::get().account_token_ownership_limit, Error::<T>::AddressOwnershipLimitExceeded);21432144 <AccountItemCount<T>>::insert(owner.clone(), count2145 .checked_add(1)2146 .ok_or(Error::<T>::NumOverflow)?);2147 }2148 else {2149 <AccountItemCount<T>>::insert(owner.clone(), 1);2150 }21512152 let list_exists = <AddressTokens<T>>::contains_key(collection_id, owner);2153 if list_exists {2154 let mut list = <AddressTokens<T>>::get(collection_id, owner);2155 let item_contains = list.contains(&item_index.clone());21562157 if !item_contains {2158 list.push(item_index.clone());2159 }21602161 <AddressTokens<T>>::insert(collection_id, owner.clone(), list);2162 } else {2163 let mut itm = Vec::new();2164 itm.push(item_index.clone());2165 <AddressTokens<T>>::insert(collection_id, owner.clone(), itm);2166 }21672168 Ok(())2169 }21702171 fn remove_token_index(2172 collection_id: CollectionId,2173 item_index: TokenId,2174 owner: &T::AccountId,2175 ) -> DispatchResult {21762177 2178 <AccountItemCount<T>>::insert(owner.clone(), 2179 <AccountItemCount<T>>::get(owner)2180 .checked_sub(1)2181 .ok_or(Error::<T>::NumOverflow)?);218221832184 let list_exists = <AddressTokens<T>>::contains_key(collection_id, owner);2185 if list_exists {2186 let mut list = <AddressTokens<T>>::get(collection_id, owner);2187 let item_contains = list.contains(&item_index.clone());21882189 if item_contains {2190 list.retain(|&item| item != item_index);2191 <AddressTokens<T>>::insert(collection_id, owner.clone(), list);2192 }2193 }21942195 Ok(())2196 }21972198 fn move_token_index(2199 collection_id: CollectionId,2200 item_index: TokenId,2201 old_owner: &T::AccountId,2202 new_owner: &T::AccountId,2203 ) -> DispatchResult {2204 Self::remove_token_index(collection_id, item_index, old_owner)?;2205 Self::add_token_index(collection_id, item_index, new_owner)?;22062207 Ok(())2208 }2209 2210 fn ensure_contract_owned(account: T::AccountId, contract: &T::AccountId) -> DispatchResult {2211 ensure!(<ContractOwner<T>>::get(contract) == Some(account), Error::<T>::NoPermission);22122213 Ok(())2214 }2215}