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 3132 33 34 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 41 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 46 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');47 48 49 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 54 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 59 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 71 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 76 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');77 78 79 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 84 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 89 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 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 106 await expect(executeTransaction(api, alice, api.tx.unique.createItem(107 collectionFT, 108 targetAddress, 109 {Fungible: {Value: 10}},110 ))).to.not.be.rejected;111112 113 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');114 115 await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');116 117 await transferExpectSuccess(collectionNFT, targetToken, alice, {Substrate: bob.address});118 119 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 134 await expect(executeTransaction(api, alice, api.tx.unique.createItem(135 collectionFT, 136 targetAddress, 137 {Fungible: {Value: 10}},138 ))).to.not.be.rejected;139140 141 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');142 143 await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');144 145 await transferExpectSuccess(collectionNFT, targetToken, alice, {Substrate: bob.address});146 147 await transferFromExpectSuccess(collectionFT, newToken, bob, targetAddress, {Substrate: bob.address}, 1, 'Fungible');148 });149 });150151 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 163 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 170 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');171 172 await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');173 174 await transferExpectSuccess(collectionNFT, targetToken, alice, {Substrate: bob.address});175 176 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 191 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 198 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');199 200 await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');201 202 await transferExpectSuccess(collectionNFT, targetToken, alice, {Substrate: bob.address});203 204 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 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 224 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 231 const newToken = await createItemExpectSuccess(alice, collection, 'NFT');232 233 await transferExpectFailure(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)}); 234 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 249 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');250251 252 await createItemExpectFailure(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});253254 255 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 271 const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');272273 274 await createItemExpectFailure(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});275276 277 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 289 const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');290291 292 await createItemExpectFailure(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});293294 295 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 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 313 await expect(executeTransaction(api, alice, api.tx.unique.createItem(314 collectionFT, 315 targetAddress, 316 {Fungible: {Value: 10}},317 ))).to.be.rejectedWith(/^common\.NestingIsDisabled$/);318319 320 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');321 322 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 336 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 342 await expect(executeTransaction(api, alice, api.tx.unique.createItem(343 collectionFT, 344 targetAddress, 345 {Fungible: {Value: 10}},346 ))).to.be.rejected;347348 349 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]}}); 358359 await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);360 await enableAllowListExpectSuccess(alice, collectionNFT);361 await enablePublicMintingExpectSuccess(alice, collectionNFT);362363 364 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 370 await expect(executeTransaction(api, alice, api.tx.unique.createItem(371 collectionFT, 372 targetAddress, 373 {Fungible: {Value: 10}},374 ))).to.be.rejected;375376 377 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 388 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 394 await expect(executeTransaction(api, alice, api.tx.unique.createItem(395 collectionFT, 396 targetAddress, 397 {Fungible: {Value: 10}},398 ))).to.be.rejected;399400 401 const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');402 await transferExpectFailure(collectionFT, newToken, alice, targetAddress, 1);403 });404 });405406 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 418 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 425 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');426 427 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 441 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');442 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};443444 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});445446 447 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 454 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 469 const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');470 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};471472 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});473474 475 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 482 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 493 const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');494 const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};495496 const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});497498 499 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 506 const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');507 await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);508 });509 });510});