1234567891011121314151617import {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';18import {Keyring} from '@polkadot/api';19import {IKeyringPair} from '@polkadot/types/types';20import {21 createCollectionExpectSuccess,22 createItemExpectSuccess,23 getGenericResult,24 normalizeAccountId,25 addCollectionAdminExpectSuccess,26 getBalance,27 isTokenExists,28} from './util/helpers';2930import chai from 'chai';31import chaiAsPromised from 'chai-as-promised';32chai.use(chaiAsPromised);33const expect = chai.expect;3435let alice: IKeyringPair;36let bob: IKeyringPair;3738describe('integration test: ext. burnItem():', () => {39 before(async () => {40 await usingApi(async () => {41 const keyring = new Keyring({type: 'sr25519'});42 alice = keyring.addFromUri('//Alice');43 bob = keyring.addFromUri('//Bob');44 });45 });4647 it('Burn item in NFT collection', async () => {48 const createMode = 'NFT';49 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});50 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);5152 await usingApi(async (api) => {53 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);54 const events = await submitTransactionAsync(alice, tx);55 const result = getGenericResult(events);5657 expect(result.success).to.be.true;58 59 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;60 });61 });6263 it('Burn item in Fungible collection', async () => {64 const createMode = 'Fungible';65 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});66 await createItemExpectSuccess(alice, collectionId, createMode); 67 const tokenId = 0; 6869 await usingApi(async (api) => {70 71 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);72 const events = await submitTransactionAsync(alice, tx);73 const result = getGenericResult(events);7475 76 const balance = await getBalance(api, collectionId, alice.address, 0);7778 79 expect(result.success).to.be.true;80 expect(balance).to.be.equal(9n);81 });82 });8384 it('Burn item in ReFungible collection', async () => {85 const createMode = 'ReFungible';86 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});87 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);8889 await usingApi(async (api) => {90 const tx = api.tx.unique.burnItem(collectionId, tokenId, 100);91 const events = await submitTransactionAsync(alice, tx);92 const result = getGenericResult(events);9394 95 const balance = await getBalance(api, collectionId, alice.address, tokenId);9697 98 expect(result.success).to.be.true;99 expect(balance).to.be.equal(0n);100 });101 });102103 it('Burn owned portion of item in ReFungible collection', async () => {104 const createMode = 'ReFungible';105 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});106 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);107108 await usingApi(async (api) => {109 110 const transfertx = api.tx.unique.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);111 const events1 = await submitTransactionAsync(alice, transfertx);112 const result1 = getGenericResult(events1);113114 115 const bobBalanceBefore = await getBalance(api, collectionId, bob.address, tokenId);116 const aliceBalanceBefore = await getBalance(api, collectionId, alice.address, tokenId);117118 119 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);120 const events2 = await submitTransactionAsync(bob, tx);121 const result2 = getGenericResult(events2);122123 124 const bobBalanceAfter = await getBalance(api, collectionId, bob.address, tokenId);125 const aliceBalanceAfter = await getBalance(api, collectionId, alice.address, tokenId);126 127128 129 expect(result1.success).to.be.true;130 expect(aliceBalanceBefore).to.be.equal(99n);131 expect(bobBalanceBefore).to.be.equal(1n);132133 134 expect(result2.success).to.be.true;135 expect(aliceBalanceAfter).to.be.equal(99n);136 expect(bobBalanceAfter).to.be.equal(0n);137 });138139 });140141});142143describe('integration test: ext. burnItem() with admin permissions:', () => {144 before(async () => {145 await usingApi(async () => {146 const keyring = new Keyring({type: 'sr25519'});147 alice = keyring.addFromUri('//Alice');148 bob = keyring.addFromUri('//Bob');149 });150 });151152 it('Burn item in NFT collection', async () => {153 const createMode = 'NFT';154 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});155 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);156 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);157158 await usingApi(async (api) => {159 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);160 const events = await submitTransactionAsync(bob, tx);161 const result = getGenericResult(events);162163 expect(result.success).to.be.true;164 165 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;166 });167 });168169 170 it('Burn item in Fungible collection', async () => {171 const createMode = 'Fungible';172 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});173 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode); 174 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);175176 await usingApi(async (api) => {177 178 const tx = api.tx.unique.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 1);179 const events = await submitTransactionAsync(bob, tx);180 const result = getGenericResult(events);181182 183 const balance = await getBalance(api, collectionId, alice.address, 0);184185 186 expect(result.success).to.be.true;187 expect(balance).to.be.equal(9n);188 });189 });190191 192 it('Burn item in ReFungible collection', async () => {193 const createMode = 'ReFungible';194 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});195 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);196 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);197198 await usingApi(async (api) => {199 const tx = api.tx.unique.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 100);200 const events = await submitTransactionAsync(bob, tx);201 const result = getGenericResult(events);202 203 expect(result.success).to.be.true;204 205 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;206 });207 });208});209210describe('Negative integration test: ext. burnItem():', () => {211 before(async () => {212 await usingApi(async () => {213 const keyring = new Keyring({type: 'sr25519'});214 alice = keyring.addFromUri('//Alice');215 bob = keyring.addFromUri('//Bob');216 });217 });218219 it('Burn a token that was never created', async () => {220 const createMode = 'NFT';221 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});222 const tokenId = 10;223224 await usingApi(async (api) => {225 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);226 const badTransaction = async function () {227 await submitTransactionExpectFailAsync(alice, tx);228 };229 await expect(badTransaction()).to.be.rejected;230 });231232 });233234 it('Burn a token using the address that does not own it', async () => {235 const createMode = 'NFT';236 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});237 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);238239 await usingApi(async (api) => {240 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);241 const badTransaction = async function () {242 await submitTransactionExpectFailAsync(bob, tx);243 };244 await expect(badTransaction()).to.be.rejected;245 });246247 });248249 it('Transfer a burned a token', async () => {250 const createMode = 'NFT';251 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});252 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);253254 await usingApi(async (api) => {255256 const burntx = api.tx.unique.burnItem(collectionId, tokenId, 1);257 const events1 = await submitTransactionAsync(alice, burntx);258 const result1 = getGenericResult(events1);259 expect(result1.success).to.be.true;260261 const tx = api.tx.unique.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);262 const badTransaction = async function () {263 await submitTransactionExpectFailAsync(alice, tx);264 };265 await expect(badTransaction()).to.be.rejected;266 });267268 });269270 it('Burn more than owned in Fungible collection', async () => {271 const createMode = 'Fungible';272 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});273 274 await createItemExpectSuccess(alice, collectionId, createMode);275 const tokenId = 0; 276277 await usingApi(async (api) => {278 279 const tx = api.tx.unique.burnItem(collectionId, tokenId, 11);280 const badTransaction = async function () {281 await submitTransactionExpectFailAsync(alice, tx);282 };283 await expect(badTransaction()).to.be.rejected;284285 286 const balance = await getBalance(api, collectionId, alice.address, 0);287288 289 expect(balance).to.be.equal(10n);290 });291292 });293294});