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

difftreelog

source

tests/src/burnItem.test.ts11.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//56import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';7import { Keyring } from '@polkadot/api';8import { IKeyringPair } from '@polkadot/types/types';9import {10  createCollectionExpectSuccess,11  createItemExpectSuccess,12  getGenericResult,13  destroyCollectionExpectSuccess,14  normalizeAccountId,15  addCollectionAdminExpectSuccess,16} from './util/helpers';1718import chai from 'chai';19import chaiAsPromised from 'chai-as-promised';20chai.use(chaiAsPromised);21const expect = chai.expect;2223let alice: IKeyringPair;24let bob: IKeyringPair;2526describe('integration test: ext. burnItem():', () => {27  before(async () => {28    await usingApi(async () => {29      const keyring = new Keyring({ type: 'sr25519' });30      alice = keyring.addFromUri('//Alice');31      bob = keyring.addFromUri('//Bob');32    });33  });3435  it('Burn item in NFT collection', async () => {36    const createMode = 'NFT';37    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});38    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);3940    await usingApi(async (api) => {41      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);42      const events = await submitTransactionAsync(alice, tx);43      const result = getGenericResult(events);44      // Get the item45      const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();46      // What to expect47      // tslint:disable-next-line:no-unused-expression48      expect(result.success).to.be.true;49      // tslint:disable-next-line:no-unused-expression50      expect(item).to.be.null;51    });52  });5354  it('Burn item in Fungible collection', async () => {55    const createMode = 'Fungible';56    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});57    await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens58    const tokenId = 0; // ignored5960    await usingApi(async (api) => {61      // Destroy 1 of 1062      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);63      const events = await submitTransactionAsync(alice, tx);64      const result = getGenericResult(events);6566      // Get alice balance67      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();6869      // What to expect70      expect(result.success).to.be.true;71      expect(balance).to.be.not.null;72      expect(balance.value).to.be.equal(9);73    });74  });7576  it('Burn item in ReFungible collection', async () => {77    const createMode = 'ReFungible';78    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});79    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);8081    await usingApi(async (api) => {82      const tx = api.tx.nft.burnItem(collectionId, tokenId, 100);83      const events = await submitTransactionAsync(alice, tx);84      const result = getGenericResult(events);8586      // Get alice balance87      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();8889      // What to expect90      expect(result.success).to.be.true;91      expect(balance).to.be.null;92    });93  });9495  it('Burn owned portion of item in ReFungible collection', async () => {96    const createMode = 'ReFungible';97    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});98    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);99100    await usingApi(async (api) => {101      // Transfer 1/100 of the token to Bob102      const transfertx = api.tx.nft.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);103      const events1 = await submitTransactionAsync(alice, transfertx);104      const result1 = getGenericResult(events1);105106      // Get balances107      const balanceBefore: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();108109      // Bob burns his portion110      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);111      const events2 = await submitTransactionAsync(bob, tx);112      const result2 = getGenericResult(events2);113114      // Get balances115      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();116      // console.log(balance);117118      // What to expect before burning119      expect(result1.success).to.be.true;120      expect(balanceBefore).to.be.not.null;121      expect(balanceBefore.owner.length).to.be.equal(2);122      expect(balanceBefore.owner[0].owner).to.be.deep.equal(normalizeAccountId(alice.address));123      expect(balanceBefore.owner[0].fraction).to.be.equal(99);124      expect(balanceBefore.owner[1].owner).to.be.deep.equal(normalizeAccountId(bob.address));125      expect(balanceBefore.owner[1].fraction).to.be.equal(1);126127      // What to expect after burning128      expect(result2.success).to.be.true;129      expect(balance).to.be.not.null;130      expect(balance.owner.length).to.be.equal(1);131      expect(balance.owner[0].fraction).to.be.equal(99);132      expect(balance.owner[0].owner).to.be.deep.equal(normalizeAccountId(alice.address));133    });134135  });136137});138139describe('integration test: ext. burnItem() with admin permissions:', () => {140  before(async () => {141    await usingApi(async () => {142      const keyring = new Keyring({ type: 'sr25519' });143      alice = keyring.addFromUri('//Alice');144      bob = keyring.addFromUri('//Bob');145    });146  });147148  it('Burn item in NFT collection', async () => {149    const createMode = 'NFT';150    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});151    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);152    await addCollectionAdminExpectSuccess(alice, collectionId, bob);153154    await usingApi(async (api) => {155      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);156      const events = await submitTransactionAsync(bob, tx);157      const result = getGenericResult(events);158      // Get the item159      const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();160      // What to expect161      // tslint:disable-next-line:no-unused-expression162      expect(result.success).to.be.true;163      // tslint:disable-next-line:no-unused-expression164      expect(item).to.be.null;165    });166  });167168169  it('Burn item in Fungible collection', async () => {170    const createMode = 'Fungible';171    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});172    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens173    await addCollectionAdminExpectSuccess(alice, collectionId, bob);174175    await usingApi(async (api) => {176      // Destroy 1 of 10177      const tx = api.tx.nft.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 1);178      const events = await submitTransactionAsync(bob, tx);179      const result = getGenericResult(events);180181      // Get alice balance182      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();183184      // What to expect185      expect(result.success).to.be.true;186      expect(balance).to.be.not.null;187      expect(balance.value).to.be.equal(9);188    });189  });190191  it('Burn item in ReFungible collection', async () => {192    const createMode = 'ReFungible';193    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});194    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);195    await addCollectionAdminExpectSuccess(alice, collectionId, bob);196197    await usingApi(async (api) => {198      const tx = api.tx.nft.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 100);199      const events = await submitTransactionAsync(bob, tx);200      const result = getGenericResult(events);201      // Get alice balance202      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();203204      // What to expect205      expect(result.success).to.be.true;206      expect(balance).to.be.null;207    });208  });209});210211describe('Negative integration test: ext. burnItem():', () => {212  before(async () => {213    await usingApi(async () => {214      const keyring = new Keyring({ type: 'sr25519' });215      alice = keyring.addFromUri('//Alice');216      bob = keyring.addFromUri('//Bob');217    });218  });219220  it('Burn a token in a destroyed collection', async () => {221    const createMode = 'NFT';222    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});223    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);224    await destroyCollectionExpectSuccess(collectionId);225226    await usingApi(async (api) => {227      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);228      const badTransaction = async function () {229        await submitTransactionExpectFailAsync(alice, tx);230      };231      await expect(badTransaction()).to.be.rejected;232    });233234  });235236  it('Burn a token that was never created', async () => {237    const createMode = 'NFT';238    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});239    const tokenId = 10;240241    await usingApi(async (api) => {242      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);243      const badTransaction = async function () {244        await submitTransactionExpectFailAsync(alice, tx);245      };246      await expect(badTransaction()).to.be.rejected;247    });248249  });250251  it('Burn a token using the address that does not own it', async () => {252    const createMode = 'NFT';253    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});254    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);255256    await usingApi(async (api) => {257      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);258      const badTransaction = async function () {259        await submitTransactionExpectFailAsync(bob, tx);260      };261      await expect(badTransaction()).to.be.rejected;262    });263264  });265266  it('Transfer a burned a token', async () => {267    const createMode = 'NFT';268    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});269    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);270271    await usingApi(async (api) => {272273      const burntx = api.tx.nft.burnItem(collectionId, tokenId, 1);274      const events1 = await submitTransactionAsync(alice, burntx);275      const result1 = getGenericResult(events1);276      expect(result1.success).to.be.true;277278      const tx = api.tx.nft.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 0);279      const badTransaction = async function () {280        await submitTransactionExpectFailAsync(alice, tx);281      };282      await expect(badTransaction()).to.be.rejected;283    });284285  });286287  it('Burn more than owned in Fungible collection', async () => {288    const createMode = 'Fungible';289    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});290    // Helper creates 10 fungible tokens291    await createItemExpectSuccess(alice, collectionId, createMode);292    const tokenId = 0; // ignored293294    await usingApi(async (api) => {295      // Destroy 11 of 10296      const tx = api.tx.nft.burnItem(collectionId, tokenId, 11);297      const badTransaction = async function () {298        await submitTransactionExpectFailAsync(alice, tx);299      };300      await expect(badTransaction()).to.be.rejected;301302      // Get alice balance303      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();304305      // What to expect306      expect(balance).to.be.not.null;307      expect(balance.value).to.be.equal(10);308    });309310  });311312});