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

difftreelog

source

tests/src/burnItem.test.ts8.7 KiBsourcehistory
1import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';2import { Keyring } from "@polkadot/api";3import { IKeyringPair } from "@polkadot/types/types";4import { 5  createCollectionExpectSuccess, 6  createItemExpectSuccess,7  getGenericResult,8  destroyCollectionExpectSuccess9} from './util/helpers';10import { nullPublicKey } from "./accounts";1112import chai from 'chai';13import chaiAsPromised from 'chai-as-promised';14chai.use(chaiAsPromised);15const expect = chai.expect;1617let alice: IKeyringPair;18let bob: IKeyringPair;1920describe('integration test: ext. burnItem():', () => {21  before(async () => {22    await usingApi(async (api) => {23      const keyring = new Keyring({ type: 'sr25519' });24      alice = keyring.addFromUri(`//Alice`);25      bob = keyring.addFromUri(`//Bob`);26    });27  });2829  it('Burn item in NFT collection', async () => {30    const createMode = 'NFT';31    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});32    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);3334    await usingApi(async (api) => {35      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);36      const events = await submitTransactionAsync(alice, tx);37      const result = getGenericResult(events);38      // Get the item39      const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();40      // What to expect41      // tslint:disable-next-line:no-unused-expression42      expect(result.success).to.be.true;43      // tslint:disable-next-line:no-unused-expression44      expect(item).to.be.not.null;45      expect(item.Owner).to.be.equal(nullPublicKey);46    });4748  });49  it('Burn item in Fungible collection', async () => {50    const createMode = 'Fungible';51    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});52    await createItemExpectSuccess(alice, collectionId, createMode); // Helper creates 10 fungible tokens53    const tokenId = 0; // ignored5455    await usingApi(async (api) => {56      // Destroy 1 of 1057      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);58      const events = await submitTransactionAsync(alice, tx);59      const result = getGenericResult(events);60  61      // Get alice balance 62      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();63 64      // What to expect65      expect(result.success).to.be.true;66      expect(balance).to.be.not.null;67      expect(balance.Value).to.be.equal(9);68    });6970  });71  it('Burn item in ReFungible collection', async () => {72    const createMode = 'ReFungible';73    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 2 }});74    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);7576    await usingApi(async (api) => {77      const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);78      const events = await submitTransactionAsync(alice, tx);79      const result = getGenericResult(events);80  81      // Get alice balance 82      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();83      84      // What to expect85      expect(result.success).to.be.true;86      expect(balance).to.be.not.null;87      expect(balance.Owner.length).to.be.equal(0);88    });8990  });9192  it('Burn owned portion of item in ReFungible collection', async () => {93    const createMode = 'ReFungible';94    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 2 }});95    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);9697    await usingApi(async (api) => {98      // Transfer 1/100 of the token to Bob99      const transfertx = api.tx.nft.transfer(bob.address, collectionId, tokenId, 1);100      const events1 = await submitTransactionAsync(alice, transfertx);101      const result1 = getGenericResult(events1);102103      // Get balances104      const balanceBefore: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();105106      // Bob burns his portion107      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);108      const events2 = await submitTransactionAsync(bob, tx);109      const result2 = getGenericResult(events2);110  111      // Get balances 112      const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();113      // console.log(balance);114      115      // What to expect before burning116      expect(result1.success).to.be.true;117      expect(balanceBefore).to.be.not.null;118      expect(balanceBefore.Owner.length).to.be.equal(2);119      expect(balanceBefore.Owner[0].Owner).to.be.equal(alice.address);120      expect(balanceBefore.Owner[0].Fraction).to.be.equal(99);121      expect(balanceBefore.Owner[1].Owner).to.be.equal(bob.address);122      expect(balanceBefore.Owner[1].Fraction).to.be.equal(1);123124      // What to expect after burning125      expect(result2.success).to.be.true;126      expect(balance).to.be.not.null;127      expect(balance.Owner.length).to.be.equal(1);128      expect(balance.Owner[0].Fraction).to.be.equal(99);129      expect(balance.Owner[0].Owner).to.be.equal(alice.address);130    });131132  });133134});135136describe('Negative integration test: ext. burnItem():', () => {137  before(async () => {138    await usingApi(async (api) => {139      const keyring = new Keyring({ type: 'sr25519' });140      alice = keyring.addFromUri(`//Alice`);141      bob = keyring.addFromUri(`//Bob`);142    });143  });144145  it('Burn a token in a destroyed collection', async () => {146    const createMode = 'NFT';147    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});148    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);149    await destroyCollectionExpectSuccess(collectionId);150151    await usingApi(async (api) => {152      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);153      const badTransaction = async function () { 154        await submitTransactionExpectFailAsync(alice, tx);155      };156      await expect(badTransaction()).to.be.rejected;157    });158159  });160161  it('Burn a token that was never created', async () => {162    const createMode = 'NFT';163    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});164    const tokenId = 10;165166    await usingApi(async (api) => {167      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);168      const badTransaction = async function () { 169        await submitTransactionExpectFailAsync(alice, tx);170      };171      await expect(badTransaction()).to.be.rejected;172    });173174  });175176  it('Burn a token using the address that does not own it', async () => {177    const createMode = 'NFT';178    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});179    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);180181    await usingApi(async (api) => {182      const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);183      const badTransaction = async function () { 184        await submitTransactionExpectFailAsync(bob, tx);185      };186      await expect(badTransaction()).to.be.rejected;187    });188189  });190191  it('Transfer a burned a token', async () => {192    const createMode = 'NFT';193    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});194    const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);195196    await usingApi(async (api) => {197198      const burntx = api.tx.nft.burnItem(collectionId, tokenId, 0);199      const events1 = await submitTransactionAsync(alice, burntx);200      const result1 = getGenericResult(events1);201      expect(result1.success).to.be.true;202  203      const tx = api.tx.nft.transfer(bob.address, collectionId, tokenId, 0);204      const badTransaction = async function () { 205        await submitTransactionExpectFailAsync(alice, tx);206      };207      await expect(badTransaction()).to.be.rejected;208    });209210  });211212  it('Burn more than owned in Fungible collection', async () => {213    const createMode = 'Fungible';214    const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});215    // Helper creates 10 fungible tokens216    await createItemExpectSuccess(alice, collectionId, createMode);217    const tokenId = 0; // ignored218219    await usingApi(async (api) => {220      // Destroy 11 of 10221      const tx = api.tx.nft.burnItem(collectionId, tokenId, 11);222      const badTransaction = async function () { 223        await submitTransactionExpectFailAsync(alice, tx);224      };225      await expect(badTransaction()).to.be.rejected;226      227      // Get alice balance 228      const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();229 230      // What to expect231      expect(balance).to.be.not.null;232      expect(balance.Value).to.be.equal(10);233    });234235  });236237});