1234567891011121314151617import {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {20 createCollectionExpectSuccess,21 createItemExpectSuccess,22 getGenericResult,23 normalizeAccountId,24 addCollectionAdminExpectSuccess,25 getBalance,26 isTokenExists,27} from './util/helpers';2829import chai from 'chai';30import chaiAsPromised from 'chai-as-promised';31chai.use(chaiAsPromised);32const expect = chai.expect;3334let alice: IKeyringPair;35let bob: IKeyringPair;3637describe('integration test: ext. burnItem():', () => {38 before(async () => {39 await usingApi(async (api, privateKeyWrapper) => {40 alice = privateKeyWrapper('//Alice');41 bob = privateKeyWrapper('//Bob');42 });43 });4445 it('Burn item in NFT collection', async () => {46 const createMode = 'NFT';47 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});48 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);4950 await usingApi(async (api) => {51 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);52 const events = await submitTransactionAsync(alice, tx);53 const result = getGenericResult(events);5455 expect(result.success).to.be.true;56 57 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;58 });59 });6061 it('Burn item in Fungible collection', async () => {62 const createMode = 'Fungible';63 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});64 await createItemExpectSuccess(alice, collectionId, createMode); 65 const tokenId = 0; 6667 await usingApi(async (api) => {68 69 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);70 const events = await submitTransactionAsync(alice, tx);71 const result = getGenericResult(events);7273 74 const balance = await getBalance(api, collectionId, alice.address, 0);7576 77 expect(result.success).to.be.true;78 expect(balance).to.be.equal(9n);79 });80 });8182 it('Burn item in ReFungible collection', async () => {83 const createMode = 'ReFungible';84 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});85 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);8687 await usingApi(async (api) => {88 const tx = api.tx.unique.burnItem(collectionId, tokenId, 100);89 const events = await submitTransactionAsync(alice, tx);90 const result = getGenericResult(events);9192 93 const balance = await getBalance(api, collectionId, alice.address, tokenId);9495 96 expect(result.success).to.be.true;97 expect(balance).to.be.equal(0n);98 });99 });100101 it('Burn owned portion of item in ReFungible collection', async () => {102 const createMode = 'ReFungible';103 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});104 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);105106 await usingApi(async (api) => {107 108 const transfertx = api.tx.unique.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);109 const events1 = await submitTransactionAsync(alice, transfertx);110 const result1 = getGenericResult(events1);111112 113 const bobBalanceBefore = await getBalance(api, collectionId, bob.address, tokenId);114 const aliceBalanceBefore = await getBalance(api, collectionId, alice.address, tokenId);115116 117 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);118 const events2 = await submitTransactionAsync(bob, tx);119 const result2 = getGenericResult(events2);120121 122 const bobBalanceAfter = await getBalance(api, collectionId, bob.address, tokenId);123 const aliceBalanceAfter = await getBalance(api, collectionId, alice.address, tokenId);124 125126 127 expect(result1.success).to.be.true;128 expect(aliceBalanceBefore).to.be.equal(99n);129 expect(bobBalanceBefore).to.be.equal(1n);130131 132 expect(result2.success).to.be.true;133 expect(aliceBalanceAfter).to.be.equal(99n);134 expect(bobBalanceAfter).to.be.equal(0n);135 });136137 });138139});140141describe('integration test: ext. burnItem() with admin permissions:', () => {142 before(async () => {143 await usingApi(async (api, privateKeyWrapper) => {144 alice = privateKeyWrapper('//Alice');145 bob = privateKeyWrapper('//Bob');146 });147 });148149 it('Burn item in NFT collection', async () => {150 const createMode = 'NFT';151 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});152 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);153 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);154155 await usingApi(async (api) => {156 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);157 const events = await submitTransactionAsync(bob, tx);158 const result = getGenericResult(events);159160 expect(result.success).to.be.true;161 162 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;163 });164 });165166 167 it('Burn item in Fungible collection', async () => {168 const createMode = 'Fungible';169 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});170 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode); 171 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);172173 await usingApi(async (api) => {174 175 const tx = api.tx.unique.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 1);176 const events = await submitTransactionAsync(bob, tx);177 const result = getGenericResult(events);178179 180 const balance = await getBalance(api, collectionId, alice.address, 0);181182 183 expect(result.success).to.be.true;184 expect(balance).to.be.equal(9n);185 });186 });187188 189 it('Burn item in ReFungible collection', async () => {190 const createMode = 'ReFungible';191 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});192 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);193 await addCollectionAdminExpectSuccess(alice, collectionId, bob.address);194195 await usingApi(async (api) => {196 const tx = api.tx.unique.burnFrom(collectionId, normalizeAccountId(alice.address), tokenId, 100);197 const events = await submitTransactionAsync(bob, tx);198 const result = getGenericResult(events);199 200 expect(result.success).to.be.true;201 202 expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;203 });204 });205});206207describe('Negative integration test: ext. burnItem():', () => {208 before(async () => {209 await usingApi(async (api, privateKeyWrapper) => {210 alice = privateKeyWrapper('//Alice');211 bob = privateKeyWrapper('//Bob');212 });213 });214215 it('Burn a token that was never created', async () => {216 const createMode = 'NFT';217 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});218 const tokenId = 10;219220 await usingApi(async (api) => {221 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);222 const badTransaction = async function () {223 await submitTransactionExpectFailAsync(alice, tx);224 };225 await expect(badTransaction()).to.be.rejected;226 });227228 });229230 it('Burn a token using the address that does not own it', async () => {231 const createMode = 'NFT';232 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});233 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);234235 await usingApi(async (api) => {236 const tx = api.tx.unique.burnItem(collectionId, tokenId, 1);237 const badTransaction = async function () {238 await submitTransactionExpectFailAsync(bob, tx);239 };240 await expect(badTransaction()).to.be.rejected;241 });242243 });244245 it('Transfer a burned a token', async () => {246 const createMode = 'NFT';247 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode}});248 const tokenId = await createItemExpectSuccess(alice, collectionId, createMode);249250 await usingApi(async (api) => {251252 const burntx = api.tx.unique.burnItem(collectionId, tokenId, 1);253 const events1 = await submitTransactionAsync(alice, burntx);254 const result1 = getGenericResult(events1);255 expect(result1.success).to.be.true;256257 const tx = api.tx.unique.transfer(normalizeAccountId(bob.address), collectionId, tokenId, 1);258 const badTransaction = async function () {259 await submitTransactionExpectFailAsync(alice, tx);260 };261 await expect(badTransaction()).to.be.rejected;262 });263264 });265266 it('Burn more than owned in Fungible collection', async () => {267 const createMode = 'Fungible';268 const collectionId = await createCollectionExpectSuccess({mode: {type: createMode, decimalPoints: 0}});269 270 await createItemExpectSuccess(alice, collectionId, createMode);271 const tokenId = 0; 272273 await usingApi(async (api) => {274 275 const tx = api.tx.unique.burnItem(collectionId, tokenId, 11);276 const badTransaction = async function () {277 await submitTransactionExpectFailAsync(alice, tx);278 };279 await expect(badTransaction()).to.be.rejected;280281 282 const balance = await getBalance(api, collectionId, alice.address, 0);283284 285 expect(balance).to.be.equal(10n);286 });287288 });289290});