difftreelog
refac: rename address -> Address
in: master
14 files changed
crates/evm-coder/procedural/src/to_log.rsdiffbeforeafterboth--- a/crates/evm-coder/procedural/src/to_log.rs
+++ b/crates/evm-coder/procedural/src/to_log.rs
@@ -231,7 +231,7 @@
#[automatically_derived]
impl ::evm_coder::events::ToLog for #name {
- fn to_log(&self, contract: address) -> ::ethereum::Log {
+ fn to_log(&self, contract: Address) -> ::ethereum::Log {
use ::evm_coder::events::ToTopic;
use ::evm_coder::abi::AbiWrite;
let mut writer = ::evm_coder::abi::AbiWriter::new();
crates/evm-coder/src/abi/test.rsdiffbeforeafterboth--- a/crates/evm-coder/src/abi/test.rs
+++ b/crates/evm-coder/src/abi/test.rs
@@ -101,7 +101,7 @@
#[test]
fn encode_decode_vec_tuple_address_uint256() {
- test_impl::<Vec<(address, U256)>>(
+ test_impl::<Vec<(Address, U256)>>(
0x1ACF2D55,
vec![
(
crates/evm-coder/src/events.rsdiffbeforeafterboth--- a/crates/evm-coder/src/events.rs
+++ b/crates/evm-coder/src/events.rs
@@ -53,7 +53,7 @@
}
}
-impl ToTopic for address {
+impl ToTopic for Address {
fn to_topic(&self) -> H256 {
let mut out = [0u8; 32];
out[12..32].copy_from_slice(&self.0);
crates/evm-coder/src/lib.rsdiffbeforeafterboth--- a/crates/evm-coder/src/lib.rs
+++ b/crates/evm-coder/src/lib.rs
@@ -131,7 +131,7 @@
use alloc::{vec::Vec};
use primitive_types::{U256, H160, H256};
- pub type address = H160;
+ pub type Address = H160;
pub type bytes4 = [u8; 4];
pub type topic = H256;
@@ -147,7 +147,7 @@
/// Makes function payable
pub type value = U256;
/// Makes function caller-sensitive
- pub type caller = address;
+ pub type caller = Address;
//#endregion
/// Ethereum typed call message, similar to solidity
crates/evm-coder/tests/log.rsdiffbeforeafterboth--- a/crates/evm-coder/tests/log.rs
+++ b/crates/evm-coder/tests/log.rs
@@ -23,14 +23,14 @@
enum ERC721Log {
Transfer {
#[indexed]
- from: address,
+ from: Address,
#[indexed]
- to: address,
+ to: Address,
value: U256,
},
Eee {
#[indexed]
- aaa: address,
+ aaa: Address,
bbb: U256,
},
}
crates/evm-coder/tests/solidity_generation.rsdiffbeforeafterboth--- a/crates/evm-coder/tests/solidity_generation.rs
+++ b/crates/evm-coder/tests/solidity_generation.rs
@@ -25,25 +25,25 @@
unreachable!()
}
/// Get balance of specified owner
- fn balance_of(&self, _owner: address) -> Result<U256> {
+ fn balance_of(&self, _owner: Address) -> Result<U256> {
unreachable!()
}
- fn transfer(&mut self, _caller: caller, _to: address, _value: U256) -> Result<bool> {
+ fn transfer(&mut self, _caller: caller, _to: Address, _value: U256) -> Result<bool> {
unreachable!()
}
fn transfer_from(
&mut self,
_caller: caller,
- _from: address,
- _to: address,
+ _from: Address,
+ _to: Address,
_value: U256,
) -> Result<bool> {
unreachable!()
}
- fn approve(&mut self, _caller: caller, _spender: address, _value: U256) -> Result<bool> {
+ fn approve(&mut self, _caller: caller, _spender: Address, _value: U256) -> Result<bool> {
unreachable!()
}
- fn allowance(&self, _owner: address, _spender: address) -> Result<U256> {
+ fn allowance(&self, _owner: Address, _spender: Address) -> Result<U256> {
unreachable!()
}
}
pallets/common/src/erc.rsdiffbeforeafterboth--- a/pallets/common/src/erc.rs
+++ b/pallets/common/src/erc.rs
@@ -43,30 +43,30 @@
CollectionCreated {
/// Collection owner.
#[indexed]
- owner: address,
+ owner: Address,
/// Collection ID.
#[indexed]
- collection_id: address,
+ collection_id: Address,
},
/// The collection has been destroyed.
CollectionDestroyed {
/// Collection ID.
#[indexed]
- collection_id: address,
+ collection_id: Address,
},
/// The collection has been changed.
CollectionChanged {
/// Collection ID.
#[indexed]
- collection_id: address,
+ collection_id: Address,
},
/// The token has been changed.
TokenChanged {
/// Collection ID.
#[indexed]
- collection_id: address,
+ collection_id: Address,
/// Token ID.
token_id: U256,
},
@@ -208,7 +208,7 @@
///
/// @param sponsor Address of the sponsor from whose account funds will be debited for operations with the contract.
#[solidity(hide)]
- fn set_collection_sponsor(&mut self, caller: caller, sponsor: address) -> Result<()> {
+ fn set_collection_sponsor(&mut self, caller: caller, sponsor: Address) -> Result<()> {
self.consume_store_reads_and_writes(1, 1)?;
let caller = T::CrossAccountId::from_eth(caller);
@@ -351,7 +351,7 @@
}
/// Get contract address.
- fn contract_address(&self) -> Result<address> {
+ fn contract_address(&self) -> Result<Address> {
Ok(crate::eth::collection_id_to_address(self.id))
}
@@ -388,7 +388,7 @@
/// Add collection admin.
/// @param newAdmin Address of the added administrator.
#[solidity(hide)]
- fn add_collection_admin(&mut self, caller: caller, new_admin: address) -> Result<()> {
+ fn add_collection_admin(&mut self, caller: caller, new_admin: Address) -> Result<()> {
self.consume_store_reads_and_writes(2, 2)?;
let caller = T::CrossAccountId::from_eth(caller);
@@ -401,7 +401,7 @@
///
/// @param admin Address of the removed administrator.
#[solidity(hide)]
- fn remove_collection_admin(&mut self, caller: caller, admin: address) -> Result<()> {
+ fn remove_collection_admin(&mut self, caller: caller, admin: Address) -> Result<()> {
self.consume_store_reads_and_writes(2, 2)?;
let caller = T::CrossAccountId::from_eth(caller);
@@ -437,7 +437,7 @@
&mut self,
caller: caller,
enable: bool,
- collections: Vec<address>,
+ collections: Vec<Address>,
) -> Result<()> {
self.consume_store_reads_and_writes(1, 1)?;
@@ -526,7 +526,7 @@
///
/// @param user Address of a trusted user.
#[solidity(hide)]
- fn add_to_collection_allow_list(&mut self, caller: caller, user: address) -> Result<()> {
+ fn add_to_collection_allow_list(&mut self, caller: caller, user: Address) -> Result<()> {
self.consume_store_writes(1)?;
let caller = T::CrossAccountId::from_eth(caller);
@@ -555,7 +555,7 @@
///
/// @param user Address of a removed user.
#[solidity(hide)]
- fn remove_from_collection_allow_list(&mut self, caller: caller, user: address) -> Result<()> {
+ fn remove_from_collection_allow_list(&mut self, caller: caller, user: Address) -> Result<()> {
self.consume_store_writes(1)?;
let caller = T::CrossAccountId::from_eth(caller);
@@ -599,7 +599,7 @@
/// @param user account to verify
/// @return "true" if account is the owner or admin
#[solidity(hide, rename_selector = "isOwnerOrAdmin")]
- fn is_owner_or_admin_eth(&self, user: address) -> Result<bool> {
+ fn is_owner_or_admin_eth(&self, user: Address) -> Result<bool> {
let user = T::CrossAccountId::from_eth(user);
Ok(self.is_owner_or_admin(&user))
}
@@ -640,7 +640,7 @@
/// @dev Owner can be changed only by current owner
/// @param newOwner new owner account
#[solidity(hide, rename_selector = "changeCollectionOwner")]
- fn set_owner(&mut self, caller: caller, new_owner: address) -> Result<()> {
+ fn set_owner(&mut self, caller: caller, new_owner: Address) -> Result<()> {
self.consume_store_writes(1)?;
let caller = T::CrossAccountId::from_eth(caller);
pallets/common/src/eth.rsdiffbeforeafterboth--- a/pallets/common/src/eth.rs
+++ b/pallets/common/src/eth.rs
@@ -18,7 +18,7 @@
use alloc::format;
use sp_std::{vec, vec::Vec};
-use evm_coder::{AbiCoder, types::address};
+use evm_coder::{AbiCoder, types::Address};
pub use pallet_evm::{Config, account::CrossAccountId};
use sp_core::{H160, U256};
use up_data_structs::CollectionId;
@@ -30,7 +30,7 @@
];
/// Maps the ethereum address of the collection in substrate.
-pub fn map_eth_to_id(eth: &H160) -> Option<CollectionId> {
+pub fn map_eth_to_id(eth: &Address) -> Option<CollectionId> {
if eth[0..16] != ETH_COLLECTION_PREFIX {
return None;
}
@@ -40,7 +40,7 @@
}
/// Maps the substrate collection id in ethereum.
-pub fn collection_id_to_address(id: CollectionId) -> H160 {
+pub fn collection_id_to_address(id: CollectionId) -> Address {
let mut out = [0; 20];
out[0..16].copy_from_slice(Ð_COLLECTION_PREFIX);
out[16..20].copy_from_slice(&u32::to_be_bytes(id.0));
@@ -48,7 +48,7 @@
}
/// Check if the ethereum address is a collection.
-pub fn is_collection(address: &H160) -> bool {
+pub fn is_collection(address: &Address) -> bool {
address[0..16] == ETH_COLLECTION_PREFIX
}
@@ -66,7 +66,7 @@
/// Cross account struct
#[derive(Debug, Default, AbiCoder)]
pub struct CrossAddress {
- pub(crate) eth: address,
+ pub(crate) eth: Address,
pub(crate) sub: U256,
}
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/eth.rs
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -49,25 +49,25 @@
ContractSponsorSet {
/// Contract address of the affected collection.
#[indexed]
- contract_address: address,
+ contract_address: Address,
/// New sponsor address.
- sponsor: address,
+ sponsor: Address,
},
/// New sponsor was confirm.
ContractSponsorshipConfirmed {
/// Contract address of the affected collection.
#[indexed]
- contract_address: address,
+ contract_address: Address,
/// New sponsor address.
- sponsor: address,
+ sponsor: Address,
},
/// Collection sponsor was removed.
ContractSponsorRemoved {
/// Contract address of the affected collection.
#[indexed]
- contract_address: address,
+ contract_address: Address,
},
}
@@ -96,7 +96,7 @@
/// @dev Returns zero address if contract does not exists
/// @param contractAddress Contract to get owner of
/// @return address Owner of contract
- fn contract_owner(&self, contract_address: address) -> Result<address> {
+ fn contract_owner(&self, contract_address: Address) -> Result<Address> {
Ok(<Owner<T>>::get(contract_address))
}
@@ -106,8 +106,8 @@
fn set_sponsor(
&mut self,
caller: caller,
- contract_address: address,
- sponsor: address,
+ contract_address: Address,
+ sponsor: Address,
) -> Result<()> {
self.recorder().consume_sload()?;
self.recorder().consume_sstore()?;
@@ -125,7 +125,7 @@
/// Set contract as self sponsored.
///
/// @param contractAddress Contract for which a self sponsoring is being enabled.
- fn self_sponsored_enable(&mut self, caller: caller, contract_address: address) -> Result<()> {
+ fn self_sponsored_enable(&mut self, caller: caller, contract_address: Address) -> Result<()> {
self.recorder().consume_sload()?;
self.recorder().consume_sstore()?;
@@ -146,7 +146,7 @@
/// Remove sponsor.
///
/// @param contractAddress Contract for which a sponsorship is being removed.
- fn remove_sponsor(&mut self, caller: caller, contract_address: address) -> Result<()> {
+ fn remove_sponsor(&mut self, caller: caller, contract_address: Address) -> Result<()> {
self.recorder().consume_sload()?;
self.recorder().consume_sstore()?;
@@ -161,7 +161,7 @@
/// @dev Caller must be same that set via [`setSponsor`].
///
/// @param contractAddress Сontract for which need to confirm sponsorship.
- fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<()> {
+ fn confirm_sponsorship(&mut self, caller: caller, contract_address: Address) -> Result<()> {
self.recorder().consume_sload()?;
self.recorder().consume_sstore()?;
@@ -175,7 +175,7 @@
///
/// @param contractAddress The contract for which a sponsor is requested.
/// @return Tuble with sponsor address and his substrate mirror. If there is no confirmed sponsor error "Contract has no sponsor" throw.
- fn sponsor(&self, contract_address: address) -> Result<Option<eth::CrossAddress>> {
+ fn sponsor(&self, contract_address: Address) -> Result<Option<eth::CrossAddress>> {
Ok(match Pallet::<T>::get_sponsor(contract_address) {
Some(ref value) => Some(eth::CrossAddress::from_sub_cross_account::<T>(value)),
None => None,
@@ -186,7 +186,7 @@
///
/// @param contractAddress The contract for which the presence of a confirmed sponsor is checked.
/// @return **true** if contract has confirmed sponsor.
- fn has_sponsor(&self, contract_address: address) -> Result<bool> {
+ fn has_sponsor(&self, contract_address: Address) -> Result<bool> {
Ok(Pallet::<T>::get_sponsor(contract_address).is_some())
}
@@ -194,21 +194,21 @@
///
/// @param contractAddress The contract for which the presence of a pending sponsor is checked.
/// @return **true** if contract has pending sponsor.
- fn has_pending_sponsor(&self, contract_address: address) -> Result<bool> {
+ fn has_pending_sponsor(&self, contract_address: Address) -> Result<bool> {
Ok(match Sponsoring::<T>::get(contract_address) {
SponsorshipState::Disabled | SponsorshipState::Confirmed(_) => false,
SponsorshipState::Unconfirmed(_) => true,
})
}
- fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {
+ fn sponsoring_enabled(&self, contract_address: Address) -> Result<bool> {
Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)
}
fn set_sponsoring_mode(
&mut self,
caller: caller,
- contract_address: address,
+ contract_address: Address,
mode: SponsoringModeT,
) -> Result<()> {
self.recorder().consume_sload()?;
@@ -223,7 +223,7 @@
/// Get current contract sponsoring rate limit
/// @param contractAddress Contract to get sponsoring rate limit of
/// @return uint32 Amount of blocks between two sponsored transactions
- fn sponsoring_rate_limit(&self, contract_address: address) -> Result<u32> {
+ fn sponsoring_rate_limit(&self, contract_address: Address) -> Result<u32> {
self.recorder().consume_sload()?;
Ok(<SponsoringRateLimit<T>>::get(contract_address)
@@ -240,7 +240,7 @@
fn set_sponsoring_rate_limit(
&mut self,
caller: caller,
- contract_address: address,
+ contract_address: Address,
rate_limit: u32,
) -> Result<()> {
self.recorder().consume_sload()?;
@@ -260,7 +260,7 @@
fn set_sponsoring_fee_limit(
&mut self,
caller: caller,
- contract_address: address,
+ contract_address: Address,
fee_limit: U256,
) -> Result<()> {
self.recorder().consume_sload()?;
@@ -276,7 +276,7 @@
/// @param contractAddress Contract to get sponsoring fee limit of
/// @return uint256 Maximum amount of fee that could be spent by single
/// transaction
- fn sponsoring_fee_limit(&self, contract_address: address) -> Result<U256> {
+ fn sponsoring_fee_limit(&self, contract_address: Address) -> Result<U256> {
self.recorder().consume_sload()?;
Ok(get_sponsoring_fee_limit::<T>(contract_address))
@@ -287,7 +287,7 @@
/// @param contractAddress Contract to check allowlist of
/// @param user User to check
/// @return bool Is specified users exists in contract allowlist
- fn allowed(&self, contract_address: address, user: address) -> Result<bool> {
+ fn allowed(&self, contract_address: Address, user: Address) -> Result<bool> {
self.0.consume_sload()?;
Ok(<Pallet<T>>::allowed(contract_address, user))
}
@@ -301,8 +301,8 @@
fn toggle_allowed(
&mut self,
caller: caller,
- contract_address: address,
- user: address,
+ contract_address: Address,
+ user: Address,
is_allowed: bool,
) -> Result<()> {
self.recorder().consume_sload()?;
@@ -320,7 +320,7 @@
/// in case of allowlist access enabled, only users from allowlist may call this contract
/// @param contractAddress Contract to get allowlist access of
/// @return bool Is specified contract has allowlist access enabled
- fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {
+ fn allowlist_enabled(&self, contract_address: Address) -> Result<bool> {
Ok(<AllowlistEnabled<T>>::get(contract_address))
}
@@ -330,7 +330,7 @@
fn toggle_allowlist(
&mut self,
caller: caller,
- contract_address: address,
+ contract_address: Address,
enabled: bool,
) -> Result<()> {
self.recorder().consume_sload()?;
@@ -441,7 +441,7 @@
}
}
-fn get_sponsoring_fee_limit<T: Config>(contract_address: address) -> U256 {
+fn get_sponsoring_fee_limit<T: Config>(contract_address: Address) -> U256 {
<SponsoringFeeLimit<T>>::get(contract_address)
.get(&0xffffffff)
.cloned()
pallets/fungible/src/erc.rsdiffbeforeafterboth--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -43,16 +43,16 @@
pub enum ERC20Events {
Transfer {
#[indexed]
- from: address,
+ from: Address,
#[indexed]
- to: address,
+ to: Address,
value: U256,
},
Approval {
#[indexed]
- owner: address,
+ owner: Address,
#[indexed]
- spender: address,
+ spender: Address,
value: U256,
},
}
@@ -79,14 +79,14 @@
unreachable!()
})
}
- fn balance_of(&self, owner: address) -> Result<U256> {
+ fn balance_of(&self, owner: Address) -> Result<U256> {
self.consume_store_reads(1)?;
let owner = T::CrossAccountId::from_eth(owner);
let balance = <Balance<T>>::get((self.id, owner));
Ok(balance.into())
}
#[weight(<SelfWeightOf<T>>::transfer())]
- fn transfer(&mut self, caller: caller, to: address, amount: U256) -> Result<bool> {
+ fn transfer(&mut self, caller: caller, to: Address, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
@@ -102,8 +102,8 @@
fn transfer_from(
&mut self,
caller: caller,
- from: address,
- to: address,
+ from: Address,
+ to: Address,
amount: U256,
) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
@@ -119,7 +119,7 @@
Ok(true)
}
#[weight(<SelfWeightOf<T>>::approve())]
- fn approve(&mut self, caller: caller, spender: address, amount: U256) -> Result<bool> {
+ fn approve(&mut self, caller: caller, spender: Address, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let spender = T::CrossAccountId::from_eth(spender);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
@@ -128,7 +128,7 @@
.map_err(dispatch_to_evm::<T>)?;
Ok(true)
}
- fn allowance(&self, owner: address, spender: address) -> Result<U256> {
+ fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {
self.consume_store_reads(1)?;
let owner = T::CrossAccountId::from_eth(owner);
let spender = T::CrossAccountId::from_eth(spender);
@@ -137,7 +137,7 @@
}
/// @notice Returns collection helper contract address
- fn collection_helper_address(&self) -> Result<address> {
+ fn collection_helper_address(&self) -> Result<Address> {
Ok(T::ContractAddress::get())
}
}
@@ -148,7 +148,7 @@
/// @param to account that will receive minted tokens
/// @param amount amount of tokens to mint
#[weight(<SelfWeightOf<T>>::create_item())]
- fn mint(&mut self, caller: caller, to: address, amount: U256) -> Result<bool> {
+ fn mint(&mut self, caller: caller, to: Address, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
@@ -214,7 +214,7 @@
/// @param amount The amount that will be burnt.
#[solidity(hide)]
#[weight(<SelfWeightOf<T>>::burn_from())]
- fn burn_from(&mut self, caller: caller, from: address, amount: U256) -> Result<bool> {
+ fn burn_from(&mut self, caller: caller, from: Address, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let from = T::CrossAccountId::from_eth(from);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
@@ -254,7 +254,7 @@
/// Mint tokens for multiple accounts.
/// @param amounts array of pairs of account address and amount
#[weight(<SelfWeightOf<T>>::create_multiple_items_ex(amounts.len() as u32))]
- fn mint_bulk(&mut self, caller: caller, amounts: Vec<(address, U256)>) -> Result<bool> {
+ fn mint_bulk(&mut self, caller: caller, amounts: Vec<(Address, U256)>) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let budget = self
.recorder
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/>.1617//! # Nonfungible Pallet EVM API18//!19//! Provides ERC-721 standart support implementation and EVM API for unique extensions for Nonfungible Pallet.20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.2122extern crate alloc;23use core::{24 char::{REPLACEMENT_CHARACTER, decode_utf16},25 convert::TryInto,26};27use evm_coder::{28 abi::AbiType, ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*,29 weight,30};31use frame_support::BoundedVec;32use up_data_structs::{33 TokenId, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey,34 CollectionPropertiesVec,35};36use pallet_evm_coder_substrate::dispatch_to_evm;37use sp_std::{vec::Vec, vec};38use pallet_common::{39 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,40 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},41 eth,42};43use pallet_evm::{account::CrossAccountId, PrecompileHandle};44use pallet_evm_coder_substrate::call;45use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};46use sp_core::{U256, Get};4748use crate::{49 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,50 SelfWeightOf, weights::WeightInfo, TokenProperties,51};5253/// @title A contract that allows to set and delete token properties and change token property permissions.54#[solidity_interface(name = TokenProperties)]55impl<T: Config> NonfungibleHandle<T> {56 /// @notice Set permissions for token property.57 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.58 /// @param key Property key.59 /// @param isMutable Permission to mutate property.60 /// @param collectionAdmin Permission to mutate property by collection admin if property is mutable.61 /// @param tokenOwner Permission to mutate property by token owner if property is mutable.62 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(1))]63 #[solidity(hide)]64 fn set_token_property_permission(65 &mut self,66 caller: caller,67 key: string,68 is_mutable: bool,69 collection_admin: bool,70 token_owner: bool,71 ) -> Result<()> {72 let caller = T::CrossAccountId::from_eth(caller);73 <Pallet<T>>::set_token_property_permissions(74 self,75 &caller,76 vec![PropertyKeyPermission {77 key: <Vec<u8>>::from(key)78 .try_into()79 .map_err(|_| "too long key")?,80 permission: PropertyPermission {81 mutable: is_mutable,82 collection_admin,83 token_owner,84 },85 }],86 )87 .map_err(dispatch_to_evm::<T>)88 }8990 /// @notice Set permissions for token property.91 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.92 /// @param permissions Permissions for keys.93 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(permissions.len() as u32))]94 fn set_token_property_permissions(95 &mut self,96 caller: caller,97 permissions: Vec<eth::TokenPropertyPermission>,98 ) -> Result<()> {99 let caller = T::CrossAccountId::from_eth(caller);100 let perms = eth::TokenPropertyPermission::into_property_key_permissions(permissions)?;101102 <Pallet<T>>::set_token_property_permissions(self, &caller, perms)103 .map_err(dispatch_to_evm::<T>)104 }105106 /// @notice Get permissions for token properties.107 fn token_property_permissions(&self) -> Result<Vec<eth::TokenPropertyPermission>> {108 let perms = <Pallet<T>>::token_property_permission(self.id);109 Ok(perms110 .into_iter()111 .map(eth::TokenPropertyPermission::from)112 .collect())113 }114115 /// @notice Set token property value.116 /// @dev Throws error if `msg.sender` has no permission to edit the property.117 /// @param tokenId ID of the token.118 /// @param key Property key.119 /// @param value Property value.120 #[solidity(hide)]121 #[weight(<SelfWeightOf<T>>::set_token_properties(1))]122 fn set_property(123 &mut self,124 caller: caller,125 token_id: U256,126 key: string,127 value: bytes,128 ) -> Result<()> {129 let caller = T::CrossAccountId::from_eth(caller);130 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;131 let key = <Vec<u8>>::from(key)132 .try_into()133 .map_err(|_| "key too long")?;134 let value = value.0.try_into().map_err(|_| "value too long")?;135136 let nesting_budget = self137 .recorder138 .weight_calls_budget(<StructureWeight<T>>::find_parent());139140 <Pallet<T>>::set_token_property(141 self,142 &caller,143 TokenId(token_id),144 Property { key, value },145 &nesting_budget,146 )147 .map_err(dispatch_to_evm::<T>)148 }149150 /// @notice Set token properties value.151 /// @dev Throws error if `msg.sender` has no permission to edit the property.152 /// @param tokenId ID of the token.153 /// @param properties settable properties154 #[weight(<SelfWeightOf<T>>::set_token_properties(properties.len() as u32))]155 fn set_properties(156 &mut self,157 caller: caller,158 token_id: U256,159 properties: Vec<eth::Property>,160 ) -> Result<()> {161 let caller = T::CrossAccountId::from_eth(caller);162 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;163164 let nesting_budget = self165 .recorder166 .weight_calls_budget(<StructureWeight<T>>::find_parent());167168 let properties = properties169 .into_iter()170 .map(eth::Property::try_into)171 .collect::<Result<Vec<_>>>()?;172173 <Pallet<T>>::set_token_properties(174 self,175 &caller,176 TokenId(token_id),177 properties.into_iter(),178 false,179 &nesting_budget,180 )181 .map_err(dispatch_to_evm::<T>)182 }183184 /// @notice Delete token property value.185 /// @dev Throws error if `msg.sender` has no permission to edit the property.186 /// @param tokenId ID of the token.187 /// @param key Property key.188 #[solidity(hide)]189 #[weight(<SelfWeightOf<T>>::delete_token_properties(1))]190 fn delete_property(&mut self, token_id: U256, caller: caller, key: string) -> Result<()> {191 let caller = T::CrossAccountId::from_eth(caller);192 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;193 let key = <Vec<u8>>::from(key)194 .try_into()195 .map_err(|_| "key too long")?;196197 let nesting_budget = self198 .recorder199 .weight_calls_budget(<StructureWeight<T>>::find_parent());200201 <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)202 .map_err(dispatch_to_evm::<T>)203 }204205 /// @notice Delete token properties value.206 /// @dev Throws error if `msg.sender` has no permission to edit the property.207 /// @param tokenId ID of the token.208 /// @param keys Properties key.209 #[weight(<SelfWeightOf<T>>::delete_token_properties(keys.len() as u32))]210 fn delete_properties(211 &mut self,212 token_id: U256,213 caller: caller,214 keys: Vec<string>,215 ) -> Result<()> {216 let caller = T::CrossAccountId::from_eth(caller);217 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;218 let keys = keys219 .into_iter()220 .map(|k| Ok(<Vec<u8>>::from(k).try_into().map_err(|_| "key too long")?))221 .collect::<Result<Vec<_>>>()?;222223 let nesting_budget = self224 .recorder225 .weight_calls_budget(<StructureWeight<T>>::find_parent());226227 <Pallet<T>>::delete_token_properties(228 self,229 &caller,230 TokenId(token_id),231 keys.into_iter(),232 &nesting_budget,233 )234 .map_err(dispatch_to_evm::<T>)235 }236237 /// @notice Get token property value.238 /// @dev Throws error if key not found239 /// @param tokenId ID of the token.240 /// @param key Property key.241 /// @return Property value bytes242 fn property(&self, token_id: U256, key: string) -> Result<bytes> {243 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;244 let key = <Vec<u8>>::from(key)245 .try_into()246 .map_err(|_| "key too long")?;247248 let props = <TokenProperties<T>>::get((self.id, token_id));249 let prop = props.get(&key).ok_or("key not found")?;250251 Ok(prop.to_vec().into())252 }253}254255#[derive(ToLog)]256pub enum ERC721Events {257 /// @dev This emits when ownership of any NFT changes by any mechanism.258 /// This event emits when NFTs are created (`from` == 0) and destroyed259 /// (`to` == 0). Exception: during contract creation, any number of NFTs260 /// may be created and assigned without emitting Transfer. At the time of261 /// any transfer, the approved address for that NFT (if any) is reset to none.262 Transfer {263 #[indexed]264 from: address,265 #[indexed]266 to: address,267 #[indexed]268 token_id: U256,269 },270 /// @dev This emits when the approved address for an NFT is changed or271 /// reaffirmed. The zero address indicates there is no approved address.272 /// When a Transfer event emits, this also indicates that the approved273 /// address for that NFT (if any) is reset to none.274 Approval {275 #[indexed]276 owner: address,277 #[indexed]278 approved: address,279 #[indexed]280 token_id: U256,281 },282 /// @dev This emits when an operator is enabled or disabled for an owner.283 /// The operator can manage all NFTs of the owner.284 #[allow(dead_code)]285 ApprovalForAll {286 #[indexed]287 owner: address,288 #[indexed]289 operator: address,290 approved: bool,291 },292}293294/// @title ERC-721 Non-Fungible Token Standard, optional metadata extension295/// @dev See https://eips.ethereum.org/EIPS/eip-721296#[solidity_interface(name = ERC721Metadata, expect_selector = 0x5b5e139f)]297impl<T: Config> NonfungibleHandle<T>298where299 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,300{301 /// @notice A descriptive name for a collection of NFTs in this contract302 /// @dev real implementation of this function lies in `ERC721UniqueExtensions`303 #[solidity(hide, rename_selector = "name")]304 fn name_proxy(&self) -> Result<string> {305 self.name()306 }307308 /// @notice An abbreviated name for NFTs in this contract309 /// @dev real implementation of this function lies in `ERC721UniqueExtensions`310 #[solidity(hide, rename_selector = "symbol")]311 fn symbol_proxy(&self) -> Result<string> {312 self.symbol()313 }314315 /// @notice A distinct Uniform Resource Identifier (URI) for a given asset.316 ///317 /// @dev If the token has a `url` property and it is not empty, it is returned.318 /// Else If the collection does not have a property with key `schemaName` or its value is not equal to `ERC721Metadata`, it return an error `tokenURI not set`.319 /// If the collection property `baseURI` is empty or absent, return "" (empty string)320 /// otherwise, if token property `suffix` present and is non-empty, return concatenation of baseURI and suffix321 /// otherwise, return concatenation of `baseURI` and stringified token id (decimal stringifying, without paddings).322 ///323 /// @return token's const_metadata324 #[solidity(rename_selector = "tokenURI")]325 fn token_uri(&self, token_id: U256) -> Result<string> {326 let token_id_u32: u32 = token_id.try_into().map_err(|_| "token id overflow")?;327328 match get_token_property(self, token_id_u32, &key::url()).as_deref() {329 Err(_) | Ok("") => (),330 Ok(url) => {331 return Ok(url.into());332 }333 };334335 let base_uri =336 pallet_common::Pallet::<T>::get_collection_property(self.id, &key::base_uri())337 .map(BoundedVec::into_inner)338 .map(string::from_utf8)339 .transpose()340 .map_err(|e| {341 Error::Revert(alloc::format!(342 "Can not convert value \"baseURI\" to string with error \"{}\"",343 e344 ))345 })?;346347 let base_uri = match base_uri.as_deref() {348 None | Some("") => {349 return Ok("".into());350 }351 Some(base_uri) => base_uri.into(),352 };353354 Ok(355 match get_token_property(self, token_id_u32, &key::suffix()).as_deref() {356 Err(_) | Ok("") => base_uri,357 Ok(suffix) => base_uri + suffix,358 },359 )360 }361}362363/// @title ERC-721 Non-Fungible Token Standard, optional enumeration extension364/// @dev See https://eips.ethereum.org/EIPS/eip-721365#[solidity_interface(name = ERC721Enumerable, expect_selector = 0x780e9d63)]366impl<T: Config> NonfungibleHandle<T> {367 /// @notice Enumerate valid NFTs368 /// @param index A counter less than `totalSupply()`369 /// @return The token identifier for the `index`th NFT,370 /// (sort order not specified)371 fn token_by_index(&self, index: U256) -> Result<U256> {372 Ok(index)373 }374375 /// @dev Not implemented376 fn token_of_owner_by_index(&self, _owner: address, _index: U256) -> Result<U256> {377 // TODO: Not implemetable378 Err("not implemented".into())379 }380381 /// @notice Count NFTs tracked by this contract382 /// @return A count of valid NFTs tracked by this contract, where each one of383 /// them has an assigned and queryable owner not equal to the zero address384 fn total_supply(&self) -> Result<U256> {385 self.consume_store_reads(1)?;386 Ok(<Pallet<T>>::total_supply(self).into())387 }388}389390/// @title ERC-721 Non-Fungible Token Standard391/// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md392#[solidity_interface(name = ERC721, events(ERC721Events), expect_selector = 0x80ac58cd)]393impl<T: Config> NonfungibleHandle<T> {394 /// @notice Count all NFTs assigned to an owner395 /// @dev NFTs assigned to the zero address are considered invalid, and this396 /// function throws for queries about the zero address.397 /// @param owner An address for whom to query the balance398 /// @return The number of NFTs owned by `owner`, possibly zero399 fn balance_of(&self, owner: address) -> Result<U256> {400 self.consume_store_reads(1)?;401 let owner = T::CrossAccountId::from_eth(owner);402 let balance = <AccountBalance<T>>::get((self.id, owner));403 Ok(balance.into())404 }405 /// @notice Find the owner of an NFT406 /// @dev NFTs assigned to zero address are considered invalid, and queries407 /// about them do throw.408 /// @param tokenId The identifier for an NFT409 /// @return The address of the owner of the NFT410 fn owner_of(&self, token_id: U256) -> Result<address> {411 self.consume_store_reads(1)?;412 let token: TokenId = token_id.try_into()?;413 Ok(*<TokenData<T>>::get((self.id, token))414 .ok_or("token not found")?415 .owner416 .as_eth())417 }418 /// @dev Not implemented419 #[solidity(rename_selector = "safeTransferFrom")]420 fn safe_transfer_from_with_data(421 &mut self,422 _from: address,423 _to: address,424 _token_id: U256,425 _data: bytes,426 ) -> Result<()> {427 // TODO: Not implemetable428 Err("not implemented".into())429 }430 /// @dev Not implemented431 fn safe_transfer_from(&mut self, _from: address, _to: address, _token_id: U256) -> Result<()> {432 // TODO: Not implemetable433 Err("not implemented".into())434 }435436 /// @notice Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE437 /// TO CONFIRM THAT `to` IS CAPABLE OF RECEIVING NFTS OR ELSE438 /// THEY MAY BE PERMANENTLY LOST439 /// @dev Throws unless `msg.sender` is the current owner or an authorized440 /// operator for this NFT. Throws if `from` is not the current owner. Throws441 /// if `to` is the zero address. Throws if `tokenId` is not a valid NFT.442 /// @param from The current owner of the NFT443 /// @param to The new owner444 /// @param tokenId The NFT to transfer445 #[weight(<SelfWeightOf<T>>::transfer_from())]446 fn transfer_from(447 &mut self,448 caller: caller,449 from: address,450 to: address,451 token_id: U256,452 ) -> Result<()> {453 let caller = T::CrossAccountId::from_eth(caller);454 let from = T::CrossAccountId::from_eth(from);455 let to = T::CrossAccountId::from_eth(to);456 let token = token_id.try_into()?;457 let budget = self458 .recorder459 .weight_calls_budget(<StructureWeight<T>>::find_parent());460461 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)462 .map_err(dispatch_to_evm::<T>)?;463 Ok(())464 }465466 /// @notice Set or reaffirm the approved address for an NFT467 /// @dev The zero address indicates there is no approved address.468 /// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized469 /// operator of the current owner.470 /// @param approved The new approved NFT controller471 /// @param tokenId The NFT to approve472 #[weight(<SelfWeightOf<T>>::approve())]473 fn approve(&mut self, caller: caller, approved: address, token_id: U256) -> Result<()> {474 let caller = T::CrossAccountId::from_eth(caller);475 let approved = T::CrossAccountId::from_eth(approved);476 let token = token_id.try_into()?;477478 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))479 .map_err(dispatch_to_evm::<T>)?;480 Ok(())481 }482483 /// @notice Sets or unsets the approval of a given operator.484 /// The `operator` is allowed to transfer all tokens of the `caller` on their behalf.485 /// @param operator Operator486 /// @param approved Should operator status be granted or revoked?487 #[weight(<SelfWeightOf<T>>::set_allowance_for_all())]488 fn set_approval_for_all(489 &mut self,490 caller: caller,491 operator: address,492 approved: bool,493 ) -> Result<()> {494 let caller = T::CrossAccountId::from_eth(caller);495 let operator = T::CrossAccountId::from_eth(operator);496497 <Pallet<T>>::set_allowance_for_all(self, &caller, &operator, approved)498 .map_err(dispatch_to_evm::<T>)?;499 Ok(())500 }501502 /// @dev Not implemented503 fn get_approved(&self, _token_id: U256) -> Result<address> {504 // TODO: Not implemetable505 Err("not implemented".into())506 }507508 /// @notice Tells whether the given `owner` approves the `operator`.509 #[weight(<SelfWeightOf<T>>::allowance_for_all())]510 fn is_approved_for_all(&self, owner: address, operator: address) -> Result<bool> {511 let owner = T::CrossAccountId::from_eth(owner);512 let operator = T::CrossAccountId::from_eth(operator);513514 Ok(<Pallet<T>>::allowance_for_all(self, &owner, &operator))515 }516}517518/// @title ERC721 Token that can be irreversibly burned (destroyed).519#[solidity_interface(name = ERC721Burnable)]520impl<T: Config> NonfungibleHandle<T> {521 /// @notice Burns a specific ERC721 token.522 /// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized523 /// operator of the current owner.524 /// @param tokenId The NFT to approve525 #[weight(<SelfWeightOf<T>>::burn_item())]526 fn burn(&mut self, caller: caller, token_id: U256) -> Result<()> {527 let caller = T::CrossAccountId::from_eth(caller);528 let token = token_id.try_into()?;529530 <Pallet<T>>::burn(self, &caller, token).map_err(dispatch_to_evm::<T>)?;531 Ok(())532 }533}534535/// @title ERC721 minting logic.536#[solidity_interface(name = ERC721UniqueMintable)]537impl<T: Config> NonfungibleHandle<T> {538 /// @notice Function to mint a token.539 /// @param to The new owner540 /// @return uint256 The id of the newly minted token541 #[weight(<SelfWeightOf<T>>::create_item())]542 fn mint(&mut self, caller: caller, to: address) -> Result<U256> {543 let token_id: U256 = <TokensMinted<T>>::get(self.id)544 .checked_add(1)545 .ok_or("item id overflow")?546 .into();547 self.mint_check_id(caller, to, token_id)?;548 Ok(token_id)549 }550551 /// @notice Function to mint a token.552 /// @dev `tokenId` should be obtained with `nextTokenId` method,553 /// unlike standard, you can't specify it manually554 /// @param to The new owner555 /// @param tokenId ID of the minted NFT556 #[solidity(hide, rename_selector = "mint")]557 #[weight(<SelfWeightOf<T>>::create_item())]558 fn mint_check_id(&mut self, caller: caller, to: address, token_id: U256) -> Result<bool> {559 let caller = T::CrossAccountId::from_eth(caller);560 let to = T::CrossAccountId::from_eth(to);561 let token_id: u32 = token_id.try_into()?;562 let budget = self563 .recorder564 .weight_calls_budget(<StructureWeight<T>>::find_parent());565566 if <TokensMinted<T>>::get(self.id)567 .checked_add(1)568 .ok_or("item id overflow")?569 != token_id570 {571 return Err("item id should be next".into());572 }573574 <Pallet<T>>::create_item(575 self,576 &caller,577 CreateItemData::<T> {578 properties: BoundedVec::default(),579 owner: to,580 },581 &budget,582 )583 .map_err(dispatch_to_evm::<T>)?;584585 Ok(true)586 }587588 /// @notice Function to mint token with the given tokenUri.589 /// @param to The new owner590 /// @param tokenUri Token URI that would be stored in the NFT properties591 /// @return uint256 The id of the newly minted token592 #[solidity(rename_selector = "mintWithTokenURI")]593 #[weight(<SelfWeightOf<T>>::create_item())]594 fn mint_with_token_uri(595 &mut self,596 caller: caller,597 to: address,598 token_uri: string,599 ) -> Result<U256> {600 let token_id: U256 = <TokensMinted<T>>::get(self.id)601 .checked_add(1)602 .ok_or("item id overflow")?603 .into();604 self.mint_with_token_uri_check_id(caller, to, token_id, token_uri)?;605 Ok(token_id)606 }607608 /// @notice Function to mint token with the given tokenUri.609 /// @dev `tokenId` should be obtained with `nextTokenId` method,610 /// unlike standard, you can't specify it manually611 /// @param to The new owner612 /// @param tokenId ID of the minted NFT613 /// @param tokenUri Token URI that would be stored in the NFT properties614 #[solidity(hide, rename_selector = "mintWithTokenURI")]615 #[weight(<SelfWeightOf<T>>::create_item())]616 fn mint_with_token_uri_check_id(617 &mut self,618 caller: caller,619 to: address,620 token_id: U256,621 token_uri: string,622 ) -> Result<bool> {623 let key = key::url();624 let permission = get_token_permission::<T>(self.id, &key)?;625 if !permission.collection_admin {626 return Err("Operation is not allowed".into());627 }628629 let caller = T::CrossAccountId::from_eth(caller);630 let to = T::CrossAccountId::from_eth(to);631 let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;632 let budget = self633 .recorder634 .weight_calls_budget(<StructureWeight<T>>::find_parent());635636 if <TokensMinted<T>>::get(self.id)637 .checked_add(1)638 .ok_or("item id overflow")?639 != token_id640 {641 return Err("item id should be next".into());642 }643644 let mut properties = CollectionPropertiesVec::default();645 properties646 .try_push(Property {647 key,648 value: token_uri649 .into_bytes()650 .try_into()651 .map_err(|_| "token uri is too long")?,652 })653 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;654655 <Pallet<T>>::create_item(656 self,657 &caller,658 CreateItemData::<T> {659 properties,660 owner: to,661 },662 &budget,663 )664 .map_err(dispatch_to_evm::<T>)?;665 Ok(true)666 }667}668669fn get_token_property<T: Config>(670 collection: &CollectionHandle<T>,671 token_id: u32,672 key: &up_data_structs::PropertyKey,673) -> Result<string> {674 collection.consume_store_reads(1)?;675 let properties = <TokenProperties<T>>::try_get((collection.id, token_id))676 .map_err(|_| Error::Revert("Token properties not found".into()))?;677 if let Some(property) = properties.get(key) {678 return Ok(string::from_utf8_lossy(property).into());679 }680681 Err("Property tokenURI not found".into())682}683684fn get_token_permission<T: Config>(685 collection_id: CollectionId,686 key: &PropertyKey,687) -> Result<PropertyPermission> {688 let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)689 .map_err(|_| Error::Revert("No permissions for collection".into()))?;690 let a = token_property_permissions691 .get(key)692 .map(Clone::clone)693 .ok_or_else(|| {694 let key = string::from_utf8(key.clone().into_inner()).unwrap_or_default();695 Error::Revert(alloc::format!("No permission for key {}", key))696 })?;697 Ok(a)698}699700/// @title Unique extensions for ERC721.701#[solidity_interface(name = ERC721UniqueExtensions)]702impl<T: Config> NonfungibleHandle<T>703where704 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,705{706 /// @notice A descriptive name for a collection of NFTs in this contract707 fn name(&self) -> Result<string> {708 Ok(decode_utf16(self.name.iter().copied())709 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))710 .collect::<string>())711 }712713 /// @notice An abbreviated name for NFTs in this contract714 fn symbol(&self) -> Result<string> {715 Ok(string::from_utf8_lossy(&self.token_prefix).into())716 }717718 /// @notice A description for the collection.719 fn description(&self) -> Result<string> {720 Ok(decode_utf16(self.description.iter().copied())721 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))722 .collect::<string>())723 }724725 /// Returns the owner (in cross format) of the token.726 ///727 /// @param tokenId Id for the token.728 fn cross_owner_of(&self, token_id: U256) -> Result<eth::CrossAddress> {729 Self::token_owner(&self, token_id.try_into()?)730 .map(|o| eth::CrossAddress::from_sub_cross_account::<T>(&o))731 .ok_or(Error::Revert("key too large".into()))732 }733734 /// Returns the token properties.735 ///736 /// @param tokenId Id for the token.737 /// @param keys Properties keys. Empty keys for all propertyes.738 /// @return Vector of properties key/value pairs.739 fn properties(&self, token_id: U256, keys: Vec<string>) -> Result<Vec<eth::Property>> {740 let keys = keys741 .into_iter()742 .map(|key| {743 <Vec<u8>>::from(key)744 .try_into()745 .map_err(|_| Error::Revert("key too large".into()))746 })747 .collect::<Result<Vec<_>>>()?;748749 <Self as CommonCollectionOperations<T>>::token_properties(750 &self,751 token_id.try_into()?,752 if keys.is_empty() { None } else { Some(keys) },753 )754 .into_iter()755 .map(eth::Property::try_from)756 .collect::<Result<Vec<_>>>()757 }758759 /// @notice Set or reaffirm the approved address for an NFT760 /// @dev The zero address indicates there is no approved address.761 /// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized762 /// operator of the current owner.763 /// @param approved The new substrate address approved NFT controller764 /// @param tokenId The NFT to approve765 #[weight(<SelfWeightOf<T>>::approve())]766 fn approve_cross(767 &mut self,768 caller: caller,769 approved: eth::CrossAddress,770 token_id: U256,771 ) -> Result<()> {772 let caller = T::CrossAccountId::from_eth(caller);773 let approved = approved.into_sub_cross_account::<T>()?;774 let token = token_id.try_into()?;775776 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))777 .map_err(dispatch_to_evm::<T>)?;778 Ok(())779 }780781 /// @notice Transfer ownership of an NFT782 /// @dev Throws unless `msg.sender` is the current owner. Throws if `to`783 /// is the zero address. Throws if `tokenId` is not a valid NFT.784 /// @param to The new owner785 /// @param tokenId The NFT to transfer786 #[weight(<SelfWeightOf<T>>::transfer())]787 fn transfer(&mut self, caller: caller, to: address, token_id: U256) -> Result<()> {788 let caller = T::CrossAccountId::from_eth(caller);789 let to = T::CrossAccountId::from_eth(to);790 let token = token_id.try_into()?;791 let budget = self792 .recorder793 .weight_calls_budget(<StructureWeight<T>>::find_parent());794795 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;796 Ok(())797 }798799 /// @notice Transfer ownership of an NFT800 /// @dev Throws unless `msg.sender` is the current owner. Throws if `to`801 /// is the zero address. Throws if `tokenId` is not a valid NFT.802 /// @param to The new owner803 /// @param tokenId The NFT to transfer804 #[weight(<SelfWeightOf<T>>::transfer())]805 fn transfer_cross(806 &mut self,807 caller: caller,808 to: eth::CrossAddress,809 token_id: U256,810 ) -> Result<()> {811 let caller = T::CrossAccountId::from_eth(caller);812 let to = to.into_sub_cross_account::<T>()?;813 let token = token_id.try_into()?;814 let budget = self815 .recorder816 .weight_calls_budget(<StructureWeight<T>>::find_parent());817818 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;819 Ok(())820 }821822 /// @notice Transfer ownership of an NFT from cross account address to cross account address823 /// @dev Throws unless `msg.sender` is the current owner. Throws if `to`824 /// is the zero address. Throws if `tokenId` is not a valid NFT.825 /// @param from Cross acccount address of current owner826 /// @param to Cross acccount address of new owner827 /// @param tokenId The NFT to transfer828 #[weight(<SelfWeightOf<T>>::transfer())]829 fn transfer_from_cross(830 &mut self,831 caller: caller,832 from: eth::CrossAddress,833 to: eth::CrossAddress,834 token_id: U256,835 ) -> Result<()> {836 let caller = T::CrossAccountId::from_eth(caller);837 let from = from.into_sub_cross_account::<T>()?;838 let to = to.into_sub_cross_account::<T>()?;839 let token_id = token_id.try_into()?;840 let budget = self841 .recorder842 .weight_calls_budget(<StructureWeight<T>>::find_parent());843 Pallet::<T>::transfer_from(self, &caller, &from, &to, token_id, &budget)844 .map_err(dispatch_to_evm::<T>)?;845 Ok(())846 }847848 /// @notice Burns a specific ERC721 token.849 /// @dev Throws unless `msg.sender` is the current owner or an authorized850 /// operator for this NFT. Throws if `from` is not the current owner. Throws851 /// if `to` is the zero address. Throws if `tokenId` is not a valid NFT.852 /// @param from The current owner of the NFT853 /// @param tokenId The NFT to transfer854 #[solidity(hide)]855 #[weight(<SelfWeightOf<T>>::burn_from())]856 fn burn_from(&mut self, caller: caller, from: address, token_id: U256) -> Result<()> {857 let caller = T::CrossAccountId::from_eth(caller);858 let from = T::CrossAccountId::from_eth(from);859 let token = token_id.try_into()?;860 let budget = self861 .recorder862 .weight_calls_budget(<StructureWeight<T>>::find_parent());863864 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)865 .map_err(dispatch_to_evm::<T>)?;866 Ok(())867 }868869 /// @notice Burns a specific ERC721 token.870 /// @dev Throws unless `msg.sender` is the current owner or an authorized871 /// operator for this NFT. Throws if `from` is not the current owner. Throws872 /// if `to` is the zero address. Throws if `tokenId` is not a valid NFT.873 /// @param from The current owner of the NFT874 /// @param tokenId The NFT to transfer875 #[weight(<SelfWeightOf<T>>::burn_from())]876 fn burn_from_cross(877 &mut self,878 caller: caller,879 from: eth::CrossAddress,880 token_id: U256,881 ) -> Result<()> {882 let caller = T::CrossAccountId::from_eth(caller);883 let from = from.into_sub_cross_account::<T>()?;884 let token = token_id.try_into()?;885 let budget = self886 .recorder887 .weight_calls_budget(<StructureWeight<T>>::find_parent());888889 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)890 .map_err(dispatch_to_evm::<T>)?;891 Ok(())892 }893894 /// @notice Returns next free NFT ID.895 fn next_token_id(&self) -> Result<U256> {896 self.consume_store_reads(1)?;897 Ok(<TokensMinted<T>>::get(self.id)898 .checked_add(1)899 .ok_or("item id overflow")?900 .into())901 }902903 /// @notice Function to mint multiple tokens.904 /// @dev `tokenIds` should be an array of consecutive numbers and first number905 /// should be obtained with `nextTokenId` method906 /// @param to The new owner907 /// @param tokenIds IDs of the minted NFTs908 #[solidity(hide)]909 #[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]910 fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec<U256>) -> Result<bool> {911 let caller = T::CrossAccountId::from_eth(caller);912 let to = T::CrossAccountId::from_eth(to);913 let mut expected_index = <TokensMinted<T>>::get(self.id)914 .checked_add(1)915 .ok_or("item id overflow")?;916 let budget = self917 .recorder918 .weight_calls_budget(<StructureWeight<T>>::find_parent());919920 let total_tokens = token_ids.len();921 for id in token_ids.into_iter() {922 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;923 if id != expected_index {924 return Err("item id should be next".into());925 }926 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;927 }928 let data = (0..total_tokens)929 .map(|_| CreateItemData::<T> {930 properties: BoundedVec::default(),931 owner: to.clone(),932 })933 .collect();934935 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)936 .map_err(dispatch_to_evm::<T>)?;937 Ok(true)938 }939940 /// @notice Function to mint multiple tokens with the given tokenUris.941 /// @dev `tokenIds` is array of pairs of token ID and token URI. Token IDs should be consecutive942 /// numbers and first number should be obtained with `nextTokenId` method943 /// @param to The new owner944 /// @param tokens array of pairs of token ID and token URI for minted tokens945 #[solidity(hide, rename_selector = "mintBulkWithTokenURI")]946 #[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32))]947 fn mint_bulk_with_token_uri(948 &mut self,949 caller: caller,950 to: address,951 tokens: Vec<(U256, string)>,952 ) -> Result<bool> {953 let key = key::url();954 let caller = T::CrossAccountId::from_eth(caller);955 let to = T::CrossAccountId::from_eth(to);956 let mut expected_index = <TokensMinted<T>>::get(self.id)957 .checked_add(1)958 .ok_or("item id overflow")?;959 let budget = self960 .recorder961 .weight_calls_budget(<StructureWeight<T>>::find_parent());962963 let mut data = Vec::with_capacity(tokens.len());964 for (id, token_uri) in tokens {965 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;966 if id != expected_index {967 return Err("item id should be next".into());968 }969 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;970971 let mut properties = CollectionPropertiesVec::default();972 properties973 .try_push(Property {974 key: key.clone(),975 value: token_uri976 .into_bytes()977 .try_into()978 .map_err(|_| "token uri is too long")?,979 })980 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;981982 data.push(CreateItemData::<T> {983 properties,984 owner: to.clone(),985 });986 }987988 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)989 .map_err(dispatch_to_evm::<T>)?;990 Ok(true)991 }992993 /// @notice Function to mint a token.994 /// @param to The new owner crossAccountId995 /// @param properties Properties of minted token996 /// @return uint256 The id of the newly minted token997 #[weight(<SelfWeightOf<T>>::create_item())]998 fn mint_cross(999 &mut self,1000 caller: caller,1001 to: eth::CrossAddress,1002 properties: Vec<eth::Property>,1003 ) -> Result<U256> {1004 let token_id = <TokensMinted<T>>::get(self.id)1005 .checked_add(1)1006 .ok_or("item id overflow")?;10071008 let to = to.into_sub_cross_account::<T>()?;10091010 let properties = properties1011 .into_iter()1012 .map(eth::Property::try_into)1013 .collect::<Result<Vec<_>>>()?1014 .try_into()1015 .map_err(|_| Error::Revert(alloc::format!("too many properties")))?;10161017 let caller = T::CrossAccountId::from_eth(caller);10181019 let budget = self1020 .recorder1021 .weight_calls_budget(<StructureWeight<T>>::find_parent());10221023 <Pallet<T>>::create_item(1024 self,1025 &caller,1026 CreateItemData::<T> {1027 properties,1028 owner: to,1029 },1030 &budget,1031 )1032 .map_err(dispatch_to_evm::<T>)?;10331034 Ok(token_id.into())1035 }10361037 /// @notice Returns collection helper contract address1038 fn collection_helper_address(&self) -> Result<address> {1039 Ok(T::ContractAddress::get())1040 }1041}10421043#[solidity_interface(1044 name = UniqueNFT,1045 is(1046 ERC721,1047 ERC721Enumerable,1048 ERC721UniqueExtensions,1049 ERC721UniqueMintable,1050 ERC721Burnable,1051 ERC721Metadata(if(this.flags.erc721metadata)),1052 Collection(via(common_mut returns CollectionHandle<T>)),1053 TokenProperties,1054 )1055)]1056impl<T: Config> NonfungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}10571058// Not a tests, but code generators1059generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);1060generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);10611062impl<T: Config> CommonEvmHandler for NonfungibleHandle<T>1063where1064 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,1065{1066 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");10671068 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {1069 call::<T, UniqueNFTCall<T>, _, _>(handle, self)1070 }1071}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/>.1617//! # Nonfungible Pallet EVM API18//!19//! Provides ERC-721 standart support implementation and EVM API for unique extensions for Nonfungible Pallet.20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.2122extern crate alloc;23use core::{24 char::{REPLACEMENT_CHARACTER, decode_utf16},25 convert::TryInto,26};27use evm_coder::{28 abi::AbiType, ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*,29 weight,30};31use frame_support::BoundedVec;32use up_data_structs::{33 TokenId, PropertyPermission, PropertyKeyPermission, Property, CollectionId, PropertyKey,34 CollectionPropertiesVec,35};36use pallet_evm_coder_substrate::dispatch_to_evm;37use sp_std::{vec::Vec, vec};38use pallet_common::{39 CollectionHandle, CollectionPropertyPermissions, CommonCollectionOperations,40 erc::{CommonEvmHandler, PrecompileResult, CollectionCall, static_property::key},41 eth,42};43use pallet_evm::{account::CrossAccountId, PrecompileHandle};44use pallet_evm_coder_substrate::call;45use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};46use sp_core::{U256, Get};4748use crate::{49 AccountBalance, Config, CreateItemData, NonfungibleHandle, Pallet, TokenData, TokensMinted,50 SelfWeightOf, weights::WeightInfo, TokenProperties,51};5253/// @title A contract that allows to set and delete token properties and change token property permissions.54#[solidity_interface(name = TokenProperties)]55impl<T: Config> NonfungibleHandle<T> {56 /// @notice Set permissions for token property.57 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.58 /// @param key Property key.59 /// @param isMutable Permission to mutate property.60 /// @param collectionAdmin Permission to mutate property by collection admin if property is mutable.61 /// @param tokenOwner Permission to mutate property by token owner if property is mutable.62 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(1))]63 #[solidity(hide)]64 fn set_token_property_permission(65 &mut self,66 caller: caller,67 key: string,68 is_mutable: bool,69 collection_admin: bool,70 token_owner: bool,71 ) -> Result<()> {72 let caller = T::CrossAccountId::from_eth(caller);73 <Pallet<T>>::set_token_property_permissions(74 self,75 &caller,76 vec![PropertyKeyPermission {77 key: <Vec<u8>>::from(key)78 .try_into()79 .map_err(|_| "too long key")?,80 permission: PropertyPermission {81 mutable: is_mutable,82 collection_admin,83 token_owner,84 },85 }],86 )87 .map_err(dispatch_to_evm::<T>)88 }8990 /// @notice Set permissions for token property.91 /// @dev Throws error if `msg.sender` is not admin or owner of the collection.92 /// @param permissions Permissions for keys.93 #[weight(<SelfWeightOf<T>>::set_token_property_permissions(permissions.len() as u32))]94 fn set_token_property_permissions(95 &mut self,96 caller: caller,97 permissions: Vec<eth::TokenPropertyPermission>,98 ) -> Result<()> {99 let caller = T::CrossAccountId::from_eth(caller);100 let perms = eth::TokenPropertyPermission::into_property_key_permissions(permissions)?;101102 <Pallet<T>>::set_token_property_permissions(self, &caller, perms)103 .map_err(dispatch_to_evm::<T>)104 }105106 /// @notice Get permissions for token properties.107 fn token_property_permissions(&self) -> Result<Vec<eth::TokenPropertyPermission>> {108 let perms = <Pallet<T>>::token_property_permission(self.id);109 Ok(perms110 .into_iter()111 .map(eth::TokenPropertyPermission::from)112 .collect())113 }114115 /// @notice Set token property value.116 /// @dev Throws error if `msg.sender` has no permission to edit the property.117 /// @param tokenId ID of the token.118 /// @param key Property key.119 /// @param value Property value.120 #[solidity(hide)]121 #[weight(<SelfWeightOf<T>>::set_token_properties(1))]122 fn set_property(123 &mut self,124 caller: caller,125 token_id: U256,126 key: string,127 value: bytes,128 ) -> Result<()> {129 let caller = T::CrossAccountId::from_eth(caller);130 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;131 let key = <Vec<u8>>::from(key)132 .try_into()133 .map_err(|_| "key too long")?;134 let value = value.0.try_into().map_err(|_| "value too long")?;135136 let nesting_budget = self137 .recorder138 .weight_calls_budget(<StructureWeight<T>>::find_parent());139140 <Pallet<T>>::set_token_property(141 self,142 &caller,143 TokenId(token_id),144 Property { key, value },145 &nesting_budget,146 )147 .map_err(dispatch_to_evm::<T>)148 }149150 /// @notice Set token properties value.151 /// @dev Throws error if `msg.sender` has no permission to edit the property.152 /// @param tokenId ID of the token.153 /// @param properties settable properties154 #[weight(<SelfWeightOf<T>>::set_token_properties(properties.len() as u32))]155 fn set_properties(156 &mut self,157 caller: caller,158 token_id: U256,159 properties: Vec<eth::Property>,160 ) -> Result<()> {161 let caller = T::CrossAccountId::from_eth(caller);162 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;163164 let nesting_budget = self165 .recorder166 .weight_calls_budget(<StructureWeight<T>>::find_parent());167168 let properties = properties169 .into_iter()170 .map(eth::Property::try_into)171 .collect::<Result<Vec<_>>>()?;172173 <Pallet<T>>::set_token_properties(174 self,175 &caller,176 TokenId(token_id),177 properties.into_iter(),178 false,179 &nesting_budget,180 )181 .map_err(dispatch_to_evm::<T>)182 }183184 /// @notice Delete token property value.185 /// @dev Throws error if `msg.sender` has no permission to edit the property.186 /// @param tokenId ID of the token.187 /// @param key Property key.188 #[solidity(hide)]189 #[weight(<SelfWeightOf<T>>::delete_token_properties(1))]190 fn delete_property(&mut self, token_id: U256, caller: caller, key: string) -> Result<()> {191 let caller = T::CrossAccountId::from_eth(caller);192 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;193 let key = <Vec<u8>>::from(key)194 .try_into()195 .map_err(|_| "key too long")?;196197 let nesting_budget = self198 .recorder199 .weight_calls_budget(<StructureWeight<T>>::find_parent());200201 <Pallet<T>>::delete_token_property(self, &caller, TokenId(token_id), key, &nesting_budget)202 .map_err(dispatch_to_evm::<T>)203 }204205 /// @notice Delete token properties value.206 /// @dev Throws error if `msg.sender` has no permission to edit the property.207 /// @param tokenId ID of the token.208 /// @param keys Properties key.209 #[weight(<SelfWeightOf<T>>::delete_token_properties(keys.len() as u32))]210 fn delete_properties(211 &mut self,212 token_id: U256,213 caller: caller,214 keys: Vec<string>,215 ) -> Result<()> {216 let caller = T::CrossAccountId::from_eth(caller);217 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;218 let keys = keys219 .into_iter()220 .map(|k| Ok(<Vec<u8>>::from(k).try_into().map_err(|_| "key too long")?))221 .collect::<Result<Vec<_>>>()?;222223 let nesting_budget = self224 .recorder225 .weight_calls_budget(<StructureWeight<T>>::find_parent());226227 <Pallet<T>>::delete_token_properties(228 self,229 &caller,230 TokenId(token_id),231 keys.into_iter(),232 &nesting_budget,233 )234 .map_err(dispatch_to_evm::<T>)235 }236237 /// @notice Get token property value.238 /// @dev Throws error if key not found239 /// @param tokenId ID of the token.240 /// @param key Property key.241 /// @return Property value bytes242 fn property(&self, token_id: U256, key: string) -> Result<bytes> {243 let token_id: u32 = token_id.try_into().map_err(|_| "token id overflow")?;244 let key = <Vec<u8>>::from(key)245 .try_into()246 .map_err(|_| "key too long")?;247248 let props = <TokenProperties<T>>::get((self.id, token_id));249 let prop = props.get(&key).ok_or("key not found")?;250251 Ok(prop.to_vec().into())252 }253}254255#[derive(ToLog)]256pub enum ERC721Events {257 /// @dev This emits when ownership of any NFT changes by any mechanism.258 /// This event emits when NFTs are created (`from` == 0) and destroyed259 /// (`to` == 0). Exception: during contract creation, any number of NFTs260 /// may be created and assigned without emitting Transfer. At the time of261 /// any transfer, the approved address for that NFT (if any) is reset to none.262 Transfer {263 #[indexed]264 from: Address,265 #[indexed]266 to: Address,267 #[indexed]268 token_id: U256,269 },270 /// @dev This emits when the approved address for an NFT is changed or271 /// reaffirmed. The zero address indicates there is no approved address.272 /// When a Transfer event emits, this also indicates that the approved273 /// address for that NFT (if any) is reset to none.274 Approval {275 #[indexed]276 owner: Address,277 #[indexed]278 approved: Address,279 #[indexed]280 token_id: U256,281 },282 /// @dev This emits when an operator is enabled or disabled for an owner.283 /// The operator can manage all NFTs of the owner.284 #[allow(dead_code)]285 ApprovalForAll {286 #[indexed]287 owner: Address,288 #[indexed]289 operator: Address,290 approved: bool,291 },292}293294/// @title ERC-721 Non-Fungible Token Standard, optional metadata extension295/// @dev See https://eips.ethereum.org/EIPS/eip-721296#[solidity_interface(name = ERC721Metadata, expect_selector = 0x5b5e139f)]297impl<T: Config> NonfungibleHandle<T>298where299 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,300{301 /// @notice A descriptive name for a collection of NFTs in this contract302 /// @dev real implementation of this function lies in `ERC721UniqueExtensions`303 #[solidity(hide, rename_selector = "name")]304 fn name_proxy(&self) -> Result<string> {305 self.name()306 }307308 /// @notice An abbreviated name for NFTs in this contract309 /// @dev real implementation of this function lies in `ERC721UniqueExtensions`310 #[solidity(hide, rename_selector = "symbol")]311 fn symbol_proxy(&self) -> Result<string> {312 self.symbol()313 }314315 /// @notice A distinct Uniform Resource Identifier (URI) for a given asset.316 ///317 /// @dev If the token has a `url` property and it is not empty, it is returned.318 /// Else If the collection does not have a property with key `schemaName` or its value is not equal to `ERC721Metadata`, it return an error `tokenURI not set`.319 /// If the collection property `baseURI` is empty or absent, return "" (empty string)320 /// otherwise, if token property `suffix` present and is non-empty, return concatenation of baseURI and suffix321 /// otherwise, return concatenation of `baseURI` and stringified token id (decimal stringifying, without paddings).322 ///323 /// @return token's const_metadata324 #[solidity(rename_selector = "tokenURI")]325 fn token_uri(&self, token_id: U256) -> Result<string> {326 let token_id_u32: u32 = token_id.try_into().map_err(|_| "token id overflow")?;327328 match get_token_property(self, token_id_u32, &key::url()).as_deref() {329 Err(_) | Ok("") => (),330 Ok(url) => {331 return Ok(url.into());332 }333 };334335 let base_uri =336 pallet_common::Pallet::<T>::get_collection_property(self.id, &key::base_uri())337 .map(BoundedVec::into_inner)338 .map(string::from_utf8)339 .transpose()340 .map_err(|e| {341 Error::Revert(alloc::format!(342 "Can not convert value \"baseURI\" to string with error \"{}\"",343 e344 ))345 })?;346347 let base_uri = match base_uri.as_deref() {348 None | Some("") => {349 return Ok("".into());350 }351 Some(base_uri) => base_uri.into(),352 };353354 Ok(355 match get_token_property(self, token_id_u32, &key::suffix()).as_deref() {356 Err(_) | Ok("") => base_uri,357 Ok(suffix) => base_uri + suffix,358 },359 )360 }361}362363/// @title ERC-721 Non-Fungible Token Standard, optional enumeration extension364/// @dev See https://eips.ethereum.org/EIPS/eip-721365#[solidity_interface(name = ERC721Enumerable, expect_selector = 0x780e9d63)]366impl<T: Config> NonfungibleHandle<T> {367 /// @notice Enumerate valid NFTs368 /// @param index A counter less than `totalSupply()`369 /// @return The token identifier for the `index`th NFT,370 /// (sort order not specified)371 fn token_by_index(&self, index: U256) -> Result<U256> {372 Ok(index)373 }374375 /// @dev Not implemented376 fn token_of_owner_by_index(&self, _owner: Address, _index: U256) -> Result<U256> {377 // TODO: Not implemetable378 Err("not implemented".into())379 }380381 /// @notice Count NFTs tracked by this contract382 /// @return A count of valid NFTs tracked by this contract, where each one of383 /// them has an assigned and queryable owner not equal to the zero address384 fn total_supply(&self) -> Result<U256> {385 self.consume_store_reads(1)?;386 Ok(<Pallet<T>>::total_supply(self).into())387 }388}389390/// @title ERC-721 Non-Fungible Token Standard391/// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md392#[solidity_interface(name = ERC721, events(ERC721Events), expect_selector = 0x80ac58cd)]393impl<T: Config> NonfungibleHandle<T> {394 /// @notice Count all NFTs assigned to an owner395 /// @dev NFTs assigned to the zero address are considered invalid, and this396 /// function throws for queries about the zero address.397 /// @param owner An address for whom to query the balance398 /// @return The number of NFTs owned by `owner`, possibly zero399 fn balance_of(&self, owner: Address) -> Result<U256> {400 self.consume_store_reads(1)?;401 let owner = T::CrossAccountId::from_eth(owner);402 let balance = <AccountBalance<T>>::get((self.id, owner));403 Ok(balance.into())404 }405 /// @notice Find the owner of an NFT406 /// @dev NFTs assigned to zero address are considered invalid, and queries407 /// about them do throw.408 /// @param tokenId The identifier for an NFT409 /// @return The address of the owner of the NFT410 fn owner_of(&self, token_id: U256) -> Result<Address> {411 self.consume_store_reads(1)?;412 let token: TokenId = token_id.try_into()?;413 Ok(*<TokenData<T>>::get((self.id, token))414 .ok_or("token not found")?415 .owner416 .as_eth())417 }418 /// @dev Not implemented419 #[solidity(rename_selector = "safeTransferFrom")]420 fn safe_transfer_from_with_data(421 &mut self,422 _from: Address,423 _to: Address,424 _token_id: U256,425 _data: bytes,426 ) -> Result<()> {427 // TODO: Not implemetable428 Err("not implemented".into())429 }430 /// @dev Not implemented431 fn safe_transfer_from(&mut self, _from: Address, _to: Address, _token_id: U256) -> Result<()> {432 // TODO: Not implemetable433 Err("not implemented".into())434 }435436 /// @notice Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE437 /// TO CONFIRM THAT `to` IS CAPABLE OF RECEIVING NFTS OR ELSE438 /// THEY MAY BE PERMANENTLY LOST439 /// @dev Throws unless `msg.sender` is the current owner or an authorized440 /// operator for this NFT. Throws if `from` is not the current owner. Throws441 /// if `to` is the zero address. Throws if `tokenId` is not a valid NFT.442 /// @param from The current owner of the NFT443 /// @param to The new owner444 /// @param tokenId The NFT to transfer445 #[weight(<SelfWeightOf<T>>::transfer_from())]446 fn transfer_from(447 &mut self,448 caller: caller,449 from: Address,450 to: Address,451 token_id: U256,452 ) -> Result<()> {453 let caller = T::CrossAccountId::from_eth(caller);454 let from = T::CrossAccountId::from_eth(from);455 let to = T::CrossAccountId::from_eth(to);456 let token = token_id.try_into()?;457 let budget = self458 .recorder459 .weight_calls_budget(<StructureWeight<T>>::find_parent());460461 <Pallet<T>>::transfer_from(self, &caller, &from, &to, token, &budget)462 .map_err(dispatch_to_evm::<T>)?;463 Ok(())464 }465466 /// @notice Set or reaffirm the approved address for an NFT467 /// @dev The zero address indicates there is no approved address.468 /// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized469 /// operator of the current owner.470 /// @param approved The new approved NFT controller471 /// @param tokenId The NFT to approve472 #[weight(<SelfWeightOf<T>>::approve())]473 fn approve(&mut self, caller: caller, approved: Address, token_id: U256) -> Result<()> {474 let caller = T::CrossAccountId::from_eth(caller);475 let approved = T::CrossAccountId::from_eth(approved);476 let token = token_id.try_into()?;477478 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))479 .map_err(dispatch_to_evm::<T>)?;480 Ok(())481 }482483 /// @notice Sets or unsets the approval of a given operator.484 /// The `operator` is allowed to transfer all tokens of the `caller` on their behalf.485 /// @param operator Operator486 /// @param approved Should operator status be granted or revoked?487 #[weight(<SelfWeightOf<T>>::set_allowance_for_all())]488 fn set_approval_for_all(489 &mut self,490 caller: caller,491 operator: Address,492 approved: bool,493 ) -> Result<()> {494 let caller = T::CrossAccountId::from_eth(caller);495 let operator = T::CrossAccountId::from_eth(operator);496497 <Pallet<T>>::set_allowance_for_all(self, &caller, &operator, approved)498 .map_err(dispatch_to_evm::<T>)?;499 Ok(())500 }501502 /// @dev Not implemented503 fn get_approved(&self, _token_id: U256) -> Result<Address> {504 // TODO: Not implemetable505 Err("not implemented".into())506 }507508 /// @notice Tells whether the given `owner` approves the `operator`.509 #[weight(<SelfWeightOf<T>>::allowance_for_all())]510 fn is_approved_for_all(&self, owner: Address, operator: Address) -> Result<bool> {511 let owner = T::CrossAccountId::from_eth(owner);512 let operator = T::CrossAccountId::from_eth(operator);513514 Ok(<Pallet<T>>::allowance_for_all(self, &owner, &operator))515 }516}517518/// @title ERC721 Token that can be irreversibly burned (destroyed).519#[solidity_interface(name = ERC721Burnable)]520impl<T: Config> NonfungibleHandle<T> {521 /// @notice Burns a specific ERC721 token.522 /// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized523 /// operator of the current owner.524 /// @param tokenId The NFT to approve525 #[weight(<SelfWeightOf<T>>::burn_item())]526 fn burn(&mut self, caller: caller, token_id: U256) -> Result<()> {527 let caller = T::CrossAccountId::from_eth(caller);528 let token = token_id.try_into()?;529530 <Pallet<T>>::burn(self, &caller, token).map_err(dispatch_to_evm::<T>)?;531 Ok(())532 }533}534535/// @title ERC721 minting logic.536#[solidity_interface(name = ERC721UniqueMintable)]537impl<T: Config> NonfungibleHandle<T> {538 /// @notice Function to mint a token.539 /// @param to The new owner540 /// @return uint256 The id of the newly minted token541 #[weight(<SelfWeightOf<T>>::create_item())]542 fn mint(&mut self, caller: caller, to: Address) -> Result<U256> {543 let token_id: U256 = <TokensMinted<T>>::get(self.id)544 .checked_add(1)545 .ok_or("item id overflow")?546 .into();547 self.mint_check_id(caller, to, token_id)?;548 Ok(token_id)549 }550551 /// @notice Function to mint a token.552 /// @dev `tokenId` should be obtained with `nextTokenId` method,553 /// unlike standard, you can't specify it manually554 /// @param to The new owner555 /// @param tokenId ID of the minted NFT556 #[solidity(hide, rename_selector = "mint")]557 #[weight(<SelfWeightOf<T>>::create_item())]558 fn mint_check_id(&mut self, caller: caller, to: Address, token_id: U256) -> Result<bool> {559 let caller = T::CrossAccountId::from_eth(caller);560 let to = T::CrossAccountId::from_eth(to);561 let token_id: u32 = token_id.try_into()?;562 let budget = self563 .recorder564 .weight_calls_budget(<StructureWeight<T>>::find_parent());565566 if <TokensMinted<T>>::get(self.id)567 .checked_add(1)568 .ok_or("item id overflow")?569 != token_id570 {571 return Err("item id should be next".into());572 }573574 <Pallet<T>>::create_item(575 self,576 &caller,577 CreateItemData::<T> {578 properties: BoundedVec::default(),579 owner: to,580 },581 &budget,582 )583 .map_err(dispatch_to_evm::<T>)?;584585 Ok(true)586 }587588 /// @notice Function to mint token with the given tokenUri.589 /// @param to The new owner590 /// @param tokenUri Token URI that would be stored in the NFT properties591 /// @return uint256 The id of the newly minted token592 #[solidity(rename_selector = "mintWithTokenURI")]593 #[weight(<SelfWeightOf<T>>::create_item())]594 fn mint_with_token_uri(595 &mut self,596 caller: caller,597 to: Address,598 token_uri: string,599 ) -> Result<U256> {600 let token_id: U256 = <TokensMinted<T>>::get(self.id)601 .checked_add(1)602 .ok_or("item id overflow")?603 .into();604 self.mint_with_token_uri_check_id(caller, to, token_id, token_uri)?;605 Ok(token_id)606 }607608 /// @notice Function to mint token with the given tokenUri.609 /// @dev `tokenId` should be obtained with `nextTokenId` method,610 /// unlike standard, you can't specify it manually611 /// @param to The new owner612 /// @param tokenId ID of the minted NFT613 /// @param tokenUri Token URI that would be stored in the NFT properties614 #[solidity(hide, rename_selector = "mintWithTokenURI")]615 #[weight(<SelfWeightOf<T>>::create_item())]616 fn mint_with_token_uri_check_id(617 &mut self,618 caller: caller,619 to: Address,620 token_id: U256,621 token_uri: string,622 ) -> Result<bool> {623 let key = key::url();624 let permission = get_token_permission::<T>(self.id, &key)?;625 if !permission.collection_admin {626 return Err("Operation is not allowed".into());627 }628629 let caller = T::CrossAccountId::from_eth(caller);630 let to = T::CrossAccountId::from_eth(to);631 let token_id: u32 = token_id.try_into().map_err(|_| "amount overflow")?;632 let budget = self633 .recorder634 .weight_calls_budget(<StructureWeight<T>>::find_parent());635636 if <TokensMinted<T>>::get(self.id)637 .checked_add(1)638 .ok_or("item id overflow")?639 != token_id640 {641 return Err("item id should be next".into());642 }643644 let mut properties = CollectionPropertiesVec::default();645 properties646 .try_push(Property {647 key,648 value: token_uri649 .into_bytes()650 .try_into()651 .map_err(|_| "token uri is too long")?,652 })653 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;654655 <Pallet<T>>::create_item(656 self,657 &caller,658 CreateItemData::<T> {659 properties,660 owner: to,661 },662 &budget,663 )664 .map_err(dispatch_to_evm::<T>)?;665 Ok(true)666 }667}668669fn get_token_property<T: Config>(670 collection: &CollectionHandle<T>,671 token_id: u32,672 key: &up_data_structs::PropertyKey,673) -> Result<string> {674 collection.consume_store_reads(1)?;675 let properties = <TokenProperties<T>>::try_get((collection.id, token_id))676 .map_err(|_| Error::Revert("Token properties not found".into()))?;677 if let Some(property) = properties.get(key) {678 return Ok(string::from_utf8_lossy(property).into());679 }680681 Err("Property tokenURI not found".into())682}683684fn get_token_permission<T: Config>(685 collection_id: CollectionId,686 key: &PropertyKey,687) -> Result<PropertyPermission> {688 let token_property_permissions = CollectionPropertyPermissions::<T>::try_get(collection_id)689 .map_err(|_| Error::Revert("No permissions for collection".into()))?;690 let a = token_property_permissions691 .get(key)692 .map(Clone::clone)693 .ok_or_else(|| {694 let key = string::from_utf8(key.clone().into_inner()).unwrap_or_default();695 Error::Revert(alloc::format!("No permission for key {}", key))696 })?;697 Ok(a)698}699700/// @title Unique extensions for ERC721.701#[solidity_interface(name = ERC721UniqueExtensions)]702impl<T: Config> NonfungibleHandle<T>703where704 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,705{706 /// @notice A descriptive name for a collection of NFTs in this contract707 fn name(&self) -> Result<string> {708 Ok(decode_utf16(self.name.iter().copied())709 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))710 .collect::<string>())711 }712713 /// @notice An abbreviated name for NFTs in this contract714 fn symbol(&self) -> Result<string> {715 Ok(string::from_utf8_lossy(&self.token_prefix).into())716 }717718 /// @notice A description for the collection.719 fn description(&self) -> Result<string> {720 Ok(decode_utf16(self.description.iter().copied())721 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))722 .collect::<string>())723 }724725 /// Returns the owner (in cross format) of the token.726 ///727 /// @param tokenId Id for the token.728 fn cross_owner_of(&self, token_id: U256) -> Result<eth::CrossAddress> {729 Self::token_owner(&self, token_id.try_into()?)730 .map(|o| eth::CrossAddress::from_sub_cross_account::<T>(&o))731 .ok_or(Error::Revert("key too large".into()))732 }733734 /// Returns the token properties.735 ///736 /// @param tokenId Id for the token.737 /// @param keys Properties keys. Empty keys for all propertyes.738 /// @return Vector of properties key/value pairs.739 fn properties(&self, token_id: U256, keys: Vec<string>) -> Result<Vec<eth::Property>> {740 let keys = keys741 .into_iter()742 .map(|key| {743 <Vec<u8>>::from(key)744 .try_into()745 .map_err(|_| Error::Revert("key too large".into()))746 })747 .collect::<Result<Vec<_>>>()?;748749 <Self as CommonCollectionOperations<T>>::token_properties(750 &self,751 token_id.try_into()?,752 if keys.is_empty() { None } else { Some(keys) },753 )754 .into_iter()755 .map(eth::Property::try_from)756 .collect::<Result<Vec<_>>>()757 }758759 /// @notice Set or reaffirm the approved address for an NFT760 /// @dev The zero address indicates there is no approved address.761 /// @dev Throws unless `msg.sender` is the current NFT owner, or an authorized762 /// operator of the current owner.763 /// @param approved The new substrate address approved NFT controller764 /// @param tokenId The NFT to approve765 #[weight(<SelfWeightOf<T>>::approve())]766 fn approve_cross(767 &mut self,768 caller: caller,769 approved: eth::CrossAddress,770 token_id: U256,771 ) -> Result<()> {772 let caller = T::CrossAccountId::from_eth(caller);773 let approved = approved.into_sub_cross_account::<T>()?;774 let token = token_id.try_into()?;775776 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))777 .map_err(dispatch_to_evm::<T>)?;778 Ok(())779 }780781 /// @notice Transfer ownership of an NFT782 /// @dev Throws unless `msg.sender` is the current owner. Throws if `to`783 /// is the zero address. Throws if `tokenId` is not a valid NFT.784 /// @param to The new owner785 /// @param tokenId The NFT to transfer786 #[weight(<SelfWeightOf<T>>::transfer())]787 fn transfer(&mut self, caller: caller, to: Address, token_id: U256) -> Result<()> {788 let caller = T::CrossAccountId::from_eth(caller);789 let to = T::CrossAccountId::from_eth(to);790 let token = token_id.try_into()?;791 let budget = self792 .recorder793 .weight_calls_budget(<StructureWeight<T>>::find_parent());794795 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;796 Ok(())797 }798799 /// @notice Transfer ownership of an NFT800 /// @dev Throws unless `msg.sender` is the current owner. Throws if `to`801 /// is the zero address. Throws if `tokenId` is not a valid NFT.802 /// @param to The new owner803 /// @param tokenId The NFT to transfer804 #[weight(<SelfWeightOf<T>>::transfer())]805 fn transfer_cross(806 &mut self,807 caller: caller,808 to: eth::CrossAddress,809 token_id: U256,810 ) -> Result<()> {811 let caller = T::CrossAccountId::from_eth(caller);812 let to = to.into_sub_cross_account::<T>()?;813 let token = token_id.try_into()?;814 let budget = self815 .recorder816 .weight_calls_budget(<StructureWeight<T>>::find_parent());817818 <Pallet<T>>::transfer(self, &caller, &to, token, &budget).map_err(dispatch_to_evm::<T>)?;819 Ok(())820 }821822 /// @notice Transfer ownership of an NFT from cross account address to cross account address823 /// @dev Throws unless `msg.sender` is the current owner. Throws if `to`824 /// is the zero address. Throws if `tokenId` is not a valid NFT.825 /// @param from Cross acccount address of current owner826 /// @param to Cross acccount address of new owner827 /// @param tokenId The NFT to transfer828 #[weight(<SelfWeightOf<T>>::transfer())]829 fn transfer_from_cross(830 &mut self,831 caller: caller,832 from: eth::CrossAddress,833 to: eth::CrossAddress,834 token_id: U256,835 ) -> Result<()> {836 let caller = T::CrossAccountId::from_eth(caller);837 let from = from.into_sub_cross_account::<T>()?;838 let to = to.into_sub_cross_account::<T>()?;839 let token_id = token_id.try_into()?;840 let budget = self841 .recorder842 .weight_calls_budget(<StructureWeight<T>>::find_parent());843 Pallet::<T>::transfer_from(self, &caller, &from, &to, token_id, &budget)844 .map_err(dispatch_to_evm::<T>)?;845 Ok(())846 }847848 /// @notice Burns a specific ERC721 token.849 /// @dev Throws unless `msg.sender` is the current owner or an authorized850 /// operator for this NFT. Throws if `from` is not the current owner. Throws851 /// if `to` is the zero address. Throws if `tokenId` is not a valid NFT.852 /// @param from The current owner of the NFT853 /// @param tokenId The NFT to transfer854 #[solidity(hide)]855 #[weight(<SelfWeightOf<T>>::burn_from())]856 fn burn_from(&mut self, caller: caller, from: Address, token_id: U256) -> Result<()> {857 let caller = T::CrossAccountId::from_eth(caller);858 let from = T::CrossAccountId::from_eth(from);859 let token = token_id.try_into()?;860 let budget = self861 .recorder862 .weight_calls_budget(<StructureWeight<T>>::find_parent());863864 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)865 .map_err(dispatch_to_evm::<T>)?;866 Ok(())867 }868869 /// @notice Burns a specific ERC721 token.870 /// @dev Throws unless `msg.sender` is the current owner or an authorized871 /// operator for this NFT. Throws if `from` is not the current owner. Throws872 /// if `to` is the zero address. Throws if `tokenId` is not a valid NFT.873 /// @param from The current owner of the NFT874 /// @param tokenId The NFT to transfer875 #[weight(<SelfWeightOf<T>>::burn_from())]876 fn burn_from_cross(877 &mut self,878 caller: caller,879 from: eth::CrossAddress,880 token_id: U256,881 ) -> Result<()> {882 let caller = T::CrossAccountId::from_eth(caller);883 let from = from.into_sub_cross_account::<T>()?;884 let token = token_id.try_into()?;885 let budget = self886 .recorder887 .weight_calls_budget(<StructureWeight<T>>::find_parent());888889 <Pallet<T>>::burn_from(self, &caller, &from, token, &budget)890 .map_err(dispatch_to_evm::<T>)?;891 Ok(())892 }893894 /// @notice Returns next free NFT ID.895 fn next_token_id(&self) -> Result<U256> {896 self.consume_store_reads(1)?;897 Ok(<TokensMinted<T>>::get(self.id)898 .checked_add(1)899 .ok_or("item id overflow")?900 .into())901 }902903 /// @notice Function to mint multiple tokens.904 /// @dev `tokenIds` should be an array of consecutive numbers and first number905 /// should be obtained with `nextTokenId` method906 /// @param to The new owner907 /// @param tokenIds IDs of the minted NFTs908 #[solidity(hide)]909 #[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]910 fn mint_bulk(&mut self, caller: caller, to: Address, token_ids: Vec<U256>) -> Result<bool> {911 let caller = T::CrossAccountId::from_eth(caller);912 let to = T::CrossAccountId::from_eth(to);913 let mut expected_index = <TokensMinted<T>>::get(self.id)914 .checked_add(1)915 .ok_or("item id overflow")?;916 let budget = self917 .recorder918 .weight_calls_budget(<StructureWeight<T>>::find_parent());919920 let total_tokens = token_ids.len();921 for id in token_ids.into_iter() {922 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;923 if id != expected_index {924 return Err("item id should be next".into());925 }926 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;927 }928 let data = (0..total_tokens)929 .map(|_| CreateItemData::<T> {930 properties: BoundedVec::default(),931 owner: to.clone(),932 })933 .collect();934935 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)936 .map_err(dispatch_to_evm::<T>)?;937 Ok(true)938 }939940 /// @notice Function to mint multiple tokens with the given tokenUris.941 /// @dev `tokenIds` is array of pairs of token ID and token URI. Token IDs should be consecutive942 /// numbers and first number should be obtained with `nextTokenId` method943 /// @param to The new owner944 /// @param tokens array of pairs of token ID and token URI for minted tokens945 #[solidity(hide, rename_selector = "mintBulkWithTokenURI")]946 #[weight(<SelfWeightOf<T>>::create_multiple_items(tokens.len() as u32))]947 fn mint_bulk_with_token_uri(948 &mut self,949 caller: caller,950 to: Address,951 tokens: Vec<(U256, string)>,952 ) -> Result<bool> {953 let key = key::url();954 let caller = T::CrossAccountId::from_eth(caller);955 let to = T::CrossAccountId::from_eth(to);956 let mut expected_index = <TokensMinted<T>>::get(self.id)957 .checked_add(1)958 .ok_or("item id overflow")?;959 let budget = self960 .recorder961 .weight_calls_budget(<StructureWeight<T>>::find_parent());962963 let mut data = Vec::with_capacity(tokens.len());964 for (id, token_uri) in tokens {965 let id: u32 = id.try_into().map_err(|_| "token id overflow")?;966 if id != expected_index {967 return Err("item id should be next".into());968 }969 expected_index = expected_index.checked_add(1).ok_or("item id overflow")?;970971 let mut properties = CollectionPropertiesVec::default();972 properties973 .try_push(Property {974 key: key.clone(),975 value: token_uri976 .into_bytes()977 .try_into()978 .map_err(|_| "token uri is too long")?,979 })980 .map_err(|e| Error::Revert(alloc::format!("Can't add property: {:?}", e)))?;981982 data.push(CreateItemData::<T> {983 properties,984 owner: to.clone(),985 });986 }987988 <Pallet<T>>::create_multiple_items(self, &caller, data, &budget)989 .map_err(dispatch_to_evm::<T>)?;990 Ok(true)991 }992993 /// @notice Function to mint a token.994 /// @param to The new owner crossAccountId995 /// @param properties Properties of minted token996 /// @return uint256 The id of the newly minted token997 #[weight(<SelfWeightOf<T>>::create_item())]998 fn mint_cross(999 &mut self,1000 caller: caller,1001 to: eth::CrossAddress,1002 properties: Vec<eth::Property>,1003 ) -> Result<U256> {1004 let token_id = <TokensMinted<T>>::get(self.id)1005 .checked_add(1)1006 .ok_or("item id overflow")?;10071008 let to = to.into_sub_cross_account::<T>()?;10091010 let properties = properties1011 .into_iter()1012 .map(eth::Property::try_into)1013 .collect::<Result<Vec<_>>>()?1014 .try_into()1015 .map_err(|_| Error::Revert(alloc::format!("too many properties")))?;10161017 let caller = T::CrossAccountId::from_eth(caller);10181019 let budget = self1020 .recorder1021 .weight_calls_budget(<StructureWeight<T>>::find_parent());10221023 <Pallet<T>>::create_item(1024 self,1025 &caller,1026 CreateItemData::<T> {1027 properties,1028 owner: to,1029 },1030 &budget,1031 )1032 .map_err(dispatch_to_evm::<T>)?;10331034 Ok(token_id.into())1035 }10361037 /// @notice Returns collection helper contract address1038 fn collection_helper_address(&self) -> Result<Address> {1039 Ok(T::ContractAddress::get())1040 }1041}10421043#[solidity_interface(1044 name = UniqueNFT,1045 is(1046 ERC721,1047 ERC721Enumerable,1048 ERC721UniqueExtensions,1049 ERC721UniqueMintable,1050 ERC721Burnable,1051 ERC721Metadata(if(this.flags.erc721metadata)),1052 Collection(via(common_mut returns CollectionHandle<T>)),1053 TokenProperties,1054 )1055)]1056impl<T: Config> NonfungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}10571058// Not a tests, but code generators1059generate_stubgen!(gen_impl, UniqueNFTCall<()>, true);1060generate_stubgen!(gen_iface, UniqueNFTCall<()>, false);10611062impl<T: Config> CommonEvmHandler for NonfungibleHandle<T>1063where1064 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,1065{1066 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNFT.raw");10671068 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {1069 call::<T, UniqueNFTCall<T>, _, _>(handle, self)1070 }1071}pallets/refungible/src/erc.rsdiffbeforeafterboth--- a/pallets/refungible/src/erc.rs
+++ b/pallets/refungible/src/erc.rs
@@ -262,18 +262,18 @@
/// may be created and assigned without emitting Transfer.
Transfer {
#[indexed]
- from: address,
+ from: Address,
#[indexed]
- to: address,
+ to: Address,
#[indexed]
token_id: U256,
},
/// @dev Not supported
Approval {
#[indexed]
- owner: address,
+ owner: Address,
#[indexed]
- approved: address,
+ approved: Address,
#[indexed]
token_id: U256,
},
@@ -281,9 +281,9 @@
#[allow(dead_code)]
ApprovalForAll {
#[indexed]
- owner: address,
+ owner: Address,
#[indexed]
- operator: address,
+ operator: Address,
approved: bool,
},
}
@@ -370,7 +370,7 @@
}
/// Not implemented
- fn token_of_owner_by_index(&self, _owner: address, _index: U256) -> Result<U256> {
+ fn token_of_owner_by_index(&self, _owner: Address, _index: U256) -> Result<U256> {
// TODO: Not implemetable
Err("not implemented".into())
}
@@ -393,7 +393,7 @@
/// function throws for queries about the zero address.
/// @param owner An address for whom to query the balance
/// @return The number of RFTs owned by `owner`, possibly zero
- fn balance_of(&self, owner: address) -> Result<U256> {
+ fn balance_of(&self, owner: Address) -> Result<U256> {
self.consume_store_reads(1)?;
let owner = T::CrossAccountId::from_eth(owner);
let balance = <AccountBalance<T>>::get((self.id, owner));
@@ -407,7 +407,7 @@
/// the tokens that are partially owned.
/// @param tokenId The identifier for an RFT
/// @return The address of the owner of the RFT
- fn owner_of(&self, token_id: U256) -> Result<address> {
+ fn owner_of(&self, token_id: U256) -> Result<Address> {
self.consume_store_reads(2)?;
let token = token_id.try_into()?;
let owner = <Pallet<T>>::token_owner(self.id, token);
@@ -420,8 +420,8 @@
#[solidity(rename_selector = "safeTransferFrom")]
fn safe_transfer_from_with_data(
&mut self,
- _from: address,
- _to: address,
+ _from: Address,
+ _to: Address,
_token_id: U256,
_data: bytes,
) -> Result<()> {
@@ -431,7 +431,7 @@
/// @dev Not implemented
#[solidity(rename_selector = "safeTransferFrom")]
- fn safe_transfer_from(&mut self, _from: address, _to: address, _token_id: U256) -> Result<()> {
+ fn safe_transfer_from(&mut self, _from: Address, _to: Address, _token_id: U256) -> Result<()> {
// TODO: Not implemetable
Err("not implemented".into())
}
@@ -450,8 +450,8 @@
fn transfer_from(
&mut self,
caller: caller,
- from: address,
- to: address,
+ from: Address,
+ to: Address,
token_id: U256,
) -> Result<()> {
let caller = T::CrossAccountId::from_eth(caller);
@@ -472,7 +472,7 @@
}
/// @dev Not implemented
- fn approve(&mut self, _caller: caller, _approved: address, _token_id: U256) -> Result<()> {
+ fn approve(&mut self, _caller: caller, _approved: Address, _token_id: U256) -> Result<()> {
Err("not implemented".into())
}
@@ -484,7 +484,7 @@
fn set_approval_for_all(
&mut self,
caller: caller,
- operator: address,
+ operator: Address,
approved: bool,
) -> Result<()> {
let caller = T::CrossAccountId::from_eth(caller);
@@ -496,14 +496,14 @@
}
/// @dev Not implemented
- fn get_approved(&self, _token_id: U256) -> Result<address> {
+ fn get_approved(&self, _token_id: U256) -> Result<Address> {
// TODO: Not implemetable
Err("not implemented".into())
}
/// @notice Tells whether the given `owner` approves the `operator`.
#[weight(<SelfWeightOf<T>>::allowance_for_all())]
- fn is_approved_for_all(&self, owner: address, operator: address) -> Result<bool> {
+ fn is_approved_for_all(&self, owner: Address, operator: Address) -> Result<bool> {
let owner = T::CrossAccountId::from_eth(owner);
let operator = T::CrossAccountId::from_eth(operator);
@@ -570,7 +570,7 @@
/// @param to The new owner
/// @return uint256 The id of the newly minted token
#[weight(<SelfWeightOf<T>>::create_item())]
- fn mint(&mut self, caller: caller, to: address) -> Result<U256> {
+ fn mint(&mut self, caller: caller, to: Address) -> Result<U256> {
let token_id: U256 = <TokensMinted<T>>::get(self.id)
.checked_add(1)
.ok_or("item id overflow")?
@@ -586,7 +586,7 @@
/// @param tokenId ID of the minted RFT
#[solidity(hide, rename_selector = "mint")]
#[weight(<SelfWeightOf<T>>::create_item())]
- fn mint_check_id(&mut self, caller: caller, to: address, token_id: U256) -> Result<bool> {
+ fn mint_check_id(&mut self, caller: caller, to: Address, token_id: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let token_id: u32 = token_id.try_into()?;
@@ -630,7 +630,7 @@
fn mint_with_token_uri(
&mut self,
caller: caller,
- to: address,
+ to: Address,
token_uri: string,
) -> Result<U256> {
let token_id: U256 = <TokensMinted<T>>::get(self.id)
@@ -652,7 +652,7 @@
fn mint_with_token_uri_check_id(
&mut self,
caller: caller,
- to: address,
+ to: Address,
token_id: U256,
token_uri: string,
) -> Result<bool> {
@@ -800,7 +800,7 @@
/// @param to The new owner
/// @param tokenId The RFT to transfer
#[weight(<SelfWeightOf<T>>::transfer_creating_removing())]
- fn transfer(&mut self, caller: caller, to: address, token_id: U256) -> Result<()> {
+ fn transfer(&mut self, caller: caller, to: Address, token_id: U256) -> Result<()> {
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let token = token_id.try_into()?;
@@ -883,7 +883,7 @@
/// @param tokenId The RFT to transfer
#[solidity(hide)]
#[weight(<SelfWeightOf<T>>::burn_from())]
- fn burn_from(&mut self, caller: caller, from: address, token_id: U256) -> Result<()> {
+ fn burn_from(&mut self, caller: caller, from: Address, token_id: U256) -> Result<()> {
let caller = T::CrossAccountId::from_eth(caller);
let from = T::CrossAccountId::from_eth(from);
let token = token_id.try_into()?;
@@ -944,7 +944,7 @@
/// @param tokenIds IDs of the minted RFTs
#[solidity(hide)]
#[weight(<SelfWeightOf<T>>::create_multiple_items(token_ids.len() as u32))]
- fn mint_bulk(&mut self, caller: caller, to: address, token_ids: Vec<U256>) -> Result<bool> {
+ fn mint_bulk(&mut self, caller: caller, to: Address, token_ids: Vec<U256>) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let mut expected_index = <TokensMinted<T>>::get(self.id)
@@ -990,7 +990,7 @@
fn mint_bulk_with_token_uri(
&mut self,
caller: caller,
- to: address,
+ to: Address,
tokens: Vec<(U256, string)>,
) -> Result<bool> {
let key = key::url();
@@ -1088,7 +1088,7 @@
/// Returns EVM address for refungible token
///
/// @param token ID of the token
- fn token_contract_address(&self, token: U256) -> Result<address> {
+ fn token_contract_address(&self, token: U256) -> Result<Address> {
Ok(T::EvmTokenAddressMapping::token_to_address(
self.id,
token.try_into().map_err(|_| "token id overflow")?,
@@ -1096,7 +1096,7 @@
}
/// @notice Returns collection helper contract address
- fn collection_helper_address(&self) -> Result<address> {
+ fn collection_helper_address(&self) -> Result<Address> {
Ok(T::ContractAddress::get())
}
}
pallets/refungible/src/erc_token.rsdiffbeforeafterboth--- a/pallets/refungible/src/erc_token.rs
+++ b/pallets/refungible/src/erc_token.rs
@@ -52,7 +52,7 @@
#[solidity_interface(name = ERC1633)]
impl<T: Config> RefungibleTokenHandle<T> {
- fn parent_token(&self) -> Result<address> {
+ fn parent_token(&self) -> Result<Address> {
Ok(collection_id_to_address(self.id))
}
@@ -69,18 +69,18 @@
/// of burning tokens the transfer is to 0.
Transfer {
#[indexed]
- from: address,
+ from: Address,
#[indexed]
- to: address,
+ to: Address,
value: U256,
},
/// @dev This event is emitted when the amount of tokens (value) is approved
/// by the owner to be used by the spender.
Approval {
#[indexed]
- owner: address,
+ owner: Address,
#[indexed]
- spender: address,
+ spender: Address,
value: U256,
},
}
@@ -118,7 +118,7 @@
/// @dev Gets the balance of the specified address.
/// @param owner The address to query the balance of.
/// @return An uint256 representing the amount owned by the passed address.
- fn balance_of(&self, owner: address) -> Result<U256> {
+ fn balance_of(&self, owner: Address) -> Result<U256> {
self.consume_store_reads(1)?;
let owner = T::CrossAccountId::from_eth(owner);
let balance = <Balance<T>>::get((self.id, self.1, owner));
@@ -129,7 +129,7 @@
/// @param to The address to transfer to.
/// @param amount The amount to be transferred.
#[weight(<CommonWeights<T>>::transfer())]
- fn transfer(&mut self, caller: caller, to: address, amount: U256) -> Result<bool> {
+ fn transfer(&mut self, caller: caller, to: Address, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let to = T::CrossAccountId::from_eth(to);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
@@ -150,8 +150,8 @@
fn transfer_from(
&mut self,
caller: caller,
- from: address,
- to: address,
+ from: Address,
+ to: Address,
amount: U256,
) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
@@ -175,7 +175,7 @@
/// @param spender The address which will spend the funds.
/// @param amount The amount of tokens to be spent.
#[weight(<SelfWeightOf<T>>::approve())]
- fn approve(&mut self, caller: caller, spender: address, amount: U256) -> Result<bool> {
+ fn approve(&mut self, caller: caller, spender: Address, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let spender = T::CrossAccountId::from_eth(spender);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
@@ -189,7 +189,7 @@
/// @param owner address The address which owns the funds.
/// @param spender address The address which will spend the funds.
/// @return A uint256 specifying the amount of tokens still available for the spender.
- fn allowance(&self, owner: address, spender: address) -> Result<U256> {
+ fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {
self.consume_store_reads(1)?;
let owner = T::CrossAccountId::from_eth(owner);
let spender = T::CrossAccountId::from_eth(spender);
@@ -209,7 +209,7 @@
/// @param amount The amount that will be burnt.
#[weight(<SelfWeightOf<T>>::burn_from())]
#[solidity(hide)]
- fn burn_from(&mut self, caller: caller, from: address, amount: U256) -> Result<bool> {
+ fn burn_from(&mut self, caller: caller, from: Address, amount: U256) -> Result<bool> {
let caller = T::CrossAccountId::from_eth(caller);
let from = T::CrossAccountId::from_eth(from);
let amount = amount.try_into().map_err(|_| "amount overflow")?;
pallets/unique/src/eth/mod.rsdiffbeforeafterboth--- a/pallets/unique/src/eth/mod.rs
+++ b/pallets/unique/src/eth/mod.rs
@@ -93,7 +93,7 @@
collection_mode: CollectionMode,
description: string,
token_prefix: string,
-) -> Result<address> {
+) -> Result<Address> {
let (caller, name, description, token_prefix) =
convert_data::<T>(caller, name, description, token_prefix)?;
let data = CreateCollectionData {
@@ -154,7 +154,7 @@
name: string,
description: string,
token_prefix: string,
- ) -> Result<address> {
+ ) -> Result<Address> {
let (caller, name, description, token_prefix) =
convert_data::<T>(caller, name, description, token_prefix)?;
let data = CreateCollectionData {
@@ -193,7 +193,7 @@
name: string,
description: string,
token_prefix: string,
- ) -> Result<address> {
+ ) -> Result<Address> {
create_collection_internal::<T>(
caller,
value,
@@ -213,7 +213,7 @@
name: string,
description: string,
token_prefix: string,
- ) -> Result<address> {
+ ) -> Result<Address> {
create_collection_internal::<T>(
caller,
value,
@@ -234,7 +234,7 @@
decimals: u8,
description: string,
token_prefix: string,
- ) -> Result<address> {
+ ) -> Result<Address> {
create_collection_internal::<T>(
caller,
value,
@@ -249,7 +249,7 @@
fn make_collection_metadata_compatible(
&mut self,
caller: caller,
- collection: address,
+ collection: Address,
base_uri: string,
) -> Result<()> {
let caller = T::CrossAccountId::from_eth(caller);
@@ -334,7 +334,7 @@
}
#[weight(<SelfWeightOf<T>>::destroy_collection())]
- fn destroy_collection(&mut self, caller: caller, collection_address: address) -> Result<()> {
+ fn destroy_collection(&mut self, caller: caller, collection_address: Address) -> Result<()> {
let caller = T::CrossAccountId::from_eth(caller);
let collection_id = pallet_common::eth::map_eth_to_id(&collection_address)
@@ -346,7 +346,7 @@
/// Check if a collection exists
/// @param collectionAddress Address of the collection in question
/// @return bool Does the collection exist?
- fn is_collection_exist(&self, _caller: caller, collection_address: address) -> Result<bool> {
+ fn is_collection_exist(&self, _caller: caller, collection_address: Address) -> Result<bool> {
if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) {
let collection_id = id;
return Ok(<CollectionById<T>>::contains_key(collection_id));
@@ -366,14 +366,14 @@
/// Returns address of a collection.
/// @param collectionId - CollectionId of the collection
/// @return eth mirror address of the collection
- fn collection_address(&self, collection_id: u32) -> Result<address> {
+ fn collection_address(&self, collection_id: u32) -> Result<Address> {
Ok(collection_id_to_address(collection_id.into()))
}
/// Returns collectionId of a collection.
/// @param collectionAddress - Eth address of the collection
/// @return collectionId of the collection
- fn collection_id(&self, collection_address: address) -> Result<u32> {
+ fn collection_id(&self, collection_address: Address) -> Result<u32> {
map_eth_to_id(&collection_address)
.map(|id| id.0)
.ok_or(Error::Revert(format!(