git.delta.rocks / unique-network / refs/commits / 008dfe41f9df

difftreelog

feat erc nesting tests + some fixes

Trubnikov Sergey2023-05-10parent: #2dd0a6a.patch.diff
in: master

5 files changed

modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
104 from: &T::CrossAccountId,104 from: &T::CrossAccountId,
105 nesting_budget: &dyn Budget,105 nesting_budget: &dyn Budget,
106 ) -> Result<u128, DispatchError> {106 ) -> Result<u128, DispatchError> {
107 if spender.conv_eq(from) {
108 return Ok(0);
109 }
110
111 if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {107 if let Some((collection_id, token_id)) =
108 T::CrossTokenAddressMapping::address_to_token(from)
109 {
112 ensure!(110 ensure!(
113 <PalletStructure<T>>::check_indirectly_owned(111 <PalletStructure<T>>::check_indirectly_owned(
114 spender.clone(),112 spender.clone(),
115 source.0,113 collection_id,
116 source.1,114 token_id,
117 None,115 None,
118 nesting_budget116 nesting_budget
119 )?,117 )?,
120 <CommonError<T>>::ApprovedValueTooLow,118 <CommonError<T>>::ApprovedValueTooLow,
121 );119 );
122 } else if spender != from {120 } else if !spender.conv_eq(from) {
123 return Ok(0);121 return Ok(0);
124 }122 }
125123
183 amount: u128,181 amount: u128,
184 nesting_budget: &dyn Budget,182 nesting_budget: &dyn Budget,
185 ) -> DispatchResultWithPostInfo {183 ) -> DispatchResultWithPostInfo {
186 let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;184 let allowance = Self::check_allowed(spender, from, nesting_budget)?;
187 if allowance < amount {185 if allowance < amount {
188 return Err(<CommonError<T>>::ApprovedValueTooLow.into());186 return Err(<CommonError<T>>::ApprovedValueTooLow.into());
189 }187 }
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -109,7 +109,8 @@
 			.recorder
 			.weight_calls_budget(<StructureWeight<T>>::find_parent());
 
-		<Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;
+		<Pallet<T>>::transfer(self, &caller, &to, amount, &budget)
+			.map_err(|e| dispatch_to_evm::<T>(e.error))?;
 		Ok(true)
 	}
 
modifiedtests/src/eth/nesting/nest.test.tsdiffbeforeafterboth
--- a/tests/src/eth/nesting/nest.test.ts
+++ b/tests/src/eth/nesting/nest.test.ts
@@ -187,4 +187,80 @@
         .call()).to.be.rejectedWith('SourceCollectionIsNotAllowedToNest');
     });
   });
+
+  describe('Fungible', () => {
+    async function createFungibleCollection(helper: EthUniqueHelper, owner: string, mode: 'ft' | 'native ft') {
+      if (mode === 'ft') {
+        const {collectionAddress} = await helper.eth.createFungibleCollection(owner, '', 18, '', '');
+        const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
+        await contract.methods.mint(owner, 100n).send({from: owner});
+        return {collectionAddress, contract};
+      }
+
+      // native ft
+      const collectionAddress = helper.ethAddress.fromCollectionId(0);
+      const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
+      return {collectionAddress, contract};
+    }
+
+    [
+      {mode: 'ft' as const},
+      {mode: 'native ft' as const},
+    ].map(testCase => {
+      itEth(`Allow nest [${testCase.mode}]`, async ({helper}) => {
+        const owner = await helper.eth.createAccountWithBalance(donor);
+        const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
+        const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
+
+        const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
+        const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
+        const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
+
+        await ftContract.methods.transfer(targetTokenAddress, 10n).send({from: owner});
+        expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('10');
+      });
+    });
+
+    [
+      {mode: 'ft' as const},
+      {mode: 'native ft' as const},
+    ].map(testCase => {
+      itEth(`Allow partial/full unnest [${testCase.mode}]`, async ({helper}) => {
+        const owner = await helper.eth.createAccountWithBalance(donor);
+        const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
+        const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
+
+        const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
+        const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
+        const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
+
+        await ftContract.methods.transfer(targetTokenAddress, 10n).send({from: owner});
+
+        await ftContract.methods.transferFrom(targetTokenAddress, owner, 5n).send({from: owner});
+        expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('5');
+
+        await ftContract.methods.transferFrom(targetTokenAddress, owner, 5n).send({from: owner});
+        expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('0');
+      });
+    });
+
+    [
+      {mode: 'ft' as const},
+      {mode: 'native ft' as const},
+    ].map(testCase => {
+      itEth(`Disallow nest into collection without nesting permission [${testCase.mode}]`, async ({helper}) => {
+        const owner = await helper.eth.createAccountWithBalance(donor);
+        const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
+        await targetContract.methods.setCollectionNesting(false).send({from: owner});
+
+        const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
+
+        const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
+        const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
+        const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
+
+        await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');
+      });
+    });
+  });
 });
modifiedtests/src/nativeFungible.test.tsdiffbeforeafterboth
--- a/tests/src/nativeFungible.test.ts
+++ b/tests/src/nativeFungible.test.ts
@@ -207,7 +207,7 @@
     )).to.be.rejectedWith('BadOrigin');
   });
 
-  itSub.only('Nest into NFT token()', async ({helper}) => {
+  itSub('Nest into NFT token()', async ({helper}) => {
     const nftCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
     const targetToken = await nftCollection.mintToken(alice);
 
modifiedtests/src/sub/nesting/unnesting.negative.test.tsdiffbeforeafterboth
--- a/tests/src/sub/nesting/unnesting.negative.test.ts
+++ b/tests/src/sub/nesting/unnesting.negative.test.ts
@@ -58,7 +58,7 @@
     {mode: md.mode, restrictedMode: true},
     {mode: md.mode, restrictedMode: false},
   ].map(testCase => {
-    itSub.only(`Fungible: disallows a non-Owner to unnest someone else's token [${testCase.mode}${testCase.restrictedMode ? ' (Restricted nesting)' : ''}]`, async ({helper}) => {
+    itSub(`Fungible: disallows a non-Owner to unnest someone else's token [${testCase.mode}${testCase.restrictedMode ? ' (Restricted nesting)' : ''}]`, async ({helper}) => {
       const collectionNFT = await helper.nft.mintCollection(alice);
       const collectionFT = await (
         testCase.mode === 'ft'