git.delta.rocks / unique-network / refs/commits / bdf204a602d4

difftreelog

source

tests/src/approve.test.ts8.4 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 BN from 'bn.js';8import chai from 'chai';9import chaiAsPromised from 'chai-as-promised';10import privateKey from './substrate/privateKey';11import { default as usingApi } from './substrate/substrate-api';12import {13  approveExpectFail,14  approveExpectSuccess,15  createCollectionExpectSuccess,16  createFungibleItemExpectSuccess,17  createItemExpectSuccess,18  destroyCollectionExpectSuccess,19  transferExpectSuccess,20  transferFromExpectSuccess,21  U128_MAX,22} from './util/helpers';2324chai.use(chaiAsPromised);25const expect = chai.expect;2627describe('Integration Test approve(spender, collection_id, item_id, amount):', () => {28  it('Execute the extrinsic and check approvedList', async () => {29    await usingApi(async (api: ApiPromise) => {30      const Alice = privateKey('//Alice');31      const Bob = privateKey('//Bob');32      const nftCollectionId = await createCollectionExpectSuccess();33      // nft34      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');35      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob);36      // fungible37      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});38      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');39      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob);40      // reFungible41      const reFungibleCollectionId =42        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});43      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');44      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob);45    });46  });4748  it('Remove approval by using 0 amount', async () => {49    await usingApi(async (api: ApiPromise) => {50      const Alice = privateKey('//Alice');51      const Bob = privateKey('//Bob');52      const nftCollectionId = await createCollectionExpectSuccess();53      // nft54      const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');55      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1);56      await approveExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 0);57      // fungible58      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});59      const newFungibleTokenId = await createItemExpectSuccess(Alice, fungibleCollectionId, 'Fungible');60      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 1);61      await approveExpectSuccess(fungibleCollectionId, newFungibleTokenId, Alice, Bob, 0);62      // reFungible63      const reFungibleCollectionId =64        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});65      const newReFungibleTokenId = await createItemExpectSuccess(Alice, reFungibleCollectionId, 'ReFungible');66      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 1);67      await approveExpectSuccess(reFungibleCollectionId, newReFungibleTokenId, Alice, Bob, 0);68    });69  });70});7172describe('Negative Integration Test approve(spender, collection_id, item_id, amount):', () => {73  let Alice: IKeyringPair;74  let Bob: IKeyringPair;7576  before(async () => {77    await usingApi(async (api) => {78      Alice = privateKey('//Alice');79      Bob = privateKey('//Bob');80    });81  });8283  it('Approve for a collection that does not exist', async () => {84    await usingApi(async (api: ApiPromise) => {8586      // nft87      const nftCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;88      await approveExpectFail(nftCollectionCount + 1, 1, Alice, Bob);89      // fungible90      const fungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;91      await approveExpectFail(fungibleCollectionCount + 1, 1, Alice, Bob);92      // reFungible93      const reFungibleCollectionCount = await api.query.nft.createdCollectionCount() as unknown as number;94      await approveExpectFail(reFungibleCollectionCount + 1, 1, Alice, Bob);95    });96  });9798  it('Approve for a collection that was destroyed', async () => {99    await usingApi(async (api: ApiPromise) => {100      // nft101      const nftCollectionId = await createCollectionExpectSuccess();102      await destroyCollectionExpectSuccess(nftCollectionId);103      await approveExpectFail(nftCollectionId, 1, Alice, Bob);104      // fungible105      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});106      await destroyCollectionExpectSuccess(fungibleCollectionId);107      await approveExpectFail(fungibleCollectionId, 1, Alice, Bob);108      // reFungible109      const reFungibleCollectionId =110        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});111      await destroyCollectionExpectSuccess(reFungibleCollectionId);112      await approveExpectFail(reFungibleCollectionId, 1, Alice, Bob);113    });114  });115116  it('Approve transfer of a token that does not exist', async () => {117    await usingApi(async (api: ApiPromise) => {118      // nft119      const nftCollectionId = await createCollectionExpectSuccess();120      await approveExpectFail(nftCollectionId, 2, Alice, Bob);121      // fungible122      const fungibleCollectionId = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});123      await approveExpectFail(fungibleCollectionId, 2, Alice, Bob);124      // reFungible125      const reFungibleCollectionId =126        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});127      await approveExpectFail(reFungibleCollectionId, 2, Alice, Bob);128    });129  });130131  it('Approve using the address that does not own the approved token', async () => {132    await usingApi(async (api: ApiPromise) => {133      const Alice = privateKey('//Alice');134      const Bob = privateKey('//Bob');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    });149  });150151  it('should fail if approved more NFTs than owned', async () => {152    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'NFT' } });153    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'NFT');154    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 1, 'NFT');155    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice);156    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice);157  });158159  it('should fail if approved more ReFungibles than owned', async () => {160    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'ReFungible' } });161    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'ReFungible');162    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 100, 'ReFungible');163    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 100);164    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);165  });166167  it('should fail if approved more Fungibles than owned', async () => {168    const nftCollectionId = await createCollectionExpectSuccess({ mode: { type: 'Fungible', decimalPoints: 0 } });169    const newNftTokenId = await createItemExpectSuccess(Alice, nftCollectionId, 'Fungible');170    await transferExpectSuccess(nftCollectionId, newNftTokenId, Alice, Bob, 10, 'Fungible');171    await approveExpectSuccess(nftCollectionId, newNftTokenId, Bob, Alice, 10);172    await approveExpectFail(nftCollectionId, newNftTokenId, Bob, Alice, 1);173  });174});