1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {ApiPromise} from '@polkadot/api';19import chai from 'chai';20import chaiAsPromised from 'chai-as-promised';21import {default as usingApi} from './substrate/substrate-api';22import {23 approveExpectFail,24 approveExpectSuccess,25 createCollectionExpectSuccess,26 createItemExpectSuccess,27 destroyCollectionExpectSuccess,28 setCollectionLimitsExpectSuccess,29 transferExpectSuccess,30 addCollectionAdminExpectSuccess,31 adminApproveFromExpectSuccess,32 getCreatedCollectionCount,33 transferFromExpectSuccess,34 transferFromExpectFail,35} from './util/helpers';3637chai.use(chaiAsPromised);3839describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {40 let alice: IKeyringPair;41 let bob: IKeyringPair;42 let charlie: IKeyringPair;4344 before(async () => {45 await usingApi(async (api, privateKeyWrapper) => {46 alice = privateKeyWrapper('//Alice');47 bob = privateKeyWrapper('//Bob');48 charlie = privateKeyWrapper('//Charlie');49 });50 });5152 it('Execute the extrinsic and check approvedList', async () => {53 const nftCollectionId = await createCollectionExpectSuccess();54 55 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');56 await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address);57 58 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});59 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');60 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);61 62 const reFungibleCollectionId =63 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});64 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');65 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address);66 });6768 it('Remove approval by using 0 amount', async () => {69 const nftCollectionId = await createCollectionExpectSuccess();70 71 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');72 await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address, 1);73 await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address, 0);74 75 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});76 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');77 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 1);78 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 0);79 80 const reFungibleCollectionId =81 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});82 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');83 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 1);84 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 0);85 });8687 it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {88 const collectionId = await createCollectionExpectSuccess();89 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);9091 await adminApproveFromExpectSuccess(collectionId, itemId, alice, bob.address, charlie.address);92 });93});9495describe('Normal user can approve other users to transfer:', () => {96 let alice: IKeyringPair;97 let bob: IKeyringPair;98 let charlie: IKeyringPair;99100 before(async () => {101 await usingApi(async (api, privateKeyWrapper) => {102 alice = privateKeyWrapper('//Alice');103 bob = privateKeyWrapper('//Bob');104 charlie = privateKeyWrapper('//Charlie');105 });106 }); 107108 it('NFT', async () => {109 const collectionId = await createCollectionExpectSuccess();110 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);111 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);112 });113114 it('Fungible up to an approved amount', async () => {115 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});116 const itemId = await createItemExpectSuccess(alice, collectionId, 'Fungible', bob.address); 117 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);118 });119120 it('ReFungible up to an approved amount', async () => {121 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});122 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', bob.address);123 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);124 });125});126127describe('Approved users can transferFrom up to approved amount:', () => {128 let alice: IKeyringPair;129 let bob: IKeyringPair;130 let charlie: IKeyringPair;131132 before(async () => {133 await usingApi(async (api, privateKeyWrapper) => {134 alice = privateKeyWrapper('//Alice');135 bob = privateKeyWrapper('//Bob');136 charlie = privateKeyWrapper('//Charlie');137 });138 }); 139140 it('NFT', async () => {141 const collectionId = await createCollectionExpectSuccess();142 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);143 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);144 await transferFromExpectSuccess(collectionId, itemId, charlie, bob, alice, 1, 'NFT');145 });146147 it('Fungible up to an approved amount', async () => {148 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});149 const itemId = await createItemExpectSuccess(alice, collectionId, 'Fungible', bob.address); 150 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);151 await transferFromExpectSuccess(collectionId, itemId, charlie, bob, alice, 1, 'Fungible');152 });153154 it('ReFungible up to an approved amount', async () => {155 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});156 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', bob.address);157 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);158 await transferFromExpectSuccess(collectionId, itemId, charlie, bob, alice, 1, 'ReFungible');159 });160});161162describe('Approved users cannot use transferFrom to repeat transfers if approved amount was already transferred:', () => {163 let alice: IKeyringPair;164 let bob: IKeyringPair;165 let charlie: IKeyringPair;166167 before(async () => {168 await usingApi(async (api, privateKeyWrapper) => {169 alice = privateKeyWrapper('//Alice');170 bob = privateKeyWrapper('//Bob');171 charlie = privateKeyWrapper('//Charlie');172 });173 }); 174175 it('NFT', async () => {176 const collectionId = await createCollectionExpectSuccess();177 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);178 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);179 await transferFromExpectSuccess(collectionId, itemId, charlie, bob, alice, 1, 'NFT');180 await transferFromExpectFail(collectionId, itemId, charlie, bob, alice, 1);181 });182183 it('Fungible up to an approved amount', async () => {184 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});185 const itemId = await createItemExpectSuccess(alice, collectionId, 'Fungible', bob.address); 186 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);187 await transferFromExpectSuccess(collectionId, itemId, charlie, bob, alice, 1, 'Fungible');188 await transferFromExpectFail(collectionId, itemId, charlie, bob, alice, 1);189 });190191 it('ReFungible up to an approved amount', async () => {192 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});193 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', bob.address);194 await approveExpectSuccess(collectionId, itemId, bob, charlie.address);195 await transferFromExpectSuccess(collectionId, itemId, charlie, bob, alice, 1, 'ReFungible');196 await transferFromExpectFail(collectionId, itemId, charlie, bob, alice, 1);197 });198});199200describe('Approved amount decreases by the transferred amount.:', () => {201 let alice: IKeyringPair;202 let bob: IKeyringPair;203 let charlie: IKeyringPair;204 let dave: IKeyringPair;205206 before(async () => {207 await usingApi(async (api, privateKeyWrapper) => {208 alice = privateKeyWrapper('//Alice');209 bob = privateKeyWrapper('//Bob');210 charlie = privateKeyWrapper('//Charlie');211 dave = privateKeyWrapper('//Dave');212 });213 }); 214215 it('If a user B is approved to transfer 10 Fungible tokens from user A, they can transfer 2 tokens to user C, which will result in decreasing approval from 10 to 8. Then user B can transfer 8 tokens to user D.', async () => {216 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});217 const itemId = await createItemExpectSuccess(alice, collectionId, 'Fungible', alice.address); 218 await approveExpectSuccess(collectionId, itemId, alice, bob.address, 10);219 await transferFromExpectSuccess(collectionId, itemId, bob, alice, charlie, 2, 'Fungible');220 await transferFromExpectSuccess(collectionId, itemId, bob, alice, dave, 8, 'Fungible');221 });222});223224describe('User may clear the approvals to approving for 0 amount:', () => {225 let alice: IKeyringPair;226 let bob: IKeyringPair;227 let charlie: IKeyringPair;228229 before(async () => {230 await usingApi(async (api, privateKeyWrapper) => {231 alice = privateKeyWrapper('//Alice');232 bob = privateKeyWrapper('//Bob');233 charlie = privateKeyWrapper('//Charlie');234 });235 });236237 it('NFT', async () => {238 const collectionId = await createCollectionExpectSuccess();239 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT');240 await approveExpectSuccess(collectionId, itemId, alice, bob.address, 1);241 await approveExpectSuccess(collectionId, itemId, alice, bob.address, 0);242 await transferFromExpectFail(collectionId, itemId, bob, bob, charlie, 1);243 });244245 it('Fungible', async () => {246 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});247 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');248 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 1);249 await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 0);250 await transferFromExpectFail(fungibleCollectionId, newFungibleTokenId, bob, bob, charlie, 1);251 });252253 it('ReFungible', async () => {254 const reFungibleCollectionId =255 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});256 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');257 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 1);258 await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 0);259 await transferFromExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, bob, charlie, 1);260 });261});262263describe('User cannot approve for the amount greater than they own:', () => {264 let alice: IKeyringPair;265 let bob: IKeyringPair;266 let charlie: IKeyringPair;267268 before(async () => {269 await usingApi(async (api, privateKeyWrapper) => {270 alice = privateKeyWrapper('//Alice');271 bob = privateKeyWrapper('//Bob');272 charlie = privateKeyWrapper('//Charlie');273 });274 });275276 it('1 for NFT', async () => {277 const collectionId = await createCollectionExpectSuccess();278 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);279 await approveExpectFail(collectionId, itemId, bob, charlie, 2);280 });281282 it('Fungible', async () => {283 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});284 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');285 await approveExpectFail(fungibleCollectionId, newFungibleTokenId, bob, charlie, 11);286 });287288 it('ReFungible', async () => {289 const reFungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});290 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');291 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, charlie, 101);292 });293});294295describe('Administrator and collection owner do not need approval in order to execute TransferFrom:', () => {296 let alice: IKeyringPair;297 let bob: IKeyringPair;298 let charlie: IKeyringPair;299 let dave: IKeyringPair;300301 before(async () => {302 await usingApi(async (api, privateKeyWrapper) => {303 alice = privateKeyWrapper('//Alice');304 bob = privateKeyWrapper('//Bob');305 charlie = privateKeyWrapper('//Charlie');306 dave = privateKeyWrapper('//Dave');307 });308 }); 309310 it('NFT', async () => {311 const collectionId = await createCollectionExpectSuccess();312 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', charlie.address);313 await transferFromExpectSuccess(collectionId, itemId, alice, charlie, dave, 1, 'NFT');314 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);315 await transferFromExpectSuccess(collectionId, itemId, bob, dave, alice, 1, 'NFT');316 });317318 it('Fungible up to an approved amount', async () => {319 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});320 const itemId = await createItemExpectSuccess(alice, collectionId, 'Fungible', charlie.address); 321 await transferFromExpectSuccess(collectionId, itemId, alice, charlie, dave, 1, 'Fungible');322 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);323 await transferFromExpectSuccess(collectionId, itemId, bob, dave, alice, 1, 'Fungible');324 });325326 it('ReFungible up to an approved amount', async () => {327 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});328 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', charlie.address);329 await transferFromExpectSuccess(collectionId, itemId, alice, charlie, dave, 1, 'ReFungible');330 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);331 await transferFromExpectSuccess(collectionId, itemId, bob, dave, alice, 1, 'ReFungible');332 });333});334335describe('Repeated approvals add up', () => {336 let alice: IKeyringPair;337 let bob: IKeyringPair;338 let charlie: IKeyringPair;339 let dave: IKeyringPair;340341 before(async () => {342 await usingApi(async (api, privateKeyWrapper) => {343 alice = privateKeyWrapper('//Alice');344 bob = privateKeyWrapper('//Bob');345 charlie = privateKeyWrapper('//Charlie');346 dave = privateKeyWrapper('//Dave');347 });348 }); 349350 it.skip('Owned 10, approval 1: 1, approval 2: 1, resulting approved value: 2. Fungible', async () => {351 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});352 await createItemExpectSuccess(alice, collectionId, 'Fungible', alice.address);353 await approveExpectSuccess(collectionId, 0, alice, bob.address, 1);354 await approveExpectSuccess(collectionId, 0, alice, charlie.address, 1);355 356 357 358 });359360 it.skip('Owned 10, approval 1: 1, approval 2: 1, resulting approved value: 2. ReFungible', async () => {361 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});362 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', alice.address);363 await approveExpectSuccess(collectionId, itemId, alice, bob.address, 1);364 await approveExpectSuccess(collectionId, itemId, alice, charlie.address, 1);365 366 367 368 });369370 371 it.skip('Cannot approve for more than total user`s amount (owned: 10, approval 1: 5 - should succeed, approval 2: 6 - should fail). Fungible', async () => {372 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});373 await createItemExpectSuccess(alice, collectionId, 'Fungible', dave.address);374 await approveExpectSuccess(collectionId, 0, dave, bob.address, 5);375 await approveExpectFail(collectionId, 0, dave, charlie, 6);376 });377378 379 it.skip('Cannot approve for more than total users amount (owned: 100, approval 1: 50 - should succeed, approval 2: 51 - should fail). ReFungible', async () => {380 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});381 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', dave.address);382 await approveExpectSuccess(collectionId, itemId, dave, bob.address, 50);383 await approveExpectFail(collectionId, itemId, dave, charlie, 51);384 });385});386387describe('Integration Test approve(spender, collection_id, item_id, amount) with collection admin permissions:', () => {388 let alice: IKeyringPair;389 let bob: IKeyringPair;390 let charlie: IKeyringPair;391392 before(async () => {393 await usingApi(async (api, privateKeyWrapper) => {394 alice = privateKeyWrapper('//Alice');395 bob = privateKeyWrapper('//Bob');396 charlie = privateKeyWrapper('//Charlie');397 });398 });399400 it('can be called by collection admin on non-owned item', async () => {401 const collectionId = await createCollectionExpectSuccess();402 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);403404 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);405 await adminApproveFromExpectSuccess(collectionId, itemId, bob, alice.address, charlie.address);406 });407});408409describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {410 let alice: IKeyringPair;411 let bob: IKeyringPair;412 let charlie: IKeyringPair;413414 before(async () => {415 await usingApi(async (api, privateKeyWrapper) => {416 alice = privateKeyWrapper('//Alice');417 bob = privateKeyWrapper('//Bob');418 charlie = privateKeyWrapper('//Charlie');419 });420 });421422 it('Approve for a collection that does not exist', async () => {423 await usingApi(async (api: ApiPromise) => {424 425 const nftCollectionCount = await getCreatedCollectionCount(api);426 await approveExpectFail(nftCollectionCount + 1, 1, alice, bob);427 428 const fungibleCollectionCount = await getCreatedCollectionCount(api);429 await approveExpectFail(fungibleCollectionCount + 1, 0, alice, bob);430 431 const reFungibleCollectionCount = await getCreatedCollectionCount(api);432 await approveExpectFail(reFungibleCollectionCount + 1, 1, alice, bob);433 });434 });435436 it('Approve for a collection that was destroyed', async () => {437 438 const nftCollectionId = await createCollectionExpectSuccess();439 await destroyCollectionExpectSuccess(nftCollectionId);440 await approveExpectFail(nftCollectionId, 1, alice, bob);441 442 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});443 await destroyCollectionExpectSuccess(fungibleCollectionId);444 await approveExpectFail(fungibleCollectionId, 0, alice, bob);445 446 const reFungibleCollectionId =447 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});448 await destroyCollectionExpectSuccess(reFungibleCollectionId);449 await approveExpectFail(reFungibleCollectionId, 1, alice, bob);450 });451452 it('Approve transfer of a token that does not exist', async () => {453 454 const nftCollectionId = await createCollectionExpectSuccess();455 await approveExpectFail(nftCollectionId, 2, alice, bob);456 457 const reFungibleCollectionId =458 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});459 await approveExpectFail(reFungibleCollectionId, 2, alice, bob);460 });461462 it('Approve using the address that does not own the approved token', async () => {463 const nftCollectionId = await createCollectionExpectSuccess();464 465 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');466 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice);467 468 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});469 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');470 await approveExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice);471 472 const reFungibleCollectionId =473 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});474 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');475 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, alice);476 });477478 it('should fail if approved more ReFungibles than owned', async () => {479 const nftCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});480 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'ReFungible');481 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 100, 'ReFungible');482 await approveExpectSuccess(nftCollectionId, newNftTokenId, bob, alice.address, 100);483 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice, 101);484 });485486 it('should fail if approved more Fungibles than owned', async () => {487 const nftCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});488 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'Fungible');489 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 10, 'Fungible');490 await approveExpectSuccess(nftCollectionId, newNftTokenId, bob, alice.address, 10);491 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice, 11);492 });493494 it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {495 const collectionId = await createCollectionExpectSuccess();496 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);497 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: false});498499 await approveExpectFail(collectionId, itemId, alice, charlie);500 });501});