git.delta.rocks / unique-network / refs/commits / 58d5f9ecaf1d

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  destroyCollectionExpectSuccess,14  normalizeAccountId,15} from './util/helpers';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 () => {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.null;50    });5152  });53  it('Burn item in Fungible collection', async () => {54    const createMode = 'Fungible';55    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});56    await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens57    const tokenId = 0; // ignored5859    await usingApi(async (api) => {60      // Destroy 1 of 1061      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);62      const events = await submitTransactionAsync(alice, tx);63      const result = getGenericResult(events);64  65      // Get alice balance 66      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();67 68      // What to expect69      expect(result.success).to.be.true;70      expect(balance).to.be.not.null;71      expect(balance.Value).to.be.equal(9);72    });7374  });75  it('Burn item in ReFungible collection', async () => {76    const createMode = 'ReFungible';77    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});78    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);7980    await usingApi(async (api) => {81      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);82      const events = await submitTransactionAsync(alice, tx);83      const result = getGenericResult(events);84  85      // Get alice balance 86      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();87      88      // What to expect89      expect(result.success).to.be.true;90      expect(balance).to.be.null;91    });9293  });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, 0);111      const events2 = await submitTransactionAsync(bob, tx);112      const result2 = getGenericResult(events2);113114      // Get balances 115      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();116      // console.log(balance);117      118      // 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('Negative integration test: ext. burnItem():', () => {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 a token in a destroyed collection', async () => {149    const createMode = 'NFT';150    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});151    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);152    await destroyCollectionExpectSuccess(collectionId);153154    await usingApi(async (api) => {155      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);156      const badTransaction = async function () { 157        await submitTransactionExpectFailAsync(alice, tx);158      };159      await expect(badTransaction()).to.be.rejected;160    });161162  });163164  it('Burn a token that was never created', async () => {165    const createMode = 'NFT';166    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});167    const tokenId = 10;168169    await usingApi(async (api) => {170      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);171      const badTransaction = async function () { 172        await submitTransactionExpectFailAsync(alice, tx);173      };174      await expect(badTransaction()).to.be.rejected;175    });176177  });178179  it('Burn a token using the address that does not own it', async () => {180    const createMode = 'NFT';181    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});182    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);183184    await usingApi(async (api) => {185      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);186      const badTransaction = async function () { 187        await submitTransactionExpectFailAsync(bob, tx);188      };189      await expect(badTransaction()).to.be.rejected;190    });191192  });193194  it('Transfer a burned a token', async () => {195    const createMode = 'NFT';196    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});197    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);198199    await usingApi(async (api) => {200201      const burntx = api.tx.nft.burnItem(collectionId, tokenId, 0);202      const events1 = await submitTransactionAsync(alice, burntx);203      const result1 = getGenericResult(events1);204      expect(result1.success).to.be.true;205  206      const tx = api.tx.nft.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 0);207      const badTransaction = async function () { 208        await submitTransactionExpectFailAsync(alice, tx);209      };210      await expect(badTransaction()).to.be.rejected;211    });212213  });214215  it('Burn more than owned in Fungible collection', async () => {216    const createMode = 'Fungible';217    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});218    // Helper creates 10 fungible tokens219    await createItemExpectSuccess(alice, collectionId, createMode);220    const tokenId = 0; // ignored221222    await usingApi(async (api) => {223      // Destroy 11 of 10224      const tx = api.tx.nft.burnItem(collectionId, tokenId, 11);225      const badTransaction = async function () { 226        await submitTransactionExpectFailAsync(alice, tx);227      };228      await expect(badTransaction()).to.be.rejected;229      230      // Get alice balance 231      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();232 233      // What to expect234      expect(balance).to.be.not.null;235      expect(balance.Value).to.be.equal(10);236    });237238  });239240});