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
--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -104,22 +104,20 @@
 			from: &T::CrossAccountId,
 			nesting_budget: &dyn Budget,
 		) -> Result<u128, DispatchError> {
-			if spender.conv_eq(from) {
-				return Ok(0);
-			}
-
-			if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) {
+			if let Some((collection_id, token_id)) =
+				T::CrossTokenAddressMapping::address_to_token(from)
+			{
 				ensure!(
 					<PalletStructure<T>>::check_indirectly_owned(
 						spender.clone(),
-						source.0,
-						source.1,
+						collection_id,
+						token_id,
 						None,
 						nesting_budget
 					)?,
 					<CommonError<T>>::ApprovedValueTooLow,
 				);
-			} else if spender != from {
+			} else if !spender.conv_eq(from) {
 				return Ok(0);
 			}
 
@@ -183,7 +181,7 @@
 			amount: u128,
 			nesting_budget: &dyn Budget,
 		) -> DispatchResultWithPostInfo {
-			let allowance = Self::check_allowed(collection, spender, from, amount, nesting_budget)?;
+			let allowance = Self::check_allowed(spender, from, nesting_budget)?;
 			if allowance < amount {
 				return Err(<CommonError<T>>::ApprovedValueTooLow.into());
 			}
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
188 });188 });
189 });189 });
190
191 describe('Fungible', () => {
192 async function createFungibleCollection(helper: EthUniqueHelper, owner: string, mode: 'ft' | 'native ft') {
193 if (mode === 'ft') {
194 const {collectionAddress} = await helper.eth.createFungibleCollection(owner, '', 18, '', '');
195 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
196 await contract.methods.mint(owner, 100n).send({from: owner});
197 return {collectionAddress, contract};
198 }
199
200 // native ft
201 const collectionAddress = helper.ethAddress.fromCollectionId(0);
202 const contract = await helper.ethNativeContract.collection(collectionAddress, 'ft', owner);
203 return {collectionAddress, contract};
204 }
205
206 [
207 {mode: 'ft' as const},
208 {mode: 'native ft' as const},
209 ].map(testCase => {
210 itEth(`Allow nest [${testCase.mode}]`, async ({helper}) => {
211 const owner = await helper.eth.createAccountWithBalance(donor);
212 const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
213 const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
214
215 const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
216 const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
217 const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
218
219 await ftContract.methods.transfer(targetTokenAddress, 10n).send({from: owner});
220 expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('10');
221 });
222 });
223
224 [
225 {mode: 'ft' as const},
226 {mode: 'native ft' as const},
227 ].map(testCase => {
228 itEth(`Allow partial/full unnest [${testCase.mode}]`, async ({helper}) => {
229 const owner = await helper.eth.createAccountWithBalance(donor);
230 const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
231 const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
232
233 const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
234 const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
235 const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
236
237 await ftContract.methods.transfer(targetTokenAddress, 10n).send({from: owner});
238
239 await ftContract.methods.transferFrom(targetTokenAddress, owner, 5n).send({from: owner});
240 expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('5');
241
242 await ftContract.methods.transferFrom(targetTokenAddress, owner, 5n).send({from: owner});
243 expect(await ftContract.methods.balanceOf(targetTokenAddress).call({from: owner})).to.be.equal('0');
244 });
245 });
246
247 [
248 {mode: 'ft' as const},
249 {mode: 'native ft' as const},
250 ].map(testCase => {
251 itEth(`Disallow nest into collection without nesting permission [${testCase.mode}]`, async ({helper}) => {
252 const owner = await helper.eth.createAccountWithBalance(donor);
253 const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);
254 await targetContract.methods.setCollectionNesting(false).send({from: owner});
255
256 const {contract: ftContract} = await createFungibleCollection(helper, owner, testCase.mode);
257
258 const mintingTargetTokenIdResult = await targetContract.methods.mint(owner).send({from: owner});
259 const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;
260 const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);
261
262 await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');
263 });
264 });
265 });
190});266});
191267
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'