123456import { 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, 0);42 const events = await submitTransactionAsync(alice, tx);43 const result = getGenericResult(events);44 45 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();46 47 48 expect(result.success).to.be.true;49 50 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); 58 const tokenId = 0; 5960 await usingApi(async (api) => {61 62 const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);63 const events = await submitTransactionAsync(alice, tx);64 const result = getGenericResult(events);6566 67 const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();6869 70 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, 1);83 const events = await submitTransactionAsync(alice, tx);84 const result = getGenericResult(events);8586 87 const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();8889 90 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 102 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 107 const balanceBefore: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();108109 110 const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);111 const events2 = await submitTransactionAsync(bob, tx);112 const result2 = getGenericResult(events2);113114 115 const balance: any = (await api.query.nft.reFungibleItemList(collectionId, tokenId)).toJSON();116 117118 119 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 128 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, 0);156 const events = await submitTransactionAsync(bob, tx);157 const result = getGenericResult(events);158 159 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();160 161 162 expect(result.success).to.be.true;163 164 expect(item).to.be.null;165 });166 });167});168169describe('Negative integration test: ext. burnItem():', () => {170 before(async () => {171 await usingApi(async () => {172 const keyring = new Keyring({ type: 'sr25519' });173 alice = keyring.addFromUri('//Alice');174 bob = keyring.addFromUri('//Bob');175 });176 });177178 it('Burn a token in a destroyed collection', async () => {179 const createMode = 'NFT';180 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});181 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);182 await destroyCollectionExpectSuccess(collectionId);183184 await usingApi(async (api) => {185 const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);186 const badTransaction = async function () {187 await submitTransactionExpectFailAsync(alice, tx);188 };189 await expect(badTransaction()).to.be.rejected;190 });191192 });193194 it('Burn a token that was never created', async () => {195 const createMode = 'NFT';196 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});197 const tokenId = 10;198199 await usingApi(async (api) => {200 const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);201 const badTransaction = async function () {202 await submitTransactionExpectFailAsync(alice, tx);203 };204 await expect(badTransaction()).to.be.rejected;205 });206207 });208209 it('Burn a token using the address that does not own it', async () => {210 const createMode = 'NFT';211 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});212 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);213214 await usingApi(async (api) => {215 const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);216 const badTransaction = async function () {217 await submitTransactionExpectFailAsync(bob, tx);218 };219 await expect(badTransaction()).to.be.rejected;220 });221222 });223224 it('Transfer a burned a token', async () => {225 const createMode = 'NFT';226 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode }});227 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);228229 await usingApi(async (api) => {230231 const burntx = api.tx.nft.burnItem(collectionId, tokenId, 0);232 const events1 = await submitTransactionAsync(alice, burntx);233 const result1 = getGenericResult(events1);234 expect(result1.success).to.be.true;235236 const tx = api.tx.nft.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 0);237 const badTransaction = async function () {238 await submitTransactionExpectFailAsync(alice, tx);239 };240 await expect(badTransaction()).to.be.rejected;241 });242243 });244245 it('Burn more than owned in Fungible collection', async () => {246 const createMode = 'Fungible';247 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0 }});248 249 await createItemExpectSuccess(alice, collectionId, createMode);250 const tokenId = 0; 251252 await usingApi(async (api) => {253 254 const tx = api.tx.nft.burnItem(collectionId, tokenId, 11);255 const badTransaction = async function () {256 await submitTransactionExpectFailAsync(alice, tx);257 };258 await expect(badTransaction()).to.be.rejected;259260 261 const balance: any = (await api.query.nft.fungibleItemList(collectionId, alice.address)).toJSON();262263 264 expect(balance).to.be.not.null;265 expect(balance.value).to.be.equal(10);266 });267268 });269270});