difftreelog
fix gas limit
in: master
2 files changed
pallets/balances-adapter/src/lib.rsdiffbeforeafterboth--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -20,6 +20,11 @@
Self(SubstrateRecorder::new(u64::MAX))
}
+ /// Creates a handle
+ pub fn new_with_gas_limit(gas_limit: u64) -> NativeFungibleHandle<T> {
+ Self(SubstrateRecorder::new(gas_limit))
+ }
+
/// Check if the collection is internal
pub fn check_is_internal(&self) -> DispatchResult {
Ok(())
runtime/common/dispatch.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/>.1617use frame_support::{dispatch::DispatchResult, ensure, fail};18use pallet_evm::{PrecompileHandle, PrecompileResult};19use sp_core::H160;20use sp_runtime::DispatchError;21use sp_std::{borrow::ToOwned, vec::Vec};22use pallet_common::{23 CollectionById, CollectionHandle, CommonCollectionOperations, erc::CommonEvmHandler,24 eth::map_eth_to_id,25};26pub use pallet_common::dispatch::CollectionDispatch;27use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};28use pallet_balances_adapter::{NativeFungibleHandle};29use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};30use pallet_refungible::{31 Pallet as PalletRefungible, RefungibleHandle, erc_token::RefungibleTokenHandle,32};33use up_data_structs::{34 CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,35 CollectionId, CollectionFlags,36};3738#[cfg(not(feature = "refungible"))]39use pallet_common::unsupported;4041pub enum CollectionDispatchT<T>42where43 T: pallet_fungible::Config44 + pallet_nonfungible::Config45 + pallet_refungible::Config46 + pallet_balances_adapter::Config,47{48 Fungible(FungibleHandle<T>),49 Nonfungible(NonfungibleHandle<T>),50 Refungible(RefungibleHandle<T>),51 NativeFungible(NativeFungibleHandle<T>),52}5354impl<T> CollectionDispatch<T> for CollectionDispatchT<T>55where56 T: pallet_common::Config57 + pallet_unique::Config58 + pallet_fungible::Config59 + pallet_nonfungible::Config60 + pallet_refungible::Config61 + pallet_balances_adapter::Config,62{63 fn check_is_internal(&self) -> DispatchResult {64 match self {65 Self::Fungible(h) => h.check_is_internal(),66 Self::Nonfungible(h) => h.check_is_internal(),67 Self::Refungible(h) => h.check_is_internal(),68 Self::NativeFungible(h) => h.check_is_internal(),69 }70 }7172 fn create(73 sender: T::CrossAccountId,74 payer: T::CrossAccountId,75 data: CreateCollectionData<T::AccountId>,76 flags: CollectionFlags,77 ) -> Result<CollectionId, DispatchError> {78 let id = match data.mode {79 CollectionMode::NFT => {80 <PalletNonfungible<T>>::init_collection(sender, payer, data, flags)?81 }82 CollectionMode::Fungible(decimal_points) => {83 // check params84 ensure!(85 decimal_points <= MAX_DECIMAL_POINTS,86 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded87 );88 <PalletFungible<T>>::init_collection(sender, payer, data, flags)?89 }9091 #[cfg(feature = "refungible")]92 CollectionMode::ReFungible => {93 <PalletRefungible<T>>::init_collection(sender, payer, data, flags)?94 }9596 #[cfg(not(feature = "refungible"))]97 CollectionMode::ReFungible => return unsupported!(T),98 };99 Ok(id)100 }101102 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {103 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {104 fail!(<pallet_common::Error<T>>::UnsupportedOperation);105 }106107 let collection = <CollectionHandle<T>>::try_get(collection_id)?;108109 match collection.mode {110 CollectionMode::ReFungible => {111 PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?112 }113 CollectionMode::Fungible(_) => {114 PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?115 }116 CollectionMode::NFT => {117 PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?118 }119 }120 Ok(())121 }122123 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {124 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {125 return Ok(Self::NativeFungible(NativeFungibleHandle::new()));126 }127128 let handle = <CollectionHandle<T>>::try_get(collection_id)?;129 Ok(match handle.mode {130 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),131 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),132 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),133 })134 }135136 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {137 match self {138 Self::Fungible(h) => h,139 Self::Nonfungible(h) => h,140 Self::Refungible(h) => h,141 Self::NativeFungible(h) => h,142 }143 }144}145146impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>147where148 T: pallet_common::Config149 + pallet_unique::Config150 + pallet_fungible::Config151 + pallet_nonfungible::Config152 + pallet_refungible::Config153 + pallet_balances_adapter::Config,154 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,155{156 fn is_reserved(target: &H160) -> bool {157 map_eth_to_id(target).is_some()158 }159 fn is_used(target: &H160) -> bool {160 map_eth_to_id(target)161 .map(<CollectionById<T>>::contains_key)162 .unwrap_or(false)163 }164 fn get_code(target: &H160) -> Option<Vec<u8>> {165 if let Some(collection_id) = map_eth_to_id(target) {166 let collection = <CollectionById<T>>::get(collection_id)?;167 Some(168 match collection.mode {169 CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,170 CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,171 CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,172 }173 .to_owned(),174 )175 } else if let Some((collection_id, _token_id)) =176 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)177 {178 let collection = <CollectionById<T>>::get(collection_id)?;179 if collection.mode != CollectionMode::ReFungible {180 return None;181 }182 // TODO: check token existence183 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())184 } else {185 None186 }187 }188 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {189 if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {190 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {191 <NativeFungibleHandle<T>>::new().call(handle)192 } else {193 let collection = <CollectionHandle<T>>::new_with_gas_limit(194 collection_id,195 handle.remaining_gas(),196 )?;197198 match collection.mode {199 CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),200 CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),201 CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),202 }203 }204 } else if let Some((collection_id, token_id)) =205 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(206 &handle.code_address(),207 ) {208 let collection =209 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;210 if collection.mode != CollectionMode::ReFungible {211 return None;212 }213214 let h = RefungibleHandle::cast(collection);215 // TODO: check token existence216 RefungibleTokenHandle(h, token_id).call(handle)217 } else {218 None219 }220 }221}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/>.1617use frame_support::{dispatch::DispatchResult, ensure, fail};18use pallet_evm::{PrecompileHandle, PrecompileResult};19use sp_core::H160;20use sp_runtime::DispatchError;21use sp_std::{borrow::ToOwned, vec::Vec};22use pallet_common::{23 CollectionById, CollectionHandle, CommonCollectionOperations, erc::CommonEvmHandler,24 eth::map_eth_to_id,25};26pub use pallet_common::dispatch::CollectionDispatch;27use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};28use pallet_balances_adapter::{NativeFungibleHandle};29use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};30use pallet_refungible::{31 Pallet as PalletRefungible, RefungibleHandle, erc_token::RefungibleTokenHandle,32};33use up_data_structs::{34 CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,35 CollectionId, CollectionFlags,36};3738#[cfg(not(feature = "refungible"))]39use pallet_common::unsupported;4041pub enum CollectionDispatchT<T>42where43 T: pallet_fungible::Config44 + pallet_nonfungible::Config45 + pallet_refungible::Config46 + pallet_balances_adapter::Config,47{48 Fungible(FungibleHandle<T>),49 Nonfungible(NonfungibleHandle<T>),50 Refungible(RefungibleHandle<T>),51 NativeFungible(NativeFungibleHandle<T>),52}5354impl<T> CollectionDispatch<T> for CollectionDispatchT<T>55where56 T: pallet_common::Config57 + pallet_unique::Config58 + pallet_fungible::Config59 + pallet_nonfungible::Config60 + pallet_refungible::Config61 + pallet_balances_adapter::Config,62{63 fn check_is_internal(&self) -> DispatchResult {64 match self {65 Self::Fungible(h) => h.check_is_internal(),66 Self::Nonfungible(h) => h.check_is_internal(),67 Self::Refungible(h) => h.check_is_internal(),68 Self::NativeFungible(h) => h.check_is_internal(),69 }70 }7172 fn create(73 sender: T::CrossAccountId,74 payer: T::CrossAccountId,75 data: CreateCollectionData<T::AccountId>,76 flags: CollectionFlags,77 ) -> Result<CollectionId, DispatchError> {78 let id = match data.mode {79 CollectionMode::NFT => {80 <PalletNonfungible<T>>::init_collection(sender, payer, data, flags)?81 }82 CollectionMode::Fungible(decimal_points) => {83 // check params84 ensure!(85 decimal_points <= MAX_DECIMAL_POINTS,86 pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded87 );88 <PalletFungible<T>>::init_collection(sender, payer, data, flags)?89 }9091 #[cfg(feature = "refungible")]92 CollectionMode::ReFungible => {93 <PalletRefungible<T>>::init_collection(sender, payer, data, flags)?94 }9596 #[cfg(not(feature = "refungible"))]97 CollectionMode::ReFungible => return unsupported!(T),98 };99 Ok(id)100 }101102 fn destroy(sender: T::CrossAccountId, collection_id: CollectionId) -> DispatchResult {103 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {104 fail!(<pallet_common::Error<T>>::UnsupportedOperation);105 }106107 let collection = <CollectionHandle<T>>::try_get(collection_id)?;108109 match collection.mode {110 CollectionMode::ReFungible => {111 PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?112 }113 CollectionMode::Fungible(_) => {114 PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?115 }116 CollectionMode::NFT => {117 PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?118 }119 }120 Ok(())121 }122123 fn dispatch(collection_id: CollectionId) -> Result<Self, DispatchError> {124 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {125 return Ok(Self::NativeFungible(126 NativeFungibleHandle::new_with_gas_limit(u64::MAX),127 ));128 }129130 let handle = <CollectionHandle<T>>::try_get(collection_id)?;131 Ok(match handle.mode {132 CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),133 CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),134 CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),135 })136 }137138 fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {139 match self {140 Self::Fungible(h) => h,141 Self::Nonfungible(h) => h,142 Self::Refungible(h) => h,143 Self::NativeFungible(h) => h,144 }145 }146}147148impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>149where150 T: pallet_common::Config151 + pallet_unique::Config152 + pallet_fungible::Config153 + pallet_nonfungible::Config154 + pallet_refungible::Config155 + pallet_balances_adapter::Config,156 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,157{158 fn is_reserved(target: &H160) -> bool {159 map_eth_to_id(target).is_some()160 }161 fn is_used(target: &H160) -> bool {162 map_eth_to_id(target)163 .map(<CollectionById<T>>::contains_key)164 .unwrap_or(false)165 }166 fn get_code(target: &H160) -> Option<Vec<u8>> {167 if let Some(collection_id) = map_eth_to_id(target) {168 let collection = <CollectionById<T>>::get(collection_id)?;169 Some(170 match collection.mode {171 CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,172 CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,173 CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,174 }175 .to_owned(),176 )177 } else if let Some((collection_id, _token_id)) =178 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)179 {180 let collection = <CollectionById<T>>::get(collection_id)?;181 if collection.mode != CollectionMode::ReFungible {182 return None;183 }184 // TODO: check token existence185 Some(<RefungibleTokenHandle<T>>::CODE.to_owned())186 } else {187 None188 }189 }190 fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {191 if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {192 if collection_id == pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {193 <NativeFungibleHandle<T>>::new_with_gas_limit(handle.remaining_gas()).call(handle)194 } else {195 let collection = <CollectionHandle<T>>::new_with_gas_limit(196 collection_id,197 handle.remaining_gas(),198 )?;199200 match collection.mode {201 CollectionMode::Fungible(_) => FungibleHandle::cast(collection).call(handle),202 CollectionMode::NFT => NonfungibleHandle::cast(collection).call(handle),203 CollectionMode::ReFungible => RefungibleHandle::cast(collection).call(handle),204 }205 }206 } else if let Some((collection_id, token_id)) =207 <T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(208 &handle.code_address(),209 ) {210 let collection =211 <CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;212 if collection.mode != CollectionMode::ReFungible {213 return None;214 }215216 let h = RefungibleHandle::cast(collection);217 // TODO: check token existence218 RefungibleTokenHandle(h, token_id).call(handle)219 } else {220 None221 }222 }223}