difftreelog
test(nesting) extended test suite
in: master
4 files changed
tests/package.jsondiffbeforeafterboth--- a/tests/package.json
+++ b/tests/package.json
@@ -33,6 +33,7 @@
"loadTransfer": "ts-node src/transfer.nload.ts",
"testCollision": "mocha --timeout 9999999 -r ts-node/register ./src/collision-tests/*.test.ts",
"testEvent": "mocha --timeout 9999999 -r ts-node/register ./src/check-event/*.test.ts",
+ "testNesting": "mocha --timeout 9999999 -r ts-node/register ./**/nesting/**.test.ts",
"testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",
"testSetSchemaVersion": "mocha --timeout 9999999 -r ts-node/register ./**/setSchemaVersion.test.ts",
"testSetVariableMetaData": "mocha --timeout 9999999 -r ts-node/register ./**/setVariableMetaData.test.ts",
tests/src/nesting/nest.test.tsdiffbeforeafterboth1import {expect} from 'chai';2import {tokenIdToAddress} from '../eth/util/helpers';3import privateKey from '../substrate/privateKey';4import usingApi from '../substrate/substrate-api';5import {createCollectionExpectSuccess, createItemExpectSuccess, getTokenOwner, getTopmostTokenOwner, setCollectionLimitsExpectSuccess, transferExpectSuccess, transferFromExpectSuccess} from '../util/helpers';67describe('nesting', () => {8 it('allows to nest/unnest token', async () => {9 await usingApi(async api => {10 const alice = privateKey('//Alice');11 const bob = privateKey('//Bob');1213 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});14 await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule:{OwnerRestricted:[collection]}});15 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');1617 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT');1819 // Nest20 await transferExpectSuccess(collection, nestedToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});21 22 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});23 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});2425 // Move bundle to different user26 await transferExpectSuccess(collection, targetToken, alice, {Substrate: bob.address});27 28 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});29 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});3031 // Unnest32 await transferFromExpectSuccess(collection, nestedToken, bob, {Ethereum: tokenIdToAddress(collection, targetToken)}, {Substrate: bob.address});3334 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});35 });36 });37});1import {expect} from 'chai';2import {tokenIdToAddress} from '../eth/util/helpers';3import privateKey from '../substrate/privateKey';4import usingApi, {executeTransaction} from '../substrate/substrate-api';5import {6 addToAllowListExpectSuccess,7 createCollectionExpectSuccess,8 createItemExpectFailure, 9 createItemExpectSuccess,10 enableAllowListExpectSuccess,11 enablePublicMintingExpectSuccess,12 getTokenOwner, 13 getTopmostTokenOwner, 14 setCollectionLimitsExpectSuccess, 15 transferExpectFailure, 16 transferExpectSuccess, 17 transferFromExpectSuccess,18} from '../util/helpers';19import {IKeyringPair} from '@polkadot/types/types';2021let alice: IKeyringPair;22let bob: IKeyringPair;2324describe('Integration Test: Nesting', () => {25 before(async () => {26 alice = privateKey('//Alice');27 bob = privateKey('//Bob');28 });2930 // ---------- Non-Fungible ----------3132 // todo refactor names33 // todo remove excessive bundling, leave it for a single test34 it('NFT: allows to nest/unnest token if nesting rule is Owner', async () => {35 await usingApi(async api => {36 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});37 await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: 'Owner'});38 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');3940 // Create a nested token41 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});42 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});43 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});4445 // Create a token to be nested46 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');47 48 // Nest49 await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});50 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});51 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5253 // Move bundle to different user54 await transferExpectSuccess(collection, targetToken, alice, {Substrate: bob.address});55 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});56 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5758 // Unnest59 await transferFromExpectSuccess(collection, newToken, bob, {Ethereum: tokenIdToAddress(collection, targetToken)}, {Substrate: bob.address});60 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});61 });62 });6364 it('NFT: allows to nest/unnest token if Owner-Restricted', async () => {65 await usingApi(async api => {66 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});67 await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: {OwnerRestricted:[collection]}});68 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');6970 // Create a nested token71 const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});72 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});73 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});7475 // Create a token to be nested76 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');77 78 // Nest79 await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});80 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});81 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});8283 // Move bundle to different user84 await transferExpectSuccess(collection, targetToken, alice, {Substrate: bob.address});85 expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});86 expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});87 88 // Unnest89 await transferFromExpectSuccess(collection, newToken, bob, {Ethereum: tokenIdToAddress(collection, targetToken)}, {Substrate: bob.address});90 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});91 });92 });9394 // ---------- Fungible ----------9596 it('Fungible: allows to nest/unnest token if Owner', async () => {97 await usingApi(async api => {98 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});99 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: 'Owner'});100 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});101 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};102103 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});104105 // Create a nested token106 await expect(executeTransaction(api, alice, api.tx.unique.createItem(107 collectionFT, 108 targetAddress, 109 {Fungible: {Value: 10}},110 ))).to.not.be.rejected;111112 // Create a token to be nested113 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');114 // Nest115 await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');116 // Move bundle to different user117 await transferExpectSuccess(collectionNFT, targetToken, alice, {Substrate: bob.address});118 // Unnest119 await transferFromExpectSuccess(collectionFT, newToken, bob, targetAddress, {Substrate: bob.address}, 1, 'Fungible');120 });121 });122123 it('Fungible: allows to nest/unnest token if Owner-Restricted', async () => {124 await usingApi(async api => {125 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});126 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});127 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};128129 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});130131 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: {OwnerRestricted:[collectionFT]}});132133 // Create a nested token134 await expect(executeTransaction(api, alice, api.tx.unique.createItem(135 collectionFT, 136 targetAddress, 137 {Fungible: {Value: 10}},138 ))).to.not.be.rejected;139140 // Create a token to be nested141 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');142 // Nest143 await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');144 // Move bundle to different user145 await transferExpectSuccess(collectionNFT, targetToken, alice, {Substrate: bob.address});146 // Unnest147 await transferFromExpectSuccess(collectionFT, newToken, bob, targetAddress, {Substrate: bob.address}, 1, 'Fungible');148 });149 });150151 // ---------- Re-Fungible ----------152153 it('ReFungible: allows to nest/unnest token if Owner', async () => {154 await usingApi(async api => {155 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});156 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: 'Owner'});157 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});158 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};159160 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});161162 // Create a nested token163 await expect(executeTransaction(api, alice, api.tx.unique.createItem(164 collectionRFT, 165 targetAddress, 166 {ReFungible: {const_data: [], variable_data: [], pieces: 100}},167 ))).to.not.be.rejected;168169 // Create a token to be nested170 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');171 // Nest172 await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');173 // Move bundle to different user174 await transferExpectSuccess(collectionNFT, targetToken, alice, {Substrate: bob.address});175 // Unnest176 await transferFromExpectSuccess(collectionRFT, newToken, bob, targetAddress, {Substrate: bob.address}, 100, 'ReFungible');177 });178 });179180 it('ReFungible: allows to nest/unnest token if Owner-Restricted', async () => {181 await usingApi(async api => {182 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});183 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});184 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};185186 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});187188 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: {OwnerRestricted:[collectionRFT]}});189190 // Create a nested token191 await expect(executeTransaction(api, alice, api.tx.unique.createItem(192 collectionRFT, 193 targetAddress,194 {ReFungible: {const_data: [], variable_data: [], pieces: 100}},195 ))).to.not.be.rejected;196197 // Create a token to be nested198 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');199 // Nest200 await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');201 // Move bundle to different user202 await transferExpectSuccess(collectionNFT, targetToken, alice, {Substrate: bob.address});203 // Unnest204 await transferFromExpectSuccess(collectionRFT, newToken, bob, targetAddress, {Substrate: bob.address}, 100, 'ReFungible');205 });206 });207});208209describe('Negative Test: Nesting', async() => {210 before(async () => {211 alice = privateKey('//Alice');212 bob = privateKey('//Bob');213 });214215 // ---------- Non-Fungible ----------216217 it('NFT: disallows to nest token if nesting is disabled', async () => {218 await usingApi(async api => {219 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});220 await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: 'Disabled'});221 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');222223 // Try to create a nested token224 await expect(executeTransaction(api, alice, api.tx.unique.createItem(225 collection, 226 {Ethereum: tokenIdToAddress(collection, targetToken)}, 227 {nft: {const_data: [], variable_data: []}} as any,228 ))).to.be.rejectedWith(/^common\.NestingIsDisabled$/);229230 // Create a token to be nested231 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');232 // Try to nest233 await transferExpectFailure(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)}); // todo to.be.rejected234 expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});235 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});236 });237 });238239 it('NFT: disallows to nest token if not Owner', async () => {240 await usingApi(async api => {241 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});242 await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: 'Owner'});243244 await addToAllowListExpectSuccess(alice, collection, bob.address);245 await enableAllowListExpectSuccess(alice, collection);246 await enablePublicMintingExpectSuccess(alice, collection);247248 // Create a token to attempt to be nested into249 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');250251 // Try to create a nested token in the wrong collection252 await createItemExpectFailure(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});253254 // Try to create and nest a token in the wrong collection255 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');256 await transferExpectFailure(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});257 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});258 });259 });260261 it('NFT: disallows to nest token if not Owner (Restricted nesting)', async () => {262 await usingApi(async api => {263 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});264 await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: {OwnerRestricted:[collection]}});265266 await addToAllowListExpectSuccess(alice, collection, bob.address);267 await enableAllowListExpectSuccess(alice, collection);268 await enablePublicMintingExpectSuccess(alice, collection);269270 // Create a token to attempt to be nested into271 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');272273 // Try to create a nested token in the wrong collection274 await createItemExpectFailure(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});275276 // Try to create and nest a token in the wrong collection277 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');278 await transferExpectFailure(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});279 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});280 });281 });282283 it('NFT: disallows to nest token to an unlisted collection', async () => {284 await usingApi(async api => {285 const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});286 await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: {OwnerRestricted:[]}});287288 // Create a token to attempt to be nested into289 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');290291 // Try to create a nested token in the wrong collection292 await createItemExpectFailure(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});293294 // Try to create and nest a token in the wrong collection295 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');296 await transferExpectFailure(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});297 expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});298 });299 });300301 // ---------- Fungible ----------302303 it('Fungible: disallows to nest token if nesting is disabled', async () => {304 await usingApi(async api => {305 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});306 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: 'Disabled'});307 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');308 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};309310 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});311312 // Try to create a nested token313 await expect(executeTransaction(api, alice, api.tx.unique.createItem(314 collectionFT, 315 targetAddress, 316 {Fungible: {Value: 10}},317 ))).to.be.rejectedWith(/^common\.NestingIsDisabled$/);318319 // Create a token to be nested320 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');321 // Try to nest322 await transferExpectFailure(collectionFT, newToken, alice, targetAddress, 1);323 });324 });325326 it('Fungible: disallows to nest token if not Owner', async () => {327 await usingApi(async api => {328 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});329 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: 'Owner'});330331 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);332 await enableAllowListExpectSuccess(alice, collectionNFT);333 await enablePublicMintingExpectSuccess(alice, collectionNFT);334335 // Create a token to attempt to be nested into336 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');337 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};338339 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});340341 // Try to create a nested token in the wrong collection342 await expect(executeTransaction(api, alice, api.tx.unique.createItem(343 collectionFT, 344 targetAddress, 345 {Fungible: {Value: 10}},346 ))).to.be.rejected;347348 // Try to create and nest a token in the wrong collection349 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');350 await transferExpectFailure(collectionFT, newToken, alice, targetAddress, 1);351 });352 });353354 it('Fungible: disallows to nest token if not Owner (Restricted nesting)', async () => {355 await usingApi(async api => {356 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});357 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: {OwnerRestricted:[collectionNFT]}}); // todo clear redundant restrictions358359 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);360 await enableAllowListExpectSuccess(alice, collectionNFT);361 await enablePublicMintingExpectSuccess(alice, collectionNFT);362363 // Create a token to attempt to be nested into364 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');365 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};366367 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});368369 // Try to create a nested token in the wrong collection370 await expect(executeTransaction(api, alice, api.tx.unique.createItem(371 collectionFT, 372 targetAddress, 373 {Fungible: {Value: 10}},374 ))).to.be.rejected;375376 // Try to create and nest a token in the wrong collection377 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');378 await transferExpectFailure(collectionFT, newToken, alice, targetAddress, 1);379 });380 });381382 it('Fungible: disallows to nest token to an unlisted collection', async () => {383 await usingApi(async api => {384 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});385 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: {OwnerRestricted:[]}});386387 // Create a token to attempt to be nested into388 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');389 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};390391 const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});392393 // Try to create a nested token in the wrong collection394 await expect(executeTransaction(api, alice, api.tx.unique.createItem(395 collectionFT, 396 targetAddress, 397 {Fungible: {Value: 10}},398 ))).to.be.rejected;399400 // Try to create and nest a token in the wrong collection401 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');402 await transferExpectFailure(collectionFT, newToken, alice, targetAddress, 1);403 });404 });405406 // ---------- Re-Fungible ----------407408 it('ReFungible: disallows to nest token if nesting is disabled', async () => {409 await usingApi(async api => {410 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});411 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: 'Disabled'});412 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');413 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};414415 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});416417 // Create a nested token418 await expect(executeTransaction(api, alice, api.tx.unique.createItem(419 collectionRFT, 420 targetAddress, 421 {ReFungible: {const_data: [], variable_data: [], pieces: 100}},422 ))).to.be.rejectedWith(/^common\.NestingIsDisabled$/);423424 // Create a token to be nested425 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');426 // Try to nest427 await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);428 });429 });430431 it('ReFungible: disallows to nest token if not Owner', async () => {432 await usingApi(async api => {433 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});434 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: 'Owner'});435436 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);437 await enableAllowListExpectSuccess(alice, collectionNFT);438 await enablePublicMintingExpectSuccess(alice, collectionNFT);439440 // Create a token to attempt to be nested into441 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');442 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};443444 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});445446 // Try to create a nested token in the wrong collection447 await expect(executeTransaction(api, alice, api.tx.unique.createItem(448 collectionRFT, 449 targetAddress, 450 {ReFungible: {const_data: [], variable_data: [], pieces: 100}},451 ))).to.be.rejected;452453 // Try to create and nest a token in the wrong collection454 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');455 await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);456 });457 });458459 it('ReFungible: disallows to nest token if not Owner (Restricted nesting)', async () => {460 await usingApi(async api => {461 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});462 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: {OwnerRestricted:[collectionNFT]}});463464 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);465 await enableAllowListExpectSuccess(alice, collectionNFT);466 await enablePublicMintingExpectSuccess(alice, collectionNFT);467468 // Create a token to attempt to be nested into469 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');470 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};471472 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});473474 // Try to create a nested token in the wrong collection475 await expect(executeTransaction(api, alice, api.tx.unique.createItem(476 collectionRFT, 477 targetAddress, 478 {ReFungible: {const_data: [], variable_data: [], pieces: 100}},479 ))).to.be.rejected;480481 // Try to create and nest a token in the wrong collection482 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');483 await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);484 });485 });486487 it('ReFungible: disallows to nest token to an unlisted collection', async () => {488 await usingApi(async api => {489 const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});490 await setCollectionLimitsExpectSuccess(alice, collectionNFT, {nestingRule: {OwnerRestricted:[]}});491492 // Create a token to attempt to be nested into493 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');494 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};495496 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});497498 // Try to create a nested token in the wrong collection499 await expect(executeTransaction(api, alice, api.tx.unique.createItem(500 collectionRFT, 501 targetAddress, 502 {ReFungible: {const_data: [], variable_data: [], pieces: 100}},503 ))).to.be.rejected;504505 // Try to create and nest a token in the wrong collection506 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');507 await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);508 });509 });510});tests/src/nesting/unnest.test.tsdiffbeforeafterboth--- /dev/null
+++ b/tests/src/nesting/unnest.test.ts
@@ -0,0 +1,130 @@
+import {expect} from 'chai';
+import {tokenIdToAddress} from '../eth/util/helpers';
+import privateKey from '../substrate/privateKey';
+import usingApi, {executeTransaction} from '../substrate/substrate-api';
+import {
+ createCollectionExpectSuccess,
+ createItemExpectFailure,
+ createItemExpectSuccess,
+ getTokenOwner,
+ getTopmostTokenOwner,
+ normalizeAccountId,
+ setCollectionLimitsExpectSuccess,
+ transferExpectFailure,
+ transferExpectSuccess,
+} from '../util/helpers';
+import {IKeyringPair} from '@polkadot/types/types';
+
+let alice: IKeyringPair;
+let bob: IKeyringPair;
+
+describe('Integration Test: Unnesting', () => {
+ before(async () => {
+ alice = privateKey('//Alice');
+ bob = privateKey('//Bob');
+ });
+
+ it('Allows the owner to successfully unnest a token', async () => {
+ await usingApi(async api => {
+ const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
+ await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: 'Owner'});
+ const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');
+ const targetAddress = {Ethereum: tokenIdToAddress(collection, targetToken)};
+
+ // Create a nested token
+ const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', targetAddress);
+
+ // Unnest
+ await expect(executeTransaction(
+ api,
+ alice,
+ api.tx.unique.transferFrom(normalizeAccountId(targetAddress), normalizeAccountId(alice), collection, nestedToken, 1),
+ )).to.not.be.rejected;
+ expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});
+
+ // Nest and burn
+ await transferExpectSuccess(collection, nestedToken, alice, targetAddress);
+ await expect(executeTransaction(
+ api,
+ alice,
+ api.tx.unique.burnFrom(collection, normalizeAccountId(alice.address), nestedToken, 1),
+ )).to.not.be.rejected;
+ await expect(getTokenOwner(api, collection, nestedToken)).to.be.rejected; // 'owner == null'
+ });
+ });
+
+ // todo refungible-fungible test just in case
+});
+
+describe('Negative Test: Unnesting', () => {
+ before(async () => {
+ alice = privateKey('//Alice');
+ bob = privateKey('//Bob');
+ });
+
+ it('Disallows a non-owner to unnest/burn a token', async () => {
+ await usingApi(async api => {
+ const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
+ await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: 'Owner'});
+ const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');
+ const targetAddress = {Ethereum: tokenIdToAddress(collection, targetToken)};
+
+ // Create a nested token
+ const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', targetAddress);
+
+ // Try to unnest
+ await expect(executeTransaction(
+ api,
+ bob,
+ api.tx.unique.transferFrom(normalizeAccountId(targetAddress), normalizeAccountId(bob), collection, nestedToken, 1),
+ )).to.be.rejectedWith(/^common\.ApprovedValueTooLow$/);
+ //await transferFromExpectSuccess(collection, nestedToken, bob, targetAddress, {Substrate: bob.address});
+ expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});
+
+ // Try to burn
+ await expect(executeTransaction(
+ api,
+ bob,
+ api.tx.unique.burnFrom(collection, normalizeAccountId(bob.address), nestedToken, 1),
+ )).to.not.be.rejectedWith(/^common\.ApprovedValueTooLow$/);
+ expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});
+ });
+ });
+
+ it('Disallows excessive token nesting', async () => {
+ await usingApi(async api => {
+ const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
+ await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: 'Owner'});
+ const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');
+
+ // Create a nested token matryoshka
+ const nestedToken1 = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});
+ const nestedToken2 = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, nestedToken1)});
+ // The nesting depth is limited by 2
+ await createItemExpectFailure(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, nestedToken2)});
+
+ expect(await getTopmostTokenOwner(api, collection, nestedToken2)).to.be.deep.equal({Substrate: alice.address});
+ });
+ });
+
+ // todo another test for creating excessive depth matryoshka with Ethereum, move this one to nest ^
+
+ // Recursive nesting
+ it('Prevents Ouroboros-nested operations', async () => {
+ await usingApi(async api => {
+ const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});
+ await setCollectionLimitsExpectSuccess(alice, collection, {nestingRule: 'Owner'});
+ const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');
+
+ // Create a nested token ouroboros
+ const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});
+ await transferExpectSuccess(collection, targetToken, alice, {Ethereum: tokenIdToAddress(collection, nestedToken)});
+
+ expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});
+
+ // Make sure the ouroboros is detected
+ await expect(getTopmostTokenOwner(api, collection, nestedToken)).to.be.rejected; // With(/^common\.DepthLimit$/);
+ // todo transferFrom, must exit with Ouroboros error
+ });
+ });
+});
\ No newline at end of file
tests/src/util/helpers.tsdiffbeforeafterboth--- a/tests/src/util/helpers.ts
+++ b/tests/src/util/helpers.ts
@@ -943,11 +943,11 @@
collectionId: number,
tokenId: number,
sender: IKeyringPair,
- recipient: IKeyringPair,
+ recipient: IKeyringPair | CrossAccountId,
value: number | bigint = 1,
) {
await usingApi(async (api: ApiPromise) => {
- const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient.address), collectionId, tokenId, value);
+ const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient), collectionId, tokenId, value);
const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;
const result = getGenericResult(events);
// if (events && Array.isArray(events)) {
@@ -1090,7 +1090,7 @@
return newItemId;
}
-export async function createItemExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {
+export async function createItemExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {
await usingApi(async (api) => {
const tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), createMode);