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 adminApproveFromExpectSuccess,21 getCreatedCollectionCount,22} from './util/helpers';2324chai.use(chaiAsPromised);2526describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {27 let alice: IKeyringPair;28 let bob: IKeyringPair;29 let charlie: IKeyringPair;3031 before(async () => {32 await usingApi(async () => {33 alice = privateKey('//Alice');34 bob = privateKey('//Bob');35 charlie = privateKey('//Charlie');36 });37 });3839 it('Execute the extrinsic and check approvedList', async () => {40 const nftCollectionId = await createCollectionExpectSuccess();41 42 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');43 await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address);44 45 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});46 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');47 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);48 49 const reFungibleCollectionId =50 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});51 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');52 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address);53 });5455 it('Remove approval by using 0 amount', async () => {56 const nftCollectionId = await createCollectionExpectSuccess();57 58 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');59 await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address, 1);60 await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address, 0);61 62 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});63 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');64 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 1);65 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 0);66 67 const reFungibleCollectionId =68 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});69 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');70 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 1);71 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 0);72 });7374 it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {75 const collectionId = await createCollectionExpectSuccess();76 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);7778 await adminApproveFromExpectSuccess(collectionId, itemId, alice, bob.address, charlie.address);79 });80});8182describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {83 let alice: IKeyringPair;84 let bob: IKeyringPair;85 let charlie: IKeyringPair;8687 before(async () => {88 await usingApi(async () => {89 alice = privateKey('//Alice');90 bob = privateKey('//Bob');91 charlie = privateKey('//Charlie');92 });93 });9495 it('Approve for a collection that does not exist', async () => {96 await usingApi(async (api: ApiPromise) => {97 98 const nftCollectionCount = await getCreatedCollectionCount(api);99 await approveExpectFail(nftCollectionCount + 1, 1, alice, bob);100 101 const fungibleCollectionCount = await getCreatedCollectionCount(api);102 await approveExpectFail(fungibleCollectionCount + 1, 0, alice, bob);103 104 const reFungibleCollectionCount = await getCreatedCollectionCount(api);105 await approveExpectFail(reFungibleCollectionCount + 1, 1, alice, bob);106 });107 });108109 it('Approve for a collection that was destroyed', async () => {110 111 const nftCollectionId = await createCollectionExpectSuccess();112 await destroyCollectionExpectSuccess(nftCollectionId);113 await approveExpectFail(nftCollectionId, 1, alice, bob);114 115 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});116 await destroyCollectionExpectSuccess(fungibleCollectionId);117 await approveExpectFail(fungibleCollectionId, 0, alice, bob);118 119 const reFungibleCollectionId =120 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});121 await destroyCollectionExpectSuccess(reFungibleCollectionId);122 await approveExpectFail(reFungibleCollectionId, 1, alice, bob);123 });124125 it('Approve transfer of a token that does not exist', async () => {126 127 const nftCollectionId = await createCollectionExpectSuccess();128 await approveExpectFail(nftCollectionId, 2, alice, bob);129 130 const reFungibleCollectionId =131 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});132 await approveExpectFail(reFungibleCollectionId, 2, alice, bob);133 });134135 it('Approve using the address that does not own the approved token', async () => {136 const nftCollectionId = await createCollectionExpectSuccess();137 138 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');139 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice);140 141 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});142 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');143 await approveExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice);144 145 const reFungibleCollectionId =146 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});147 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');148 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, alice);149 });150151 it('should fail if approved more ReFungibles than owned', async () => {152 const nftCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});153 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'ReFungible');154 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 100, 'ReFungible');155 await approveExpectSuccess(nftCollectionId, newNftTokenId, bob, alice.address, 100);156 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice, 101);157 });158159 it('should fail if approved more Fungibles than owned', async () => {160 const nftCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});161 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'Fungible');162 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 10, 'Fungible');163 await approveExpectSuccess(nftCollectionId, newNftTokenId, bob, alice.address, 10);164 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice, 11);165 });166167 it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {168 const collectionId = await createCollectionExpectSuccess();169 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);170 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: false});171172 await approveExpectFail(collectionId, itemId, alice, charlie);173 });174});175176describe('Integration Test approve(spender, collection_id, item_id, amount) with collection admin permissions:', () => {177 let alice: IKeyringPair;178 let bob: IKeyringPair;179 let charlie: IKeyringPair;180181 before(async () => {182 await usingApi(async () => {183 alice = privateKey('//Alice');184 bob = privateKey('//Bob');185 charlie = privateKey('//Charlie');186 });187 });188189 it('can be called by collection admin on non-owned item', async () => {190 const collectionId = await createCollectionExpectSuccess();191 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);192193 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);194 await adminApproveFromExpectSuccess(collectionId, itemId, bob, alice.address, charlie.address);195 });196});