difftreelog
refactor use modify_token_properties
in: master
4 files changed
pallets/nonfungible/src/common.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/common.rs
+++ b/pallets/nonfungible/src/common.rs
@@ -229,7 +229,7 @@
self,
&sender,
token_id,
- properties,
+ properties.into_iter(),
false,
nesting_budget,
),
@@ -251,7 +251,7 @@
self,
&sender,
token_id,
- property_keys,
+ property_keys.into_iter(),
nesting_budget,
),
weight,
pallets/nonfungible/src/erc.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617extern crate alloc;18use core::{19 char::{REPLACEMENT_CHARACTER, decode_utf16},20 convert::TryInto,21};22use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};23use frame_support::BoundedVec;24use up_data_structs::{25 TokenId, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey,26 CollectionPropertiesVec,27};28use pallet_evm_coder_substrate::dispatch_to_evm;29use sp_std::vec::Vec;30use pallet_common::{31 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, token_uri_key},32 CollectionHandle, CollectionPropertyPermissions,33};34use pallet_evm::{account::CrossAccountId, PrecompileHandle};35use pallet_evm_coder_substrate::call;36use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};3738use crate::{39 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,40 SelfWeightOf, weights::WeightInfo, TokenProperties, property_guard::*,41};4243#[solidity_interface(name = "TokenProperties")]44impl<T: Config> NonfungibleHandle<T> {45 fn set_token_property_permission(46 &mut self,47 caller: caller,48 key: string,49 is_mutable: bool,50 collection_admin: bool,51 token_owner: bool,52 ) -> Result<()> {53 let caller = T::CrossAccountId::from_eth(caller);54 <Pallet<T>>::set_property_permission(55 self,56 &caller,57 PropertyKeyPermission {58 key: <Vec<u8>>::from(key)59 .try_into()60 .map_err(|_| "too long key")?,61 permission: PropertyPermission {62 mutable: is_mutable,63 collection_admin,64 token_owner,65 },66 },67 )68 .map_err(dispatch_to_evm::<T>)69 }7071 fn set_property(72 &mut self,73 caller: caller,74 token_id: uint256,75 key: string,76 value: bytes,77 ) -> Result<()> {78 let caller = T::CrossAccountId::from_eth(caller);79 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;80 let key = <Vec<u8>>::from(key)81 .try_into()82 .map_err(|_| "key too long")?;83 let value = value.try_into().map_err(|_| "value too long")?;8485 let is_token_create = false;86 let nesting_budget = self87 .recorder88 .weight_calls_budget(<StructureWeight<T>>::find_parent());8990 let mut guard = PropertyGuard::new(PropertyGuardData {91 sender: &caller,92 collection: self,93 token_id: TokenId(token_id),94 is_token_create,95 nesting_budget: &nesting_budget,96 });9798 <Pallet<T>>::set_token_property(Property { key, value }, &mut guard)99 .map_err(dispatch_to_evm::<T>)100 }101102 fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {103 let caller = T::CrossAccountId::from_eth(caller);104 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;105 let key = <Vec<u8>>::from(key)106 .try_into()107 .map_err(|_| "key too long")?;108109 let is_token_create = false;110 let nesting_budget = self111 .recorder112 .weight_calls_budget(<StructureWeight<T>>::find_parent());113114 let mut guard = PropertyGuard::new(PropertyGuardData {115 sender: &caller,116 collection: self,117 token_id: TokenId(token_id),118 is_token_create,119 nesting_budget: &nesting_budget,120 });121122 <Pallet<T>>::delete_token_property(key, &mut guard).map_err(dispatch_to_evm::<T>)123 }124125 /// Throws error if key not found126 fn property(&self, token_id: uint256, key: string) -> Result<bytes> {127 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;128 let key = <Vec<u8>>::from(key)129 .try_into()130 .map_err(|_| "key too long")?;131132 let props = <TokenProperties<T>>::get((self.id, token_id));133 let prop = props.get(&key).ok_or("key not found")?;134135 Ok(prop.to_vec())136 }137}138139#[derive(ToLog)]140pub enum ERC721Events {141 Transfer {142 #[indexed]143 from: address,144 #[indexed]145 to: address,146 #[indexed]147 token_id: uint256,148 },149 Approval {150 #[indexed]151 owner: address,152 #[indexed]153 approved: address,154 #[indexed]155 token_id: uint256,156 },157 #[allow(dead_code)]158 ApprovalForAll {159 #[indexed]160 owner: address,161 #[indexed]162 operator: address,163 approved: bool,164 },165}166167#[derive(ToLog)]168pub enum ERC721MintableEvents {169 #[allow(dead_code)]170 MintingFinished {},171}172173#[solidity_interface(name = "ERC721Metadata")]174impl<T: Config> NonfungibleHandle<T> {175 fn name(&self) -> Result<string> {176 Ok(decode_utf16(self.name.iter().copied())177 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))178 .collect::<string>())179 }180181 fn symbol(&self) -> Result<string> {182 Ok(string::from_utf8_lossy(&self.token_prefix).into())183 }184185 /// Returns token's const_metadata186 #[solidity(rename_selector = "tokenURI")]187 fn token_uri(&self, token_id: uint256) -> Result<string> {188 let key = token_uri_key();189 if !has_token_permission::<T>(self.id, &key) {190 return Err("No tokenURI permission".into());191 }192193 self.consume_store_reads(1)?;194 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;195196 let properties = <TokenProperties<T>>::try_get((self.id, token_id))197 .map_err(|_| Error::Revert("Token properties not found".into()))?;198 if let Some(property) = properties.get(&key) {199 return Ok(string::from_utf8_lossy(property).into());200 }201202 Err("Property tokenURI not found".into())203 }204}205206#[solidity_interface(name = "ERC721Enumerable")]207impl<T: Config> NonfungibleHandle<T> {208 fn token_by_index(&self, index: uint256) -> Result<uint256> {209 Ok(index)210 }211212 /// Not implemented213 fn token_of_owner_by_index(&self, _owner: address, _index: uint256) -> Result<uint256> {214 // TODO: Not implemetable215 Err("not implemented".into())216 }217218 fn total_supply(&self) -> Result<uint256> {219 self.consume_store_reads(1)?;220 Ok(<Pallet<T>>::total_supply(self).into())221 }222}223224#[solidity_interface(name = "ERC721", events(ERC721Events))]225impl<T: Config> NonfungibleHandle<T> {226 fn balance_of(&self, owner: address) -> Result<uint256> {227 self.consume_store_reads(1)?;228 let owner = T::CrossAccountId::from_eth(owner);229 let balance = <AccountBalance<T>>::get((self.id, owner));230 Ok(balance.into())231 }232 fn owner_of(&self, token_id: uint256) -> Result<address> {233 self.consume_store_reads(1)?;234 let token: TokenId = token_id.try_into()?;235 Ok(*<TokenData<T>>::get((self.id, token))236 .ok_or("token not found")?237 .owner238 .as_eth())239 }240 /// Not implemented241 fn safe_transfer_from_with_data(242 &mut self,243 _from: address,244 _to: address,245 _token_id: uint256,246 _data: bytes,247 _value: value,248 ) -> Result<void> {249 // TODO: Not implemetable250 Err("not implemented".into())251 }252 /// Not implemented253 fn safe_transfer_from(254 &mut self,255 _from: address,256 _to: address,257 _token_id: uint256,258 _value: value,259 ) -> Result<void> {260 // TODO: Not implemetable261 Err("not implemented".into())262 }263264 #[weight(<SelfWeightOf<T>>::transfer_from())]265 fn transfer_from(266 &mut self,267 caller: caller,268 from: address,269 to: address,270 token_id: uint256,271 _value: value,272 ) -> Result<void> {273 let caller = T::CrossAccountId::from_eth(caller);274 let from = T::CrossAccountId::from_eth(from);275 let to = T::CrossAccountId::from_eth(to);276 let token = token_id.try_into()?;277 let budget = self278 .recorder279 .weight_calls_budget(<StructureWeight<T>>::find_parent());280281 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)282 .map_err(dispatch_to_evm::<T>)?;283 Ok(())284 }285286 #[weight(<SelfWeightOf<T>>::approve())]287 fn approve(288 &mut self,289 caller: caller,290 approved: address,291 token_id: uint256,292 _value: value,293 ) -> Result<void> {294 let caller = T::CrossAccountId::from_eth(caller);295 let approved = T::CrossAccountId::from_eth(approved);296 let token = token_id.try_into()?;297298 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))299 .map_err(dispatch_to_evm::<T>)?;300 Ok(())301 }302303 /// Not implemented304 fn set_approval_for_all(305 &mut self,306 _caller: caller,307 _operator: address,308 _approved: bool,309 ) -> Result<void> {310 // TODO: Not implemetable311 Err("not implemented".into())312 }313314 /// Not implemented315 fn get_approved(&self, _token_id: uint256) -> Result<address> {316 // TODO: Not implemetable317 Err("not implemented".into())318 }319320 /// Not implemented321 fn is_approved_for_all(&self, _owner: address, _operator: address) -> Result<address> {322 // TODO: Not implemetable323 Err("not implemented".into())324 }325}326327#[solidity_interface(name = "ERC721Burnable")]328impl<T: Config> NonfungibleHandle<T> {329 #[weight(<SelfWeightOf<T>>::burn_item())]330 fn burn(&mut self, caller: caller, token_id: uint256) -> Result<void> {331 let caller = T::CrossAccountId::from_eth(caller);332 let token = token_id.try_into()?;333334 <Pallet<T>>::burn(self, &caller, token).map_err(dispatch_to_evm::<T>)?;335 Ok(())336 }337}338339#[solidity_interface(name = "ERC721Mintable", events(ERC721MintableEvents))]340impl<T: Config> NonfungibleHandle<T> {341 fn minting_finished(&self) -> Result<bool> {342 Ok(false)343 }344345 /// `token_id` should be obtained with `next_token_id` method,346 /// unlike standard, you can't specify it manually347 #[weight(<SelfWeightOf<T>>::create_item())]348 fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result<bool> {349 let caller = T::CrossAccountId::from_eth(caller);350 let to = T::CrossAccountId::from_eth(to);351 let token_id: u32 = token_id.try_into()?;352 let budget = self353 .recorder354 .weight_calls_budget(<StructureWeight<T>>::find_parent());355356 if <TokensMinted<T>>::get(self.id)357 .checked_add(1)358 .ok_or("item id overflow")?359 != token_id360 {361 return Err("item id should be next".into());362 }363364 <Pallet<T>>::create_item(365 self,366 &caller,367 CreateItemData::<T> {368 properties: BoundedVec::default(),369 owner: to,370 },371 &budget,372 )373 .map_err(dispatch_to_evm::<T>)?;374375 Ok(true)376 }377378 /// `token_id` should be obtained with `next_token_id` method,379 /// unlike standard, you can't specify it manually380 #[solidity(rename_selector = "mintWithTokenURI")]381 #[weight(<SelfWeightOf<T>>::create_item())]382 fn mint_with_token_uri(383 &mut self,384 caller: caller,385 to: address,386 token_id: uint256,387 token_uri: string,388 ) -> Result<bool> {389 let key = token_uri_key();390 let permission = get_token_permission::<T>(self.id, &key)?;391 if !permission.collection_admin {392 return Err("Operation is not allowed".into());393 }394395 let caller = T::CrossAccountId::from_eth(caller);396 let to = T::CrossAccountId::from_eth(to);397 let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;398 let budget = self399 .recorder400 .weight_calls_budget(<StructureWeight<T>>::find_parent());401402 if <TokensMinted<T>>::get(self.id)403 .checked_add(1)404 .ok_or("item id overflow")?405 != token_id406 {407 return Err("item id should be next".into());408 }409410 let mut properties = CollectionPropertiesVec::default();411 properties412 .try_push(Property {413 key,414 value: token_uri415 .into_bytes()416 .try_into()417 .map_err(|_| "token uri is too long")?,418 })419 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;420421 <Pallet<T>>::create_item(422 self,423 &caller,424 CreateItemData::<T> {425 properties,426 owner: to,427 },428 &budget,429 )430 .map_err(dispatch_to_evm::<T>)?;431 Ok(true)432 }433434 /// Not implemented435 fn finish_minting(&mut self, _caller: caller) -> Result<bool> {436 Err("not implementable".into())437 }438}439440fn get_token_permission<T: Config>(441 collection_id: CollectionId,442 key: &PropertyKey,443) -> Result<PropertyPermission> {444 let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)445 .map_err(|_| Error::Revert("No permissions for collection".into()))?;446 let a = token_property_permissions447 .get(key)448 .map(|p| p.clone())449 .ok_or_else(|| Error::Revert("No permission".into()))?;450 Ok(a)451}452453fn has_token_permission<T: Config>(collection_id: CollectionId, key: &PropertyKey) -> bool {454 if let Ok(token_property_permissions) =455 CollectionPropertyPermissions::<T>::try_get(collection_id)456 {457 return token_property_permissions.contains_key(key);458 }459460 false461}462463#[solidity_interface(name = "ERC721UniqueExtensions")]464impl<T: Config> NonfungibleHandle<T> {465 #[weight(<SelfWeightOf<T>>::transfer())]466 fn transfer(467 &mut self,468 caller: caller,469 to: address,470 token_id: uint256,471 _value: value,472 ) -> Result<void> {473 let caller = T::CrossAccountId::from_eth(caller);474 let to = T::CrossAccountId::from_eth(to);475 let token = token_id.try_into()?;476 let budget = self477 .recorder478 .weight_calls_budget(<StructureWeight<T>>::find_parent());479480 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;481 Ok(())482 }483484 #[weight(<SelfWeightOf<T>>::burn_from())]485 fn burn_from(486 &mut self,487 caller: caller,488 from: address,489 token_id: uint256,490 _value: value,491 ) -> Result<void> {492 let caller = T::CrossAccountId::from_eth(caller);493 let from = T::CrossAccountId::from_eth(from);494 let token = token_id.try_into()?;495 let budget = self496 .recorder497 .weight_calls_budget(<StructureWeight<T>>::find_parent());498499 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)500 .map_err(dispatch_to_evm::<T>)?;501 Ok(())502 }503504 fn next_token_id(&self) -> Result<uint256> {505 self.consume_store_reads(1)?;506 Ok(<TokensMinted<T>>::get(self.id)507 .checked_add(1)508 .ok_or("item id overflow")?509 .into())510 }511512 #[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]513 fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec<uint256>) -> Result<bool> {514 let caller = T::CrossAccountId::from_eth(caller);515 let to = T::CrossAccountId::from_eth(to);516 let mut expected_index = <TokensMinted<T>>::get(self.id)517 .checked_add(1)518 .ok_or("item id overflow")?;519 let budget = self520 .recorder521 .weight_calls_budget(<StructureWeight<T>>::find_parent());522523 let total_tokens = token_ids.len();524 for id in token_ids.into_iter() {525 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;526 if id != expected_index {527 return Err("item id should be next".into());528 }529 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;530 }531 let data = (0..total_tokens)532 .map(|_| CreateItemData::<T> {533 properties: BoundedVec::default(),534 owner: to.clone(),535 })536 .collect();537538 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)539 .map_err(dispatch_to_evm::<T>)?;540 Ok(true)541 }542543 #[solidity(rename_selector = "mintBulkWithTokenURI")]544 #[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32))]545 fn mint_bulk_with_token_uri(546 &mut self,547 caller: caller,548 to: address,549 tokens: Vec<(uint256, string)>,550 ) -> Result<bool> {551 let key = token_uri_key();552 let caller = T::CrossAccountId::from_eth(caller);553 let to = T::CrossAccountId::from_eth(to);554 let mut expected_index = <TokensMinted<T>>::get(self.id)555 .checked_add(1)556 .ok_or("item id overflow")?;557 let budget = self558 .recorder559 .weight_calls_budget(<StructureWeight<T>>::find_parent());560561 let mut data = Vec::with_capacity(tokens.len());562 for (id, token_uri) in tokens {563 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;564 if id != expected_index {565 return Err("item id should be next".into());566 }567 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;568569 let mut properties = CollectionPropertiesVec::default();570 properties571 .try_push(Property {572 key: key.clone(),573 value: token_uri574 .into_bytes()575 .try_into()576 .map_err(|_| "token uri is too long")?,577 })578 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;579580 data.push(CreateItemData::<T> {581 properties,582 owner: to.clone(),583 });584 }585586 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)587 .map_err(dispatch_to_evm::<T>)?;588 Ok(true)589 }590}591592#[solidity_interface(593 name = "UniqueNFT",594 is(595 ERC721,596 ERC721Metadata,597 ERC721Enumerable,598 ERC721UniqueExtensions,599 ERC721Mintable,600 ERC721Burnable,601 via("CollectionHandle<T>", common_mut, Collection),602 TokenProperties,603 )604)]605impl<T: Config> NonfungibleHandle<T> where T::AccountId: From<[u8; 32]> {}606607// Not a tests, but code generators608generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);609generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);610611impl<T: Config> CommonEvmHandler for NonfungibleHandle<T>612where613 T::AccountId: From<[u8; 32]>,614{615 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");616617 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {618 call::<T, UniqueNFTCall<T>, _, _>(handle, self)619 }620}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617extern crate alloc;18use core::{19 char::{REPLACEMENT_CHARACTER, decode_utf16},20 convert::TryInto,21};22use evm_coder::{ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*, weight};23use frame_support::BoundedVec;24use up_data_structs::{25 TokenId, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey,26 CollectionPropertiesVec,27};28use pallet_evm_coder_substrate::dispatch_to_evm;29use sp_std::vec::Vec;30use pallet_common::{31 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, token_uri_key},32 CollectionHandle, CollectionPropertyPermissions,33};34use pallet_evm::{account::CrossAccountId, PrecompileHandle};35use pallet_evm_coder_substrate::call;36use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};3738use crate::{39 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,40 SelfWeightOf, weights::WeightInfo, TokenProperties,41};4243#[solidity_interface(name = "TokenProperties")]44impl<T: Config> NonfungibleHandle<T> {45 fn set_token_property_permission(46 &mut self,47 caller: caller,48 key: string,49 is_mutable: bool,50 collection_admin: bool,51 token_owner: bool,52 ) -> Result<()> {53 let caller = T::CrossAccountId::from_eth(caller);54 <Pallet<T>>::set_property_permission(55 self,56 &caller,57 PropertyKeyPermission {58 key: <Vec<u8>>::from(key)59 .try_into()60 .map_err(|_| "too long key")?,61 permission: PropertyPermission {62 mutable: is_mutable,63 collection_admin,64 token_owner,65 },66 },67 )68 .map_err(dispatch_to_evm::<T>)69 }7071 fn set_property(72 &mut self,73 caller: caller,74 token_id: uint256,75 key: string,76 value: bytes,77 ) -> Result<()> {78 let caller = T::CrossAccountId::from_eth(caller);79 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;80 let key = <Vec<u8>>::from(key)81 .try_into()82 .map_err(|_| "key too long")?;83 let value = value.try_into().map_err(|_| "value too long")?;8485 let nesting_budget = self86 .recorder87 .weight_calls_budget(<StructureWeight<T>>::find_parent());8889 <Pallet<T>>::set_token_property(90 self,91 &caller,92 TokenId(token_id),93 Property { key, value },94 &nesting_budget,95 )96 .map_err(dispatch_to_evm::<T>)97 }9899 fn delete_property(&mut self, token_id: uint256, caller: caller, key: string) -> Result<()> {100 let caller = T::CrossAccountId::from_eth(caller);101 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;102 let key = <Vec<u8>>::from(key)103 .try_into()104 .map_err(|_| "key too long")?;105106 let nesting_budget = self107 .recorder108 .weight_calls_budget(<StructureWeight<T>>::find_parent());109110 <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)111 .map_err(dispatch_to_evm::<T>)112 }113114 /// Throws error if key not found115 fn property(&self, token_id: uint256, key: string) -> Result<bytes> {116 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;117 let key = <Vec<u8>>::from(key)118 .try_into()119 .map_err(|_| "key too long")?;120121 let props = <TokenProperties<T>>::get((self.id, token_id));122 let prop = props.get(&key).ok_or("key not found")?;123124 Ok(prop.to_vec())125 }126}127128#[derive(ToLog)]129pub enum ERC721Events {130 Transfer {131 #[indexed]132 from: address,133 #[indexed]134 to: address,135 #[indexed]136 token_id: uint256,137 },138 Approval {139 #[indexed]140 owner: address,141 #[indexed]142 approved: address,143 #[indexed]144 token_id: uint256,145 },146 #[allow(dead_code)]147 ApprovalForAll {148 #[indexed]149 owner: address,150 #[indexed]151 operator: address,152 approved: bool,153 },154}155156#[derive(ToLog)]157pub enum ERC721MintableEvents {158 #[allow(dead_code)]159 MintingFinished {},160}161162#[solidity_interface(name = "ERC721Metadata")]163impl<T: Config> NonfungibleHandle<T> {164 fn name(&self) -> Result<string> {165 Ok(decode_utf16(self.name.iter().copied())166 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))167 .collect::<string>())168 }169170 fn symbol(&self) -> Result<string> {171 Ok(string::from_utf8_lossy(&self.token_prefix).into())172 }173174 /// Returns token's const_metadata175 #[solidity(rename_selector = "tokenURI")]176 fn token_uri(&self, token_id: uint256) -> Result<string> {177 let key = token_uri_key();178 if !has_token_permission::<T>(self.id, &key) {179 return Err("No tokenURI permission".into());180 }181182 self.consume_store_reads(1)?;183 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;184185 let properties = <TokenProperties<T>>::try_get((self.id, token_id))186 .map_err(|_| Error::Revert("Token properties not found".into()))?;187 if let Some(property) = properties.get(&key) {188 return Ok(string::from_utf8_lossy(property).into());189 }190191 Err("Property tokenURI not found".into())192 }193}194195#[solidity_interface(name = "ERC721Enumerable")]196impl<T: Config> NonfungibleHandle<T> {197 fn token_by_index(&self, index: uint256) -> Result<uint256> {198 Ok(index)199 }200201 /// Not implemented202 fn token_of_owner_by_index(&self, _owner: address, _index: uint256) -> Result<uint256> {203 // TODO: Not implemetable204 Err("not implemented".into())205 }206207 fn total_supply(&self) -> Result<uint256> {208 self.consume_store_reads(1)?;209 Ok(<Pallet<T>>::total_supply(self).into())210 }211}212213#[solidity_interface(name = "ERC721", events(ERC721Events))]214impl<T: Config> NonfungibleHandle<T> {215 fn balance_of(&self, owner: address) -> Result<uint256> {216 self.consume_store_reads(1)?;217 let owner = T::CrossAccountId::from_eth(owner);218 let balance = <AccountBalance<T>>::get((self.id, owner));219 Ok(balance.into())220 }221 fn owner_of(&self, token_id: uint256) -> Result<address> {222 self.consume_store_reads(1)?;223 let token: TokenId = token_id.try_into()?;224 Ok(*<TokenData<T>>::get((self.id, token))225 .ok_or("token not found")?226 .owner227 .as_eth())228 }229 /// Not implemented230 fn safe_transfer_from_with_data(231 &mut self,232 _from: address,233 _to: address,234 _token_id: uint256,235 _data: bytes,236 _value: value,237 ) -> Result<void> {238 // TODO: Not implemetable239 Err("not implemented".into())240 }241 /// Not implemented242 fn safe_transfer_from(243 &mut self,244 _from: address,245 _to: address,246 _token_id: uint256,247 _value: value,248 ) -> Result<void> {249 // TODO: Not implemetable250 Err("not implemented".into())251 }252253 #[weight(<SelfWeightOf<T>>::transfer_from())]254 fn transfer_from(255 &mut self,256 caller: caller,257 from: address,258 to: address,259 token_id: uint256,260 _value: value,261 ) -> Result<void> {262 let caller = T::CrossAccountId::from_eth(caller);263 let from = T::CrossAccountId::from_eth(from);264 let to = T::CrossAccountId::from_eth(to);265 let token = token_id.try_into()?;266 let budget = self267 .recorder268 .weight_calls_budget(<StructureWeight<T>>::find_parent());269270 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)271 .map_err(dispatch_to_evm::<T>)?;272 Ok(())273 }274275 #[weight(<SelfWeightOf<T>>::approve())]276 fn approve(277 &mut self,278 caller: caller,279 approved: address,280 token_id: uint256,281 _value: value,282 ) -> Result<void> {283 let caller = T::CrossAccountId::from_eth(caller);284 let approved = T::CrossAccountId::from_eth(approved);285 let token = token_id.try_into()?;286287 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))288 .map_err(dispatch_to_evm::<T>)?;289 Ok(())290 }291292 /// Not implemented293 fn set_approval_for_all(294 &mut self,295 _caller: caller,296 _operator: address,297 _approved: bool,298 ) -> Result<void> {299 // TODO: Not implemetable300 Err("not implemented".into())301 }302303 /// Not implemented304 fn get_approved(&self, _token_id: uint256) -> Result<address> {305 // TODO: Not implemetable306 Err("not implemented".into())307 }308309 /// Not implemented310 fn is_approved_for_all(&self, _owner: address, _operator: address) -> Result<address> {311 // TODO: Not implemetable312 Err("not implemented".into())313 }314}315316#[solidity_interface(name = "ERC721Burnable")]317impl<T: Config> NonfungibleHandle<T> {318 #[weight(<SelfWeightOf<T>>::burn_item())]319 fn burn(&mut self, caller: caller, token_id: uint256) -> Result<void> {320 let caller = T::CrossAccountId::from_eth(caller);321 let token = token_id.try_into()?;322323 <Pallet<T>>::burn(self, &caller, token).map_err(dispatch_to_evm::<T>)?;324 Ok(())325 }326}327328#[solidity_interface(name = "ERC721Mintable", events(ERC721MintableEvents))]329impl<T: Config> NonfungibleHandle<T> {330 fn minting_finished(&self) -> Result<bool> {331 Ok(false)332 }333334 /// `token_id` should be obtained with `next_token_id` method,335 /// unlike standard, you can't specify it manually336 #[weight(<SelfWeightOf<T>>::create_item())]337 fn mint(&mut self, caller: caller, to: address, token_id: uint256) -> Result<bool> {338 let caller = T::CrossAccountId::from_eth(caller);339 let to = T::CrossAccountId::from_eth(to);340 let token_id: u32 = token_id.try_into()?;341 let budget = self342 .recorder343 .weight_calls_budget(<StructureWeight<T>>::find_parent());344345 if <TokensMinted<T>>::get(self.id)346 .checked_add(1)347 .ok_or("item id overflow")?348 != token_id349 {350 return Err("item id should be next".into());351 }352353 <Pallet<T>>::create_item(354 self,355 &caller,356 CreateItemData::<T> {357 properties: BoundedVec::default(),358 owner: to,359 },360 &budget,361 )362 .map_err(dispatch_to_evm::<T>)?;363364 Ok(true)365 }366367 /// `token_id` should be obtained with `next_token_id` method,368 /// unlike standard, you can't specify it manually369 #[solidity(rename_selector = "mintWithTokenURI")]370 #[weight(<SelfWeightOf<T>>::create_item())]371 fn mint_with_token_uri(372 &mut self,373 caller: caller,374 to: address,375 token_id: uint256,376 token_uri: string,377 ) -> Result<bool> {378 let key = token_uri_key();379 let permission = get_token_permission::<T>(self.id, &key)?;380 if !permission.collection_admin {381 return Err("Operation is not allowed".into());382 }383384 let caller = T::CrossAccountId::from_eth(caller);385 let to = T::CrossAccountId::from_eth(to);386 let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;387 let budget = self388 .recorder389 .weight_calls_budget(<StructureWeight<T>>::find_parent());390391 if <TokensMinted<T>>::get(self.id)392 .checked_add(1)393 .ok_or("item id overflow")?394 != token_id395 {396 return Err("item id should be next".into());397 }398399 let mut properties = CollectionPropertiesVec::default();400 properties401 .try_push(Property {402 key,403 value: token_uri404 .into_bytes()405 .try_into()406 .map_err(|_| "token uri is too long")?,407 })408 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;409410 <Pallet<T>>::create_item(411 self,412 &caller,413 CreateItemData::<T> {414 properties,415 owner: to,416 },417 &budget,418 )419 .map_err(dispatch_to_evm::<T>)?;420 Ok(true)421 }422423 /// Not implemented424 fn finish_minting(&mut self, _caller: caller) -> Result<bool> {425 Err("not implementable".into())426 }427}428429fn get_token_permission<T: Config>(430 collection_id: CollectionId,431 key: &PropertyKey,432) -> Result<PropertyPermission> {433 let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)434 .map_err(|_| Error::Revert("No permissions for collection".into()))?;435 let a = token_property_permissions436 .get(key)437 .map(|p| p.clone())438 .ok_or_else(|| Error::Revert("No permission".into()))?;439 Ok(a)440}441442fn has_token_permission<T: Config>(collection_id: CollectionId, key: &PropertyKey) -> bool {443 if let Ok(token_property_permissions) =444 CollectionPropertyPermissions::<T>::try_get(collection_id)445 {446 return token_property_permissions.contains_key(key);447 }448449 false450}451452#[solidity_interface(name = "ERC721UniqueExtensions")]453impl<T: Config> NonfungibleHandle<T> {454 #[weight(<SelfWeightOf<T>>::transfer())]455 fn transfer(456 &mut self,457 caller: caller,458 to: address,459 token_id: uint256,460 _value: value,461 ) -> Result<void> {462 let caller = T::CrossAccountId::from_eth(caller);463 let to = T::CrossAccountId::from_eth(to);464 let token = token_id.try_into()?;465 let budget = self466 .recorder467 .weight_calls_budget(<StructureWeight<T>>::find_parent());468469 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;470 Ok(())471 }472473 #[weight(<SelfWeightOf<T>>::burn_from())]474 fn burn_from(475 &mut self,476 caller: caller,477 from: address,478 token_id: uint256,479 _value: value,480 ) -> Result<void> {481 let caller = T::CrossAccountId::from_eth(caller);482 let from = T::CrossAccountId::from_eth(from);483 let token = token_id.try_into()?;484 let budget = self485 .recorder486 .weight_calls_budget(<StructureWeight<T>>::find_parent());487488 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)489 .map_err(dispatch_to_evm::<T>)?;490 Ok(())491 }492493 fn next_token_id(&self) -> Result<uint256> {494 self.consume_store_reads(1)?;495 Ok(<TokensMinted<T>>::get(self.id)496 .checked_add(1)497 .ok_or("item id overflow")?498 .into())499 }500501 #[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]502 fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec<uint256>) -> Result<bool> {503 let caller = T::CrossAccountId::from_eth(caller);504 let to = T::CrossAccountId::from_eth(to);505 let mut expected_index = <TokensMinted<T>>::get(self.id)506 .checked_add(1)507 .ok_or("item id overflow")?;508 let budget = self509 .recorder510 .weight_calls_budget(<StructureWeight<T>>::find_parent());511512 let total_tokens = token_ids.len();513 for id in token_ids.into_iter() {514 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;515 if id != expected_index {516 return Err("item id should be next".into());517 }518 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;519 }520 let data = (0..total_tokens)521 .map(|_| CreateItemData::<T> {522 properties: BoundedVec::default(),523 owner: to.clone(),524 })525 .collect();526527 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)528 .map_err(dispatch_to_evm::<T>)?;529 Ok(true)530 }531532 #[solidity(rename_selector = "mintBulkWithTokenURI")]533 #[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32))]534 fn mint_bulk_with_token_uri(535 &mut self,536 caller: caller,537 to: address,538 tokens: Vec<(uint256, string)>,539 ) -> Result<bool> {540 let key = token_uri_key();541 let caller = T::CrossAccountId::from_eth(caller);542 let to = T::CrossAccountId::from_eth(to);543 let mut expected_index = <TokensMinted<T>>::get(self.id)544 .checked_add(1)545 .ok_or("item id overflow")?;546 let budget = self547 .recorder548 .weight_calls_budget(<StructureWeight<T>>::find_parent());549550 let mut data = Vec::with_capacity(tokens.len());551 for (id, token_uri) in tokens {552 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;553 if id != expected_index {554 return Err("item id should be next".into());555 }556 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;557558 let mut properties = CollectionPropertiesVec::default();559 properties560 .try_push(Property {561 key: key.clone(),562 value: token_uri563 .into_bytes()564 .try_into()565 .map_err(|_| "token uri is too long")?,566 })567 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;568569 data.push(CreateItemData::<T> {570 properties,571 owner: to.clone(),572 });573 }574575 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)576 .map_err(dispatch_to_evm::<T>)?;577 Ok(true)578 }579}580581#[solidity_interface(582 name = "UniqueNFT",583 is(584 ERC721,585 ERC721Metadata,586 ERC721Enumerable,587 ERC721UniqueExtensions,588 ERC721Mintable,589 ERC721Burnable,590 via("CollectionHandle<T>", common_mut, Collection),591 TokenProperties,592 )593)]594impl<T: Config> NonfungibleHandle<T> where T::AccountId: From<[u8; 32]> {}595596// Not a tests, but code generators597generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);598generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);599600impl<T: Config> CommonEvmHandler for NonfungibleHandle<T>601where602 T::AccountId: From<[u8; 32]>,603{604 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");605606 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {607 call::<T, UniqueNFTCall<T>, _, _>(handle, self)608 }609}pallets/nonfungible/src/lib.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -28,7 +28,8 @@
use up_data_structs::{
AccessMode, CollectionId, CustomDataLimit, TokenId, CreateCollectionData, CreateNftExData,
mapping::TokenAddressMapping, budget::Budget, Property, PropertyPermission, PropertyKey,
- PropertyKeyPermission, Properties, PropertyScope, TrySetProperty, TokenChild, AuxPropertyValue,
+ PropertyValue, PropertyKeyPermission, Properties, PropertyScope, TrySetProperty, TokenChild,
+ AuxPropertyValue,
};
use pallet_evm::{account::CrossAccountId, Pallet as PalletEvm};
use pallet_common::{
@@ -51,10 +52,6 @@
pub mod common;
pub mod erc;
pub mod weights;
-
-mod property_guard;
-
-use property_guard::*;
pub type CreateItemData<T> = CreateNftExData<<T as pallet_evm::account::Config>::CrossAccountId>;
pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
@@ -89,6 +86,8 @@
NonfungibleItemsHaveNoAmount,
/// Unable to burn NFT with children
CantBurnNftWithChildren,
+ /// Unable to create an empty property
+ UnableToCreateEmptyProperty,
}
#[pallet::config]
@@ -487,113 +486,152 @@
pays_fee: Pays::Yes,
})
}
-
- pub fn set_token_property(
- property: Property,
- guard: &mut PropertyGuard<'_, T>,
- ) -> DispatchResult {
- Self::check_token_change_permission(&property.key, guard)?;
- <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token_id), |properties| {
- let property = property.clone();
- properties.try_set(property.key, property.value)
- })
- .map_err(<CommonError<T>>::from)?;
-
- <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
- guard.collection.id,
- guard.token_id,
- property.key,
- ));
-
- Ok(())
- }
-
#[transactional]
- pub fn set_token_properties(
+ fn modify_token_properties(
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token_id: TokenId,
- properties: Vec<Property>,
+ properties: impl Iterator<Item = (PropertyKey, Option<PropertyValue>)>,
is_token_create: bool,
nesting_budget: &dyn Budget,
) -> DispatchResult {
- let mut guard = PropertyGuard::new(PropertyGuardData {
- sender,
- collection,
- token_id,
- is_token_create,
- nesting_budget,
- });
+ let mut collection_admin_result = None;
+ let mut token_owner_result = None;
- for property in properties {
- Self::set_token_property(property, &mut guard)?;
- }
+ let mut check_collection_admin = || {
+ *collection_admin_result
+ .get_or_insert_with(|| collection.check_is_owner_or_admin(sender))
+ };
- Ok(())
- }
+ let mut check_token_owner = || {
+ *token_owner_result.get_or_insert_with(|| {
+ let is_owned = <PalletStructure<T>>::check_indirectly_owned(
+ sender.clone(),
+ collection.id,
+ token_id,
+ None,
+ nesting_budget,
+ )?;
- pub fn delete_token_property(
- property_key: PropertyKey,
- guard: &mut PropertyGuard<'_, T>,
- ) -> DispatchResult {
- Self::check_token_change_permission(&property_key, guard)?;
+ if is_owned {
+ Ok(())
+ } else {
+ Err(<CommonError<T>>::NoPermission.into())
+ }
+ })
+ };
- <TokenProperties<T>>::try_mutate((guard.collection.id, guard.token_id), |properties| {
- properties.remove(&property_key)
- })
- .map_err(<CommonError<T>>::from)?;
+ for (key, value) in properties {
+ let permission = <PalletCommon<T>>::property_permissions(collection.id)
+ .get(&key)
+ .cloned()
+ .unwrap_or_else(PropertyPermission::none);
- <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
- guard.collection.id,
- guard.token_id,
- property_key,
- ));
+ let is_property_exists = TokenProperties::<T>::get((collection.id, token_id))
+ .get(&key)
+ .is_some();
- Ok(())
- }
+ match permission {
+ PropertyPermission { mutable: false, .. } if is_property_exists => {
+ return Err(<CommonError<T>>::NoPermission.into());
+ }
- fn check_token_change_permission(
- property_key: &PropertyKey,
- guard: &mut PropertyGuard<'_, T>,
- ) -> DispatchResult {
- let permission = <PalletCommon<T>>::property_permissions(guard.collection.id)
- .get(property_key)
- .cloned()
- .unwrap_or_else(PropertyPermission::none);
+ PropertyPermission {
+ collection_admin,
+ token_owner,
+ ..
+ } => {
+ //TODO: investigate threats during public minting.
+ if is_token_create && (collection_admin || token_owner) {
+ if value.is_some() {
+ return Ok(());
+ } else {
+ return Err(<Error<T>>::UnableToCreateEmptyProperty.into());
+ }
+ }
- let is_property_exists = TokenProperties::<T>::get((guard.collection.id, guard.token_id))
- .get(property_key)
- .is_some();
+ let mut check_result = Err(<CommonError<T>>::NoPermission.into());
- match permission {
- PropertyPermission { mutable: false, .. } if is_property_exists => {
- Err(<CommonError<T>>::NoPermission.into())
- }
+ if collection_admin {
+ check_result = check_collection_admin();
+ }
- PropertyPermission {
- collection_admin,
- token_owner,
- ..
- } => {
- //TODO: investigate threats during public minting.
- if guard.is_token_create && (collection_admin || token_owner) {
- return Ok(());
+ if token_owner {
+ check_result = check_result.or_else(|_| check_token_owner())
+ }
+
+ check_result?;
}
+ }
- let mut check_result = Err(<CommonError<T>>::NoPermission.into());
+ match value {
+ Some(value) => {
+ <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
+ properties.try_set(key.clone(), value)
+ })
+ .map_err(<CommonError<T>>::from)?;
- if collection_admin {
- check_result = guard.check_collection_admin();
+ <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertySet(
+ collection.id,
+ token_id,
+ key,
+ ));
}
+ None => {
+ <TokenProperties<T>>::try_mutate((collection.id, token_id), |properties| {
+ properties.remove(&key)
+ })
+ .map_err(<CommonError<T>>::from)?;
- if token_owner {
- check_result.or_else(|_| guard.check_token_owner())
- } else {
- check_result
+ <PalletCommon<T>>::deposit_event(CommonEvent::TokenPropertyDeleted(
+ collection.id,
+ token_id,
+ key,
+ ));
}
}
}
+
+ Ok(())
+ }
+
+ #[transactional]
+ pub fn set_token_properties(
+ collection: &NonfungibleHandle<T>,
+ sender: &T::CrossAccountId,
+ token_id: TokenId,
+ properties: impl Iterator<Item = Property>,
+ is_token_create: bool,
+ nesting_budget: &dyn Budget,
+ ) -> DispatchResult {
+ Self::modify_token_properties(
+ collection,
+ sender,
+ token_id,
+ properties.map(|p| (p.key, Some(p.value))),
+ is_token_create,
+ nesting_budget,
+ )
+ }
+
+ pub fn set_token_property(
+ collection: &NonfungibleHandle<T>,
+ sender: &T::CrossAccountId,
+ token_id: TokenId,
+ property: Property,
+ nesting_budget: &dyn Budget,
+ ) -> DispatchResult {
+ let is_token_create = false;
+
+ Self::set_token_properties(
+ collection,
+ sender,
+ token_id,
+ [property].into_iter(),
+ is_token_create,
+ nesting_budget,
+ )
}
#[transactional]
@@ -601,24 +639,35 @@
collection: &NonfungibleHandle<T>,
sender: &T::CrossAccountId,
token_id: TokenId,
- property_keys: Vec<PropertyKey>,
+ property_keys: impl Iterator<Item = PropertyKey>,
nesting_budget: &dyn Budget,
) -> DispatchResult {
let is_token_create = false;
- let mut guard = PropertyGuard::new(PropertyGuardData {
+ Self::modify_token_properties(
+ collection,
sender,
- collection,
token_id,
+ property_keys.into_iter().map(|key| (key, None)),
is_token_create,
nesting_budget,
- });
+ )
+ }
- for key in property_keys {
- Self::delete_token_property(key, &mut guard)?;
- }
-
- Ok(())
+ pub fn delete_token_property(
+ collection: &NonfungibleHandle<T>,
+ sender: &T::CrossAccountId,
+ token_id: TokenId,
+ property_key: PropertyKey,
+ nesting_budget: &dyn Budget,
+ ) -> DispatchResult {
+ Self::delete_token_properties(
+ collection,
+ sender,
+ token_id,
+ [property_key].into_iter(),
+ nesting_budget,
+ )
}
pub fn set_collection_properties(
@@ -829,7 +878,7 @@
collection,
sender,
TokenId(token),
- data.properties.clone().into_inner(),
+ data.properties.clone().into_iter(),
true,
nesting_budget,
) {
pallets/nonfungible/src/property_guard.rsdiffbeforeafterboth--- a/pallets/nonfungible/src/property_guard.rs
+++ /dev/null
@@ -1,59 +0,0 @@
-use super::*;
-
-pub struct PropertyGuard<'a, T: Config> {
- pub sender: &'a T::CrossAccountId,
- pub collection: &'a NonfungibleHandle<T>,
- pub token_id: TokenId,
- pub is_token_create: bool,
- nesting_budget: &'a dyn Budget,
-
- collection_admin_result: Option<DispatchResult>,
- token_owner_result: Option<DispatchResult>,
-}
-
-pub struct PropertyGuardData<'a, T: Config> {
- pub sender: &'a T::CrossAccountId,
- pub collection: &'a NonfungibleHandle<T>,
- pub token_id: TokenId,
- pub is_token_create: bool,
- pub nesting_budget: &'a dyn Budget,
-}
-
-impl<'a, T: Config> PropertyGuard<'a, T> {
- pub fn new(data: PropertyGuardData<'a, T>) -> Self {
- Self {
- sender: data.sender,
- collection: data.collection,
- token_id: data.token_id,
- is_token_create: data.is_token_create,
- nesting_budget: data.nesting_budget,
-
- collection_admin_result: None,
- token_owner_result: None,
- }
- }
-
- pub fn check_collection_admin(&mut self) -> DispatchResult {
- *self
- .collection_admin_result
- .get_or_insert_with(|| self.collection.check_is_owner_or_admin(self.sender))
- }
-
- pub fn check_token_owner(&mut self) -> DispatchResult {
- *self.token_owner_result.get_or_insert_with(|| {
- let is_owned = <PalletStructure<T>>::check_indirectly_owned(
- self.sender.clone(),
- self.collection.id,
- self.token_id,
- None,
- self.nesting_budget,
- )?;
-
- if is_owned {
- Ok(())
- } else {
- Err(<CommonError<T>>::NoPermission.into())
- }
- })
- }
-}