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 getBalance,17 isTokenExists,18} from './util/helpers';1920import chai from 'chai';21import chaiAsPromised from 'chai-as-promised';22chai.use(chaiAsPromised);23const expect = chai.expect;2425let alice: IKeyringPair;26let bob: IKeyringPair;2728describe('integration test: ext. burnItem():', () => {29 before(async () => {30 await usingApi(async () => {31 const keyring = new Keyring({type: 'sr25519'});32 alice = keyring.addFromUri('//Alice');33 bob = keyring.addFromUri('//Bob');34 });35 });3637 it('Burn item in NFT collection', async () => {38 const createMode = 'NFT';39 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});40 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);4142 await usingApi(async (api) => {43 const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);44 const events = await submitTransactionAsync(alice, tx);45 const result = getGenericResult(events);4647 expect(result.success).to.be.true;48 49 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;50 });51 });5253 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); 57 const tokenId = 0; 5859 await usingApi(async (api) => {60 61 const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);62 const events = await submitTransactionAsync(alice, tx);63 const result = getGenericResult(events);6465 66 const balance = await getBalance(api, collectionId, alice.address, 0);6768 69 expect(result.success).to.be.true;70 expect(balance).to.be.equal(9n);71 });72 });7374 it('Burn item in ReFungible collection', async () => {75 const createMode = 'ReFungible';76 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});77 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);7879 await usingApi(async (api) => {80 const tx = api.tx.nft.burnItem(collectionId, tokenId, 100);81 const events = await submitTransactionAsync(alice, tx);82 const result = getGenericResult(events);8384 85 const balance = await getBalance(api, collectionId, alice.address, tokenId);8687 88 expect(result.success).to.be.true;89 expect(balance).to.be.equal(0n);90 });91 });9293 it('Burn owned portion of item in ReFungible collection', async () => {94 const createMode = 'ReFungible';95 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});96 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);9798 await usingApi(async (api) => {99 100 const transfertx = api.tx.nft.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);101 const events1 = await submitTransactionAsync(alice, transfertx);102 const result1 = getGenericResult(events1);103104 105 const bobBalanceBefore = await getBalance(api, collectionId, bob.address, tokenId);106 const aliceBalanceBefore = await getBalance(api, collectionId, alice.address, tokenId);107108 109 const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);110 const events2 = await submitTransactionAsync(bob, tx);111 const result2 = getGenericResult(events2);112113 114 const bobBalanceAfter = await getBalance(api, collectionId, bob.address, tokenId);115 const aliceBalanceAfter = await getBalance(api, collectionId, alice.address, tokenId);116 117118 119 expect(result1.success).to.be.true;120 expect(aliceBalanceBefore).to.be.equal(99n);121 expect(bobBalanceBefore).to.be.equal(1n);122123 124 expect(result2.success).to.be.true;125 expect(aliceBalanceAfter).to.be.equal(99n);126 expect(bobBalanceAfter).to.be.equal(0n);127 });128129 });130131});132133describe('integration test: ext. burnItem() with admin permissions:', () => {134 before(async () => {135 await usingApi(async () => {136 const keyring = new Keyring({type: 'sr25519'});137 alice = keyring.addFromUri('//Alice');138 bob = keyring.addFromUri('//Bob');139 });140 });141142 it('Burn item in NFT collection', async () => {143 const createMode = 'NFT';144 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});145 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);146 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);147148 await usingApi(async (api) => {149 const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);150 const events = await submitTransactionAsync(bob, tx);151 const result = getGenericResult(events);152153 expect(result.success).to.be.true;154 155 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;156 });157 });158159 160 it('Burn item in Fungible collection', async () => {161 const createMode = 'Fungible';162 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});163 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode); 164 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);165166 await usingApi(async (api) => {167 168 const tx = api.tx.nft.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 1);169 const events = await submitTransactionAsync(bob, tx);170 const result = getGenericResult(events);171172 173 const balance = await getBalance(api, collectionId, alice.address, 0);174175 176 expect(result.success).to.be.true;177 expect(balance).to.be.equal(9n);178 });179 });180181 182 it('Burn item in ReFungible collection', async () => {183 const createMode = 'ReFungible';184 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});185 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);186 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);187188 await usingApi(async (api) => {189 const tx = api.tx.nft.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 100);190 const events = await submitTransactionAsync(bob, tx);191 const result = getGenericResult(events);192 193 expect(result.success).to.be.true;194 195 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;196 });197 });198});199200describe('Negative integration test: ext. burnItem():', () => {201 before(async () => {202 await usingApi(async () => {203 const keyring = new Keyring({type: 'sr25519'});204 alice = keyring.addFromUri('//Alice');205 bob = keyring.addFromUri('//Bob');206 });207 });208209 it('Burn a token in a destroyed collection', async () => {210 const createMode = 'NFT';211 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});212 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);213 await destroyCollectionExpectSuccess(collectionId);214215 await usingApi(async (api) => {216 const tx = api.tx.nft.burnItem(collectionId, tokenId, 0);217 const badTransaction = async function () {218 await submitTransactionExpectFailAsync(alice, tx);219 };220 await expect(badTransaction()).to.be.rejected;221 });222223 });224225 it('Burn a token that was never created', async () => {226 const createMode = 'NFT';227 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});228 const tokenId = 10;229230 await usingApi(async (api) => {231 const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);232 const badTransaction = async function () {233 await submitTransactionExpectFailAsync(alice, tx);234 };235 await expect(badTransaction()).to.be.rejected;236 });237238 });239240 it('Burn a token using the address that does not own it', async () => {241 const createMode = 'NFT';242 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});243 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);244245 await usingApi(async (api) => {246 const tx = api.tx.nft.burnItem(collectionId, tokenId, 1);247 const badTransaction = async function () {248 await submitTransactionExpectFailAsync(bob, tx);249 };250 await expect(badTransaction()).to.be.rejected;251 });252253 });254255 it('Transfer a burned a token', async () => {256 const createMode = 'NFT';257 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});258 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);259260 await usingApi(async (api) => {261262 const burntx = api.tx.nft.burnItem(collectionId, tokenId, 1);263 const events1 = await submitTransactionAsync(alice, burntx);264 const result1 = getGenericResult(events1);265 expect(result1.success).to.be.true;266267 const tx = api.tx.nft.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);268 const badTransaction = async function () {269 await submitTransactionExpectFailAsync(alice, tx);270 };271 await expect(badTransaction()).to.be.rejected;272 });273274 });275276 it('Burn more than owned in Fungible collection', async () => {277 const createMode = 'Fungible';278 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});279 280 await createItemExpectSuccess(alice, collectionId, createMode);281 const tokenId = 0; 282283 await usingApi(async (api) => {284 285 const tx = api.tx.nft.burnItem(collectionId, tokenId, 11);286 const badTransaction = async function () {287 await submitTransactionExpectFailAsync(alice, tx);288 };289 await expect(badTransaction()).to.be.rejected;290291 292 const balance = await getBalance(api, collectionId, alice.address, 0);293294 295 expect(balance).to.be.equal(10n);296 });297298 });299300});