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

difftreelog

feat impl transfer, transer_from, transfer_cross, transfer_from_cross,

Trubnikov Sergey2023-04-21parent: #1e5b247.patch.diff
in: master

6 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 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, Get};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 balance = <T as Config>::Currency::free_balance(owner.as_sub());62		Ok(balance.into())63	}6465	fn decimals(&self) -> Result<u8> {66		Ok(T::Decimals::get())67	}6869	fn name(&self) -> Result<String> {70		Ok(T::Name::get())71	}7273	fn symbol(&self) -> Result<String> {74		Ok(T::Symbol::get())75	}7677	fn total_supply(&self) -> Result<U256> {78		// self.consume_store_reads(1)?;79		let total = <T as Config>::Currency::total_issuance();80		Ok(total.into())81	}8283	// #[weight(<SelfWeightOf<T>>::transfer())]84	fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {85		// let caller = T::CrossAccountId::from_eth(caller);86		// let to = T::CrossAccountId::from_eth(to);87		// let amount = amount.try_into().map_err(|_| "amount overflow")?;88		// let budget = self89		// 	.recorder90		// 	.weight_calls_budget(<StructureWeight<T>>::find_parent());9192		// <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;93		// Ok(true)94		todo!()95	}9697	// #[weight(<SelfWeightOf<T>>::transfer_from())]98	fn transfer_from(99		&mut self,100		caller: Caller,101		from: Address,102		to: Address,103		amount: U256,104	) -> Result<bool> {105		// let caller = T::CrossAccountId::from_eth(caller);106		// let from = T::CrossAccountId::from_eth(from);107		// let to = T::CrossAccountId::from_eth(to);108		// let amount = amount.try_into().map_err(|_| "amount overflow")?;109		// let budget = self110		// 	.recorder111		// 	.weight_calls_budget(<StructureWeight<T>>::find_parent());112113		// <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)114		// 	.map_err(dispatch_to_evm::<T>)?;115		// Ok(true)116		todo!()117	}118}119120#[solidity_interface(121	name = UniqueNativeFungible,122	is(ERC20),123	enum(derive(PreDispatch))124)]125impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}126127generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);128generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);129130impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>131where132	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,133{134	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");135136	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {137		call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)138	}139}
modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -17,7 +17,7 @@
 			Self::AccountId,
 			Balance = Self::CurrencyBalance,
 		>;
-		type CurrencyBalance: Into<U256>;
+		type CurrencyBalance: Into<U256> + TryFrom<U256>;
 
 		type Decimals: Get<u8>;
 		type Name: Get<String>;
modifiedpallets/balances-adapter/src/stubs/UniqueNativeFungible.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/balances-adapter/src/stubs/UniqueNativeFungible.soldiffbeforeafterboth
--- a/pallets/balances-adapter/src/stubs/UniqueNativeFungible.sol
+++ b/pallets/balances-adapter/src/stubs/UniqueNativeFungible.sol
@@ -17,6 +17,40 @@
 	}
 }
 
+/// @dev the ERC-165 identifier for this interface is 0xff15c6f4
+contract ERC20UniqueExtensions is Dummy, ERC165 {
+	/// @dev EVM selector for this function is: 0x2ada85ff,
+	///  or in textual repr: transferCross((address,uint256),uint256)
+	function transferCross(CrossAddress memory to, uint256 amount) public returns (bool) {
+		require(false, stub_error);
+		to;
+		amount;
+		dummy = 0;
+		return false;
+	}
+
+	/// @dev EVM selector for this function is: 0xd5cf430b,
+	///  or in textual repr: transferFromCross((address,uint256),(address,uint256),uint256)
+	function transferFromCross(
+		CrossAddress memory from,
+		CrossAddress memory to,
+		uint256 amount
+	) public returns (bool) {
+		require(false, stub_error);
+		from;
+		to;
+		amount;
+		dummy = 0;
+		return false;
+	}
+}
+
+/// Cross account struct
+struct CrossAddress {
+	address eth;
+	uint256 sub;
+}
+
 /// @dev inlined interface
 contract ERC20Events {
 	event Transfer(address indexed from, address indexed to, uint256 value);
@@ -25,26 +59,31 @@
 
 /// @dev the ERC-165 identifier for this interface is 0x942e8b22
 contract ERC20 is Dummy, ERC165, ERC20Events {
-	/// @dev EVM selector for this function is: 0x06fdde03,
-	///  or in textual repr: name()
-	function name() public view returns (string memory) {
+	/// @dev EVM selector for this function is: 0xdd62ed3e,
+	///  or in textual repr: allowance(address,address)
+	function allowance(address owner, address spender) public view returns (uint256) {
 		require(false, stub_error);
+		owner;
+		spender;
 		dummy;
-		return "";
+		return 0;
 	}
 
-	/// @dev EVM selector for this function is: 0x95d89b41,
-	///  or in textual repr: symbol()
-	function symbol() public view returns (string memory) {
+	/// @dev EVM selector for this function is: 0x095ea7b3,
+	///  or in textual repr: approve(address,uint256)
+	function approve(address spender, uint256 amount) public returns (bool) {
 		require(false, stub_error);
-		dummy;
-		return "";
+		spender;
+		amount;
+		dummy = 0;
+		return false;
 	}
 
-	/// @dev EVM selector for this function is: 0x18160ddd,
-	///  or in textual repr: totalSupply()
-	function totalSupply() public view returns (uint256) {
+	/// @dev EVM selector for this function is: 0x70a08231,
+	///  or in textual repr: balanceOf(address)
+	function balanceOf(address owner) public view returns (uint256) {
 		require(false, stub_error);
+		owner;
 		dummy;
 		return 0;
 	}
@@ -57,11 +96,26 @@
 		return 0;
 	}
 
-	/// @dev EVM selector for this function is: 0x70a08231,
-	///  or in textual repr: balanceOf(address)
-	function balanceOf(address owner) public view returns (uint256) {
+	/// @dev EVM selector for this function is: 0x06fdde03,
+	///  or in textual repr: name()
+	function name() public view returns (string memory) {
+		require(false, stub_error);
+		dummy;
+		return "";
+	}
+
+	/// @dev EVM selector for this function is: 0x95d89b41,
+	///  or in textual repr: symbol()
+	function symbol() public view returns (string memory) {
+		require(false, stub_error);
+		dummy;
+		return "";
+	}
+
+	/// @dev EVM selector for this function is: 0x18160ddd,
+	///  or in textual repr: totalSupply()
+	function totalSupply() public view returns (uint256) {
 		require(false, stub_error);
-		owner;
 		dummy;
 		return 0;
 	}
@@ -90,26 +144,6 @@
 		dummy = 0;
 		return false;
 	}
-
-	/// @dev EVM selector for this function is: 0x095ea7b3,
-	///  or in textual repr: approve(address,uint256)
-	function approve(address spender, uint256 amount) public returns (bool) {
-		require(false, stub_error);
-		spender;
-		amount;
-		dummy = 0;
-		return false;
-	}
-
-	/// @dev EVM selector for this function is: 0xdd62ed3e,
-	///  or in textual repr: allowance(address,address)
-	function allowance(address owner, address spender) public view returns (uint256) {
-		require(false, stub_error);
-		owner;
-		spender;
-		dummy;
-		return 0;
-	}
 }
 
-contract UniqueNativeFungible is Dummy, ERC165, ERC20 {}
+contract UniqueNativeFungible is Dummy, ERC165, ERC20, ERC20UniqueExtensions {}
modifiedtests/src/eth/abi/nativeFungible.jsondiffbeforeafterboth
--- a/tests/src/eth/abi/nativeFungible.json
+++ b/tests/src/eth/abi/nativeFungible.json
@@ -127,6 +127,24 @@
   },
   {
     "inputs": [
+      {
+        "components": [
+          { "internalType": "address", "name": "eth", "type": "address" },
+          { "internalType": "uint256", "name": "sub", "type": "uint256" }
+        ],
+        "internalType": "struct CrossAddress",
+        "name": "to",
+        "type": "tuple"
+      },
+      { "internalType": "uint256", "name": "amount", "type": "uint256" }
+    ],
+    "name": "transferCross",
+    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
+    "stateMutability": "nonpayable",
+    "type": "function"
+  },
+  {
+    "inputs": [
       { "internalType": "address", "name": "from", "type": "address" },
       { "internalType": "address", "name": "to", "type": "address" },
       { "internalType": "uint256", "name": "amount", "type": "uint256" }
@@ -135,5 +153,32 @@
     "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
     "stateMutability": "nonpayable",
     "type": "function"
+  },
+  {
+    "inputs": [
+      {
+        "components": [
+          { "internalType": "address", "name": "eth", "type": "address" },
+          { "internalType": "uint256", "name": "sub", "type": "uint256" }
+        ],
+        "internalType": "struct CrossAddress",
+        "name": "from",
+        "type": "tuple"
+      },
+      {
+        "components": [
+          { "internalType": "address", "name": "eth", "type": "address" },
+          { "internalType": "uint256", "name": "sub", "type": "uint256" }
+        ],
+        "internalType": "struct CrossAddress",
+        "name": "to",
+        "type": "tuple"
+      },
+      { "internalType": "uint256", "name": "amount", "type": "uint256" }
+    ],
+    "name": "transferFromCross",
+    "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
+    "stateMutability": "nonpayable",
+    "type": "function"
   }
 ]
modifiedtests/src/eth/api/UniqueNativeFungible.soldiffbeforeafterboth
--- a/tests/src/eth/api/UniqueNativeFungible.sol
+++ b/tests/src/eth/api/UniqueNativeFungible.sol
@@ -12,6 +12,27 @@
 	function supportsInterface(bytes4 interfaceID) external view returns (bool);
 }
 
+/// @dev the ERC-165 identifier for this interface is 0xff15c6f4
+interface ERC20UniqueExtensions is Dummy, ERC165 {
+	/// @dev EVM selector for this function is: 0x2ada85ff,
+	///  or in textual repr: transferCross((address,uint256),uint256)
+	function transferCross(CrossAddress memory to, uint256 amount) external returns (bool);
+
+	/// @dev EVM selector for this function is: 0xd5cf430b,
+	///  or in textual repr: transferFromCross((address,uint256),(address,uint256),uint256)
+	function transferFromCross(
+		CrossAddress memory from,
+		CrossAddress memory to,
+		uint256 amount
+	) external returns (bool);
+}
+
+/// Cross account struct
+struct CrossAddress {
+	address eth;
+	uint256 sub;
+}
+
 /// @dev inlined interface
 interface ERC20Events {
 	event Transfer(address indexed from, address indexed to, uint256 value);
@@ -20,6 +41,22 @@
 
 /// @dev the ERC-165 identifier for this interface is 0x942e8b22
 interface ERC20 is Dummy, ERC165, ERC20Events {
+	/// @dev EVM selector for this function is: 0xdd62ed3e,
+	///  or in textual repr: allowance(address,address)
+	function allowance(address owner, address spender) external view returns (uint256);
+
+	/// @dev EVM selector for this function is: 0x095ea7b3,
+	///  or in textual repr: approve(address,uint256)
+	function approve(address spender, uint256 amount) external returns (bool);
+
+	/// @dev EVM selector for this function is: 0x70a08231,
+	///  or in textual repr: balanceOf(address)
+	function balanceOf(address owner) external view returns (uint256);
+
+	/// @dev EVM selector for this function is: 0x313ce567,
+	///  or in textual repr: decimals()
+	function decimals() external view returns (uint8);
+
 	/// @dev EVM selector for this function is: 0x06fdde03,
 	///  or in textual repr: name()
 	function name() external view returns (string memory);
@@ -32,14 +69,6 @@
 	///  or in textual repr: totalSupply()
 	function totalSupply() external view returns (uint256);
 
-	/// @dev EVM selector for this function is: 0x313ce567,
-	///  or in textual repr: decimals()
-	function decimals() external view returns (uint8);
-
-	/// @dev EVM selector for this function is: 0x70a08231,
-	///  or in textual repr: balanceOf(address)
-	function balanceOf(address owner) external view returns (uint256);
-
 	/// @dev EVM selector for this function is: 0xa9059cbb,
 	///  or in textual repr: transfer(address,uint256)
 	function transfer(address to, uint256 amount) external returns (bool);
@@ -51,14 +80,6 @@
 		address to,
 		uint256 amount
 	) external returns (bool);
-
-	/// @dev EVM selector for this function is: 0x095ea7b3,
-	///  or in textual repr: approve(address,uint256)
-	function approve(address spender, uint256 amount) external returns (bool);
-
-	/// @dev EVM selector for this function is: 0xdd62ed3e,
-	///  or in textual repr: allowance(address,address)
-	function allowance(address owner, address spender) external view returns (uint256);
 }
 
-interface UniqueNativeFungible is Dummy, ERC165, ERC20 {}
+interface UniqueNativeFungible is Dummy, ERC165, ERC20, ERC20UniqueExtensions {}