difftreelog
feat Using EthCrossAccount in appropriate functions
in: master
11 files changed
crates/evm-coder/src/abi.rsdiffbeforeafterboth450 }450 }451}451}452452453impl AbiWrite for &EthCrossAccount {453impl AbiWrite for EthCrossAccount {454 fn abi_write(&self, writer: &mut AbiWriter) {454 fn abi_write(&self, writer: &mut AbiWriter) {455 self.eth.abi_write(writer);455 self.eth.abi_write(writer);456 self.sub.abi_write(writer);456 self.sub.abi_write(writer);crates/evm-coder/src/lib.rsdiffbeforeafterboth218 }218 }219 }219 }220220221 #[derive(Debug)]221 #[derive(Debug, Default)]222 pub struct EthCrossAccount {222 pub struct EthCrossAccount {223 pub(crate) eth: address,223 pub(crate) eth: address,224 pub(crate) sub: uint256,224 pub(crate) sub: uint256,crates/evm-coder/src/solidity.rsdiffbeforeafterboth203 }203 }204204205 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {205 fn solidity_default(writer: &mut impl fmt::Write, tc: &TypeCollector) -> fmt::Result {206 write!(writer, "{}(", tc.collect_tuple::<Self>())?;206 write!(writer, "{}(", tc.collect_struct::<Self>())?;207 address::solidity_default(writer, tc)?;207 address::solidity_default(writer, tc)?;208 write!(writer, ",")?;208 write!(writer, ",")?;209 uint256::solidity_default(writer, tc)?;209 uint256::solidity_default(writer, tc)?;pallets/common/src/erc.rsdiffbeforeafterboth231 fn set_collection_sponsor_cross(231 fn set_collection_sponsor_cross(232 &mut self,232 &mut self,233 caller: caller,233 caller: caller,234 sponsor: (address, uint256),234 sponsor: EthCrossAccount,235 ) -> Result<void> {235 ) -> Result<void> {236 self.consume_store_reads_and_writes(1, 1)?;236 self.consume_store_reads_and_writes(1, 1)?;237237238 check_is_owner_or_admin(caller, self)?;238 check_is_owner_or_admin(caller, self)?;239239240 let sponsor = convert_tuple_to_cross_account::<T>(sponsor)?;240 let sponsor = sponsor.into_sub_cross_account::<T>()?;241 self.set_sponsor(sponsor.as_sub().clone())241 self.set_sponsor(sponsor.as_sub().clone())242 .map_err(dispatch_to_evm::<T>)?;242 .map_err(dispatch_to_evm::<T>)?;243 save(self)243 save(self)388 fn add_collection_admin_cross(388 fn add_collection_admin_cross(389 &mut self,389 &mut self,390 caller: caller,390 caller: caller,391 new_admin: (address, uint256),391 new_admin: EthCrossAccount,392 ) -> Result<void> {392 ) -> Result<void> {393 self.consume_store_writes(2)?;393 self.consume_store_writes(2)?;394394395 let caller = T::CrossAccountId::from_eth(caller);395 let caller = T::CrossAccountId::from_eth(caller);396 let new_admin = convert_tuple_to_cross_account::<T>(new_admin)?;396 let new_admin = new_admin.into_sub_cross_account::<T>()?;397 <Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;397 <Pallet<T>>::toggle_admin(self, &caller, &new_admin, true).map_err(dispatch_to_evm::<T>)?;398 Ok(())398 Ok(())399 }399 }403 fn remove_collection_admin_cross(403 fn remove_collection_admin_cross(404 &mut self,404 &mut self,405 caller: caller,405 caller: caller,406 admin: (address, uint256),406 admin: EthCrossAccount,407 ) -> Result<void> {407 ) -> Result<void> {408 self.consume_store_writes(2)?;408 self.consume_store_writes(2)?;409409410 let caller = T::CrossAccountId::from_eth(caller);410 let caller = T::CrossAccountId::from_eth(caller);411 let admin = convert_tuple_to_cross_account::<T>(admin)?;411 let admin = admin.into_sub_cross_account::<T>()?;412 <Pallet<T>>::toggle_admin(self, &caller, &admin, false).map_err(dispatch_to_evm::<T>)?;412 <Pallet<T>>::toggle_admin(self, &caller, &admin, false).map_err(dispatch_to_evm::<T>)?;413 Ok(())413 Ok(())414 }414 }566 fn add_to_collection_allow_list_cross(566 fn add_to_collection_allow_list_cross(567 &mut self,567 &mut self,568 caller: caller,568 caller: caller,569 user: (address, uint256),569 user: EthCrossAccount,570 ) -> Result<void> {570 ) -> Result<void> {571 self.consume_store_writes(1)?;571 self.consume_store_writes(1)?;572572573 let caller = T::CrossAccountId::from_eth(caller);573 let caller = T::CrossAccountId::from_eth(caller);574 let user = convert_tuple_to_cross_account::<T>(user)?;574 let user = user.into_sub_cross_account::<T>()?;575 Pallet::<T>::toggle_allowlist(self, &caller, &user, true).map_err(dispatch_to_evm::<T>)?;575 Pallet::<T>::toggle_allowlist(self, &caller, &user, true).map_err(dispatch_to_evm::<T>)?;576 Ok(())576 Ok(())577 }577 }594 fn remove_from_collection_allow_list_cross(594 fn remove_from_collection_allow_list_cross(595 &mut self,595 &mut self,596 caller: caller,596 caller: caller,597 user: (address, uint256),597 user: EthCrossAccount,598 ) -> Result<void> {598 ) -> Result<void> {599 self.consume_store_writes(1)?;599 self.consume_store_writes(1)?;600600601 let caller = T::CrossAccountId::from_eth(caller);601 let caller = T::CrossAccountId::from_eth(caller);602 let user = convert_tuple_to_cross_account::<T>(user)?;602 let user = user.into_sub_cross_account::<T>()?;603 Pallet::<T>::toggle_allowlist(self, &caller, &user, false).map_err(dispatch_to_evm::<T>)?;603 Pallet::<T>::toggle_allowlist(self, &caller, &user, false).map_err(dispatch_to_evm::<T>)?;604 Ok(())604 Ok(())605 }605 }639 ///639 ///640 /// @param user User cross account to verify640 /// @param user User cross account to verify641 /// @return "true" if account is the owner or admin641 /// @return "true" if account is the owner or admin642 fn is_owner_or_admin_cross(&self, user: (address, uint256)) -> Result<bool> {642 fn is_owner_or_admin_cross(&self, user: EthCrossAccount) -> Result<bool> {643 let user = convert_tuple_to_cross_account::<T>(user)?;643 let user = user.into_sub_cross_account::<T>()?;644 Ok(self.is_owner_or_admin(&user))644 Ok(self.is_owner_or_admin(&user))645 }645 }646646660 ///660 ///661 /// @return Tuble with sponsor address and his substrate mirror.661 /// @return Tuble with sponsor address and his substrate mirror.662 /// If address is canonical then substrate mirror is zero and vice versa.662 /// If address is canonical then substrate mirror is zero and vice versa.663 fn collection_owner(&self) -> Result<(address, uint256)> {663 fn collection_owner(&self) -> Result<EthCrossAccount> {664 Ok(convert_cross_account_to_tuple::<T>(664 Ok(EthCrossAccount::from_sub_cross_account::<T>(665 &T::CrossAccountId::from_sub(self.owner.clone()),665 &T::CrossAccountId::from_sub(self.owner.clone()),666 ))666 ))667 }667 }684 ///684 ///685 /// @return Vector of tuples with admins address and his substrate mirror.685 /// @return Vector of tuples with admins address and his substrate mirror.686 /// If address is canonical then substrate mirror is zero and vice versa.686 /// If address is canonical then substrate mirror is zero and vice versa.687 fn collection_admins(&self) -> Result<Vec<(address, uint256)>> {687 fn collection_admins(&self) -> Result<Vec<EthCrossAccount>> {688 let result = crate::IsAdmin::<T>::iter_prefix((self.id,))688 let result = crate::IsAdmin::<T>::iter_prefix((self.id,))689 .map(|(admin, _)| crate::eth::convert_cross_account_to_tuple::<T>(&admin))689 .map(|(admin, _)| EthCrossAccount::from_sub_cross_account::<T>(&admin))690 .collect();690 .collect();691 Ok(result)691 Ok(result)692 }692 }695 ///695 ///696 /// @dev Owner can be changed only by current owner696 /// @dev Owner can be changed only by current owner697 /// @param newOwner new owner cross account697 /// @param newOwner new owner cross account698 fn set_owner_cross(&mut self, caller: caller, new_owner: (address, uint256)) -> Result<void> {698 fn set_owner_cross(&mut self, caller: caller, new_owner: EthCrossAccount) -> Result<void> {699 self.consume_store_writes(1)?;699 self.consume_store_writes(1)?;700700701 let caller = T::CrossAccountId::from_eth(caller);701 let caller = T::CrossAccountId::from_eth(caller);702 let new_owner = convert_tuple_to_cross_account::<T>(new_owner)?;702 let new_owner = new_owner.into_sub_cross_account::<T>()?;703 self.set_owner_internal(caller, new_owner)703 self.set_owner_internal(caller, new_owner)704 .map_err(dispatch_to_evm::<T>)704 .map_err(dispatch_to_evm::<T>)705 }705 }pallets/fungible/src/erc.rsdiffbeforeafterboth167 fn approve_cross(167 fn approve_cross(168 &mut self,168 &mut self,169 caller: caller,169 caller: caller,170 spender: (address, uint256),170 spender: EthCrossAccount,171 amount: uint256,171 amount: uint256,172 ) -> Result<bool> {172 ) -> Result<bool> {173 let caller = T::CrossAccountId::from_eth(caller);173 let caller = T::CrossAccountId::from_eth(caller);174 let spender = convert_tuple_to_cross_account::<T>(spender)?;174 let spender = spender.into_sub_cross_account::<T>()?;175 let amount = amount.try_into().map_err(|_| "amount overflow")?;175 let amount = amount.try_into().map_err(|_| "amount overflow")?;176176177 <Pallet<T>>::set_allowance(self, &caller, &spender, amount)177 <Pallet<T>>::set_allowance(self, &caller, &spender, amount)207 fn burn_from_cross(207 fn burn_from_cross(208 &mut self,208 &mut self,209 caller: caller,209 caller: caller,210 from: (address, uint256),210 from: EthCrossAccount,211 amount: uint256,211 amount: uint256,212 ) -> Result<bool> {212 ) -> Result<bool> {213 let caller = T::CrossAccountId::from_eth(caller);213 let caller = T::CrossAccountId::from_eth(caller);214 let from = convert_tuple_to_cross_account::<T>(from)?;214 let from = from.into_sub_cross_account::<T>()?;215 let amount = amount.try_into().map_err(|_| "amount overflow")?;215 let amount = amount.try_into().map_err(|_| "amount overflow")?;216 let budget = self216 let budget = self217 .recorder217 .recorder249 fn transfer_from_cross(249 fn transfer_from_cross(250 &mut self,250 &mut self,251 caller: caller,251 caller: caller,252 from: (address, uint256),252 from: EthCrossAccount,253 to: (address, uint256),253 to: EthCrossAccount,254 amount: uint256,254 amount: uint256,255 ) -> Result<bool> {255 ) -> Result<bool> {256 let caller = T::CrossAccountId::from_eth(caller);256 let caller = T::CrossAccountId::from_eth(caller);257 let from = convert_tuple_to_cross_account::<T>(from)?;257 let from = from.into_sub_cross_account::<T>()?;258 let to = convert_tuple_to_cross_account::<T>(to)?;258 let to = to.into_sub_cross_account::<T>()?;259 let amount = amount.try_into().map_err(|_| "amount overflow")?;259 let amount = amount.try_into().map_err(|_| "amount overflow")?;260 let budget = self260 let budget = self261 .recorder261 .recorderpallets/nonfungible/src/erc.rsdiffbeforeafterboth684 fn approve_cross(684 fn approve_cross(685 &mut self,685 &mut self,686 caller: caller,686 caller: caller,687 approved: (address, uint256),687 approved: EthCrossAccount,688 token_id: uint256,688 token_id: uint256,689 ) -> Result<void> {689 ) -> Result<void> {690 let caller = T::CrossAccountId::from_eth(caller);690 let caller = T::CrossAccountId::from_eth(caller);691 let approved = convert_tuple_to_cross_account::<T>(approved)?;691 let approved = approved.into_sub_cross_account::<T>()?;692 let token = token_id.try_into()?;692 let token = token_id.try_into()?;693693694 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))694 <Pallet<T>>::set_allowance(self, &caller, token, Some(&approved))770 fn burn_from_cross(770 fn burn_from_cross(771 &mut self,771 &mut self,772 caller: caller,772 caller: caller,773 from: (address, uint256),773 from: EthCrossAccount,774 token_id: uint256,774 token_id: uint256,775 ) -> Result<void> {775 ) -> Result<void> {776 let caller = T::CrossAccountId::from_eth(caller);776 let caller = T::CrossAccountId::from_eth(caller);777 let from = convert_tuple_to_cross_account::<T>(from)?;777 let from = from.into_sub_cross_account::<T>()?;778 let token = token_id.try_into()?;778 let token = token_id.try_into()?;779 let budget = self779 let budget = self780 .recorder780 .recorderpallets/refungible/src/erc.rsdiffbeforeafterboth734 fn transfer_from_cross(734 fn transfer_from_cross(735 &mut self,735 &mut self,736 caller: caller,736 caller: caller,737 from: (address, uint256),737 from: EthCrossAccount,738 to: (address, uint256),738 to: EthCrossAccount,739 token_id: uint256,739 token_id: uint256,740 ) -> Result<void> {740 ) -> Result<void> {741 let caller = T::CrossAccountId::from_eth(caller);741 let caller = T::CrossAccountId::from_eth(caller);742 // let from = convert_tuple_to_cross_account::<T>(from)?;742 let from = from.into_sub_cross_account::<T>()?;743 // let to = convert_tuple_to_cross_account::<T>(to)?;743 let to = to.into_sub_cross_account::<T>()?;744 // let token_id = token_id.try_into()?;744 let token_id = token_id.try_into()?;745 // let budget = self745 let budget = self746 // .recorder746 .recorder747 // .weight_calls_budget(<StructureWeight<T>>::find_parent());747 .weight_calls_budget(<StructureWeight<T>>::find_parent());748748749 // let balance = balance(self, token_id, &from)?;749 let balance = balance(self, token_id, &from)?;750 // ensure_single_owner(self, token_id, balance)?;750 ensure_single_owner(self, token_id, balance)?;751751752 // Pallet::<T>::transfer_from(self, &caller, &from, &to, token_id, balance, &budget)752 Pallet::<T>::transfer_from(self, &caller, &from, &to, token_id, balance, &budget)753 // .map_err(dispatch_to_evm::<T>)?;753 .map_err(dispatch_to_evm::<T>)?;754 Ok(())754 Ok(())755 }755 }756756789 fn burn_from_cross(789 fn burn_from_cross(790 &mut self,790 &mut self,791 caller: caller,791 caller: caller,792 from: (address, uint256),792 from: EthCrossAccount,793 token_id: uint256,793 token_id: uint256,794 ) -> Result<void> {794 ) -> Result<void> {795 let caller = T::CrossAccountId::from_eth(caller);795 let caller = T::CrossAccountId::from_eth(caller);796 let from = convert_tuple_to_cross_account::<T>(from)?;796 let from = from.into_sub_cross_account::<T>()?;797 let token = token_id.try_into()?;797 let token = token_id.try_into()?;798 let budget = self798 let budget = self799 .recorder799 .recordertests/src/eth/collectionAdmin.test.tsdiffbeforeafterboth95 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;95 expect(await collectionEvm.methods.isOwnerOrAdmin(newAdmin).call()).to.be.true;96 });96 });97 97 98 itEth.skip('Check adminlist', async ({helper, privateKey}) => {99 const owner = await helper.eth.createAccountWithBalance(donor);100 101 const {collectionAddress, collectionId} = await helper.eth.createNonfungibleCollection(owner, 'A', 'B', 'C');102 const collectionEvm = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);103104 const admin1 = helper.eth.createAccount();105 const admin2 = privateKey('admin');106 await collectionEvm.methods.addCollectionAdmin(admin1).send();107 await collectionEvm.methods.addCollectionAdminSubstrate(admin2.addressRaw).send();108109 const adminListRpc = await helper.collection.getAdmins(collectionId);110 let adminListEth = await collectionEvm.methods.collectionAdmins().call();111 adminListEth = adminListEth.map((element: IEthCrossAccountId) => {112 return helper.address.convertCrossAccountFromEthCrossAcoount(element);113 });114 expect(adminListRpc).to.be.like(adminListEth);115 }); 116 117 itEth('(!negative tests!) Add admin by ADMIN is not allowed', async ({helper}) => {98 itEth('(!negative tests!) Add admin by ADMIN is not allowed', async ({helper}) => {118 const owner = await helper.eth.createAccountWithBalance(donor);99 const owner = await helper.eth.createAccountWithBalance(donor);tests/src/eth/nonFungible.test.tsdiffbeforeafterboth409 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));409 expect(cost < BigInt(0.2 * Number(helper.balance.getOneTokenNominal())));410 });410 });411411412 itEth.only('Can perform transferFromCross()', async ({helper, privateKey}) => {412 itEth('Can perform transferFromCross()', async ({helper, privateKey}) => {413 const alice = privateKey('//Alice');413 const alice = privateKey('//Alice');414 const collection = await helper.nft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});414 const collection = await helper.nft.mintCollection(alice, {name: 'A', description: 'B', tokenPrefix: 'C'});415415tests/src/util/playgrounds/types.tsdiffbeforeafterboth73export interface IEthCrossAccountId {73export interface IEthCrossAccountId {74 0: TEthereumAccount;74 0: TEthereumAccount;75 1: TSubstrateAccount;75 1: TSubstrateAccount;76 field_0: TEthereumAccount;76 eth: TEthereumAccount;77 field_1: TSubstrateAccount;77 sub: TSubstrateAccount;78}78}797980export interface ICollectionLimits {80export interface ICollectionLimits {tests/src/util/playgrounds/unique.tsdiffbeforeafterboth2430 * @returns substrate cross account id2430 * @returns substrate cross account id2431 */2431 */2432 convertCrossAccountFromEthCrossAcoount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {2432 convertCrossAccountFromEthCrossAcoount(ethCrossAccount: IEthCrossAccountId): ICrossAccountId {2433 if (ethCrossAccount.field_1 === '0') {2433 if (ethCrossAccount.sub === '0') {2434 return {Ethereum: ethCrossAccount.field_0.toLocaleLowerCase()};2434 return {Ethereum: ethCrossAccount.eth.toLocaleLowerCase()};2435 }2435 }2436 2436 2437 const ss58 = this.restoreCrossAccountFromBigInt(BigInt(ethCrossAccount.field_1));2437 const ss58 = this.restoreCrossAccountFromBigInt(BigInt(ethCrossAccount.sub));2438 return {Substrate: ss58};2438 return {Substrate: ss58};2439 }2439 }24402440