12345import { IKeyringPair } from '@polkadot/types/types';6import { ApiPromise } from '@polkadot/api';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';10import { default as usingApi } from './substrate/substrate-api';11import {12 approveExpectFail,13 approveExpectSuccess,14 createCollectionExpectSuccess,15 createItemExpectSuccess,16 destroyCollectionExpectSuccess,17 setCollectionLimitsExpectSuccess,18 transferExpectSuccess,19 addCollectionAdminExpectSuccess,20} from './util/helpers';2122chai.use(chaiAsPromised);2324describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {25 let Alice: IKeyringPair;26 let Bob: IKeyringPair;27 let Charlie: IKeyringPair;2829 before(async () => {30 await usingApi(async () => {31 Alice = privateKey('//Alice');32 Bob = privateKey('//Bob');33 Charlie = privateKey('//Charlie');34 });35 });3637 it('Execute the extrinsic and check approvedList', async () => {38 const nftCollectionId = await createCollectionExpectSuccess();39 40 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');41 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);42 43 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});44 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');45 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);46 47 const reFungibleCollectionId =48 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});49 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');50 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);51 });5253 it('Remove approval by using 0 amount', async () => {54 const nftCollectionId = await createCollectionExpectSuccess();55 56 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');57 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);58 await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);59 60 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});61 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');62 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);63 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);64 65 const reFungibleCollectionId =66 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});67 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');68 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);69 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);70 });7172 it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {73 const collectionId = await createCollectionExpectSuccess();74 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Bob.address);7576 await approveExpectSuccess(collectionId, itemId, Alice, Charlie);77 });78});7980describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {81 let Alice: IKeyringPair;82 let Bob: IKeyringPair;83 let Charlie: IKeyringPair;8485 before(async () => {86 await usingApi(async () => {87 Alice = privateKey('//Alice');88 Bob = privateKey('//Bob');89 Charlie = privateKey('//Charlie');90 });91 });9293 it('Approve for a collection that does not exist', async () => {94 await usingApi(async (api: ApiPromise) => {95 96 const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;97 await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);98 99 const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;100 await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);101 102 const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;103 await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);104 });105 });106107 it('Approve for a collection that was destroyed', async () => {108 109 const nftCollectionId = await createCollectionExpectSuccess();110 await destroyCollectionExpectSuccess(nftCollectionId);111 await approveExpectFail(nftCollectionId, 1, Alice, Bob);112 113 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});114 await destroyCollectionExpectSuccess(fungibleCollectionId);115 await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);116 117 const reFungibleCollectionId =118 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});119 await destroyCollectionExpectSuccess(reFungibleCollectionId);120 await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);121 });122123 it('Approve transfer of a token that does not exist', async () => {124 125 const nftCollectionId = await createCollectionExpectSuccess();126 await approveExpectFail(nftCollectionId, 2, Alice, Bob);127 128 const reFungibleCollectionId =129 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});130 await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);131 });132133 it('Approve using the address that does not own the approved token', async () => {134 const nftCollectionId = await createCollectionExpectSuccess();135 136 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');137 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);138 139 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});140 const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');141 await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);142 143 const reFungibleCollectionId =144 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});145 const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');146 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);147 });148149 it('should fail if approved more NFTs than owned', async () => {150 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });151 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');152 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');153 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice);154 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);155 });156157 it('should fail if approved more ReFungibles than owned', async () => {158 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'ReFungible' } });159 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'ReFungible');160 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 100, 'ReFungible');161 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 100);162 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);163 });164165 it('should fail if approved more Fungibles than owned', async () => {166 const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });167 const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'Fungible');168 await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 10, 'Fungible');169 await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 10);170 await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);171 });172173 it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {174 const collectionId = await createCollectionExpectSuccess();175 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Bob.address);176 await setCollectionLimitsExpectSuccess(Alice, collectionId, { ownerCanTransfer: false });177178 await approveExpectFail(collectionId, itemId, Alice, Charlie);179 });180});181182describe('Integration Test approve(spender, collection_id, item_id, amount) with collection admin permissions:', () => {183 let Alice: IKeyringPair;184 let Bob: IKeyringPair;185 let Charlie: IKeyringPair;186187 before(async () => {188 await usingApi(async () => {189 Alice = privateKey('//Alice');190 Bob = privateKey('//Bob');191 Charlie = privateKey('//Charlie');192 });193 });194195 it('can be called by collection admin on non-owned item', async () => {196 const collectionId = await createCollectionExpectSuccess();197 const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Alice.address);198199 await addCollectionAdminExpectSuccess(Alice, collectionId, Bob);200 await approveExpectSuccess(collectionId, itemId, Bob, Charlie);201 });202});