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

difftreelog

source

js-packages/tests/burnItem.test.ts7.5 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import type {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, usingPlaygrounds} from './util/index.js';192021describe('integration test: ext. burnItem():', () => {22  let donor: IKeyringPair;23  let alice: IKeyringPair;2425  before(async () => {26    await usingPlaygrounds(async (helper, privateKey) => {27      donor = await privateKey({url: import.meta.url});28      [alice] = await helper.arrange.createAccounts([100n], donor);29    });30  });3132  itSub('Burn item in NFT collection', async ({helper}) => {33    const collection = await helper.nft.mintCollection(alice);34    const token = await collection.mintToken(alice);3536    await token.burn(alice);37    expect(await token.doesExist()).to.be.false;38  });3940  itSub('Burn item in Fungible collection', async ({helper}) => {41    const collection = await helper.ft.mintCollection(alice, {}, 10);42    await collection.mint(alice, 10n);4344    await collection.burnTokens(alice, 1n);45    expect(await collection.getBalance({Substrate: alice.address})).to.eq(9n);46  });47});4849describe('integration test: ext. burnItem() with admin permissions:', () => {50  let donor: IKeyringPair;51  let alice: IKeyringPair;52  let bob: IKeyringPair;5354  before(async () => {55    await usingPlaygrounds(async (helper, privateKey) => {56      donor = await privateKey({url: import.meta.url});57      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);58    });59  });6061  itSub('Burn item in NFT collection', async ({helper}) => {62    const collection = await helper.nft.mintCollection(alice);63    await collection.setLimits(alice, {ownerCanTransfer: true});64    await collection.addAdmin(alice, {Substrate: bob.address});65    const token = await collection.mintToken(alice);6667    await token.burnFrom(bob, {Substrate: alice.address});68    expect(await token.doesExist()).to.be.false;69  });7071  itSub('Burn item in Fungible collection', async ({helper}) => {72    const collection = await helper.ft.mintCollection(alice, {}, 0);73    await collection.setLimits(alice, {ownerCanTransfer: true});74    await collection.addAdmin(alice, {Substrate: bob.address});75    await collection.mint(alice, 10n);7677    await collection.burnTokensFrom(bob, {Substrate: alice.address}, 1n);78    expect(await collection.getBalance({Substrate: alice.address})).to.eq(9n);79  });80});8182describe('Negative integration test: ext. burnItem():', () => {83  let donor: IKeyringPair;84  let alice: IKeyringPair;85  let bob: IKeyringPair;8687  before(async () => {88    await usingPlaygrounds(async (helper, privateKey) => {89      donor = await privateKey({url: import.meta.url});90      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);91    });92  });9394  itSub('Burn a token that was never created', async ({helper}) => {95    const collection = await helper.nft.mintCollection(alice);96    await expect(collection.burnToken(alice, 10)).to.be.rejectedWith('common.TokenNotFound');97  });9899  itSub('Burn a token using the address that does not own it', async ({helper}) => {100    const collection = await helper.nft.mintCollection(alice);101    const token = await collection.mintToken(alice);102103    await expect(token.burn(bob)).to.be.rejectedWith('common.NoPermission');104  });105106  itSub('Transfer a burned token', async ({helper}) => {107    const collection = await helper.nft.mintCollection(alice);108    const token = await collection.mintToken(alice);109    await token.burn(alice);110111    await expect(token.transfer(alice, {Substrate: bob.address})).to.be.rejectedWith('common.TokenNotFound');112  });113114  itSub('Burn more than owned in Fungible collection', async ({helper}) => {115    const collection = await helper.ft.mintCollection(alice, {}, 0);116    await collection.mint(alice, 10n);117118    await expect(collection.burnTokens(alice, 11n)).to.be.rejectedWith('common.TokenValueTooLow');119    expect(await collection.getBalance({Substrate: alice.address})).to.eq(10n);120  });121122  itSub('Zero burn NFT', async ({helper}) => {123    const collection = await helper.nft.mintCollection(alice, {name: 'Coll', description: 'Desc', tokenPrefix: 'T'});124    const tokenAlice = await collection.mintToken(alice, {Substrate: alice.address});125    const tokenBob = await collection.mintToken(alice, {Substrate: bob.address});126127    // 1. Zero burn of own tokens allowed:128    await helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, tokenAlice.tokenId, 0]);129    // 2. Zero burn of non-owned tokens not allowed:130    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, tokenBob.tokenId, 0])).to.be.rejectedWith('common.NoPermission');131    // 3. Zero burn of non-existing tokens not allowed:132    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, 9999, 0])).to.be.rejectedWith('common.TokenNotFound');133    expect(await tokenAlice.doesExist()).to.be.true;134    expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: alice.address});135    expect(await tokenBob.getOwner()).to.deep.eq({Substrate: bob.address});136    // 4. Storage is not corrupted:137    await tokenAlice.transfer(alice, {Substrate: bob.address});138    await tokenBob.transfer(bob, {Substrate: alice.address});139    expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: bob.address});140    expect(await tokenBob.getOwner()).to.deep.eq({Substrate: alice.address});141  });142143  itSub('zero burnFrom NFT', async ({helper}) => {144    const collection = await helper.nft.mintCollection(alice, {name: 'Zero', description: 'Zero transfer', tokenPrefix: 'TF'});145    const notApprovedNft = await collection.mintToken(alice, {Substrate: bob.address});146    const approvedNft = await collection.mintToken(alice, {Substrate: bob.address});147    await approvedNft.approve(bob, {Substrate: alice.address});148149    // 1. Zero burnFrom of non-existing tokens not allowed:150    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, 9999, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');151    // 2. Zero burnFrom of not approved tokens not allowed:152    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, notApprovedNft.tokenId, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');153    // 3. Zero burnFrom of approved tokens allowed:154    await helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, approvedNft.tokenId, 0]);155156    // 4.1 approvedNft still approved:157    expect(await approvedNft.isApproved({Substrate: alice.address})).to.be.true;158    // 4.2 bob is still the owner:159    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: bob.address});160    expect(await notApprovedNft.getOwner()).to.deep.eq({Substrate: bob.address});161    // 4.3 Alice can burn approved nft:162    await approvedNft.burnFrom(alice, {Substrate: bob.address});163    expect(await approvedNft.doesExist()).to.be.false;164  });165});