git.delta.rocks / unique-network / refs/commits / 82bedeffe28d

difftreelog

source

tests/src/approve.test.ts9.0 KiBsourcehistory
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//5import {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} from './util/helpers';2223chai.use(chaiAsPromised);2425describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {26  let alice: IKeyringPair;27  let bob: IKeyringPair;28  let charlie: IKeyringPair;2930  before(async () => {31    await usingApi(async () => {32      alice = privateKey('//Alice');33      bob = privateKey('//Bob');34      charlie = privateKey('//Charlie');35    });36  });3738  it('Execute the extrinsic and check approvedList', async () => {39    const nftCollectionId = await createCollectionExpectSuccess();40    // nft41    const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');42    await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address);43    // fungible44    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});45    const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');46    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address);47    // reFungible48    const reFungibleCollectionId =49      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});50    const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');51    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address);52  });5354  it('Remove approval by using 0 amount', async () => {55    const nftCollectionId = await createCollectionExpectSuccess();56    // nft57    const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');58    await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address, 1);59    await approveExpectSuccess(nftCollectionId, newNftTokenId, alice, bob.address, 0);60    // fungible61    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});62    const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');63    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 1);64    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, alice, bob.address, 0);65    // reFungible66    const reFungibleCollectionId =67      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});68    const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');69    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 1);70    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, alice, bob.address, 0);71  });7273  it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {74    const collectionId = await createCollectionExpectSuccess();75    const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);7677    await adminApproveFromExpectSuccess(collectionId, itemId, alice, bob.address, charlie.address);78  });79});8081describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {82  let alice: IKeyringPair;83  let bob: IKeyringPair;84  let charlie: IKeyringPair;8586  before(async () => {87    await usingApi(async () => {88      alice = privateKey('//Alice');89      bob = privateKey('//Bob');90      charlie = privateKey('//Charlie');91    });92  });9394  it('Approve for a collection that does not exist', async () => {95    await usingApi(async (api: ApiPromise) => {96      // nft97      const nftCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();98      await approveExpectFail(nftCollectionCount + 1, 1, alice, bob);99      // fungible100      const fungibleCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();101      await approveExpectFail(fungibleCollectionCount + 1, 0, alice, bob);102      // reFungible103      const reFungibleCollectionCount = (await api.query.common.createdCollectionCount()).toNumber();104      await approveExpectFail(reFungibleCollectionCount + 1, 1, alice, bob);105    });106  });107108  it('Approve for a collection that was destroyed', async () => {109    // nft110    const nftCollectionId = await createCollectionExpectSuccess();111    await destroyCollectionExpectSuccess(nftCollectionId);112    await approveExpectFail(nftCollectionId, 1, alice, bob);113    // fungible114    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});115    await destroyCollectionExpectSuccess(fungibleCollectionId);116    await approveExpectFail(fungibleCollectionId, 0, alice, bob);117    // reFungible118    const reFungibleCollectionId =119      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});120    await destroyCollectionExpectSuccess(reFungibleCollectionId);121    await approveExpectFail(reFungibleCollectionId, 1, alice, bob);122  });123124  it('Approve transfer of a token that does not exist', async () => {125    // nft126    const nftCollectionId = await createCollectionExpectSuccess();127    await approveExpectFail(nftCollectionId, 2, alice, bob);128    // reFungible129    const reFungibleCollectionId =130      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});131    await approveExpectFail(reFungibleCollectionId, 2, alice, bob);132  });133134  it('Approve using the address that does not own the approved token', async () => {135    const nftCollectionId = await createCollectionExpectSuccess();136    // nft137    const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'NFT');138    await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice);139    // fungible140    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});141    const newFungibleTokenId = await createItemExpectSuccess(alice, fungibleCollectionId, 'Fungible');142    await approveExpectFail(fungibleCollectionId, newFungibleTokenId, bob, alice);143    // reFungible144    const reFungibleCollectionId =145      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});146    const newReFungibleTokenId = await createItemExpectSuccess(alice, reFungibleCollectionId, 'ReFungible');147    await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, bob, alice);148  });149150  it('should fail if approved more ReFungibles than owned', async () => {151    const nftCollectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});152    const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'ReFungible');153    await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 100, 'ReFungible');154    await approveExpectSuccess(nftCollectionId, newNftTokenId, bob, alice.address, 100);155    await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice, 101);156  });157158  it('should fail if approved more Fungibles than owned', async () => {159    const nftCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});160    const newNftTokenId = await createItemExpectSuccess(alice, nftCollectionId, 'Fungible');161    await transferExpectSuccess(nftCollectionId, newNftTokenId, alice, bob, 10, 'Fungible');162    await approveExpectSuccess(nftCollectionId, newNftTokenId, bob, alice.address, 10);163    await approveExpectFail(nftCollectionId, newNftTokenId, bob, alice, 11);164  });165166  it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {167    const collectionId = await createCollectionExpectSuccess();168    const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', bob.address);169    await setCollectionLimitsExpectSuccess(alice, collectionId, {ownerCanTransfer: false});170171    await approveExpectFail(collectionId, itemId, alice, charlie);172  });173});174175describe('Integration Test approve(spender, collection_id, item_id, amount) with collection admin permissions:', () => {176  let alice: IKeyringPair;177  let bob: IKeyringPair;178  let charlie: IKeyringPair;179180  before(async () => {181    await usingApi(async () => {182      alice = privateKey('//Alice');183      bob = privateKey('//Bob');184      charlie = privateKey('//Charlie');185    });186  });187188  it('can be called by collection admin on non-owned item', async () => {189    const collectionId = await createCollectionExpectSuccess();190    const itemId = await createItemExpectSuccess(alice, collectionId, 'NFT', alice.address);191192    await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);193    await adminApproveFromExpectSuccess(collectionId, itemId, bob, alice.address, charlie.address);194  });195});