difftreelog
fix native ft nesting
in: master
6 files changed
Cargo.lockdiffbeforeafterboth6969 "frame-benchmarking",6969 "frame-benchmarking",6970 "frame-support",6970 "frame-support",6971 "frame-system",6971 "frame-system",6972 "pallet-balances",6972 "pallet-common",6973 "pallet-common",6973 "pallet-evm",6974 "pallet-evm",6974 "pallet-evm-coder-substrate",6975 "pallet-evm-coder-substrate",pallets/balances-adapter/src/lib.rsdiffbeforeafterboth149 ExistenceRequirement::AllowDeath,149 ExistenceRequirement::AllowDeath,150 )?;150 )?;151152 <PalletStructure<T>>::nest_if_sent_to_token(153 from.clone(),154 to,155 NATIVE_FUNGIBLE_COLLECTION_ID,156 TokenId::default(),157 nesting_budget,158 )?;159160 let balance_from: u128 =161 <T as Config>::Currency::free_balance(from.as_sub()).into();162 if balance_from == 0 {163 <PalletStructure<T>>::unnest_if_nested(164 from,165 NATIVE_FUNGIBLE_COLLECTION_ID,166 TokenId::default(),167 );168 }169 };151 };170152171 Ok(PostDispatchInfo {153 Ok(PostDispatchInfo {pallets/nonfungible/Cargo.tomldiffbeforeafterboth12frame-benchmarking = { workspace = true, optional = true }12frame-benchmarking = { workspace = true, optional = true }13frame-support = { workspace = true }13frame-support = { workspace = true }14frame-system = { workspace = true }14frame-system = { workspace = true }15pallet-balances = { workspace = true }15pallet-common = { workspace = true }16pallet-common = { workspace = true }16pallet-evm = { workspace = true }17pallet-evm = { workspace = true }17pallet-evm-coder-substrate = { workspace = true }18pallet-evm-coder-substrate = { workspace = true }pallets/nonfungible/src/lib.rsdiffbeforeafterboth170 + pallet_common::Config171 + pallet_structure::Config172 + pallet_evm::Config173 + pallet_balances::Config170 {174 {171 type WeightInfo: WeightInfo;175 type WeightInfo: WeightInfo;172 }176 }1325 }1329 }132613301327 fn nest(under: (CollectionId, TokenId), to_nest: (CollectionId, TokenId)) {1331 fn nest(under: (CollectionId, TokenId), to_nest: (CollectionId, TokenId)) {1332 if to_nest.0 != pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {1328 <TokenChildren<T>>::insert((under.0, under.1, (to_nest.0, to_nest.1)), true);1333 <TokenChildren<T>>::insert((under.0, under.1, to_nest), true);1334 }1329 }1335 }133013361331 fn unnest(under: (CollectionId, TokenId), to_unnest: (CollectionId, TokenId)) {1337 fn unnest(under: (CollectionId, TokenId), to_unnest: (CollectionId, TokenId)) {1338 if to_unnest.0 != pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID {1332 <TokenChildren<T>>::remove((under.0, under.1, to_unnest));1339 <TokenChildren<T>>::remove((under.0, under.1, to_unnest));1333 }1340 }1341 }133413421335 fn collection_has_tokens(collection_id: CollectionId) -> bool {1343 fn collection_has_tokens(collection_id: CollectionId) -> bool {1339 }1347 }134013481341 fn token_has_children(collection_id: CollectionId, token_id: TokenId) -> bool {1349 fn token_has_children(collection_id: CollectionId, token_id: TokenId) -> bool {1350 let address = T::CrossTokenAddressMapping::token_to_address(collection_id, token_id);1351 let balance = <pallet_balances::Pallet<T>>::free_balance(address.as_sub());13521353 balance > T::Balance::default()1342 <TokenChildren<T>>::iter_prefix((collection_id, token_id))1354 || <TokenChildren<T>>::iter_prefix((collection_id, token_id))1343 .next()1355 .next()1344 .is_some()1356 .is_some()1345 }1357 }134613581347 pub fn token_children_ids(collection_id: CollectionId, token_id: TokenId) -> Vec<TokenChild> {1359 pub fn token_children_ids(collection_id: CollectionId, token_id: TokenId) -> Vec<TokenChild> {1360 let mut tokens: Vec<_> = vec![];13611362 let address = T::CrossTokenAddressMapping::token_to_address(collection_id, token_id);1363 let balance = <pallet_balances::Pallet<T>>::free_balance(address.as_sub());1364 if balance > T::Balance::default() {1365 tokens.push(TokenChild {1366 token: TokenId(0),1367 collection: pallet_common::NATIVE_FUNGIBLE_COLLECTION_ID,1368 })1369 }13701371 tokens.extend(1348 <TokenChildren<T>>::iter_prefix((collection_id, token_id))1372 <TokenChildren<T>>::iter_prefix((collection_id, token_id)).map(1349 .map(|((child_collection_id, child_id), _)| TokenChild {1373 |((child_collection_id, child_id), _)| TokenChild {1350 collection: child_collection_id,1374 collection: child_collection_id,1351 token: child_id,1375 token: child_id,1352 })1376 },1353 .collect()1377 ),1378 );13791380 tokens1354 }1381 }135513821356 /// Mint single NFT token.1383 /// Mint single NFT token.tests/src/eth/nesting/nest.test.tsdiffbeforeafterboth248 {mode: 'ft' as const},248 {mode: 'ft' as const},249 {mode: 'native ft' as const},249 {mode: 'native ft' as const},250 ].map(testCase => {250 ].map(testCase => {251 itEth(`Disallow nest into collection without nesting permission [${testCase.mode}]`, async ({helper}) => {251 itEth(`Disallow nest into collection without nesting permission [${testCase.mode}] (except for native fungible collection)`, async ({helper}) => {252 const owner = await helper.eth.createAccountWithBalance(donor);252 const owner = await helper.eth.createAccountWithBalance(donor);253 const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);253 const {collectionId: targetCollectionId, contract: targetContract} = await createNestingCollection(helper, owner);254 await targetContract.methods.setCollectionNesting(false).send({from: owner});254 await targetContract.methods.setCollectionNesting(false).send({from: owner});259 const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;259 const targetTokenId = mintingTargetTokenIdResult.events.Transfer.returnValues.tokenId;260 const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);260 const targetTokenAddress = helper.ethAddress.fromTokenId(targetCollectionId, targetTokenId);261261262 if (testCase.mode === 'ft') {262 await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');263 await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.rejectedWith('UserIsNotAllowedToNest');264 } else {265 await expect(ftContract.methods.transfer(targetTokenAddress, 10n).call({from: owner})).to.be.not.rejected;266 }263 });267 });264 });268 });265 });269 });tests/src/sub/nesting/nesting.negative.test.tsdiffbeforeafterboth58 {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);656566 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);676768 // 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 }727473 // 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 }7980 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 });808784 {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 });145152146 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 error149 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 });185192200 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 });202209203 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 });225232226 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);232239233 // Collection restricted to allowedCollectionId240 // Collection restricted to allowedCollectionId234 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 });259266260 itSub('Cannot create nesting chains greater than 5', async ({helper}) => {267 itSub('Cannot create nesting chains greater than 5', async ({helper}) => {