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 adminApproveFromExpectFail,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`t be called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {88 const collectionId = await createCollectionExpectSuccess();89 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);9091 await adminApproveFromExpectFail(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 (with owner_can_transfer_flag = true):', () => {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 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});313 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', charlie.address);314 await transferFromExpectSuccess(collectionId, itemId, alice, charlie, dave, 1, 'NFT');315 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);316 await transferFromExpectSuccess(collectionId, itemId, bob, dave, alice, 1, 'NFT');317 });318319 it('Fungible up to an approved amount', async () => {320 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});321 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});322 const itemId = await createItemExpectSuccess(alice, collectionId, 'Fungible', charlie.address); 323 await transferFromExpectSuccess(collectionId, itemId, alice, charlie, dave, 1, 'Fungible');324 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);325 await transferFromExpectSuccess(collectionId, itemId, bob, dave, alice, 1, 'Fungible');326 });327328 it('ReFungible up to an approved amount', async () => {329 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});330 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: true});331 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', charlie.address);332 await transferFromExpectSuccess(collectionId, itemId, alice, charlie, dave, 1, 'ReFungible');333 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);334 await transferFromExpectSuccess(collectionId, itemId, bob, dave, alice, 1, 'ReFungible');335 });336});337338describe('Repeated approvals add up', () => {339 let alice: IKeyringPair;340 let bob: IKeyringPair;341 let charlie: IKeyringPair;342 let dave: IKeyringPair;343344 before(async () => {345 await usingApi(async (api, privateKeyWrapper) => {346 alice = privateKeyWrapper('//Alice');347 bob = privateKeyWrapper('//Bob');348 charlie = privateKeyWrapper('//Charlie');349 dave = privateKeyWrapper('//Dave');350 });351 }); 352353 it.skip('Owned 10, approval 1: 1, approval 2: 1, resulting approved value: 2. Fungible', async () => {354 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});355 await createItemExpectSuccess(alice, collectionId, 'Fungible', alice.address);356 await approveExpectSuccess(collectionId, 0, alice, bob.address, 1);357 await approveExpectSuccess(collectionId, 0, alice, charlie.address, 1);358 359 360 361 });362363 it.skip('Owned 10, approval 1: 1, approval 2: 1, resulting approved value: 2. ReFungible', async () => {364 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});365 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', alice.address);366 await approveExpectSuccess(collectionId, itemId, alice, bob.address, 1);367 await approveExpectSuccess(collectionId, itemId, alice, charlie.address, 1);368 369 370 371 });372373 374 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 () => {375 const collectionId = await createCollectionExpectSuccess({mode:{type: 'Fungible', decimalPoints: 0}});376 await createItemExpectSuccess(alice, collectionId, 'Fungible', dave.address);377 await approveExpectSuccess(collectionId, 0, dave, bob.address, 5);378 await approveExpectFail(collectionId, 0, dave, charlie, 6);379 });380381 382 it.skip('Cannot approve for more than total users amount (owned: 100, approval 1: 50 - should succeed, approval 2: 51 - should fail). ReFungible', async () => {383 const collectionId = await createCollectionExpectSuccess({mode:{type: 'ReFungible'}});384 const itemId = await createItemExpectSuccess(alice, collectionId, 'ReFungible', dave.address);385 await approveExpectSuccess(collectionId, itemId, dave, bob.address, 50);386 await approveExpectFail(collectionId, itemId, dave, charlie, 51);387 });388});389390describe('Integration Test approve(spender, collection_id, item_id, amount) with collection admin permissions:', () => {391 let alice: IKeyringPair;392 let bob: IKeyringPair;393 let charlie: IKeyringPair;394395 before(async () => {396 await usingApi(async (api, privateKeyWrapper) => {397 alice = privateKeyWrapper('//Alice');398 bob = privateKeyWrapper('//Bob');399 charlie = privateKeyWrapper('//Charlie');400 });401 });402403 it('can be called by collection admin on non-owned item', async () => {404 const collectionId = await createCollectionExpectSuccess();405 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);406407 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);408 await adminApproveFromExpectFail(collectionId, itemId, bob, alice.address, charlie.address);409 });410});411412describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {413 let alice: IKeyringPair;414 let bob: IKeyringPair;415 let charlie: IKeyringPair;416417 before(async () => {418 await usingApi(async (api, privateKeyWrapper) => {419 alice = privateKeyWrapper('//Alice');420 bob = privateKeyWrapper('//Bob');421 charlie = privateKeyWrapper('//Charlie');422 });423 });424425 it('Approve for a collection that does not exist', async () => {426 await usingApi(async (api: ApiPromise) => {427 428 const nftCollectionCount = await getCreatedCollectionCount(api);429 await approveExpectFail(nftCollectionCount + 1, 1, alice, bob);430 431 const fungibleCollectionCount = await getCreatedCollectionCount(api);432 await approveExpectFail(fungibleCollectionCount + 1, 0, alice, bob);433 434 const reFungibleCollectionCount = await getCreatedCollectionCount(api);435 await approveExpectFail(reFungibleCollectionCount + 1, 1, alice, bob);436 });437 });438439 it('Approve for a collection that was destroyed', async () => {440 441 const nftCollectionId = await createCollectionExpectSuccess();442 await destroyCollectionExpectSuccess(nftCollectionId);443 await approveExpectFail(nftCollectionId, 1, alice, bob);444 445 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});446 await destroyCollectionExpectSuccess(fungibleCollectionId);447 await approveExpectFail(fungibleCollectionId, 0, alice, bob);448 449 const reFungibleCollectionId =450 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});451 await destroyCollectionExpectSuccess(reFungibleCollectionId);452 await approveExpectFail(reFungibleCollectionId, 1, alice, bob);453 });454455 it('Approve transfer of a token that does not exist', async () => {456 457 const nftCollectionId = await createCollectionExpectSuccess();458 await approveExpectFail(nftCollectionId, 2, alice, bob);459 460 const reFungibleCollectionId =461 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});462 await approveExpectFail(reFungibleCollectionId, 2, alice, bob);463 });464465 it('Approve using the address that does not own the approved token', async () => {466 const nftCollectionId = await createCollectionExpectSuccess();467 468 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');469 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice);470 471 const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});472 const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');473 await approveExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice);474 475 const reFungibleCollectionId =476 await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});477 const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');478 await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, alice);479 });480481 it('should fail if approved more ReFungibles than owned', async () => {482 const nftCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});483 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'ReFungible');484 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 100, 'ReFungible');485 await approveExpectSuccess(nftCollectionId, newNftTokenId, bob, alice.address, 100);486 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice, 101);487 });488489 it('should fail if approved more Fungibles than owned', async () => {490 const nftCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});491 const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'Fungible');492 await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 10, 'Fungible');493 await approveExpectSuccess(nftCollectionId, newNftTokenId, bob, alice.address, 10);494 await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice, 11);495 });496497 it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {498 const collectionId = await createCollectionExpectSuccess();499 const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);500 await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: false});501502 await approveExpectFail(collectionId, itemId, alice, charlie);503 });504});