12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788#![cfg_attr(not(feature = "std"), no_std)]8990use frame_support::{ensure, BoundedVec, transactional};91use up_data_structs::{92 AccessMode, CollectionId, CustomDataLimit, MAX_REFUNGIBLE_PIECES, TokenId,93 CreateCollectionData, CreateRefungibleExData, mapping::TokenAddressMapping, budget::Budget,94 Property, PropertyScope, TrySetProperty, PropertyKey, PropertyPermission95};96use pallet_evm::account::CrossAccountId;97use pallet_common::{Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, CommonCollectionOperations as _};98use pallet_structure::Pallet as PalletStructure;99use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};100use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};101use core::ops::Deref;102use codec::{Encode, Decode, MaxEncodedLen};103use scale_info::TypeInfo;104105pub use pallet::*;106#[cfg(feature = "runtime-benchmarks")]107pub mod benchmarking;108pub mod common;109pub mod erc;110pub mod weights;111pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;112113#[struct_versioning::versioned(version = 2, upper)]114#[derive(Encode, Decode, Default, TypeInfo, MaxEncodedLen)]115pub struct ItemData {116 pub const_data: BoundedVec<u8, CustomDataLimit>,117118 #[version(..2)]119 pub variable_data: BoundedVec<u8, CustomDataLimit>,120}121122#[frame_support::pallet]123pub mod pallet {124 use super::*;125 use frame_support::{126 Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key,127 traits::StorageVersion,128 };129 use frame_system::pallet_prelude::*;130 use up_data_structs::{CollectionId, TokenId};131 use super::weights::WeightInfo;132133 #[pallet::error]134 pub enum Error<T> {135 136 NotRefungibleDataUsedToMintFungibleCollectionToken,137 138 WrongRefungiblePieces,139 140 RepartitionWhileNotOwningAllPieces,141 142 RefungibleDisallowsNesting,143 144 SettingPropertiesNotAllowed,145 }146147 #[pallet::config]148 pub trait Config:149 frame_system::Config + pallet_common::Config + pallet_structure::Config150 {151 type WeightInfo: WeightInfo;152 }153154 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);155156 #[pallet::pallet]157 #[pallet::storage_version(STORAGE_VERSION)]158 #[pallet::generate_store(pub(super) trait Store)]159 pub struct Pallet<T>(_);160161 162 #[pallet::storage]163 pub type TokensMinted<T: Config> =164 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;165166 167 #[pallet::storage]168 pub type TokensBurnt<T: Config> =169 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u32, QueryKind = ValueQuery>;170171 172 #[pallet::storage]173 pub type TokenData<T: Config> = StorageNMap<174 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),175 Value = ItemData,176 QueryKind = ValueQuery,177 >;178179 #[pallet::storage]180 #[pallet::getter(fn token_properties)]181 pub type TokenProperties<T: Config> = StorageNMap<182 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),183 Value = up_data_structs::Properties,184 QueryKind = ValueQuery,185 OnEmpty = up_data_structs::TokenProperties,186 >;187188 189 #[pallet::storage]190 pub type TotalSupply<T: Config> = StorageNMap<191 Key = (Key<Twox64Concat, CollectionId>, Key<Twox64Concat, TokenId>),192 Value = u128,193 QueryKind = ValueQuery,194 >;195196 197 #[pallet::storage]198 pub type Owned<T: Config> = StorageNMap<199 Key = (200 Key<Twox64Concat, CollectionId>,201 Key<Blake2_128Concat, T::CrossAccountId>,202 Key<Twox64Concat, TokenId>,203 ),204 Value = bool,205 QueryKind = ValueQuery,206 >;207208 209 #[pallet::storage]210 pub type AccountBalance<T: Config> = StorageNMap<211 Key = (212 Key<Twox64Concat, CollectionId>,213 214 Key<Blake2_128Concat, T::CrossAccountId>,215 ),216 Value = u32,217 QueryKind = ValueQuery,218 >;219220 221 #[pallet::storage]222 pub type Balance<T: Config> = StorageNMap<223 Key = (224 Key<Twox64Concat, CollectionId>,225 Key<Twox64Concat, TokenId>,226 227 Key<Blake2_128Concat, T::CrossAccountId>,228 ),229 Value = u128,230 QueryKind = ValueQuery,231 >;232233 234 #[pallet::storage]235 pub type Allowance<T: Config> = StorageNMap<236 Key = (237 Key<Twox64Concat, CollectionId>,238 Key<Twox64Concat, TokenId>,239 240 Key<Blake2_128, T::CrossAccountId>,241 242 Key<Blake2_128Concat, T::CrossAccountId>,243 ),244 Value = u128,245 QueryKind = ValueQuery,246 >;247248 #[pallet::hooks]249 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {250 fn on_runtime_upgrade() -> Weight {251 if StorageVersion::get::<Pallet<T>>() < StorageVersion::new(1) {252 <TokenData<T>>::translate_values::<ItemDataVersion1, _>(|v| {253 Some(<ItemDataVersion2>::from(v))254 })255 }256257 0258 }259 }260}261262pub struct RefungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);263impl<T: Config> RefungibleHandle<T> {264 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {265 Self(inner)266 }267 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {268 self.0269 }270}271impl<T: Config> Deref for RefungibleHandle<T> {272 type Target = pallet_common::CollectionHandle<T>;273274 fn deref(&self) -> &Self::Target {275 &self.0276 }277}278279impl<T: Config> Pallet<T> {280 281 pub fn total_supply(collection: &RefungibleHandle<T>) -> u32 {282 <TokensMinted<T>>::get(collection.id) - <TokensBurnt<T>>::get(collection.id)283 }284285 286 287 288 pub fn token_exists(collection: &RefungibleHandle<T>, token: TokenId) -> bool {289 <TotalSupply<T>>::contains_key((collection.id, token))290 }291292 pub fn set_scoped_token_property(293 collection_id: CollectionId,294 token_id: TokenId,295 scope: PropertyScope,296 property: Property,297 ) -> DispatchResult {298 TokenProperties::<T>::try_mutate((collection_id, token_id), |properties| {299 properties.try_scoped_set(scope, property.key, property.value)300 })301 .map_err(<CommonError<T>>::from)?;302303 Ok(())304 }305306 pub fn set_scoped_token_properties(307 collection_id: CollectionId,308 token_id: TokenId,309 scope: PropertyScope,310 properties: impl Iterator<Item = Property>,311 ) -> DispatchResult {312 TokenProperties::<T>::try_mutate((collection_id, token_id), |stored_properties| {313 stored_properties.try_scoped_set_from_iter(scope, properties)314 })315 .map_err(<CommonError<T>>::from)?;316317 Ok(())318 }319}320321322impl<T: Config> Pallet<T> {323 324 325 326 327 328 pub fn init_collection(329 owner: T::CrossAccountId,330 data: CreateCollectionData<T::AccountId>,331 ) -> Result<CollectionId, DispatchError> {332 <PalletCommon<T>>::init_collection(owner, data, false)333 }334335 336 337 338 339 pub fn destroy_collection(340 collection: RefungibleHandle<T>,341 sender: &T::CrossAccountId,342 ) -> DispatchResult {343 let id = collection.id;344345 if Self::collection_has_tokens(id) {346 return Err(<CommonError<T>>::CantDestroyNotEmptyCollection.into());347 }348349 350351 PalletCommon::destroy_collection(collection.0, sender)?;352353 <TokensMinted<T>>::remove(id);354 <TokensBurnt<T>>::remove(id);355 <TokenData<T>>::remove_prefix((id,), None);356 <TotalSupply<T>>::remove_prefix((id,), None);357 <Balance<T>>::remove_prefix((id,), None);358 <Allowance<T>>::remove_prefix((id,), None);359 <Owned<T>>::remove_prefix((id,), None);360 <AccountBalance<T>>::remove_prefix((id,), None);361 Ok(())362 }363364 fn collection_has_tokens(collection_id: CollectionId) -> bool {365 <TokenData<T>>::iter_prefix((collection_id,))366 .next()367 .is_some()368 }369370 pub fn burn_token_unchecked(371 collection: &RefungibleHandle<T>,372 token_id: TokenId,373 ) -> DispatchResult {374 let burnt = <TokensBurnt<T>>::get(collection.id)375 .checked_add(1)376 .ok_or(ArithmeticError::Overflow)?;377378 <TokensBurnt<T>>::insert(collection.id, burnt);379 <TokenData<T>>::remove((collection.id, token_id));380 <TokenProperties<T>>::remove((collection.id, token_id));381 <TotalSupply<T>>::remove((collection.id, token_id));382 <Balance<T>>::remove_prefix((collection.id, token_id), None);383 <Allowance<T>>::remove_prefix((collection.id, token_id), None);384 385 Ok(())386 }387388 389 390 391 392 393 394 395 396 397 398 399 pub fn burn(400 collection: &RefungibleHandle<T>,401 owner: &T::CrossAccountId,402 token: TokenId,403 amount: u128,404 ) -> DispatchResult {405 let total_supply = <TotalSupply<T>>::get((collection.id, token))406 .checked_sub(amount)407 .ok_or(<CommonError<T>>::TokenValueTooLow)?;408409 410 if total_supply == 0 {411 412 ensure!(413 <Balance<T>>::get((collection.id, token, owner)) == amount,414 <CommonError<T>>::TokenValueTooLow415 );416 let account_balance = <AccountBalance<T>>::get((collection.id, owner))417 .checked_sub(1)418 419 .ok_or(ArithmeticError::Underflow)?;420421 422423 <Owned<T>>::remove((collection.id, owner, token));424 <PalletStructure<T>>::unnest_if_nested(owner, collection.id, token);425 <AccountBalance<T>>::insert((collection.id, owner), account_balance);426 Self::burn_token_unchecked(collection, token)?;427 <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(428 collection.id,429 token,430 owner.clone(),431 amount,432 ));433 return Ok(());434 }435436 let balance = <Balance<T>>::get((collection.id, token, owner))437 .checked_sub(amount)438 .ok_or(<CommonError<T>>::TokenValueTooLow)?;439 let account_balance = if balance == 0 {440 <AccountBalance<T>>::get((collection.id, owner))441 .checked_sub(1)442 443 .ok_or(ArithmeticError::Underflow)?444 } else {445 0446 };447448 449450 if balance == 0 {451 <Owned<T>>::remove((collection.id, owner, token));452 <PalletStructure<T>>::unnest_if_nested(owner, collection.id, token);453 <Balance<T>>::remove((collection.id, token, owner));454 <AccountBalance<T>>::insert((collection.id, owner), account_balance);455 } else {456 <Balance<T>>::insert((collection.id, token, owner), balance);457 }458 <TotalSupply<T>>::insert((collection.id, token), total_supply);459 460 <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(461 collection.id,462 token,463 owner.clone(),464 amount,465 ));466 Ok(())467 }468469 pub fn set_token_property(470 collection: &RefungibleHandle<T>,471 sender: &T::CrossAccountId,472 token_id: TokenId,473 property: Property,474 is_token_create: bool,475 ) -> DispatchResult {476 Self::check_token_change_permission(477 collection,478 sender,479 token_id,480 &property.key,481 is_token_create,482 )?;483484 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {485 let property = property.clone();486 properties.try_set(property.key, property.value)487 })488 .map_err(<CommonError<T>>::from)?;489490 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(491 collection.id,492 token_id,493 property.key,494 ));495496 Ok(())497 }498499 #[transactional]500 pub fn set_token_properties(501 collection: &RefungibleHandle<T>,502 sender: &T::CrossAccountId,503 token_id: TokenId,504 properties: Vec<Property>,505 is_token_create: bool,506 ) -> DispatchResult {507 for property in properties {508 Self::set_token_property(collection, sender, token_id, property, is_token_create)?;509 }510511 Ok(())512 }513514 pub fn delete_token_property(515 collection: &RefungibleHandle<T>,516 sender: &T::CrossAccountId,517 token_id: TokenId,518 property_key: PropertyKey,519 ) -> DispatchResult {520 Self::check_token_change_permission(collection, sender, token_id, &property_key, false)?;521522 <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {523 properties.remove(&property_key)524 })525 .map_err(<CommonError<T>>::from)?;526527 <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(528 collection.id,529 token_id,530 property_key,531 ));532533 Ok(())534 }535536 fn check_token_change_permission(537 collection: &RefungibleHandle<T>,538 sender: &T::CrossAccountId,539 token_id: TokenId,540 property_key: &PropertyKey,541 is_token_create: bool,542 ) -> DispatchResult {543 let permission = <PalletCommon<T>>::property_permissions(collection.id)544 .get(property_key)545 .cloned()546 .unwrap_or_else(PropertyPermission::none);547548 549 let total_pieces: u128 = <Balance<T>>::iter_prefix((collection.id, token_id,)).fold(0, |total, piece| total + piece.1);550 let balance = collection.balance(sender.clone(), token_id);551552 let check_token_owner = || -> DispatchResult {553 ensure!(balance == total_pieces, <CommonError<T>>::NoPermission);554 Ok(())555 };556557 let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))558 .get(property_key)559 .is_some();560561 match permission {562 PropertyPermission { mutable: false, .. } if is_property_exists => {563 Err(<CommonError<T>>::NoPermission.into())564 }565566 PropertyPermission {567 collection_admin,568 token_owner,569 ..570 } => {571 572 if is_token_create && (collection_admin || token_owner) {573 return Ok(());574 }575576 let mut check_result = Err(<CommonError<T>>::NoPermission.into());577578 if collection_admin {579 check_result = collection.check_is_owner_or_admin(sender);580 }581582 if token_owner {583 check_result.or_else(|_| check_token_owner())584 } else {585 check_result586 }587 }588 }589 }590591 #[transactional]592 pub fn delete_token_properties(593 collection: &RefungibleHandle<T>,594 sender: &T::CrossAccountId,595 token_id: TokenId,596 property_keys: Vec<PropertyKey>,597 ) -> DispatchResult {598 for key in property_keys {599 Self::delete_token_property(collection, sender, token_id, key)?;600 }601602 Ok(())603 }604605 606 607 608 609 610 611 612 613 614 pub fn transfer(615 collection: &RefungibleHandle<T>,616 from: &T::CrossAccountId,617 to: &T::CrossAccountId,618 token: TokenId,619 amount: u128,620 nesting_budget: &dyn Budget,621 ) -> DispatchResult {622 ensure!(623 collection.limits.transfers_enabled(),624 <CommonError<T>>::TransferNotAllowed625 );626627 if collection.permissions.access() == AccessMode::AllowList {628 collection.check_allowlist(from)?;629 collection.check_allowlist(to)?;630 }631 <PalletCommon<T>>::ensure_correct_receiver(to)?;632633 let balance_from = <Balance<T>>::get((collection.id, token, from))634 .checked_sub(amount)635 .ok_or(<CommonError<T>>::TokenValueTooLow)?;636 let mut create_target = false;637 let from_to_differ = from != to;638 let balance_to = if from != to {639 let old_balance = <Balance<T>>::get((collection.id, token, to));640 if old_balance == 0 {641 create_target = true;642 }643 Some(644 old_balance645 .checked_add(amount)646 .ok_or(ArithmeticError::Overflow)?,647 )648 } else {649 None650 };651652 let account_balance_from = if balance_from == 0 {653 Some(654 <AccountBalance<T>>::get((collection.id, from))655 .checked_sub(1)656 657 .ok_or(ArithmeticError::Underflow)?,658 )659 } else {660 None661 };662 663 664 let account_balance_to = if create_target && from_to_differ {665 let account_balance_to = <AccountBalance<T>>::get((collection.id, to))666 .checked_add(1)667 .ok_or(ArithmeticError::Overflow)?;668 ensure!(669 account_balance_to < collection.limits.account_token_ownership_limit(),670 <CommonError<T>>::AccountTokenLimitExceeded,671 );672673 Some(account_balance_to)674 } else {675 None676 };677678 679680 <PalletStructure<T>>::nest_if_sent_to_token(681 from.clone(),682 to,683 collection.id,684 token,685 nesting_budget,686 )?;687688 if let Some(balance_to) = balance_to {689 690 if balance_from == 0 {691 <Balance<T>>::remove((collection.id, token, from));692 <PalletStructure<T>>::unnest_if_nested(from, collection.id, token);693 } else {694 <Balance<T>>::insert((collection.id, token, from), balance_from);695 }696 <Balance<T>>::insert((collection.id, token, to), balance_to);697 if let Some(account_balance_from) = account_balance_from {698 <AccountBalance<T>>::insert((collection.id, from), account_balance_from);699 <Owned<T>>::remove((collection.id, from, token));700 }701 if let Some(account_balance_to) = account_balance_to {702 <AccountBalance<T>>::insert((collection.id, to), account_balance_to);703 <Owned<T>>::insert((collection.id, to, token), true);704 }705 }706707 708 <PalletCommon<T>>::deposit_event(CommonEvent::Transfer(709 collection.id,710 token,711 from.clone(),712 to.clone(),713 amount,714 ));715 Ok(())716 }717718 719 720 721 722 723 pub fn create_multiple_items(724 collection: &RefungibleHandle<T>,725 sender: &T::CrossAccountId,726 data: Vec<CreateRefungibleExData<T::CrossAccountId>>,727 nesting_budget: &dyn Budget,728 ) -> DispatchResult {729 if !collection.is_owner_or_admin(sender) {730 ensure!(731 collection.permissions.mint_mode(),732 <CommonError<T>>::PublicMintingNotAllowed733 );734 collection.check_allowlist(sender)?;735736 for item in data.iter() {737 for user in item.users.keys() {738 collection.check_allowlist(user)?;739 }740 }741 }742743 for item in data.iter() {744 for (owner, _) in item.users.iter() {745 <PalletCommon<T>>::ensure_correct_receiver(owner)?;746 }747 }748749 750 let totals = data751 .iter()752 .map(|data| {753 Ok(data754 .users755 .iter()756 .map(|u| u.1)757 .try_fold(0u128, |acc, v| acc.checked_add(*v))758 .ok_or(ArithmeticError::Overflow)?)759 })760 .collect::<Result<Vec<_>, DispatchError>>()?;761 for total in &totals {762 ensure!(763 *total <= MAX_REFUNGIBLE_PIECES,764 <Error<T>>::WrongRefungiblePieces765 );766 }767768 let first_token_id = <TokensMinted<T>>::get(collection.id);769 let tokens_minted = first_token_id770 .checked_add(data.len() as u32)771 .ok_or(ArithmeticError::Overflow)?;772 ensure!(773 tokens_minted < collection.limits.token_limit(),774 <CommonError<T>>::CollectionTokenLimitExceeded775 );776777 let mut balances = BTreeMap::new();778 for data in &data {779 for owner in data.users.keys() {780 let balance = balances781 .entry(owner)782 .or_insert_with(|| <AccountBalance<T>>::get((collection.id, owner)));783 *balance = balance.checked_add(1).ok_or(ArithmeticError::Overflow)?;784785 ensure!(786 *balance <= collection.limits.account_token_ownership_limit(),787 <CommonError<T>>::AccountTokenLimitExceeded,788 );789 }790 }791792 for (i, token) in data.iter().enumerate() {793 let token_id = TokenId(first_token_id + i as u32 + 1);794 for (to, _) in token.users.iter() {795 <PalletStructure<T>>::check_nesting(796 sender.clone(),797 to,798 collection.id,799 token_id,800 nesting_budget,801 )?;802 }803 }804805 806807 <TokensMinted<T>>::insert(collection.id, tokens_minted);808 for (account, balance) in balances {809 <AccountBalance<T>>::insert((collection.id, account), balance);810 }811 for (i, token) in data.into_iter().enumerate() {812 let token_id = first_token_id + i as u32 + 1;813 <TotalSupply<T>>::insert((collection.id, token_id), totals[i]);814815 <TokenData<T>>::insert(816 (collection.id, token_id),817 ItemData {818 const_data: token.const_data,819 },820 );821822 for (user, amount) in token.users.into_iter() {823 if amount == 0 {824 continue;825 }826 <Balance<T>>::insert((collection.id, token_id, &user), amount);827 <Owned<T>>::insert((collection.id, &user, TokenId(token_id)), true);828 <PalletStructure<T>>::nest_if_sent_to_token_unchecked(829 &user,830 collection.id,831 TokenId(token_id),832 );833834 835 <PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(836 collection.id,837 TokenId(token_id),838 user,839 amount,840 ));841 }842 }843 Ok(())844 }845846 pub fn set_allowance_unchecked(847 collection: &RefungibleHandle<T>,848 sender: &T::CrossAccountId,849 spender: &T::CrossAccountId,850 token: TokenId,851 amount: u128,852 ) {853 if amount == 0 {854 <Allowance<T>>::remove((collection.id, token, sender, spender));855 } else {856 <Allowance<T>>::insert((collection.id, token, sender, spender), amount);857 }858 859 <PalletCommon<T>>::deposit_event(CommonEvent::Approved(860 collection.id,861 token,862 sender.clone(),863 spender.clone(),864 amount,865 ))866 }867868 869 870 871 pub fn set_allowance(872 collection: &RefungibleHandle<T>,873 sender: &T::CrossAccountId,874 spender: &T::CrossAccountId,875 token: TokenId,876 amount: u128,877 ) -> DispatchResult {878 if collection.permissions.access() == AccessMode::AllowList {879 collection.check_allowlist(sender)?;880 collection.check_allowlist(spender)?;881 }882883 <PalletCommon<T>>::ensure_correct_receiver(spender)?;884885 if <Balance<T>>::get((collection.id, token, sender)) < amount {886 ensure!(887 collection.ignores_owned_amount(sender) && Self::token_exists(collection, token),888 <CommonError<T>>::CantApproveMoreThanOwned889 );890 }891892 893894 Self::set_allowance_unchecked(collection, sender, spender, token, amount);895 Ok(())896 }897898 899 fn check_allowed(900 collection: &RefungibleHandle<T>,901 spender: &T::CrossAccountId,902 from: &T::CrossAccountId,903 token: TokenId,904 amount: u128,905 nesting_budget: &dyn Budget,906 ) -> Result<Option<u128>, DispatchError> {907 if spender.conv_eq(from) {908 return Ok(None);909 }910 if collection.permissions.access() == AccessMode::AllowList {911 912 collection.check_allowlist(spender)?;913 }914 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {915 916 ensure!(917 <PalletStructure<T>>::check_indirectly_owned(918 spender.clone(),919 source.0,920 source.1,921 None,922 nesting_budget923 )?,924 <CommonError<T>>::ApprovedValueTooLow,925 );926 return Ok(None);927 }928 let allowance =929 <Allowance<T>>::get((collection.id, token, from, &spender)).checked_sub(amount);930 if allowance.is_none() {931 ensure!(932 collection.ignores_allowance(spender),933 <CommonError<T>>::ApprovedValueTooLow934 );935 }936 Ok(allowance)937 }938939 940 941 942 943 944 945 pub fn transfer_from(946 collection: &RefungibleHandle<T>,947 spender: &T::CrossAccountId,948 from: &T::CrossAccountId,949 to: &T::CrossAccountId,950 token: TokenId,951 amount: u128,952 nesting_budget: &dyn Budget,953 ) -> DispatchResult {954 let allowance =955 Self::check_allowed(collection, spender, from, token, amount, nesting_budget)?;956957 958959 Self::transfer(collection, from, to, token, amount, nesting_budget)?;960 if let Some(allowance) = allowance {961 Self::set_allowance_unchecked(collection, from, spender, token, allowance);962 }963 Ok(())964 }965966 967 968 969 970 971 972 pub fn burn_from(973 collection: &RefungibleHandle<T>,974 spender: &T::CrossAccountId,975 from: &T::CrossAccountId,976 token: TokenId,977 amount: u128,978 nesting_budget: &dyn Budget,979 ) -> DispatchResult {980 let allowance =981 Self::check_allowed(collection, spender, from, token, amount, nesting_budget)?;982983 984985 Self::burn(collection, from, token, amount)?;986 if let Some(allowance) = allowance {987 Self::set_allowance_unchecked(collection, from, spender, token, allowance);988 }989 Ok(())990 }991992 993 994 995 996 997 998 999 pub fn create_item(1000 collection: &RefungibleHandle<T>,1001 sender: &T::CrossAccountId,1002 data: CreateRefungibleExData<T::CrossAccountId>,1003 nesting_budget: &dyn Budget,1004 ) -> DispatchResult {1005 Self::create_multiple_items(collection, sender, vec![data], nesting_budget)1006 }10071008 1009 1010 1011 1012 1013 1014 1015 pub fn repartition(1016 collection: &RefungibleHandle<T>,1017 owner: &T::CrossAccountId,1018 token: TokenId,1019 amount: u128,1020 ) -> DispatchResult {1021 ensure!(1022 amount <= MAX_REFUNGIBLE_PIECES,1023 <Error<T>>::WrongRefungiblePieces1024 );1025 ensure!(amount > 0, <CommonError<T>>::TokenValueTooLow);1026 1027 let total_supply = <TotalSupply<T>>::get((collection.id, token));1028 let balance = <Balance<T>>::get((collection.id, token, owner));1029 ensure!(1030 total_supply == balance,1031 <Error<T>>::RepartitionWhileNotOwningAllPieces1032 );10331034 <Balance<T>>::insert((collection.id, token, owner), amount);1035 <TotalSupply<T>>::insert((collection.id, token), amount);1036 Ok(())1037 }10381039 fn total_pieces(collection_id: CollectionId, token_id: TokenId) -> Option<u128> {1040 <TotalSupply<T>>::try_get((collection_id, token_id)).ok()1041 }1042}