git.delta.rocks / unique-network / refs/commits / d5c411b79f24

difftreelog

feat impl approve, allowance, balance_of

Trubnikov Sergey2023-04-20parent: #b3a27fe.patch.diff
in: master

3 files changed

modifiedpallets/balances-adapter/src/erc.rsdiffbeforeafterboth
before · pallets/balances-adapter/src/erc.rs
1use crate::Config;2use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};3use pallet_common::erc::{CommonEvmHandler, PrecompileHandle, PrecompileResult};4use pallet_evm_coder_substrate::{5	call, dispatch_to_evm,6	execution::{PreDispatch, Result},7	frontier_contract, WithRecorder, SubstrateRecorder,8};9use sp_core::{U256};10use sp_std::vec::Vec;1112frontier_contract! {13	macro_rules! NativeFungibleHandle_result {...}14	impl<T: Config> Contract for NativeFungibleHandle<T> {...}15}1617#[derive(ToLog)]18pub enum ERC20Events {19	Transfer {20		#[indexed]21		from: Address,22		#[indexed]23		to: Address,24		value: U256,25	},26	Approval {27		#[indexed]28		owner: Address,29		#[indexed]30		spender: Address,31		value: U256,32	},33}3435pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);3637impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {38	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {39		&self.040	}41	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {42		self.043	}44}4546#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]47impl<T: Config> NativeFungibleHandle<T> {48	fn name(&self) -> Result<String> {49		// Ok(decode_utf16(self.name.iter().copied())50		// 	.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))51		// 	.collect::<String>())52		todo!()53	}5455	fn symbol(&self) -> Result<String> {56		// Ok(String::from_utf8_lossy(&self.token_prefix).into())57		todo!()58	}59	fn total_supply(&self) -> Result<U256> {60		// self.consume_store_reads(1)?;61		// Ok(<TotalSupply<T>>::get(self.id).into())62		todo!()63	}6465	fn decimals(&self) -> Result<u8> {66		// Ok(if let CollectionMode::Fungible(decimals) = &self.mode {67		// 	*decimals68		// } else {69		// 	unreachable!()70		// })71		todo!()72	}7374	fn balance_of(&self, owner: Address) -> Result<U256> {75		// self.consume_store_reads(1)?;76		// let owner = T::CrossAccountId::from_eth(owner);77		// let balance = <Balance<T>>::get((self.id, owner));78		// Ok(balance.into())79		todo!()80	}8182	// #[weight(<SelfWeightOf<T>>::transfer())]83	fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {84		// let caller = T::CrossAccountId::from_eth(caller);85		// let to = T::CrossAccountId::from_eth(to);86		// let amount = amount.try_into().map_err(|_| "amount overflow")?;87		// let budget = self88		// 	.recorder89		// 	.weight_calls_budget(<StructureWeight<T>>::find_parent());9091		// <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;92		// Ok(true)93		todo!()94	}9596	// #[weight(<SelfWeightOf<T>>::transfer_from())]97	fn transfer_from(98		&mut self,99		caller: Caller,100		from: Address,101		to: Address,102		amount: U256,103	) -> Result<bool> {104		// let caller = T::CrossAccountId::from_eth(caller);105		// let from = T::CrossAccountId::from_eth(from);106		// let to = T::CrossAccountId::from_eth(to);107		// let amount = amount.try_into().map_err(|_| "amount overflow")?;108		// let budget = self109		// 	.recorder110		// 	.weight_calls_budget(<StructureWeight<T>>::find_parent());111112		// <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)113		// 	.map_err(dispatch_to_evm::<T>)?;114		// Ok(true)115		todo!()116	}117118	// #[weight(<SelfWeightOf<T>>::approve())]119	fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {120		// let caller = T::CrossAccountId::from_eth(caller);121		// let spender = T::CrossAccountId::from_eth(spender);122		// let amount = amount.try_into().map_err(|_| "amount overflow")?;123124		// <Pallet<T>>::set_allowance(self, &caller, &spender, amount)125		// 	.map_err(dispatch_to_evm::<T>)?;126		// Ok(true)127		todo!()128	}129130	fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {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, owner, spender)).into())136		todo!()137	}138}139140#[solidity_interface(141	name = UniqueNativeFungible,142	is(ERC20),143	enum(derive(PreDispatch))144)]145impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}146147generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);148generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);149150impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>151where152	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,153{154	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");155156	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {157		call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)158	}159}
after · pallets/balances-adapter/src/erc.rs
1use crate::Config;2use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};3use frame_support::traits::Currency;4use pallet_common::erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult};5use pallet_evm_coder_substrate::{6	call, dispatch_to_evm,7	execution::{PreDispatch, Result},8	frontier_contract, WithRecorder, SubstrateRecorder,9};10use sp_core::{U256};11use sp_std::vec::Vec;1213frontier_contract! {14	macro_rules! NativeFungibleHandle_result {...}15	impl<T: Config> Contract for NativeFungibleHandle<T> {...}16}1718#[derive(ToLog)]19pub enum ERC20Events {20	Transfer {21		#[indexed]22		from: Address,23		#[indexed]24		to: Address,25		value: U256,26	},27	Approval {28		#[indexed]29		owner: Address,30		#[indexed]31		spender: Address,32		value: U256,33	},34}3536pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);3738impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {39	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {40		&self.041	}42	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {43		self.044	}45}4647#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]48impl<T: Config> NativeFungibleHandle<T> {49	fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {50		Ok(U256::zero())51	}5253	// #[weight(<SelfWeightOf<T>>::approve())]54	fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {55		// self.consume_store_reads(1)?;56		Err("Approve not supported now".into())57	}58	fn balance_of(&self, owner: Address) -> Result<U256> {59		// self.consume_store_reads(1)?;60		let owner = T::CrossAccountId::from_eth(owner);61		let a = <T as Config>::Currency::free_balance(owner.as_sub());62		Ok(a.into())63	}6465	fn decimals(&self) -> Result<u8> {66		// Ok(if let CollectionMode::Fungible(decimals) = &self.mode {67		// 	*decimals68		// } else {69		// 	unreachable!70		// })7172		// From config 1873		todo!()74	}7576	fn name(&self) -> Result<String> {77		// Ok(decode_utf16(self.name.iter().copied())78		// 	.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))79		// 	.collect::<String>())80		todo!()81	}8283	fn symbol(&self) -> Result<String> {84		// Ok(String::from_utf8_lossy(&self.token_prefix).into())85		todo!()86	}8788	fn total_supply(&self) -> Result<U256> {89		// self.consume_store_reads(1)?;90		// Ok(<TotalSupply<T>>::get(self.id).into())91		todo!()92	}9394	// #[weight(<SelfWeightOf<T>>::transfer())]95	fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {96		// let caller = T::CrossAccountId::from_eth(caller);97		// let to = T::CrossAccountId::from_eth(to);98		// let amount = amount.try_into().map_err(|_| "amount overflow")?;99		// let budget = self100		// 	.recorder101		// 	.weight_calls_budget(<StructureWeight<T>>::find_parent());102103		// <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;104		// Ok(true)105		todo!()106	}107108	// #[weight(<SelfWeightOf<T>>::transfer_from())]109	fn transfer_from(110		&mut self,111		caller: Caller,112		from: Address,113		to: Address,114		amount: U256,115	) -> Result<bool> {116		// let caller = T::CrossAccountId::from_eth(caller);117		// let from = T::CrossAccountId::from_eth(from);118		// let to = T::CrossAccountId::from_eth(to);119		// let amount = amount.try_into().map_err(|_| "amount overflow")?;120		// let budget = self121		// 	.recorder122		// 	.weight_calls_budget(<StructureWeight<T>>::find_parent());123124		// <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)125		// 	.map_err(dispatch_to_evm::<T>)?;126		// Ok(true)127		todo!()128	}129}130131#[solidity_interface(132	name = UniqueNativeFungible,133	is(ERC20),134	enum(derive(PreDispatch))135)]136impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}137138generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);139generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);140141impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>142where143	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,144{145	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");146147	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {148		call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)149	}150}
modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -8,8 +8,16 @@
 
 #[frame_support::pallet]
 pub mod pallet {
+	use sp_core::U256;
+
 	#[pallet::config]
-	pub trait Config: frame_system::Config + pallet_evm_coder_substrate::Config {}
+	pub trait Config: frame_system::Config + pallet_evm_coder_substrate::Config {
+		type Currency: frame_support::traits::Currency<
+			Self::AccountId,
+			Balance = Self::CurrencyBalance,
+		>;
+		type CurrencyBalance: Into<U256>;
+	}
 	#[pallet::pallet]
 	pub struct Pallet<T>(_);
 
modifiedruntime/common/config/pallets/mod.rsdiffbeforeafterboth
--- a/runtime/common/config/pallets/mod.rs
+++ b/runtime/common/config/pallets/mod.rs
@@ -25,7 +25,7 @@
 	},
 	Runtime, RuntimeEvent, RuntimeCall, RuntimeOrigin, Balances,
 };
-use frame_support::traits::{ConstU32, ConstU64};
+use frame_support::traits::{ConstU32, ConstU64, Currency};
 use up_common::{
 	types::{AccountId, Balance, BlockNumber},
 	constants::*,
@@ -84,7 +84,8 @@
 	type WeightInfo = pallet_nonfungible::weights::SubstrateWeight<Self>;
 }
 impl pallet_balances_adapter::Config for Runtime {
-	// type WeightInfo = pallet_nonfungible::weights::SubstrateWeight<Self>;
+	type Currency = Balances;
+	type CurrencyBalance = <Balances as Currency<Self::AccountId>>::Balance;
 }
 
 parameter_types! {