git.delta.rocks / unique-network / refs/commits / 012bbc1934d8

difftreelog

fix native ft nesting

Trubnikov Sergey2023-05-17parent: #53c316b.patch.diff
in: master

6 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6969,6 +6969,7 @@
  "frame-benchmarking",
  "frame-support",
  "frame-system",
+ "pallet-balances",
  "pallet-common",
  "pallet-evm",
  "pallet-evm-coder-substrate",
modifiedpallets/balances-adapter/src/lib.rsdiffbeforeafterboth
--- a/pallets/balances-adapter/src/lib.rs
+++ b/pallets/balances-adapter/src/lib.rs
@@ -148,24 +148,6 @@
 						.map_err(|_| sp_runtime::ArithmeticError::Overflow)?,
 					ExistenceRequirement::AllowDeath,
 				)?;
-
-				<PalletStructure<T>>::nest_if_sent_to_token(
-					from.clone(),
-					to,
-					NATIVE_FUNGIBLE_COLLECTION_ID,
-					TokenId::default(),
-					nesting_budget,
-				)?;
-
-				let balance_from: u128 =
-					<T as Config>::Currency::free_balance(from.as_sub()).into();
-				if balance_from == 0 {
-					<PalletStructure<T>>::unnest_if_nested(
-						from,
-						NATIVE_FUNGIBLE_COLLECTION_ID,
-						TokenId::default(),
-					);
-				}
 			};
 
 			Ok(PostDispatchInfo {
modifiedpallets/nonfungible/Cargo.tomldiffbeforeafterboth
--- a/pallets/nonfungible/Cargo.toml
+++ b/pallets/nonfungible/Cargo.toml
@@ -12,6 +12,7 @@
 frame-benchmarking = { workspace = true, optional = true }
 frame-support = { workspace = true }
 frame-system = { workspace = true }
+pallet-balances = { workspace = true }
 pallet-common = { workspace = true }
 pallet-evm = { workspace = true }
 pallet-evm-coder-substrate = { workspace = true }
modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -166,7 +166,11 @@
 
 	#[pallet::config]
 	pub trait Config:
-		frame_system::Config + pallet_common::Config + pallet_structure::Config + pallet_evm::Config
+		frame_system::Config
+		+ pallet_common::Config
+		+ pallet_structure::Config
+		+ pallet_evm::Config
+		+ pallet_balances::Config
 	{
 		type WeightInfo: WeightInfo;
 	}
@@ -1325,11 +1329,15 @@
 	}
 
 	fn nest(under: (CollectionId, TokenId), to_nest: (CollectionId, TokenId)) {
-		<TokenChildren<T>>::insert((under.0, under.1, (to_nest.0, to_nest.1)), true);
+		if to_nest.0 != pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {
+			<TokenChildren<T>>::insert((under.0, under.1, to_nest), true);
+		}
 	}
 
 	fn unnest(under: (CollectionId, TokenId), to_unnest: (CollectionId, TokenId)) {
-		<TokenChildren<T>>::remove((under.0, under.1, to_unnest));
+		if to_unnest.0 != pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {
+			<TokenChildren<T>>::remove((under.0, under.1, to_unnest));
+		}
 	}
 
 	fn collection_has_tokens(collection_id: CollectionId) -> bool {
@@ -1339,18 +1347,37 @@
 	}
 
 	fn token_has_children(collection_id: CollectionId, token_id: TokenId) -> bool {
-		<TokenChildren<T>>::iter_prefix((collection_id, token_id))
-			.next()
-			.is_some()
+		let address = T::CrossTokenAddressMapping::token_to_address(collection_id, token_id);
+		let balance = <pallet_balances::Pallet<T>>::free_balance(address.as_sub());
+
+		balance > T::Balance::default()
+			|| <TokenChildren<T>>::iter_prefix((collection_id, token_id))
+				.next()
+				.is_some()
 	}
 
 	pub fn token_children_ids(collection_id: CollectionId, token_id: TokenId) -> Vec<TokenChild> {
-		<TokenChildren<T>>::iter_prefix((collection_id, token_id))
-			.map(|((child_collection_id, child_id), _)| TokenChild {
-				collection: child_collection_id,
-				token: child_id,
+		let mut tokens: Vec<_> = vec![];
+
+		let address = T::CrossTokenAddressMapping::token_to_address(collection_id, token_id);
+		let balance = <pallet_balances::Pallet<T>>::free_balance(address.as_sub());
+		if balance > T::Balance::default() {
+			tokens.push(TokenChild {
+				token: TokenId(0),
+				collection: pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID,
 			})
-			.collect()
+		}
+
+		tokens.extend(
+			<TokenChildren<T>>::iter_prefix((collection_id, token_id)).map(
+				|((child_collection_id, child_id), _)| TokenChild {
+					collection: child_collection_id,
+					token: child_id,
+				},
+			),
+		);
+
+		tokens
 	}
 
 	/// Mint single NFT token.
modifiedtests/src/eth/nesting/nest.test.tsdiffbeforeafterboth
--- a/tests/src/eth/nesting/nest.test.ts
+++ b/tests/src/eth/nesting/nest.test.ts
@@ -248,7 +248,7 @@
       {mode: 'ft' as const},
       {mode: 'native ft' as const},
     ].map(testCase => {
-      itEth(`Disallow nest into collection without nesting permission [${testCase.mode}]`, async ({helper}) => {
+      itEth(`Disallow nest into collection without nesting permission [${testCase.mode}] (except for native fungible collection)`, 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});
@@ -259,7 +259,11 @@
         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');
+        if (testCase.mode === 'ft') {
+          await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');
+        } else {
+          await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.not.rejected;
+        }
       });
     });
   });
modifiedtests/src/sub/nesting/nesting.negative.test.tsdiffbeforeafterboth
58 {mode: 'ft'},58 {mode: 'ft'},
59 {mode: 'nativeFt'},59 {mode: 'nativeFt'},
60 ].map(testCase => {60 ].map(testCase => {
61 itSub(`Owner cannot nest [${testCase.mode}] if nesting is disabled`, async ({helper}) => {61 itSub(`Owner cannot nest [${testCase.mode}] if nesting is disabled (except for native fungible collection)`, async ({helper}) => {
62 // Create default collection, permissions are not set:62 // Create default collection, permissions are not set:
63 const aliceNFTCollection = await helper.nft.mintCollection(alice);63 const aliceNFTCollection = await helper.nft.mintCollection(alice);
64 const targetToken = await aliceNFTCollection.mintToken(alice);64 const targetToken = await aliceNFTCollection.mintToken(alice);
6565
66 const collectionForNesting = testCase.mode === 'ft' ? await helper.ft.mintCollection(alice) : helper.ft.getCollectionObject(0);66 const collectionForNesting = testCase.mode === 'ft' ? await helper.ft.mintCollection(alice) : helper.ft.getCollectionObject(0);
6767
68 // Alice cannot create immediately nested tokens:68 // Alice cannot create immediately nested tokens:
69 if (testCase.mode === 'ft') {
69 await expect(testCase.mode === 'ft'70 await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');
70 ? collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())71 } else {
71 : collectionForNesting.transfer(alice, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');72 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 100n)).to.be.not.rejected;
73 }
7274
73 // Alice can't mint and nest tokens:75 // Alice can't mint and nest tokens:
74 if (testCase.mode === 'ft') {76 if (testCase.mode === 'ft') {
75 await collectionForNesting.mint(alice, 100n);77 await collectionForNesting.mint(alice, 100n);
76 }78 }
79
80 if (testCase.mode === 'ft') {
77 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');81 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
82 } else {
83 await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.not.rejected;
84 }
78 });85 });
79 });86 });
8087
84 {mode: 'ft' as const},91 {mode: 'ft' as const},
85 {mode: 'native ft' as const},92 {mode: 'native ft' as const},
86 ].map(testCase => {93 ].map(testCase => {
87 itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens`, async ({helper}) => {94 itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens (except for native fungible collection)`, async ({helper}) => {
88 const targetCollection = await helper.nft.mintCollection(alice, {permissions:95 const targetCollection = await helper.nft.mintCollection(alice, {permissions:
89 {nesting: {tokenOwner: true, collectionAdmin: true}},96 {nesting: {tokenOwner: true, collectionAdmin: true}},
90 });97 });
118 case 'nft':125 case 'nft':
119 case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;126 case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;
120 case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;127 case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;
121 case 'native ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;128 case 'native ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.not.rejected; break;
122 }129 }
123 });130 });
124 });131 });
143 });150 });
144 });151 });
145152
146 itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {153 itSub.ifWithPallets('Cannot nest in non existing token (except for native fungible collection)', [Pallets.ReFungible], async ({helper}) => {
147 const collection = await helper.nft.mintCollection(alice);154 const collection = await helper.nft.mintCollection(alice);
148 // To avoid UserIsNotAllowedToNest error155 // To avoid UserIsNotAllowedToNest error
149 await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});156 await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});
179 await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);186 await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
180 await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);187 await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
181 await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);188 await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);
182 await expect(nativeFtCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);189 await expect(nativeFtCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.not.rejected;
183 }190 }
184 });191 });
185192
200 await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');207 await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
201 });208 });
202209
203 itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {210 itEth.ifWithPallets('Cannot nest in RFT or FT (except for native fungible collection)', [Pallets.ReFungible], async ({helper}) => {
204 // Create default collection, permissions are not set:211 // Create default collection, permissions are not set:
205 const rftCollection = await helper.rft.mintCollection(alice);212 const rftCollection = await helper.rft.mintCollection(alice);
206 const ftCollection = await helper.ft.mintCollection(alice);213 const ftCollection = await helper.ft.mintCollection(alice);
220 const nestedToken2 = await collectionForNesting.mintToken(alice);227 const nestedToken2 = await collectionForNesting.mintToken(alice);
221 await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');228 await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
222 await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');229 await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
223 await expect(nativeFtCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(nativeFtCollection.collectionId, 0)})).to.be.rejectedWith('common.UnsupportedOperation');230 await expect(nativeFtCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(nativeFtCollection.collectionId, 0)})).to.be.not.rejected;
224 });231 });
225232
226 itSub('Cannot nest in restricted collection if collection is not in the list', async ({helper}) => {233 itSub('Cannot nest in restricted collection if collection is not in the list (except native fungible collection)', async ({helper}) => {
227 const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);234 const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);
228 const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);235 const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);
229 const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);236 const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);
230 const notAllowedCollectionFT = await helper.ft.mintCollection(alice);237 const notAllowedCollectionFT = await helper.ft.mintCollection(alice);
231 const notAllowedCollectionNativeFT = helper.ft.getCollectionObject(0);238 const allowedCollectionNativeFT = helper.ft.getCollectionObject(0);
232239
233 // Collection restricted to allowedCollectionId240 // Collection restricted to allowedCollectionId
234 const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:241 const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:
253 await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);260 await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
254 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);261 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
255 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);262 await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);
256 await expect(notAllowedCollectionNativeFT.transfer(alice, targetTokenA.nestingAccount(), 100n)).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);263 await expect(allowedCollectionNativeFT.transfer(alice, targetTokenA.nestingAccount(), 100n)).to.be.not.rejected;
257 await expect(notAllowedCollectionNativeFT.transfer(alice, targetTokenB.nestingAccount(), 100n)).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);264 await expect(allowedCollectionNativeFT.transfer(alice, targetTokenB.nestingAccount(), 100n)).to.be.not.rejected;
258 });265 });
259266
260 itSub('Cannot create nesting chains greater than 5', async ({helper}) => {267 itSub('Cannot create nesting chains greater than 5', async ({helper}) => {