1import {expect} from 'chai';2import {getApiConnection} from '../substrate/substrate-api';3import {createCollection, mintNft, sendNft} from './util/tx';4import {NftIdTuple} from './util/fetch';5import {isNftChildOfAnother, expectTxFailure} from './util/helpers';67describe('integration test: send NFT', () => {8 let api: any;9 before(async () => { api = await getApiConnection(); });1011 const maxNftId = 0xFFFFFFFF;1213 const alice = '//Alice';14 const bob = '//Bob';1516 const createTestCollection = async (issuerUri: string) => {17 return await createCollection(18 api,19 issuerUri,20 'nft-collection-metadata',21 null,22 'nft-collection',23 );24 };252627 it('send NFT to another user', async () => {28 const originalOwnerUri = alice;29 const newOwnerUri = bob;3031 const collectionId = await createTestCollection(alice);3233 const nftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'nft-metadata');3435 await sendNft(api, 'sent', originalOwnerUri, collectionId, nftId, newOwnerUri);36 });3738 it('[negative] unable to send non-existing NFT', async () => {39 const originalOwnerUri = alice;40 const newOwnerUri = bob;4142 const collectionId = 0;43 const tx = sendNft(api, 'sent', originalOwnerUri, collectionId, maxNftId, newOwnerUri);4445 await expectTxFailure(/rmrkCore\.NoAvailableNftId/, tx);46 });4748 it('[negative] unable to send NFT by a not-an-owner', async () => {49 const originalOwnerUri = alice;50 const newOwnerUri = bob;5152 const collectionId = await createTestCollection(alice);5354 const nftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'nft-metadata');5556 const tx = sendNft(api, 'sent', newOwnerUri, collectionId, nftId, newOwnerUri);57 await expectTxFailure(/rmrkCore\.NoPermission/, tx);58 });5960 it('send NFT to another NFT (same owner)', async () => {61 const originalOwnerUri = alice;6263 const collectionId = await createTestCollection(alice);6465 const parentNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'parent-nft-metadata');66 const childNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'child-nft-metadata');6768 const newOwnerNFT: NftIdTuple = [collectionId, parentNftId];6970 await sendNft(api, 'sent', alice, collectionId, childNftId, newOwnerNFT);7172 const isChild = await isNftChildOfAnother(api, collectionId, childNftId, newOwnerNFT);73 expect(isChild).to.be.true;74 });7576 it('[negative] send non-existing NFT to another NFT', async () => {77 const originalOwnerUri = alice;7879 const collectionId = await createTestCollection(alice);8081 const parentNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'parent-nft-metadata');82 const childNftId = maxNftId;8384 const newOwnerNFT: NftIdTuple = [collectionId, parentNftId];8586 const tx = sendNft(api, 'sent', alice, collectionId, childNftId, newOwnerNFT);8788 await expectTxFailure(/rmrkCore\.NoAvailableNftId/, tx);8990 const isChild = await isNftChildOfAnother(api, collectionId, childNftId, newOwnerNFT);91 expect(isChild).to.be.false;92 });9394 it('send NFT to another NFT (by not-an-owner)', async () => {95 const originalOwnerUri = alice;9697 const collectionId = await createTestCollection(alice);9899 const author = alice;100 const attacker = bob;101102 const parentNftId = await mintNft(api, author, originalOwnerUri, collectionId, 'parent-nft-metadata');103 const childNftId = await mintNft(api, author, originalOwnerUri, collectionId, 'child-nft-metadata');104105 const newOwnerNFT: NftIdTuple = [collectionId, parentNftId];106107 const tx = sendNft(api, 'sent', attacker, collectionId, childNftId, newOwnerNFT);108109 await expectTxFailure(/rmrkCore\.NoPermission/, tx);110111 const isChild = await isNftChildOfAnother(api, collectionId, childNftId, newOwnerNFT);112 expect(isChild).to.be.false;113 });114115 it('[negative] send NFT to non-existing NFT', async () => {116 const originalOwnerUri = alice;117118 const collectionId = await createTestCollection(alice);119120 const parentNftId = maxNftId;121 const childNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'child-nft-metadata');122123 const newOwnerNFT: NftIdTuple = [collectionId, parentNftId];124125 const tx = sendNft(api, 'sent', alice, collectionId, childNftId, newOwnerNFT);126127 await expectTxFailure(/rmrkCore\.NoAvailableNftId/, tx);128129 const isChild = await isNftChildOfAnother(api, collectionId, childNftId, newOwnerNFT);130 expect(isChild).to.be.false;131 });132133 it('send NFT to another NFT owned by another user', async () => {134 const ownerAlice = alice;135 const ownerBob = bob;136137 const aliceCollectionId = await createTestCollection(alice);138 const bobCollectionId = await createTestCollection(bob);139140 const parentNftId = await mintNft(api, alice, ownerAlice, aliceCollectionId, 'parent-nft-metadata');141 const childNftId = await mintNft(api, bob, ownerBob, bobCollectionId, 'child-nft-metadata');142143 const newOwnerNFT: NftIdTuple = [aliceCollectionId, parentNftId];144145 await sendNft(api, 'pending', bob, bobCollectionId, childNftId, newOwnerNFT);146 });147148 it('[negative] unable to send NFT to itself', async () => {149 const nftOwner = alice;150 const collectionId = await createTestCollection(alice);151152 const nftId = await mintNft(api, alice, nftOwner, collectionId, 'ouroboros-nft-metadata');153154 const newOwnerNFT: NftIdTuple = [collectionId, nftId];155156 const tx = sendNft(api, 'sent', alice, collectionId, nftId, newOwnerNFT);157158 await expectTxFailure(/rmrkCore\.CannotSendToDescendentOrSelf/, tx);159160 const isChild = await isNftChildOfAnother(api, collectionId, nftId, newOwnerNFT);161 expect(isChild).to.be.false;162 });163164 it('[negative] unable to send NFT to child NFT', async () => {165 const originalOwnerUri = alice;166167 const collectionId = await createTestCollection(alice);168169 const parentNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'parent-nft-metadata');170 const childNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'child-nft-metadata');171172 const newOwnerNFT: NftIdTuple = [collectionId, parentNftId];173174 await sendNft(api, 'sent', alice, collectionId, childNftId, newOwnerNFT);175176 const isChild = await isNftChildOfAnother(api, collectionId, childNftId, newOwnerNFT);177 expect(isChild).to.be.true;178179 const descendentOwner: NftIdTuple = [collectionId, childNftId];180 const tx = sendNft(api, 'sent', alice, collectionId, parentNftId, descendentOwner);181182 await expectTxFailure(/rmrkCore\.CannotSendToDescendentOrSelf/, tx);183 const isOuroboros = await isNftChildOfAnother(api, collectionId, parentNftId, descendentOwner);184 expect(isOuroboros).to.be.false;185 });186187 it('[negative] unable to send NFT to descendent NFT', async () => {188 const originalOwnerUri = alice;189190 const collectionId = await createTestCollection(alice);191192 const parentNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'parent-nft-metadata');193 const childNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'child-nft-metadata');194 const grandsonNftId = await mintNft(api, alice, originalOwnerUri, collectionId, 'grandson-nft-metadata');195196 const ownerParentNFT: NftIdTuple = [collectionId, parentNftId];197198 await sendNft(api, 'sent', alice, collectionId, childNftId, ownerParentNFT);199200 const isChild = await isNftChildOfAnother(api, collectionId, childNftId, ownerParentNFT);201 expect(isChild).to.be.true;202203 const ownerChildNFT: NftIdTuple = [collectionId, childNftId];204 await sendNft(api, 'sent', alice, collectionId, grandsonNftId, ownerChildNFT);205206 const isGrandson = await isNftChildOfAnother(api, collectionId, grandsonNftId, ownerChildNFT);207 expect(isGrandson).to.be.true;208209 const ownerGrandsonNFT: NftIdTuple = [collectionId, grandsonNftId];210 const tx = sendNft(api, 'sent', alice, collectionId, parentNftId, ownerGrandsonNFT);211212 await expectTxFailure(/rmrkCore\.CannotSendToDescendentOrSelf/, tx);213 const isOuroboros = await isNftChildOfAnother(api, collectionId, parentNftId, ownerGrandsonNFT);214 expect(isOuroboros).to.be.false;215 });216217 it('send nested NFT to another user', async () => {218 const originalOwner = alice;219 const newOwner = bob;220221 const collectionId = await createTestCollection(alice);222223 const parentNftId = await mintNft(api, alice, originalOwner, collectionId, 'parent-nft-metadata');224 const childNftId = await mintNft(api, alice, originalOwner, collectionId, 'child-nft-metadata');225226 const parentNftTuple: NftIdTuple = [collectionId, parentNftId];227228 await sendNft(api, 'sent', originalOwner, collectionId, childNftId, parentNftTuple);229230 await sendNft(api, 'sent', originalOwner, collectionId, childNftId, newOwner);231 });232233 it('[negative] send nested NFT to another user (by a not-root-owner)', async () => {234 const originalOwner = alice;235 const newOwner = bob;236237 const collectionId = await createTestCollection(alice);238239 const parentNftId = await mintNft(api, alice, originalOwner, collectionId, 'parent-nft-metadata');240 const childNftId = await mintNft(api, alice, originalOwner, collectionId, 'child-nft-metadata');241242 const parentNftTuple: NftIdTuple = [collectionId, parentNftId];243244 await sendNft(api, 'sent', originalOwner, collectionId, childNftId, parentNftTuple);245246 const tx = sendNft(api, 'sent', newOwner, collectionId, childNftId, newOwner);247248 await expectTxFailure(/rmrkCore\.NoPermission/, tx);249 });250251 after(() => { api.disconnect(); });252});