git.delta.rocks / unique-network / refs/commits / 898c743eda26

difftreelog

fixed code review issues

Grigoriy Simonov2022-07-22parent: #0f86ef0.patch.diff
in: master

8 files changed

modifiedMakefilediffbeforeafterboth
--- a/Makefile
+++ b/Makefile
@@ -36,8 +36,8 @@
 	PACKAGE=pallet-nonfungible NAME=erc::gen_impl OUTPUT=$(NONFUNGIBLE_EVM_STUBS)/$@ ./.maintain/scripts/generate_sol.sh
 	
 UniqueRefungibleToken.sol:
-	PACKAGE=pallet-refungible NAME=erc20::gen_iface OUTPUT=$(TESTS_API)/$@ ./.maintain/scripts/generate_sol.sh
-	PACKAGE=pallet-refungible NAME=erc20::gen_impl OUTPUT=$(REFUNGIBLE_EVM_STUBS)/$@ ./.maintain/scripts/generate_sol.sh
+	PACKAGE=pallet-refungible NAME=erc_token::gen_iface OUTPUT=$(TESTS_API)/$@ ./.maintain/scripts/generate_sol.sh
+	PACKAGE=pallet-refungible NAME=erc_token::gen_impl OUTPUT=$(REFUNGIBLE_EVM_STUBS)/$@ ./.maintain/scripts/generate_sol.sh
 
 ContractHelpers.sol:
 	PACKAGE=pallet-evm-contract-helpers NAME=eth::contract_helpers_iface OUTPUT=$(TESTS_API)/$@ ./.maintain/scripts/generate_sol.sh
addedpallets/refungible/src/erc.rsdiffbeforeafterboth
--- /dev/null
+++ b/pallets/refungible/src/erc.rs
@@ -0,0 +1,32 @@
+extern crate alloc;
+use evm_coder::{generate_stubgen, solidity_interface, types::*};
+
+use pallet_common::{CollectionHandle, erc::CollectionCall, erc::CommonEvmHandler};
+
+use pallet_evm::PrecompileHandle;
+use pallet_evm_coder_substrate::call;
+
+use crate::{Config, RefungibleHandle};
+
+#[solidity_interface(
+	name = "UniqueRFT",
+	is(via("CollectionHandle<T>", common_mut, Collection),)
+)]
+impl<T: Config> RefungibleHandle<T> where T::AccountId: From<[u8; 32]> {}
+
+// Not a tests, but code generators
+generate_stubgen!(gen_impl, UniqueRFTCall<()>, true);
+generate_stubgen!(gen_iface, UniqueRFTCall<()>, false);
+
+impl<T: Config> CommonEvmHandler for RefungibleHandle<T>
+where
+	T::AccountId: From<[u8; 32]>,
+{
+	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungible.raw");
+	fn call(
+		self,
+		handle: &mut impl PrecompileHandle,
+	) -> Option<pallet_common::erc::PrecompileResult> {
+		call::<T, UniqueRFTCall<T>, _, _>(handle, self)
+	}
+}
deletedpallets/refungible/src/erc20.rsdiffbeforeafterboth
--- a/pallets/refungible/src/erc20.rs
+++ /dev/null
@@ -1,204 +0,0 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// Unique Network is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-extern crate alloc;
-use core::{
-	char::{REPLACEMENT_CHARACTER, decode_utf16},
-	convert::TryInto,
-	ops::Deref,
-};
-use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};
-use pallet_common::{
-	CommonWeightInfo,
-	erc::{CommonEvmHandler, PrecompileResult},
-};
-use pallet_evm::{account::CrossAccountId, PrecompileHandle};
-use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};
-use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};
-use sp_std::vec::Vec;
-use up_data_structs::{CollectionMode, TokenId};
-
-use crate::{
-	Allowance, Balance, common::CommonWeights, Config, erc721::UniqueRFTCall, Pallet,
-	RefungibleHandle, SelfWeightOf, weights::WeightInfo, TotalSupply,
-};
-
-pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);
-
-#[derive(ToLog)]
-pub enum ERC20Events {
-	Transfer {
-		#[indexed]
-		from: address,
-		#[indexed]
-		to: address,
-		value: uint256,
-	},
-	Approval {
-		#[indexed]
-		owner: address,
-		#[indexed]
-		spender: address,
-		value: uint256,
-	},
-}
-
-#[solidity_interface(name = "ERC20", events(ERC20Events))]
-impl<T: Config> RefungibleTokenHandle<T> {
-	fn name(&self) -> Result<string> {
-		Ok(decode_utf16(self.name.iter().copied())
-			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
-			.collect::<string>())
-	}
-	fn symbol(&self) -> Result<string> {
-		Ok(string::from_utf8_lossy(&self.token_prefix).into())
-	}
-	fn total_supply(&self) -> Result<uint256> {
-		self.consume_store_reads(1)?;
-		Ok(<TotalSupply<T>>::get((self.id, self.1)).into())
-	}
-
-	fn decimals(&self) -> Result<uint8> {
-		Ok(if let CollectionMode::Fungible(decimals) = &self.mode {
-			*decimals
-		} else {
-			unreachable!()
-		})
-	}
-	fn balance_of(&self, owner: address) -> Result<uint256> {
-		self.consume_store_reads(1)?;
-		let owner = T::CrossAccountId::from_eth(owner);
-		let balance = <Balance<T>>::get((self.id, self.1, owner));
-		Ok(balance.into())
-	}
-	#[weight(<CommonWeights<T>>::transfer())]
-	fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> 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")?;
-		let budget = self
-			.recorder
-			.weight_calls_budget(<StructureWeight<T>>::find_parent());
-
-		<Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)
-			.map_err(|_| "transfer error")?;
-		Ok(true)
-	}
-	#[weight(<CommonWeights<T>>::transfer_from())]
-	fn transfer_from(
-		&mut self,
-		caller: caller,
-		from: address,
-		to: address,
-		amount: uint256,
-	) -> Result<bool> {
-		let caller = T::CrossAccountId::from_eth(caller);
-		let from = T::CrossAccountId::from_eth(from);
-		let to = T::CrossAccountId::from_eth(to);
-		let amount = amount.try_into().map_err(|_| "amount overflow")?;
-		let budget = self
-			.recorder
-			.weight_calls_budget(<StructureWeight<T>>::find_parent());
-
-		<Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)
-			.map_err(dispatch_to_evm::<T>)?;
-		Ok(true)
-	}
-	#[weight(<SelfWeightOf<T>>::approve())]
-	fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> 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")?;
-
-		<Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)
-			.map_err(dispatch_to_evm::<T>)?;
-		Ok(true)
-	}
-	fn allowance(&self, owner: address, spender: address) -> Result<uint256> {
-		self.consume_store_reads(1)?;
-		let owner = T::CrossAccountId::from_eth(owner);
-		let spender = T::CrossAccountId::from_eth(spender);
-
-		Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())
-	}
-}
-
-#[solidity_interface(name = "ERC20UniqueExtensions")]
-impl<T: Config> RefungibleTokenHandle<T> {
-	#[weight(<SelfWeightOf<T>>::burn_from())]
-	fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> 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")?;
-		let budget = self
-			.recorder
-			.weight_calls_budget(<StructureWeight<T>>::find_parent());
-
-		<Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)
-			.map_err(dispatch_to_evm::<T>)?;
-		Ok(true)
-	}
-}
-
-impl<T: Config> RefungibleTokenHandle<T> {
-	pub fn into_inner(self) -> RefungibleHandle<T> {
-		self.0
-	}
-	pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {
-		&mut self.0
-	}
-}
-
-impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {
-	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {
-		self.0.recorder()
-	}
-	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {
-		self.0.into_recorder()
-	}
-}
-
-impl<T: Config> Deref for RefungibleTokenHandle<T> {
-	type Target = RefungibleHandle<T>;
-
-	fn deref(&self) -> &Self::Target {
-		&self.0
-	}
-}
-
-#[solidity_interface(
-	name = "UniqueRefungibleToken",
-	is(
-		ERC20,
-		ERC20UniqueExtensions,
-		via("RefungibleHandle<T>", common_mut, UniqueRFT)
-	)
-)]
-impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}
-
-generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);
-generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);
-
-impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>
-where
-	T::AccountId: From<[u8; 32]>,
-{
-	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");
-
-	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
-		call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)
-	}
-}
deletedpallets/refungible/src/erc721.rsdiffbeforeafterboth
--- a/pallets/refungible/src/erc721.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-extern crate alloc;
-use evm_coder::{generate_stubgen, solidity_interface, types::*};
-
-use pallet_common::{CollectionHandle, erc::CollectionCall, erc::CommonEvmHandler};
-
-use pallet_evm::PrecompileHandle;
-use pallet_evm_coder_substrate::call;
-
-use crate::{Config, RefungibleHandle};
-
-#[solidity_interface(
-	name = "UniqueRFT",
-	is(via("CollectionHandle<T>", common_mut, Collection),)
-)]
-impl<T: Config> RefungibleHandle<T> where T::AccountId: From<[u8; 32]> {}
-
-// Not a tests, but code generators
-generate_stubgen!(gen_impl, UniqueRFTCall<()>, true);
-generate_stubgen!(gen_iface, UniqueRFTCall<()>, false);
-
-impl<T: Config> CommonEvmHandler for RefungibleHandle<T>
-where
-	T::AccountId: From<[u8; 32]>,
-{
-	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungible.raw");
-	fn call(
-		self,
-		handle: &mut impl PrecompileHandle,
-	) -> Option<pallet_common::erc::PrecompileResult> {
-		call::<T, UniqueRFTCall<T>, _, _>(handle, self)
-	}
-}
addedpallets/refungible/src/erc_token.rsdiffbeforeafterboth
--- /dev/null
+++ b/pallets/refungible/src/erc_token.rs
@@ -0,0 +1,195 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+extern crate alloc;
+use core::{
+	char::{REPLACEMENT_CHARACTER, decode_utf16},
+	convert::TryInto,
+	ops::Deref,
+};
+use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};
+use pallet_common::{
+	CommonWeightInfo,
+	erc::{CommonEvmHandler, PrecompileResult},
+};
+use pallet_evm::{account::CrossAccountId, PrecompileHandle};
+use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};
+use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};
+use sp_std::vec::Vec;
+use up_data_structs::TokenId;
+
+use crate::{
+	Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,
+	weights::WeightInfo, TotalSupply,
+};
+
+pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);
+
+#[derive(ToLog)]
+pub enum ERC20Events {
+	Transfer {
+		#[indexed]
+		from: address,
+		#[indexed]
+		to: address,
+		value: uint256,
+	},
+	Approval {
+		#[indexed]
+		owner: address,
+		#[indexed]
+		spender: address,
+		value: uint256,
+	},
+}
+
+#[solidity_interface(name = "ERC20", events(ERC20Events))]
+impl<T: Config> RefungibleTokenHandle<T> {
+	fn name(&self) -> Result<string> {
+		Ok(decode_utf16(self.name.iter().copied())
+			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
+			.collect::<string>())
+	}
+	fn symbol(&self) -> Result<string> {
+		Ok(string::from_utf8_lossy(&self.token_prefix).into())
+	}
+	fn total_supply(&self) -> Result<uint256> {
+		self.consume_store_reads(1)?;
+		Ok(<TotalSupply<T>>::get((self.id, self.1)).into())
+	}
+
+	fn decimals(&self) -> Result<uint8> {
+		// Decimals aren't supported for refungible tokens
+		Ok(0)
+	}
+
+	fn balance_of(&self, owner: address) -> Result<uint256> {
+		self.consume_store_reads(1)?;
+		let owner = T::CrossAccountId::from_eth(owner);
+		let balance = <Balance<T>>::get((self.id, self.1, owner));
+		Ok(balance.into())
+	}
+	#[weight(<CommonWeights<T>>::transfer())]
+	fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> 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")?;
+		let budget = self
+			.recorder
+			.weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+		<Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)
+			.map_err(|_| "transfer error")?;
+		Ok(true)
+	}
+	#[weight(<CommonWeights<T>>::transfer_from())]
+	fn transfer_from(
+		&mut self,
+		caller: caller,
+		from: address,
+		to: address,
+		amount: uint256,
+	) -> Result<bool> {
+		let caller = T::CrossAccountId::from_eth(caller);
+		let from = T::CrossAccountId::from_eth(from);
+		let to = T::CrossAccountId::from_eth(to);
+		let amount = amount.try_into().map_err(|_| "amount overflow")?;
+		let budget = self
+			.recorder
+			.weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+		<Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)
+			.map_err(dispatch_to_evm::<T>)?;
+		Ok(true)
+	}
+	#[weight(<SelfWeightOf<T>>::approve())]
+	fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> 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")?;
+
+		<Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)
+			.map_err(dispatch_to_evm::<T>)?;
+		Ok(true)
+	}
+	fn allowance(&self, owner: address, spender: address) -> Result<uint256> {
+		self.consume_store_reads(1)?;
+		let owner = T::CrossAccountId::from_eth(owner);
+		let spender = T::CrossAccountId::from_eth(spender);
+
+		Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())
+	}
+}
+
+#[solidity_interface(name = "ERC20UniqueExtensions")]
+impl<T: Config> RefungibleTokenHandle<T> {
+	#[weight(<SelfWeightOf<T>>::burn_from())]
+	fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> 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")?;
+		let budget = self
+			.recorder
+			.weight_calls_budget(<StructureWeight<T>>::find_parent());
+
+		<Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)
+			.map_err(dispatch_to_evm::<T>)?;
+		Ok(true)
+	}
+}
+
+impl<T: Config> RefungibleTokenHandle<T> {
+	pub fn into_inner(self) -> RefungibleHandle<T> {
+		self.0
+	}
+	pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {
+		&mut self.0
+	}
+}
+
+impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {
+	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {
+		self.0.recorder()
+	}
+	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {
+		self.0.into_recorder()
+	}
+}
+
+impl<T: Config> Deref for RefungibleTokenHandle<T> {
+	type Target = RefungibleHandle<T>;
+
+	fn deref(&self) -> &Self::Target {
+		&self.0
+	}
+}
+
+#[solidity_interface(name = "UniqueRefungibleToken", is(ERC20, ERC20UniqueExtensions,))]
+impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}
+
+generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);
+generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);
+
+impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>
+where
+	T::AccountId: From<[u8; 32]>,
+{
+	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");
+
+	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {
+		call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)
+	}
+}
modifiedpallets/refungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/refungible/src/lib.rs
+++ b/pallets/refungible/src/lib.rs
@@ -87,7 +87,7 @@
 
 #![cfg_attr(not(feature = "std"), no_std)]
 
-use crate::erc20::ERC20Events;
+use crate::erc_token::ERC20Events;
 
 use codec::{Encode, Decode, MaxEncodedLen};
 use core::ops::Deref;
@@ -110,8 +110,8 @@
 #[cfg(feature = "runtime-benchmarks")]
 pub mod benchmarking;
 pub mod common;
-pub mod erc20;
-pub mod erc721;
+pub mod erc;
+pub mod erc_token;
 pub mod weights;
 pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
 
modifiedruntime/common/src/dispatch.rsdiffbeforeafterboth
before · runtime/common/src/dispatch.rs
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};18use pallet_evm::{PrecompileHandle, PrecompileResult};19use sp_core::H160;20use sp_std::{borrow::ToOwned, vec::Vec};21use pallet_common::{22	CollectionById, CollectionHandle, CommonCollectionOperations, erc::CommonEvmHandler,23	eth::map_eth_to_id,24};25pub use pallet_common::dispatch::CollectionDispatch;26use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};27use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};28use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle, erc20::RefungibleTokenHandle};29use up_data_structs::{30	CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,31};3233pub enum CollectionDispatchT<T>34where35	T: pallet_fungible::Config + pallet_nonfungible::Config + pallet_refungible::Config,36{37	Fungible(FungibleHandle<T>),38	Nonfungible(NonfungibleHandle<T>),39	Refungible(RefungibleHandle<T>),40}41impl<T> CollectionDispatch<T> for CollectionDispatchT<T>42where43	T: pallet_common::Config44		+ pallet_unique::Config45		+ pallet_fungible::Config46		+ pallet_nonfungible::Config47		+ pallet_refungible::Config,48{49	fn create(50		sender: T::CrossAccountId,51		data: CreateCollectionData<T::AccountId>,52	) -> DispatchResult {53		let _id = match data.mode {54			CollectionMode::NFT => <PalletNonfungible<T>>::init_collection(sender, data, false)?,55			CollectionMode::Fungible(decimal_points) => {56				// check params57				ensure!(58					decimal_points <= MAX_DECIMAL_POINTS,59					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded60				);61				<PalletFungible<T>>::init_collection(sender, data)?62			}63			CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, data)?,64		};65		Ok(())66	}6768	fn destroy(sender: T::CrossAccountId, collection: CollectionHandle<T>) -> DispatchResult {69		match collection.mode {70			CollectionMode::ReFungible => {71				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?72			}73			CollectionMode::Fungible(_) => {74				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?75			}76			CollectionMode::NFT => {77				PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?78			}79		}80		Ok(())81	}8283	fn dispatch(handle: CollectionHandle<T>) -> Self {84		match handle.mode {85			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),86			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),87			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),88		}89	}9091	fn into_inner(self) -> CollectionHandle<T> {92		match self {93			Self::Fungible(f) => f.into_inner(),94			Self::Nonfungible(f) => f.into_inner(),95			Self::Refungible(f) => f.into_inner(),96		}97	}9899	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {100		match self {101			Self::Fungible(h) => h,102			Self::Nonfungible(h) => h,103			Self::Refungible(h) => h,104		}105	}106}107108impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>109where110	T: pallet_common::Config111		+ pallet_unique::Config112		+ pallet_fungible::Config113		+ pallet_nonfungible::Config114		+ pallet_refungible::Config,115	T::AccountId: From<[u8; 32]>,116{117	fn is_reserved(target: &H160) -> bool {118		map_eth_to_id(target).is_some()119	}120	fn is_used(target: &H160) -> bool {121		map_eth_to_id(target)122			.map(<CollectionById<T>>::contains_key)123			.unwrap_or(false)124	}125	fn get_code(target: &H160) -> Option<Vec<u8>> {126		if let Some(collection_id) = map_eth_to_id(target) {127			let collection = <CollectionById<T>>::get(collection_id)?;128			Some(129				match collection.mode {130					CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,131					CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,132					CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,133				}134				.to_owned(),135			)136		} else if let Some((collection_id, _token_id)) =137			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)138		{139			let collection = <CollectionById<T>>::get(collection_id)?;140			if collection.mode != CollectionMode::ReFungible {141				return None;142			}143			// TODO: check token existence144			Some(<RefungibleTokenHandle<T>>::CODE.to_owned())145		} else {146			None147		}148	}149	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {150		if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {151			let collection =152				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;153			let dispatched = Self::dispatch(collection);154155			match dispatched {156				Self::Fungible(h) => h.call(handle),157				Self::Nonfungible(h) => h.call(handle),158				Self::Refungible(h) => h.call(handle),159			}160		} else if let Some((collection_id, token_id)) =161			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(162				&handle.code_address(),163			) {164			let collection =165				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;166			if collection.mode != CollectionMode::ReFungible {167				return None;168			}169170			let h = RefungibleHandle::cast(collection);171			// TODO: check token existence172			RefungibleTokenHandle(h, token_id).call(handle)173		} else {174			None175		}176	}177}
after · runtime/common/src/dispatch.rs
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};18use pallet_evm::{PrecompileHandle, PrecompileResult};19use sp_core::H160;20use sp_std::{borrow::ToOwned, vec::Vec};21use pallet_common::{22	CollectionById, CollectionHandle, CommonCollectionOperations, erc::CommonEvmHandler,23	eth::map_eth_to_id,24};25pub use pallet_common::dispatch::CollectionDispatch;26use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};27use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};28use pallet_refungible::{29	Pallet as PalletRefungible, RefungibleHandle, erc_token::RefungibleTokenHandle,30};31use up_data_structs::{32	CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,33};3435pub enum CollectionDispatchT<T>36where37	T: pallet_fungible::Config + pallet_nonfungible::Config + pallet_refungible::Config,38{39	Fungible(FungibleHandle<T>),40	Nonfungible(NonfungibleHandle<T>),41	Refungible(RefungibleHandle<T>),42}43impl<T> CollectionDispatch<T> for CollectionDispatchT<T>44where45	T: pallet_common::Config46		+ pallet_unique::Config47		+ pallet_fungible::Config48		+ pallet_nonfungible::Config49		+ pallet_refungible::Config,50{51	fn create(52		sender: T::CrossAccountId,53		data: CreateCollectionData<T::AccountId>,54	) -> DispatchResult {55		let _id = match data.mode {56			CollectionMode::NFT => <PalletNonfungible<T>>::init_collection(sender, data, false)?,57			CollectionMode::Fungible(decimal_points) => {58				// check params59				ensure!(60					decimal_points <= MAX_DECIMAL_POINTS,61					pallet_unique::Error::<T>::CollectionDecimalPointLimitExceeded62				);63				<PalletFungible<T>>::init_collection(sender, data)?64			}65			CollectionMode::ReFungible => <PalletRefungible<T>>::init_collection(sender, data)?,66		};67		Ok(())68	}6970	fn destroy(sender: T::CrossAccountId, collection: CollectionHandle<T>) -> DispatchResult {71		match collection.mode {72			CollectionMode::ReFungible => {73				PalletRefungible::destroy_collection(RefungibleHandle::cast(collection), &sender)?74			}75			CollectionMode::Fungible(_) => {76				PalletFungible::destroy_collection(FungibleHandle::cast(collection), &sender)?77			}78			CollectionMode::NFT => {79				PalletNonfungible::destroy_collection(NonfungibleHandle::cast(collection), &sender)?80			}81		}82		Ok(())83	}8485	fn dispatch(handle: CollectionHandle<T>) -> Self {86		match handle.mode {87			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),88			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),89			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),90		}91	}9293	fn into_inner(self) -> CollectionHandle<T> {94		match self {95			Self::Fungible(f) => f.into_inner(),96			Self::Nonfungible(f) => f.into_inner(),97			Self::Refungible(f) => f.into_inner(),98		}99	}100101	fn as_dyn(&self) -> &dyn CommonCollectionOperations<T> {102		match self {103			Self::Fungible(h) => h,104			Self::Nonfungible(h) => h,105			Self::Refungible(h) => h,106		}107	}108}109110impl<T> pallet_evm::OnMethodCall<T> for CollectionDispatchT<T>111where112	T: pallet_common::Config113		+ pallet_unique::Config114		+ pallet_fungible::Config115		+ pallet_nonfungible::Config116		+ pallet_refungible::Config,117	T::AccountId: From<[u8; 32]>,118{119	fn is_reserved(target: &H160) -> bool {120		map_eth_to_id(target).is_some()121	}122	fn is_used(target: &H160) -> bool {123		map_eth_to_id(target)124			.map(<CollectionById<T>>::contains_key)125			.unwrap_or(false)126	}127	fn get_code(target: &H160) -> Option<Vec<u8>> {128		if let Some(collection_id) = map_eth_to_id(target) {129			let collection = <CollectionById<T>>::get(collection_id)?;130			Some(131				match collection.mode {132					CollectionMode::NFT => <NonfungibleHandle<T>>::CODE,133					CollectionMode::Fungible(_) => <FungibleHandle<T>>::CODE,134					CollectionMode::ReFungible => <RefungibleHandle<T>>::CODE,135				}136				.to_owned(),137			)138		} else if let Some((collection_id, _token_id)) =139			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(target)140		{141			let collection = <CollectionById<T>>::get(collection_id)?;142			if collection.mode != CollectionMode::ReFungible {143				return None;144			}145			// TODO: check token existence146			Some(<RefungibleTokenHandle<T>>::CODE.to_owned())147		} else {148			None149		}150	}151	fn call(handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {152		if let Some(collection_id) = map_eth_to_id(&handle.code_address()) {153			let collection =154				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;155			let dispatched = Self::dispatch(collection);156157			match dispatched {158				Self::Fungible(h) => h.call(handle),159				Self::Nonfungible(h) => h.call(handle),160				Self::Refungible(h) => h.call(handle),161			}162		} else if let Some((collection_id, token_id)) =163			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(164				&handle.code_address(),165			) {166			let collection =167				<CollectionHandle<T>>::new_with_gas_limit(collection_id, handle.remaining_gas())?;168			if collection.mode != CollectionMode::ReFungible {169				return None;170			}171172			let h = RefungibleHandle::cast(collection);173			// TODO: check token existence174			RefungibleTokenHandle(h, token_id).call(handle)175		} else {176			None177		}178	}179}
modifiedtests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth
--- a/tests/src/eth/reFungibleToken.test.ts
+++ b/tests/src/eth/reFungibleToken.test.ts
@@ -55,6 +55,22 @@
 
     expect(balance).to.equal('200');
   });
+
+  itWeb3('decimals', async ({api, web3, privateKeyWrapper}) => {
+    const alice = privateKeyWrapper('//Alice');
+
+    const collectionId = (await createCollection(api, alice, {name: 'token name', mode: {type: 'ReFungible'}})).collectionId;
+
+    const caller = await createEthAccountWithBalance(api, web3, privateKeyWrapper);
+
+    const tokenId = (await createRefungibleToken(api, alice, collectionId, 200n, {Ethereum: caller})).itemId;
+
+    const address = tokenIdToAddress(collectionId, tokenId);
+    const contract = new web3.eth.Contract(reFungibleTokenAbi as any, address, {from: caller, ...GAS_ARGS});
+    const decimals = await contract.methods.decimals().call();
+
+    expect(decimals).to.equal('0');
+  });
 });
 
 describe('Refungible: Plain calls', () => {