difftreelog
feat add dispatching
in: master
6 files changed
pallets/balances-adapter/src/common.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/balances-adapter/src/common.rs
@@ -0,0 +1,352 @@
+use alloc::vec::Vec;
+use core::marker::PhantomData;
+use crate::{Config, NativeFungibleHandle};
+use pallet_common::{CommonCollectionOperations, CommonWeightInfo};
+
+pub struct CommonWeights<T: Config>(PhantomData<T>);
+impl<T: Config> CommonWeightInfo<T::CrossAccountId> for CommonWeights<T> {
+ fn create_multiple_items(
+ amount: &[up_data_structs::CreateItemData],
+ ) -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn create_multiple_items_ex(
+ cost: &up_data_structs::CreateItemExData<T::CrossAccountId>,
+ ) -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn burn_item() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn set_collection_properties(amount: u32) -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn delete_collection_properties(amount: u32) -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn set_token_properties(amount: u32) -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn delete_token_properties(amount: u32) -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn set_token_property_permissions(amount: u32) -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn transfer() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn approve() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn approve_from() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn transfer_from() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn burn_from() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn burn_recursively_self_raw() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn burn_recursively_breadth_raw(amount: u32) -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn token_owner() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn set_allowance_for_all() -> frame_support::weights::Weight {
+ todo!()
+ }
+
+ fn force_repair_item() -> frame_support::weights::Weight {
+ todo!()
+ }
+}
+
+/// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallete
+/// methods and adds weight info.
+impl<T: Config> CommonCollectionOperations<T> for NativeFungibleHandle<T> {
+ fn create_item(
+ &self,
+ sender: <T>::CrossAccountId,
+ to: <T>::CrossAccountId,
+ data: up_data_structs::CreateItemData,
+ nesting_budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn create_multiple_items(
+ &self,
+ sender: <T>::CrossAccountId,
+ to: <T>::CrossAccountId,
+ data: Vec<up_data_structs::CreateItemData>,
+ nesting_budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn create_multiple_items_ex(
+ &self,
+ sender: <T>::CrossAccountId,
+ data: up_data_structs::CreateItemExData<<T>::CrossAccountId>,
+ nesting_budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn burn_item(
+ &self,
+ sender: <T>::CrossAccountId,
+ token: up_data_structs::TokenId,
+ amount: u128,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn burn_item_recursively(
+ &self,
+ sender: <T>::CrossAccountId,
+ token: up_data_structs::TokenId,
+ self_budget: &dyn up_data_structs::budget::Budget,
+ breadth_budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn set_collection_properties(
+ &self,
+ sender: <T>::CrossAccountId,
+ properties: Vec<up_data_structs::Property>,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn delete_collection_properties(
+ &self,
+ sender: &<T>::CrossAccountId,
+ property_keys: Vec<up_data_structs::PropertyKey>,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn set_token_properties(
+ &self,
+ sender: <T>::CrossAccountId,
+ token_id: up_data_structs::TokenId,
+ properties: Vec<up_data_structs::Property>,
+ budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn delete_token_properties(
+ &self,
+ sender: <T>::CrossAccountId,
+ token_id: up_data_structs::TokenId,
+ property_keys: Vec<up_data_structs::PropertyKey>,
+ budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn set_token_property_permissions(
+ &self,
+ sender: &<T>::CrossAccountId,
+ property_permissions: Vec<up_data_structs::PropertyKeyPermission>,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn transfer(
+ &self,
+ sender: <T>::CrossAccountId,
+ to: <T>::CrossAccountId,
+ token: up_data_structs::TokenId,
+ amount: u128,
+ budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn approve(
+ &self,
+ sender: <T>::CrossAccountId,
+ spender: <T>::CrossAccountId,
+ token: up_data_structs::TokenId,
+ amount: u128,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn approve_from(
+ &self,
+ sender: <T>::CrossAccountId,
+ from: <T>::CrossAccountId,
+ to: <T>::CrossAccountId,
+ token: up_data_structs::TokenId,
+ amount: u128,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn transfer_from(
+ &self,
+ sender: <T>::CrossAccountId,
+ from: <T>::CrossAccountId,
+ to: <T>::CrossAccountId,
+ token: up_data_structs::TokenId,
+ amount: u128,
+ budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn burn_from(
+ &self,
+ sender: <T>::CrossAccountId,
+ from: <T>::CrossAccountId,
+ token: up_data_structs::TokenId,
+ amount: u128,
+ budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn check_nesting(
+ &self,
+ sender: <T>::CrossAccountId,
+ from: (up_data_structs::CollectionId, up_data_structs::TokenId),
+ under: up_data_structs::TokenId,
+ budget: &dyn up_data_structs::budget::Budget,
+ ) -> frame_support::sp_runtime::DispatchResult {
+ todo!()
+ }
+
+ fn nest(
+ &self,
+ under: up_data_structs::TokenId,
+ to_nest: (up_data_structs::CollectionId, up_data_structs::TokenId),
+ ) {
+ todo!()
+ }
+
+ fn unnest(
+ &self,
+ under: up_data_structs::TokenId,
+ to_nest: (up_data_structs::CollectionId, up_data_structs::TokenId),
+ ) {
+ todo!()
+ }
+
+ fn account_tokens(&self, account: <T>::CrossAccountId) -> Vec<up_data_structs::TokenId> {
+ todo!()
+ }
+
+ fn collection_tokens(&self) -> Vec<up_data_structs::TokenId> {
+ todo!()
+ }
+
+ fn token_exists(&self, token: up_data_structs::TokenId) -> bool {
+ todo!()
+ }
+
+ fn last_token_id(&self) -> up_data_structs::TokenId {
+ todo!()
+ }
+
+ fn token_owner(
+ &self,
+ token: up_data_structs::TokenId,
+ ) -> Result<<T>::CrossAccountId, up_data_structs::TokenOwnerError> {
+ todo!()
+ }
+
+ fn token_owners(&self, token: up_data_structs::TokenId) -> Vec<<T>::CrossAccountId> {
+ todo!()
+ }
+
+ fn token_property(
+ &self,
+ token_id: up_data_structs::TokenId,
+ key: &up_data_structs::PropertyKey,
+ ) -> Option<up_data_structs::PropertyValue> {
+ todo!()
+ }
+
+ fn token_properties(
+ &self,
+ token: up_data_structs::TokenId,
+ keys: Option<Vec<up_data_structs::PropertyKey>>,
+ ) -> Vec<up_data_structs::Property> {
+ todo!()
+ }
+
+ fn total_supply(&self) -> u32 {
+ todo!()
+ }
+
+ fn account_balance(&self, account: <T>::CrossAccountId) -> u32 {
+ todo!()
+ }
+
+ fn balance(&self, account: <T>::CrossAccountId, token: up_data_structs::TokenId) -> u128 {
+ todo!()
+ }
+
+ fn total_pieces(&self, token: up_data_structs::TokenId) -> Option<u128> {
+ todo!()
+ }
+
+ fn allowance(
+ &self,
+ sender: <T>::CrossAccountId,
+ spender: <T>::CrossAccountId,
+ token: up_data_structs::TokenId,
+ ) -> u128 {
+ todo!()
+ }
+
+ fn refungible_extensions(&self) -> Option<&dyn pallet_common::RefungibleExtensions<T>> {
+ todo!()
+ }
+
+ fn set_allowance_for_all(
+ &self,
+ owner: <T>::CrossAccountId,
+ operator: <T>::CrossAccountId,
+ approve: bool,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+
+ fn allowance_for_all(&self, owner: <T>::CrossAccountId, operator: <T>::CrossAccountId) -> bool {
+ todo!()
+ }
+
+ fn repair_item(
+ &self,
+ token: up_data_structs::TokenId,
+ ) -> frame_support::pallet_prelude::DispatchResultWithPostInfo {
+ todo!()
+ }
+}
pallets/balances-adapter/src/erc.rsdiffbeforeafterboth1use crate::Config;2use evm_coder::{abi::AbiType, AbiCoder, ToLog, generate_stubgen, solidity_interface, types::*};3use frame_support::traits::{Currency, ExistenceRequirement};4use pallet_common::{5 erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},6 eth::CrossAddress,7};8use pallet_evm_coder_substrate::{9 call, dispatch_to_evm,10 execution::{PreDispatch, Result},11 frontier_contract, WithRecorder, SubstrateRecorder,12};13use sp_core::{U256, Get};14use sp_std::vec::Vec;1516frontier_contract! {17 macro_rules! NativeFungibleHandle_result {...}18 impl<T: Config> Contract for NativeFungibleHandle<T> {...}19}2021#[derive(ToLog)]22pub enum ERC20Events {23 Transfer {24 #[indexed]25 from: Address,26 #[indexed]27 to: Address,28 value: U256,29 },30 Approval {31 #[indexed]32 owner: Address,33 #[indexed]34 spender: Address,35 value: U256,36 },37}3839pub struct NativeFungibleHandle<T: Config>(SubstrateRecorder<T>);4041impl<T: Config> WithRecorder<T> for NativeFungibleHandle<T> {42 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {43 &self.044 }45 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {46 self.047 }48}4950#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]51impl<T: Config> NativeFungibleHandle<T> {52 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {53 Ok(U256::zero())54 }5556 // #[weight(<SelfWeightOf<T>>::approve())]57 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {58 // self.consume_store_reads(1)?;59 Err("Approve not supported now".into())60 }6162 fn balance_of(&self, owner: Address) -> Result<U256> {63 // self.consume_store_reads(1)?;64 let owner = T::CrossAccountId::from_eth(owner);65 let balance = <T as Config>::Currency::free_balance(owner.as_sub());66 Ok(balance.into())67 }6869 fn decimals(&self) -> Result<u8> {70 Ok(T::Decimals::get())71 }7273 fn name(&self) -> Result<String> {74 Ok(T::Name::get())75 }7677 fn symbol(&self) -> Result<String> {78 Ok(T::Symbol::get())79 }8081 fn total_supply(&self) -> Result<U256> {82 // self.consume_store_reads(1)?;83 let total = <T as Config>::Currency::total_issuance();84 Ok(total.into())85 }8687 // #[weight(<SelfWeightOf<T>>::transfer())]88 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> 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, amount, &budget).map_err(|_| "transfer error")?;97 <T as Config>::Currency::transfer(98 caller.as_sub(),99 to.as_sub(),100 amount,101 ExistenceRequirement::KeepAlive,102 )103 .map_err(dispatch_to_evm::<T>)?;104 Ok(true)105 }106107 // #[weight(<SelfWeightOf<T>>::transfer_from())]108 fn transfer_from(109 &mut self,110 caller: Caller,111 from: Address,112 to: Address,113 amount: U256,114 ) -> Result<bool> {115 let caller = T::CrossAccountId::from_eth(caller);116 let from = T::CrossAccountId::from_eth(from);117 let to = T::CrossAccountId::from_eth(to);118 let amount = amount.try_into().map_err(|_| "amount overflow")?;119120 if from != to {121 return Err("no permission".into());122 }123 // let budget = self124 // .recorder125 // .weight_calls_budget(<StructureWeight<T>>::find_parent());126127 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)128 // .map_err(dispatch_to_evm::<T>)?;129 <T as Config>::Currency::transfer(130 caller.as_sub(),131 to.as_sub(),132 amount,133 ExistenceRequirement::KeepAlive,134 )135 .map_err(dispatch_to_evm::<T>)?;136 Ok(true)137 }138}139140#[solidity_interface(name = ERC20UniqueExtensions, enum(derive(PreDispatch)), enum_attr(weight))]141impl<T: Config> NativeFungibleHandle<T>142where143 T::AccountId: From<[u8; 32]>,144{145 fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {146 // self.consume_store_reads(1)?;147 let owner = owner.into_sub_cross_account::<T>()?;148 let balance = <T as Config>::Currency::free_balance(owner.as_sub());149 Ok(balance.into())150 }151152 // #[weight(<SelfWeightOf<T>>::transfer())]153 fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {154 let caller = T::CrossAccountId::from_eth(caller);155 let to = to.into_sub_cross_account::<T>()?;156 let amount = amount.try_into().map_err(|_| "amount overflow")?;157 // let budget = self158 // .recorder159 // .weight_calls_budget(<StructureWeight<T>>::find_parent());160161 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;162 <T as Config>::Currency::transfer(163 caller.as_sub(),164 to.as_sub(),165 amount,166 ExistenceRequirement::KeepAlive,167 )168 .map_err(dispatch_to_evm::<T>)?;169 Ok(true)170 }171172 // #[weight(<SelfWeightOf<T>>::transfer_from())]173 fn transfer_from_cross(174 &mut self,175 caller: Caller,176 from: CrossAddress,177 to: CrossAddress,178 amount: U256,179 ) -> Result<bool> {180 let caller = T::CrossAccountId::from_eth(caller);181 let from = from.into_sub_cross_account::<T>()?;182 let to = to.into_sub_cross_account::<T>()?;183 let amount = amount.try_into().map_err(|_| "amount overflow")?;184185 if from != to {186 return Err("no permission".into());187 }188189 // let budget = self190 // .recorder191 // .weight_calls_budget(<StructureWeight<T>>::find_parent());192193 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)194 // .map_err(dispatch_to_evm::<T>)?;195 <T as Config>::Currency::transfer(196 caller.as_sub(),197 to.as_sub(),198 amount,199 ExistenceRequirement::KeepAlive,200 )201 .map_err(dispatch_to_evm::<T>)?;202 Ok(true)203 }204}205206#[solidity_interface(207 name = UniqueNativeFungible,208 is(ERC20, ERC20UniqueExtensions),209 enum(derive(PreDispatch))210)]211impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}212213generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);214generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);215216impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>217where218 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,219{220 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");221222 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {223 call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)224 }225}1use crate::{Config, NativeFungibleHandle};2use evm_coder::{abi::AbiType, ToLog, generate_stubgen, solidity_interface, types::*};3use frame_support::traits::{Currency, ExistenceRequirement};4use pallet_common::{5 erc::{CommonEvmHandler, CrossAccountId, PrecompileHandle, PrecompileResult},6 eth::CrossAddress,7};8use pallet_evm_coder_substrate::{9 call, dispatch_to_evm,10 execution::{PreDispatch, Result},11 frontier_contract, WithRecorder, SubstrateRecorder,12};13use sp_core::{U256, Get};14use sp_std::vec::Vec;1516frontier_contract! {17 macro_rules! NativeFungibleHandle_result {...}18 impl<T: Config> Contract for NativeFungibleHandle<T> {...}19}2021#[derive(ToLog)]22pub enum ERC20Events {23 Transfer {24 #[indexed]25 from: Address,26 #[indexed]27 to: Address,28 value: U256,29 },30 Approval {31 #[indexed]32 owner: Address,33 #[indexed]34 spender: Address,35 value: U256,36 },37}3839#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight), expect_selector = 0x942e8b22)]40impl<T: Config> NativeFungibleHandle<T> {41 fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {42 Ok(U256::zero())43 }4445 // #[weight(<SelfWeightOf<T>>::approve())]46 fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {47 // self.consume_store_reads(1)?;48 Err("Approve not supported now".into())49 }5051 fn balance_of(&self, owner: Address) -> Result<U256> {52 // self.consume_store_reads(1)?;53 let owner = T::CrossAccountId::from_eth(owner);54 let balance = <T as Config>::Currency::free_balance(owner.as_sub());55 Ok(balance.into())56 }5758 fn decimals(&self) -> Result<u8> {59 Ok(T::Decimals::get())60 }6162 fn name(&self) -> Result<String> {63 Ok(T::Name::get())64 }6566 fn symbol(&self) -> Result<String> {67 Ok(T::Symbol::get())68 }6970 fn total_supply(&self) -> Result<U256> {71 // self.consume_store_reads(1)?;72 let total = <T as Config>::Currency::total_issuance();73 Ok(total.into())74 }7576 // #[weight(<SelfWeightOf<T>>::transfer())]77 fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {78 let caller = T::CrossAccountId::from_eth(caller);79 let to = T::CrossAccountId::from_eth(to);80 let amount = amount.try_into().map_err(|_| "amount overflow")?;81 // let budget = self82 // .recorder83 // .weight_calls_budget(<StructureWeight<T>>::find_parent());8485 // <Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;86 <T as Config>::Currency::transfer(87 caller.as_sub(),88 to.as_sub(),89 amount,90 ExistenceRequirement::KeepAlive,91 )92 .map_err(dispatch_to_evm::<T>)?;93 Ok(true)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")?;108109 if from != to {110 return Err("no permission".into());111 }112 // let budget = self113 // .recorder114 // .weight_calls_budget(<StructureWeight<T>>::find_parent());115116 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)117 // .map_err(dispatch_to_evm::<T>)?;118 <T as Config>::Currency::transfer(119 caller.as_sub(),120 to.as_sub(),121 amount,122 ExistenceRequirement::KeepAlive,123 )124 .map_err(dispatch_to_evm::<T>)?;125 Ok(true)126 }127}128129#[solidity_interface(name = ERC20UniqueExtensions, enum(derive(PreDispatch)), enum_attr(weight))]130impl<T: Config> NativeFungibleHandle<T>131where132 T::AccountId: From<[u8; 32]>,133{134 fn balance_of_cross(&self, owner: CrossAddress) -> Result<U256> {135 // self.consume_store_reads(1)?;136 let owner = owner.into_sub_cross_account::<T>()?;137 let balance = <T as Config>::Currency::free_balance(owner.as_sub());138 Ok(balance.into())139 }140141 // #[weight(<SelfWeightOf<T>>::transfer())]142 fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {143 let caller = T::CrossAccountId::from_eth(caller);144 let to = to.into_sub_cross_account::<T>()?;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>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;151 <T as Config>::Currency::transfer(152 caller.as_sub(),153 to.as_sub(),154 amount,155 ExistenceRequirement::KeepAlive,156 )157 .map_err(dispatch_to_evm::<T>)?;158 Ok(true)159 }160161 // #[weight(<SelfWeightOf<T>>::transfer_from())]162 fn transfer_from_cross(163 &mut self,164 caller: Caller,165 from: CrossAddress,166 to: CrossAddress,167 amount: U256,168 ) -> Result<bool> {169 let caller = T::CrossAccountId::from_eth(caller);170 let from = from.into_sub_cross_account::<T>()?;171 let to = to.into_sub_cross_account::<T>()?;172 let amount = amount.try_into().map_err(|_| "amount overflow")?;173174 if from != to {175 return Err("no permission".into());176 }177178 // let budget = self179 // .recorder180 // .weight_calls_budget(<StructureWeight<T>>::find_parent());181182 // <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)183 // .map_err(dispatch_to_evm::<T>)?;184 <T as Config>::Currency::transfer(185 caller.as_sub(),186 to.as_sub(),187 amount,188 ExistenceRequirement::KeepAlive,189 )190 .map_err(dispatch_to_evm::<T>)?;191 Ok(true)192 }193}194195#[solidity_interface(196 name = UniqueNativeFungible,197 is(ERC20, ERC20UniqueExtensions),198 enum(derive(PreDispatch))199)]200impl<T: Config> NativeFungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}201202generate_stubgen!(gen_impl, UniqueNativeFungibleCall<()>, true);203generate_stubgen!(gen_iface, UniqueNativeFungibleCall<()>, false);204205impl<T: Config> CommonEvmHandler for NativeFungibleHandle<T>206where207 T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,208{209 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueNativeFungible.raw");210211 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {212 call::<T, UniqueNativeFungibleCall<T>, _, _>(handle, self)213 }214}pallets/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(())
+ }
+ }
}
runtime/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(
tests/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
tests/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,