From ffa590c7d9c44e931562ffb7b3e25d6a92327bc0 Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Fri, 27 May 2022 07:45:34 +0000 Subject: [PATCH] CORE-346 Remove evm_collection namespace --- --- a/Makefile +++ b/Makefile @@ -15,7 +15,7 @@ CONTRACT_HELPERS_STUBS=./pallets/evm-contract-helpers/src/stubs/ CONTRACT_HELPERS_ABI=./tests/src/eth/util/contractHelpersAbi.json -COLLECTION_HELPER_STUBS=$(COLLECTION_STUBS) +COLLECTION_HELPER_STUBS=./pallets/unique/src/eth/stubs/ COLLECTION_HELPER_ABI=./tests/src/eth/collectionHelperAbi.json TESTS_API=./tests/src/eth/api/ @@ -36,8 +36,8 @@ PACKAGE=pallet-evm-contract-helpers NAME=eth::contract_helpers_impl OUTPUT=$(CONTRACT_HELPERS_STUBS)/$@ ./.maintain/scripts/generate_sol.sh CollectionHelper.sol: - PACKAGE=pallet-unique NAME=eth::evm_collection::collection_helper_iface OUTPUT=$(TESTS_API)/$@ ./.maintain/scripts/generate_sol.sh - PACKAGE=pallet-unique NAME=eth::evm_collection::collection_helper_impl OUTPUT=$(COLLECTION_HELPER_STUBS)/$@ ./.maintain/scripts/generate_sol.sh + PACKAGE=pallet-unique NAME=eth::collection_helper_iface OUTPUT=$(TESTS_API)/$@ ./.maintain/scripts/generate_sol.sh + PACKAGE=pallet-unique NAME=eth::collection_helper_impl OUTPUT=$(COLLECTION_HELPER_STUBS)/$@ ./.maintain/scripts/generate_sol.sh UniqueFungible: UniqueFungible.sol INPUT=$(FUNGIBLE_EVM_STUBS)/$< OUTPUT=$(FUNGIBLE_EVM_STUBS)/UniqueFungible.raw ./.maintain/scripts/compile_stub.sh --- a/pallets/unique/src/eth/mod.rs +++ b/pallets/unique/src/eth/mod.rs @@ -14,161 +14,159 @@ // You should have received a copy of the GNU General Public License // along with Unique Network. If not, see . -pub mod evm_collection { - use core::marker::PhantomData; - use evm_coder::{execution::*, generate_stubgen, solidity_interface, types::*, ToLog}; - use ethereum as _; - use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder}; - use pallet_evm::{OnMethodCall, PrecompileResult, account::CrossAccountId, Pallet as PalletEvm}; - use up_data_structs::{ - CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, - MAX_COLLECTION_NAME_LENGTH, - }; - use frame_support::traits::Get; - use sp_core::H160; - use pallet_common::CollectionById; - - use sp_std::vec::Vec; - use alloc::format; - - pub trait Config: - frame_system::Config - + pallet_evm_coder_substrate::Config - + pallet_evm::account::Config - + pallet_nonfungible::Config - { - type ContractAddress: Get; +use core::marker::PhantomData; +use evm_coder::{execution::*, generate_stubgen, solidity_interface, types::*, ToLog}; +use ethereum as _; +use pallet_evm_coder_substrate::{SubstrateRecorder, WithRecorder}; +use pallet_evm::{OnMethodCall, PrecompileResult, account::CrossAccountId, Pallet as PalletEvm}; +use up_data_structs::{ + CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, + MAX_COLLECTION_NAME_LENGTH, +}; +use frame_support::traits::Get; +use sp_core::H160; +use pallet_common::CollectionById; + +use sp_std::vec::Vec; +use alloc::format; + +pub trait Config: + frame_system::Config + + pallet_evm_coder_substrate::Config + + pallet_evm::account::Config + + pallet_nonfungible::Config +{ + type ContractAddress: Get; +} + +struct EvmCollectionHelper(SubstrateRecorder); +impl WithRecorder for EvmCollectionHelper { + fn recorder(&self) -> &SubstrateRecorder { + &self.0 } - struct EvmCollectionHelper(SubstrateRecorder); - impl WithRecorder for EvmCollectionHelper { - fn recorder(&self) -> &SubstrateRecorder { - &self.0 - } - - fn into_recorder(self) -> SubstrateRecorder { - self.0 - } + fn into_recorder(self) -> SubstrateRecorder { + self.0 } +} + +#[solidity_interface(name = "CollectionHelper")] +impl EvmCollectionHelper { + fn create_721_collection( + &self, + caller: caller, + name: string, + description: string, + token_prefix: string, + ) -> Result
{ + let caller = T::CrossAccountId::from_eth(caller); + let name = name + .encode_utf16() + .collect::>() + .try_into() + .map_err(|_| error_feild_too_long(stringify!(name), MAX_COLLECTION_NAME_LENGTH))?; + let description = description + .encode_utf16() + .collect::>() + .try_into() + .map_err(|_| { + error_feild_too_long(stringify!(description), MAX_COLLECTION_DESCRIPTION_LENGTH) + })?; + let token_prefix = token_prefix + .into_bytes() + .try_into() + .map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?; + + let key: string = "tokenURI".into(); //TODO: make static + let key: up_data_structs::PropertyKey = key.into_bytes().try_into().map_err(|_| Error::Revert("".into()))?; + let permission = up_data_structs::PropertyPermission { + mutable: true, + collection_admin: true, + token_owner: false, + }; + let mut token_property_permissions = up_data_structs::CollectionPropertiesPermissionsVec::default(); + token_property_permissions.try_push(up_data_structs::PropertyKeyPermission{ + key, + permission, + }).map_err(|e| Error::Revert(format!("{:?}", e)))?; - #[solidity_interface(name = "CollectionHelper")] - impl EvmCollectionHelper { - fn create_721_collection( - &self, - caller: caller, - name: string, - description: string, - token_prefix: string, - ) -> Result
{ - let caller = T::CrossAccountId::from_eth(caller); - let name = name - .encode_utf16() - .collect::>() - .try_into() - .map_err(|_| error_feild_too_long(stringify!(name), MAX_COLLECTION_NAME_LENGTH))?; - let description = description - .encode_utf16() - .collect::>() - .try_into() - .map_err(|_| { - error_feild_too_long(stringify!(description), MAX_COLLECTION_DESCRIPTION_LENGTH) - })?; - let token_prefix = token_prefix - .into_bytes() - .try_into() - .map_err(|_| error_feild_too_long(stringify!(token_prefix), MAX_TOKEN_PREFIX_LENGTH))?; - - let key: string = "tokenURI".into(); //TODO: make static - let key: up_data_structs::PropertyKey = key.into_bytes().try_into().map_err(|_| Error::Revert("".into()))?; - let permission = up_data_structs::PropertyPermission { - mutable: true, - collection_admin: true, - token_owner: false, - }; - let mut token_property_permissions = up_data_structs::CollectionPropertiesPermissionsVec::default(); - token_property_permissions.try_push(up_data_structs::PropertyKeyPermission{ - key, - permission, - }).map_err(|e| Error::Revert(format!("{:?}", e)))?; + let data = CreateCollectionData { + name, + description, + token_prefix, + token_property_permissions, + ..Default::default() + }; - let data = CreateCollectionData { - name, - description, - token_prefix, - token_property_permissions, - ..Default::default() - }; - - let collection_id = - >::init_collection(caller.as_sub().clone(), data) - .map_err(pallet_evm_coder_substrate::dispatch_to_evm::)?; - - let address = pallet_common::eth::collection_id_to_address(collection_id); - >::deposit_log( - EthCollectionEvent::CollectionCreated { - owner: *caller.as_eth(), - collection_id: address, - } - .to_log(address), - ); - Ok(address) - } + let collection_id = + >::init_collection(caller.as_sub().clone(), data) + .map_err(pallet_evm_coder_substrate::dispatch_to_evm::)?; - fn is_collection_exist(&self, _caller: caller, collection_address: address) -> Result { - if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) { - let collection_id = id; - return Ok(>::contains_key(collection_id)); + let address = pallet_common::eth::collection_id_to_address(collection_id); + >::deposit_log( + EthCollectionEvent::CollectionCreated { + owner: *caller.as_eth(), + collection_id: address, } + .to_log(address), + ); + Ok(address) + } - Ok(false) + fn is_collection_exist(&self, _caller: caller, collection_address: address) -> Result { + if let Some(id) = pallet_common::eth::map_eth_to_id(&collection_address) { + let collection_id = id; + return Ok(>::contains_key(collection_id)); } + + Ok(false) + } +} + +#[derive(ToLog)] +pub enum EthCollectionEvent { + CollectionCreated { + #[indexed] + owner: address, + #[indexed] + collection_id: address, + }, +} + +pub struct CollectionHelperOnMethodCall(PhantomData<*const T>); +impl OnMethodCall for CollectionHelperOnMethodCall { + fn is_reserved(contract: &sp_core::H160) -> bool { + contract == &T::ContractAddress::get() } - - #[derive(ToLog)] - pub enum EthCollectionEvent { - CollectionCreated { - #[indexed] - owner: address, - #[indexed] - collection_id: address, - }, + + fn is_used(contract: &sp_core::H160) -> bool { + contract == &T::ContractAddress::get() } - pub struct CollectionHelperOnMethodCall(PhantomData<*const T>); - impl OnMethodCall for CollectionHelperOnMethodCall { - fn is_reserved(contract: &sp_core::H160) -> bool { - contract == &T::ContractAddress::get() - } - - fn is_used(contract: &sp_core::H160) -> bool { - contract == &T::ContractAddress::get() + fn call( + source: &sp_core::H160, + target: &sp_core::H160, + gas_left: u64, + input: &[u8], + value: sp_core::U256, + ) -> Option { + if target != &T::ContractAddress::get() { + return None; } - - fn call( - source: &sp_core::H160, - target: &sp_core::H160, - gas_left: u64, - input: &[u8], - value: sp_core::U256, - ) -> Option { - if target != &T::ContractAddress::get() { - return None; - } - - let helpers = EvmCollectionHelper::(SubstrateRecorder::::new(gas_left)); - pallet_evm_coder_substrate::call(*source, helpers, value, input) - } - - fn get_code(contract: &sp_core::H160) -> Option> { - (contract == &T::ContractAddress::get()) - .then(|| include_bytes!("./stubs/CollectionHelper.raw").to_vec()) - } + + let helpers = EvmCollectionHelper::(SubstrateRecorder::::new(gas_left)); + pallet_evm_coder_substrate::call(*source, helpers, value, input) } - - generate_stubgen!(collection_helper_impl, CollectionHelperCall<()>, true); - generate_stubgen!(collection_helper_iface, CollectionHelperCall<()>, false); - fn error_feild_too_long(feild: &str, bound: u32) -> Error { - Error::Revert(format!("{} is too long. Max length is {}.", feild, bound)) + fn get_code(contract: &sp_core::H160) -> Option> { + (contract == &T::ContractAddress::get()) + .then(|| include_bytes!("./stubs/CollectionHelper.raw").to_vec()) } } + +generate_stubgen!(collection_helper_impl, CollectionHelperCall<()>, true); +generate_stubgen!(collection_helper_iface, CollectionHelperCall<()>, false); + +fn error_feild_too_long(feild: &str, bound: u32) -> Error { + Error::Revert(format!("{} is too long. Max length is {}.", feild, bound)) +} --- a/runtime/opal/src/lib.rs +++ b/runtime/opal/src/lib.rs @@ -80,7 +80,6 @@ }; use smallvec::smallvec; use codec::{Encode, Decode}; -use pallet_unique::eth::evm_collection; use fp_rpc::TransactionStatus; use sp_runtime::{ traits::{BlockNumberProvider, Dispatchable, PostDispatchInfoOf, Saturating}, @@ -307,7 +306,7 @@ pallet_evm_migration::OnMethodCall, pallet_evm_contract_helpers::HelpersOnMethodCall, CollectionDispatchT, - evm_collection::CollectionHelperOnMethodCall, + pallet_unique::eth::CollectionHelperOnMethodCall, ); type OnCreate = pallet_evm_contract_helpers::HelpersOnCreate; type ChainId = ChainId; @@ -988,7 +987,7 @@ type DefaultSponsoringRateLimit = DefaultSponsoringRateLimit; } -impl evm_collection::Config for Runtime { +impl pallet_unique::eth::Config for Runtime { type ContractAddress = EvmCollectionHelperAddress; } --- a/runtime/quartz/src/lib.rs +++ b/runtime/quartz/src/lib.rs @@ -78,7 +78,6 @@ }; use smallvec::smallvec; use codec::{Encode, Decode}; -use pallet_unique::eth::evm_collection; use pallet_evm::{Account as EVMAccount, FeeCalculator, GasWeightMapping}; use fp_rpc::TransactionStatus; use sp_runtime::{ @@ -286,7 +285,7 @@ pallet_evm_migration::OnMethodCall, pallet_evm_contract_helpers::HelpersOnMethodCall, CollectionDispatchT, - evm_collection::CollectionHelperOnMethodCall, + pallet_unique::eth::CollectionHelperOnMethodCall, ); type OnCreate = pallet_evm_contract_helpers::HelpersOnCreate; type ChainId = ChainId; @@ -973,7 +972,7 @@ type DefaultSponsoringRateLimit = DefaultSponsoringRateLimit; } -impl evm_collection::Config for Runtime { +impl pallet_unique::eth::Config for Runtime { type ContractAddress = EvmCollectionHelperAddress; } --- a/runtime/unique/src/lib.rs +++ b/runtime/unique/src/lib.rs @@ -85,7 +85,6 @@ }; use smallvec::smallvec; use codec::{Encode, Decode}; -use pallet_unique::eth::evm_collection; use fp_rpc::TransactionStatus; use sp_runtime::{ traits::{BlockNumberProvider, Dispatchable, PostDispatchInfoOf, Saturating}, @@ -291,7 +290,7 @@ pallet_evm_migration::OnMethodCall, pallet_evm_contract_helpers::HelpersOnMethodCall, CollectionDispatchT, - evm_collection::CollectionHelperOnMethodCall, + pallet_unique::eth::CollectionHelperOnMethodCall, ); type OnCreate = pallet_evm_contract_helpers::HelpersOnCreate; type ChainId = ChainId; @@ -978,7 +977,7 @@ type DefaultSponsoringRateLimit = DefaultSponsoringRateLimit; } -impl evm_collection::Config for Runtime { +impl pallet_unique::eth::Config for Runtime { type ContractAddress = EvmCollectionHelperAddress; } -- gitstuff