git.delta.rocks / unique-network / refs/commits / 3974c7b137e6

difftreelog

source

tests/src/approve.test.ts8.6 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} from './util/helpers';2021chai.use(chaiAsPromised);2223describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {24  let Alice: IKeyringPair;25  let Bob: IKeyringPair;26  let Charlie: IKeyringPair;2728  before(async () => {29    await usingApi(async () => {30      Alice = privateKey('//Alice');31      Bob = privateKey('//Bob');32      Charlie = privateKey('//Charlie');33    });34  });3536  it('Execute the extrinsic and check approvedList', async () => {37    const nftCollectionId = await createCollectionExpectSuccess();38    // nft39    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');40    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);41    // fungible42    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});43    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');44    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);45    // reFungible46    const reFungibleCollectionId =47      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});48    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');49    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);50  });5152  it('Remove approval by using 0 amount', async () => {53    const nftCollectionId = await createCollectionExpectSuccess();54    // nft55    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');56    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);57    await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);58    // fungible59    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});60    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');61    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);62    await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);63    // reFungible64    const reFungibleCollectionId =65      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});66    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');67    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);68    await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);69  });7071  it('can be called by collection owner on non-owned item when OwnerCanTransfer == true', async () => {72    const collectionId = await createCollectionExpectSuccess();73    const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Bob.address);7475    await approveExpectSuccess(collectionId, itemId, Alice, Charlie);76  });77});7879describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {80  let Alice: IKeyringPair;81  let Bob: IKeyringPair;82  let Charlie: IKeyringPair;8384  before(async () => {85    await usingApi(async (api) => {86      Alice = privateKey('//Alice');87      Bob = privateKey('//Bob');88      Charlie = privateKey('//Charlie');89    });90  });9192  it('Approve for a collection that does not exist', async () => {93    await usingApi(async (api: ApiPromise) => {94      // nft95      const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;96      await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);97      // fungible98      const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;99      await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);100      // reFungible101      const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;102      await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);103    });104  });105106  it('Approve for a collection that was destroyed', async () => {107    // nft108    const nftCollectionId = await createCollectionExpectSuccess();109    await destroyCollectionExpectSuccess(nftCollectionId);110    await approveExpectFail(nftCollectionId, 1, Alice, Bob);111    // fungible112    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});113    await destroyCollectionExpectSuccess(fungibleCollectionId);114    await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);115    // reFungible116    const reFungibleCollectionId =117      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});118    await destroyCollectionExpectSuccess(reFungibleCollectionId);119    await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);120  });121122  it('Approve transfer of a token that does not exist', async () => {123    // nft124    const nftCollectionId = await createCollectionExpectSuccess();125    await approveExpectFail(nftCollectionId, 2, Alice, Bob);126    // reFungible127    const reFungibleCollectionId =128      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});129    await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);130  });131132  it('Approve using the address that does not own the approved token', async () => {133    const nftCollectionId = await createCollectionExpectSuccess();134    // nft135    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');136    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);137    // fungible138    const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});139    const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');140    await approveExpectFail(fungibleCollectionId, newFungibleTokenId, Bob, Alice);141    // reFungible142    const reFungibleCollectionId =143      await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});144    const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');145    await approveExpectFail(reFungibleCollectionId, newReFungibleTokenId, Bob, Alice);146  });147148  it('should fail if approved more NFTs than owned', async () => {149    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });150    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');151    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');152    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice);153    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);154  });155156  it('should fail if approved more ReFungibles than owned', async () => {157    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'ReFungible' } });158    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'ReFungible');159    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 100, 'ReFungible');160    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 100);161    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);162  });163164  it('should fail if approved more Fungibles than owned', async () => {165    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });166    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'Fungible');167    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 10, 'Fungible');168    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 10);169    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);170  });171172  it('fails when called by collection owner on non-owned item when OwnerCanTransfer == false', async () => {173    const collectionId = await createCollectionExpectSuccess();174    const itemId = await createItemExpectSuccess(Alice, collectionId, 'NFT', Bob.address);175    await setCollectionLimitsExpectSuccess(Alice, collectionId, { OwnerCanTransfer: false });176177    await approveExpectFail(collectionId, itemId, Alice, Charlie);178  });179});