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
before · pallets/refungible/src/erc20.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/>.1617extern crate alloc;18use core::{19	char::{REPLACEMENT_CHARACTER, decode_utf16},20	convert::TryInto,21	ops::Deref,22};23use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};24use pallet_common::{25	CommonWeightInfo,26	erc::{CommonEvmHandler, PrecompileResult},27};28use pallet_evm::{account::CrossAccountId, PrecompileHandle};29use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};30use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};31use sp_std::vec::Vec;32use up_data_structs::{CollectionMode, TokenId};3334use crate::{35	Allowance, Balance, common::CommonWeights, Config, erc721::UniqueRFTCall, Pallet,36	RefungibleHandle, SelfWeightOf, weights::WeightInfo, TotalSupply,37};3839pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);4041#[derive(ToLog)]42pub enum ERC20Events {43	Transfer {44		#[indexed]45		from: address,46		#[indexed]47		to: address,48		value: uint256,49	},50	Approval {51		#[indexed]52		owner: address,53		#[indexed]54		spender: address,55		value: uint256,56	},57}5859#[solidity_interface(name = "ERC20", events(ERC20Events))]60impl<T: Config> RefungibleTokenHandle<T> {61	fn name(&self) -> Result<string> {62		Ok(decode_utf16(self.name.iter().copied())63			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))64			.collect::<string>())65	}66	fn symbol(&self) -> Result<string> {67		Ok(string::from_utf8_lossy(&self.token_prefix).into())68	}69	fn total_supply(&self) -> Result<uint256> {70		self.consume_store_reads(1)?;71		Ok(<TotalSupply<T>>::get((self.id, self.1)).into())72	}7374	fn decimals(&self) -> Result<uint8> {75		Ok(if let CollectionMode::Fungible(decimals) = &self.mode {76			*decimals77		} else {78			unreachable!()79		})80	}81	fn balance_of(&self, owner: address) -> Result<uint256> {82		self.consume_store_reads(1)?;83		let owner = T::CrossAccountId::from_eth(owner);84		let balance = <Balance<T>>::get((self.id, self.1, owner));85		Ok(balance.into())86	}87	#[weight(<CommonWeights<T>>::transfer())]88	fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {89		let caller = T::CrossAccountId::from_eth(caller);90		let to = T::CrossAccountId::from_eth(to);91		let amount = amount.try_into().map_err(|_| "amount overflow")?;92		let budget = self93			.recorder94			.weight_calls_budget(<StructureWeight<T>>::find_parent());9596		<Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)97			.map_err(|_| "transfer error")?;98		Ok(true)99	}100	#[weight(<CommonWeights<T>>::transfer_from())]101	fn transfer_from(102		&mut self,103		caller: caller,104		from: address,105		to: address,106		amount: uint256,107	) -> Result<bool> {108		let caller = T::CrossAccountId::from_eth(caller);109		let from = T::CrossAccountId::from_eth(from);110		let to = T::CrossAccountId::from_eth(to);111		let amount = amount.try_into().map_err(|_| "amount overflow")?;112		let budget = self113			.recorder114			.weight_calls_budget(<StructureWeight<T>>::find_parent());115116		<Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)117			.map_err(dispatch_to_evm::<T>)?;118		Ok(true)119	}120	#[weight(<SelfWeightOf<T>>::approve())]121	fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {122		let caller = T::CrossAccountId::from_eth(caller);123		let spender = T::CrossAccountId::from_eth(spender);124		let amount = amount.try_into().map_err(|_| "amount overflow")?;125126		<Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)127			.map_err(dispatch_to_evm::<T>)?;128		Ok(true)129	}130	fn allowance(&self, owner: address, spender: address) -> Result<uint256> {131		self.consume_store_reads(1)?;132		let owner = T::CrossAccountId::from_eth(owner);133		let spender = T::CrossAccountId::from_eth(spender);134135		Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())136	}137}138139#[solidity_interface(name = "ERC20UniqueExtensions")]140impl<T: Config> RefungibleTokenHandle<T> {141	#[weight(<SelfWeightOf<T>>::burn_from())]142	fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {143		let caller = T::CrossAccountId::from_eth(caller);144		let from = T::CrossAccountId::from_eth(from);145		let amount = amount.try_into().map_err(|_| "amount overflow")?;146		let budget = self147			.recorder148			.weight_calls_budget(<StructureWeight<T>>::find_parent());149150		<Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)151			.map_err(dispatch_to_evm::<T>)?;152		Ok(true)153	}154}155156impl<T: Config> RefungibleTokenHandle<T> {157	pub fn into_inner(self) -> RefungibleHandle<T> {158		self.0159	}160	pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {161		&mut self.0162	}163}164165impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {166	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {167		self.0.recorder()168	}169	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {170		self.0.into_recorder()171	}172}173174impl<T: Config> Deref for RefungibleTokenHandle<T> {175	type Target = RefungibleHandle<T>;176177	fn deref(&self) -> &Self::Target {178		&self.0179	}180}181182#[solidity_interface(183	name = "UniqueRefungibleToken",184	is(185		ERC20,186		ERC20UniqueExtensions,187		via("RefungibleHandle<T>", common_mut, UniqueRFT)188	)189)]190impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}191192generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);193generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);194195impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>196where197	T::AccountId: From<[u8; 32]>,198{199	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");200201	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {202		call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)203	}204}
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
--- a/runtime/common/src/dispatch.rs
+++ b/runtime/common/src/dispatch.rs
@@ -25,7 +25,9 @@
 pub use pallet_common::dispatch::CollectionDispatch;
 use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};
 use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};
-use pallet_refungible::{Pallet as PalletRefungible, RefungibleHandle, erc20::RefungibleTokenHandle};
+use pallet_refungible::{
+	Pallet as PalletRefungible, RefungibleHandle, erc_token::RefungibleTokenHandle,
+};
 use up_data_structs::{
 	CollectionMode, CreateCollectionData, MAX_DECIMAL_POINTS, mapping::TokenAddressMapping,
 };
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', () => {