difftreelog
COR-302 Refactor
in: master
1 file changed
pallets/evm-collection/src/eth.rsdiffbeforeafterboth26use sp_core::H160;26use sp_core::H160;27use up_data_structs::{27use up_data_structs::{28 CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,28 CreateCollectionData, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,29 MAX_COLLECTION_NAME_LENGTH,29 MAX_COLLECTION_NAME_LENGTH, OFFCHAIN_SCHEMA_LIMIT, VARIABLE_ON_CHAIN_SCHEMA_LIMIT,30 CONST_ON_CHAIN_SCHEMA_LIMIT,30};31};31use crate::{Config, Pallet};32use crate::{Config, Pallet};32use frame_support::traits::Get;33use frame_support::traits::Get;56}57}575858#[solidity_interface(name = "Collection")]59#[solidity_interface(name = "Collection")]59impl<T: Config> EvmCollection<T> {60impl<T: Config> EvmCollection<T> {60 fn create_721_collection(61 fn create_721_collection(61 &self,62 &self,62 caller: caller,63 caller: caller,102 Ok(address)103 Ok(address)103 }104 }104105105 // fn set_sponsor(collection_id: address, sponsor: address) -> Result<void> {106 fn set_sponsor(106 // let collection_id =107 &self,107 // pallet_common::eth::map_eth_to_id(&collection_id).ok_or(Error::Revert("".into()))?;108 caller: caller,108 // let mut collection = <CollectionById<T>>::get(collection_id).ok_or(Error::Revert("".into()))?;109 collection_address: address,109 // let sponsor = T::CrossAccountId::from_eth(sponsor);110 sponsor: address,110 // collection.sponsorship = SponsorshipState::Unconfirmed(sponsor.as_sub().clone());111 ) -> Result<void> {111 // <CollectionById<T>>::insert(collection_id, collection);112 let mut collection = collection_from_address(collection_address, &self.0)?;112 // Ok(())113 check_is_owner(caller, &collection)?;113 // }114114115 fn set_offchain_shema(shema: string) -> Result<void> {115 fn set_offchain_shema(shema: string) -> Result<void> {116 let shema = shema116 let shema = shema122 }122 }123123124 fn confirm_sponsorship(&self, caller: caller, collection_address: address) -> Result<void> {124 fn confirm_sponsorship(&self, caller: caller, collection_address: address) -> Result<void> {125 let (_, mut collection) = collection_from_address(collection_address, &self.0)?;125 let mut collection = collection_from_address(collection_address, &self.0)?;126 let caller = T::CrossAccountId::from_eth(caller);126 let caller = T::CrossAccountId::from_eth(caller);127 if !collection.confirm_sponsorship(caller.as_sub()) {127 if !collection.confirm_sponsorship(caller.as_sub()) {128 return Err(Error::Revert("Caller is not set as sponsor".into()));128 return Err(Error::Revert("Caller is not set as sponsor".into()));129 }129 }130 save(collection)130 save(collection)131 }131 }132132133 // fn set_variable_on_chain_schema(shema: string) -> Result<void> {133 fn set_offchain_shema(134 // Ok(())134 &self,135 // }135 caller: caller,136 collection_address: address,137 shema: string,138 ) -> Result<void> {139 let mut collection = collection_from_address(collection_address, &self.0)?;140 check_is_owner(caller, &collection)?;141142 let shema = shema143 .into_bytes()144 .try_into()145 .map_err(|_| error_feild_too_long(stringify!(shema), OFFCHAIN_SCHEMA_LIMIT))?;146 collection.offchain_schema = shema;147 save(collection)148 }136149137 fn set_variable_on_chain_schema(150 fn set_variable_on_chain_schema(138 &self,151 &self,139 caller: caller,152 caller: caller,140 collection_address: address,153 collection_address: address,141 variable: string,154 variable: string,142 ) -> Result<void> {155 ) -> Result<void> {143 let (_, mut collection) = collection_from_address(collection_address, &self.0)?;156 let mut collection = collection_from_address(collection_address, &self.0)?;144 check_is_owner(caller, &collection)?;157 check_is_owner(caller, &collection)?;145158146 let variable = variable.into_bytes().try_into().map_err(|_| {159 let variable = variable.into_bytes().try_into().map_err(|_| {156 collection_address: address,169 collection_address: address,157 const_on_chain: string,170 const_on_chain: string,158 ) -> Result<void> {171 ) -> Result<void> {159 let (_, mut collection) = collection_from_address(collection_address, &self.0)?;172 let mut collection = collection_from_address(collection_address, &self.0)?;160 check_is_owner(caller, &collection)?;173 check_is_owner(caller, &collection)?;161174162 let const_on_chain = const_on_chain.into_bytes().try_into().map_err(|_| {175 let const_on_chain = const_on_chain.into_bytes().try_into().map_err(|_| {172 collection_address: address,185 collection_address: address,173 limits_json: string,186 limits_json: string,174 ) -> Result<void> {187 ) -> Result<void> {175 let (_, mut collection) = collection_from_address(collection_address, &self.0)?;188 let mut collection = collection_from_address(collection_address, &self.0)?;176 check_is_owner(caller, &collection)?;189 check_is_owner(caller, &collection)?;177190178 let limits = serde_json::from_str(limits_json.as_ref())191 let limits = serde_json::from_str(limits_json.as_ref())179 .map_err(|e| Error::Revert(format!("Parse JSON error: {}", e)))?;192 .map_err(|e| Error::Revert(format!("Parse JSON error: {}", e)))?;180 collection.limits = limits;193 collection.limits = limits;181 save(collection)194 save(collection)182 }195 }183}196}184197185fn error_feild_too_long(feild: &str, bound: u32) -> Error {198fn error_feild_too_long(feild: &str, bound: u32) -> Error {186 Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))199 Error::Revert(format!("{} is too long. Max length is {}.", feild, bound))187}200}188201189pub struct CollectionOnMethodCall<T: Config>(PhantomData<*const T>);202fn collection_from_address<T: Config>(190impl<T: Config> OnMethodCall<T> for CollectionOnMethodCall<T> {203 collection_address: address,191 fn is_reserved(contract: &sp_core::H160) -> bool {204 recorder: &Rc<SubstrateRecorder<T>>,192 contract == &T::ContractAddress::get()205) -> Result<CollectionHandle<T>> {193 }206 let collection_id = pallet_common::eth::map_eth_to_id(&collection_address)194207 .ok_or(Error::Revert("Bad ETH prefix".into()))?;195 fn is_used(contract: &sp_core::H160) -> bool {208 let collection =196 contract == &T::ContractAddress::get()209 pallet_common::CollectionHandle::new_with_recorder(collection_id, recorder.clone())197 }210 .ok_or(Error::Revert("Create collection handle error".into()))?;198211 Ok(collection)199 fn call(212}200 source: &sp_core::H160,213201 target: &sp_core::H160,214fn check_is_owner<T: Config>(caller: caller, collection: &CollectionHandle<T>) -> Result<()> {202 gas_left: u64,215 let caller = T::CrossAccountId::from_eth(caller);203 input: &[u8],216 collection204 value: sp_core::U256,217 .check_is_owner(&caller)205 ) -> Option<PrecompileResult> {218 .map_err(|e| Error::Revert(format!("{:?}", e)))?;206 // TODO: Extract to another OnMethodCall handler219 Ok(())207 if target != &T::ContractAddress::get() {220}208 return None;221209 }222fn save<T: Config>(collection: CollectionHandle<T>) -> Result<()> {210223 collection211 let helpers = EvmCollection::<T>(SubstrateRecorder::new(gas_left));224 .save()212 pallet_evm_coder_substrate::call(*source, helpers, value, input)225 .map_err(|e| Error::Revert(format!("{:?}", e)))213 }226}214227215 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {228pub struct CollectionOnMethodCall<T: Config>(PhantomData<*const T>);216 (contract == &T::ContractAddress::get())229impl<T: Config> OnMethodCall<T> for CollectionOnMethodCall<T> {217 .then(|| include_bytes!("./stubs/Collection.raw").to_vec())230 fn is_reserved(contract: &sp_core::H160) -> bool {218 }231 contract == &T::ContractAddress::get()219}232 }220233221generate_stubgen!(collection_impl, CollectionCall<()>, true);234 fn is_used(contract: &sp_core::H160) -> bool {222generate_stubgen!(collection_iface, CollectionCall<()>, false);235 contract == &T::ContractAddress::get()236 }237238 fn call(239 source: &sp_core::H160,240 target: &sp_core::H160,241 gas_left: u64,242 input: &[u8],243 value: sp_core::U256,244 ) -> Option<PrecompileResult> {245 // TODO: Extract to another OnMethodCall handler246 if target != &T::ContractAddress::get() {247 return None;248 }249250 let helpers = EvmCollection::<T>(SubstrateRecorder::new(gas_left));251 pallet_evm_coder_substrate::call(*source, helpers, value, input)252 }253254 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {255 (contract == &T::ContractAddress::get())256 .then(|| include_bytes!("./stubs/Collection.raw").to_vec())257 }258}259260generate_stubgen!(collection_impl, CollectionCall<()>, true);261generate_stubgen!(collection_iface, CollectionCall<()>, false);223262