git.delta.rocks / unique-network / refs/commits / 2b06b09aefa4

difftreelog

source

tests/src/burnItem.test.ts8.8 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  destroyCollectionExpectSuccess14} from './util/helpers';15import { nullPublicKey } from "./accounts";1617import chai from 'chai';18import chaiAsPromised from 'chai-as-promised';19chai.use(chaiAsPromised);20const expect = chai.expect;2122let alice: IKeyringPair;23let bob: IKeyringPair;2425describe('integration test: ext. burnItem():', () => {26  before(async () => {27    await usingApi(async (api) => {28      const keyring = new Keyring({ type: 'sr25519' });29      alice = keyring.addFromUri(`//Alice`);30      bob = keyring.addFromUri(`//Bob`);31    });32  });3334  it('Burn item in NFT collection', async () => {35    const createMode = 'NFT';36    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});37    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);3839    await usingApi(async (api) => {40      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);41      const events = await submitTransactionAsync(alice, tx);42      const result = getGenericResult(events);43      // Get the item44      const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();45      // What to expect46      // tslint:disable-next-line:no-unused-expression47      expect(result.success).to.be.true;48      // tslint:disable-next-line:no-unused-expression49      expect(item).to.be.not.null;50      expect(item.Owner).to.be.equal(nullPublicKey);51    });5253  });54  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);65  66      // Get alice balance 67      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();68 69      // 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    });7475  });76  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, 1);83      const events = await submitTransactionAsync(alice, tx);84      const result = getGenericResult(events);85  86      // Get alice balance 87      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();88      89      // What to expect90      expect(result.success).to.be.true;91      expect(balance).to.be.not.null;92      expect(balance.Owner.length).to.be.equal(0);93    });9495  });9697  it('Burn owned portion of item in ReFungible collection', async () => {98    const createMode = 'ReFungible';99    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});100    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);101102    await usingApi(async (api) => {103      // Transfer 1/100 of the token to Bob104      const transfertx = api.tx.nft.transfer(bob.address, collectionId, tokenId, 1);105      const events1 = await submitTransactionAsync(alice, transfertx);106      const result1 = getGenericResult(events1);107108      // Get balances109      const balanceBefore: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();110111      // Bob burns his portion112      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);113      const events2 = await submitTransactionAsync(bob, tx);114      const result2 = getGenericResult(events2);115116      // Get balances 117      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();118      // console.log(balance);119      120      // What to expect before burning121      expect(result1.success).to.be.true;122      expect(balanceBefore).to.be.not.null;123      expect(balanceBefore.Owner.length).to.be.equal(2);124      expect(balanceBefore.Owner[0].Owner).to.be.equal(alice.address);125      expect(balanceBefore.Owner[0].Fraction).to.be.equal(99);126      expect(balanceBefore.Owner[1].Owner).to.be.equal(bob.address);127      expect(balanceBefore.Owner[1].Fraction).to.be.equal(1);128129      // What to expect after burning130      expect(result2.success).to.be.true;131      expect(balance).to.be.not.null;132      expect(balance.Owner.length).to.be.equal(1);133      expect(balance.Owner[0].Fraction).to.be.equal(99);134      expect(balance.Owner[0].Owner).to.be.equal(alice.address);135    });136137  });138139});140141describe('Negative integration test: ext. burnItem():', () => {142  before(async () => {143    await usingApi(async (api) => {144      const keyring = new Keyring({ type: 'sr25519' });145      alice = keyring.addFromUri(`//Alice`);146      bob = keyring.addFromUri(`//Bob`);147    });148  });149150  it('Burn a token in a destroyed collection', async () => {151    const createMode = 'NFT';152    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});153    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);154    await destroyCollectionExpectSuccess(collectionId);155156    await usingApi(async (api) => {157      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);158      const badTransaction = async function () { 159        await submitTransactionExpectFailAsync(alice, tx);160      };161      await expect(badTransaction()).to.be.rejected;162    });163164  });165166  it('Burn a token that was never created', async () => {167    const createMode = 'NFT';168    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});169    const tokenId = 10;170171    await usingApi(async (api) => {172      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);173      const badTransaction = async function () { 174        await submitTransactionExpectFailAsync(alice, tx);175      };176      await expect(badTransaction()).to.be.rejected;177    });178179  });180181  it('Burn a token using the address that does not own it', async () => {182    const createMode = 'NFT';183    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});184    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);185186    await usingApi(async (api) => {187      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);188      const badTransaction = async function () { 189        await submitTransactionExpectFailAsync(bob, tx);190      };191      await expect(badTransaction()).to.be.rejected;192    });193194  });195196  it('Transfer a burned a token', async () => {197    const createMode = 'NFT';198    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});199    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);200201    await usingApi(async (api) => {202203      const burntx = api.tx.nft.burnItem(collectionId, tokenId, 0);204      const events1 = await submitTransactionAsync(alice, burntx);205      const result1 = getGenericResult(events1);206      expect(result1.success).to.be.true;207  208      const tx = api.tx.nft.transfer(bob.address, collectionId, tokenId, 0);209      const badTransaction = async function () { 210        await submitTransactionExpectFailAsync(alice, tx);211      };212      await expect(badTransaction()).to.be.rejected;213    });214215  });216217  it('Burn more than owned in Fungible collection', async () => {218    const createMode = 'Fungible';219    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});220    // Helper creates 10 fungible tokens221    await createItemExpectSuccess(alice, collectionId, createMode);222    const tokenId = 0; // ignored223224    await usingApi(async (api) => {225      // Destroy 11 of 10226      const tx = api.tx.nft.burnItem(collectionId, tokenId, 11);227      const badTransaction = async function () { 228        await submitTransactionExpectFailAsync(alice, tx);229      };230      await expect(badTransaction()).to.be.rejected;231      232      // Get alice balance 233      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();234 235      // What to expect236      expect(balance).to.be.not.null;237      expect(balance.Value).to.be.equal(10);238    });239240  });241242});