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

difftreelog

feat add dispatching

Trubnikov Sergey2023-04-21parent: #60662cc.patch.diff
in: master

6 files changed

addedpallets/balances-adapter/src/common.rsdiffbeforeafterboth

no changes

modifiedpallets/balances-adapter/src/erc.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/erc.rs
+++ b/pallets/balances-adapter/src/erc.rs
@@ -1,5 +1,5 @@
-use crate::Config;
-use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};
+use crate::{Config, NativeFungibleHandle};
+use evm_coder::{abi::AbiType, ToLog, generate_stubgen, solidity_interface, types::*};
 use frame_support::traits::{Currency, ExistenceRequirement};
 use pallet_common::{
 	erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},
@@ -34,17 +34,6 @@
 		spender: Address,
 		value: U256,
 	},
-}
-
-pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);
-
-impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {
-	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {
-		&self.0
-	}
-	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {
-		self.0
-	}
 }
 
 #[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]
modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -4,17 +4,42 @@
 
 extern crate alloc;
 pub use pallet::*;
+use pallet_common::CollectionHandle;
+use pallet_evm_coder_substrate::{WithRecorder, SubstrateRecorder};
 
+pub mod common;
 pub mod erc;
 
+pub struct NativeFungibleHandle<T: Config>(CollectionHandle<T>);
+impl<T: Config> NativeFungibleHandle<T> {
+	pub fn cast(inner: CollectionHandle<T>) -> Self {
+		Self(inner)
+	}
+
+	/// Casts [`NativeFungibleHandle`] into [`CollectionHandle`][`pallet_common::CollectionHandle`].
+	pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {
+		self.0
+	}
+}
+
+impl<T: Config> WithRecorder<T> for NativeFungibleHandle<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.recorder
+	}
+}
 #[frame_support::pallet]
 pub mod pallet {
 	use alloc::string::String;
-	use frame_support::traits::Get;
+	use frame_support::{traits::Get, sp_runtime::DispatchResult};
 	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 + pallet_common::Config
+	{
 		type Currency: frame_support::traits::Currency<
 			Self::AccountId,
 			Balance = Self::CurrencyBalance,
@@ -28,6 +53,10 @@
 	#[pallet::pallet]
 	pub struct Pallet<T>(_);
 
-	#[pallet::call]
-	impl<T: Config> Pallet<T> {}
+	// #[pallet::call]
+	impl<T: Config> Pallet<T> {
+		pub fn dummy() -> DispatchResult {
+			Ok(())
+		}
+	}
 }
modifiedruntime/common/dispatch.rsdiffbeforeafterboth
--- a/runtime/common/dispatch.rs
+++ b/runtime/common/dispatch.rs
@@ -25,6 +25,7 @@
 };
 pub use pallet_common::dispatch::CollectionDispatch;
 use pallet_fungible::{Pallet as PalletFungible, FungibleHandle};
+use pallet_balances_adapter::{Pallet as PalletNativeFungible, NativeFungibleHandle};
 use pallet_nonfungible::{Pallet as PalletNonfungible, NonfungibleHandle};
 use pallet_refungible::{
 	Pallet as PalletRefungible, RefungibleHandle, erc_token::RefungibleTokenHandle,
@@ -39,11 +40,15 @@
 
 pub enum CollectionDispatchT<T>
 where
-	T: pallet_fungible::Config + pallet_nonfungible::Config + pallet_refungible::Config,
+	T: pallet_fungible::Config
+		+ pallet_nonfungible::Config
+		+ pallet_refungible::Config
+		+ pallet_balances_adapter::Config,
 {
 	Fungible(FungibleHandle<T>),
 	Nonfungible(NonfungibleHandle<T>),
 	Refungible(RefungibleHandle<T>),
+	NativeFungible(NativeFungibleHandle<T>),
 }
 impl<T> CollectionDispatch<T> for CollectionDispatchT<T>
 where
@@ -51,7 +56,8 @@
 		+ pallet_unique::Config
 		+ pallet_fungible::Config
 		+ pallet_nonfungible::Config
-		+ pallet_refungible::Config,
+		+ pallet_refungible::Config
+		+ pallet_balances_adapter::Config,
 {
 	fn create(
 		sender: T::CrossAccountId,
@@ -100,7 +106,13 @@
 
 	fn dispatch(handle: CollectionHandle<T>) -> Self {
 		match handle.mode {
-			CollectionMode::Fungible(_) => Self::Fungible(FungibleHandle::cast(handle)),
+			CollectionMode::Fungible(_) => {
+				if handle.id != up_data_structs::CollectionId(0) {
+					Self::Fungible(FungibleHandle::cast(handle))
+				} else {
+					Self::NativeFungible(NativeFungibleHandle::cast(handle))
+				}
+			}
 			CollectionMode::NFT => Self::Nonfungible(NonfungibleHandle::cast(handle)),
 			CollectionMode::ReFungible => Self::Refungible(RefungibleHandle::cast(handle)),
 		}
@@ -111,6 +123,7 @@
 			Self::Fungible(f) => f.into_inner(),
 			Self::Nonfungible(f) => f.into_inner(),
 			Self::Refungible(f) => f.into_inner(),
+			Self::NativeFungible(f) => f.into_inner(),
 		}
 	}
 
@@ -119,6 +132,7 @@
 			Self::Fungible(h) => h,
 			Self::Nonfungible(h) => h,
 			Self::Refungible(h) => h,
+			Self::NativeFungible(h) => h,
 		}
 	}
 }
@@ -129,7 +143,8 @@
 		+ pallet_unique::Config
 		+ pallet_fungible::Config
 		+ pallet_nonfungible::Config
-		+ pallet_refungible::Config,
+		+ pallet_refungible::Config
+		+ pallet_balances_adapter::Config,
 	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,
 {
 	fn is_reserved(target: &H160) -> bool {
@@ -174,6 +189,7 @@
 				Self::Fungible(h) => h.call(handle),
 				Self::Nonfungible(h) => h.call(handle),
 				Self::Refungible(h) => h.call(handle),
+				Self::NativeFungible(f) => todo!(),
 			}
 		} else if let Some((collection_id, token_id)) =
 			<T as pallet_common::Config>::EvmTokenAddressMapping::address_to_token(
addedtests/src/eth/nativeFungible.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/eth/nativeFungible.test.ts
@@ -0,0 +1,44 @@
+// Copyright 2019-2023 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/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import {itEth, usingEthPlaygrounds} from './util';
+
+describe('NativeFungible: Plain calls', () => {
+  let donor: IKeyringPair;
+  let alice: IKeyringPair;
+  let owner: IKeyringPair;
+
+  before(async function() {
+    await usingEthPlaygrounds(async (helper, privateKey) => {
+      donor = await privateKey({url: import.meta.url});
+      [alice, owner] = await helper.arrange.createAccounts([30n, 20n], donor);
+    });
+  });
+
+  itEth.only('Can perform approve()', async ({helper}) => {
+    const owner = await helper.eth.createAccountWithBalance(donor);
+    const spender = helper.eth.createAccount();
+    const collection = await helper.ft.mintCollection(alice);
+    await collection.mint(alice, 200n, {Ethereum: owner});
+
+    const collectionAddress = helper.ethAddress.fromCollectionId(0);
+    const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
+
+
+    await contract.methods.approve(spender, 100).send({from: owner});
+  });
+});
\ No newline at end of file
modifiedtests/src/eth/util/playgrounds/unique.dev.tsdiffbeforeafterboth
--- a/tests/src/eth/util/playgrounds/unique.dev.ts
+++ b/tests/src/eth/util/playgrounds/unique.dev.ts
@@ -141,11 +141,16 @@
   }
 
   async collection(address: string, mode: TCollectionMode, caller?: string, mergeDeprecated = false) {
-    let abi = {
-      'nft': nonFungibleAbi,
-      'rft': refungibleAbi,
-      'ft': fungibleAbi,
-    }[mode];
+    let abi;
+    if (address === '0' && mode === 'ft') {
+      abi = nativeFungibleAbi;
+    } else {
+      abi ={
+        'nft': nonFungibleAbi,
+        'rft': refungibleAbi,
+        'ft': fungibleAbi,
+      }[mode];
+    }
     if (mergeDeprecated) {
       const deprecated = {
         'nft': nonFungibleDeprecatedAbi,