git.delta.rocks / unique-network / refs/commits / 15d448e077f4

difftreelog

source

tests/src/burnItem.test.ts7.6 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 {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from './util';192021describe('integration test: ext. burnItem():', () => {22  let donor: IKeyringPair;23  let alice: IKeyringPair;24  let bob: IKeyringPair;2526  before(async () => {27    await usingPlaygrounds(async (helper, privateKey) => {28      donor = await privateKey({filename: __filename});29      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);30    });31  });3233  itSub('Burn item in NFT collection', async ({helper}) => {34    const collection = await helper.nft.mintCollection(alice);35    const token = await collection.mintToken(alice);3637    await token.burn(alice);38    expect(await token.doesExist()).to.be.false;39  });4041  itSub('Burn item in Fungible collection', async ({helper}) => {42    const collection = await helper.ft.mintCollection(alice, {}, 10);43    await collection.mint(alice, 10n);4445    await collection.burnTokens(alice, 1n);46    expect(await collection.getBalance({Substrate: alice.address})).to.eq(9n);47  });48});4950describe('integration test: ext. burnItem() with admin permissions:', () => {51  let donor: IKeyringPair;52  let alice: IKeyringPair;53  let bob: IKeyringPair;5455  before(async () => {56    await usingPlaygrounds(async (helper, privateKey) => {57      donor = await privateKey({filename: __filename});58      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);59    });60  });6162  itSub('Burn item in NFT collection', async ({helper}) => {63    const collection = await helper.nft.mintCollection(alice);64    await collection.setLimits(alice, {ownerCanTransfer: true});65    await collection.addAdmin(alice, {Substrate: bob.address});66    const token = await collection.mintToken(alice);6768    await token.burnFrom(bob, {Substrate: alice.address});69    expect(await token.doesExist()).to.be.false;70  });7172  itSub('Burn item in Fungible collection', async ({helper}) => {73    const collection = await helper.ft.mintCollection(alice, {}, 0);74    await collection.setLimits(alice, {ownerCanTransfer: true});75    await collection.addAdmin(alice, {Substrate: bob.address});76    await collection.mint(alice, 10n);7778    await collection.burnTokensFrom(bob, {Substrate: alice.address}, 1n);79    expect(await collection.getBalance({Substrate: alice.address})).to.eq(9n);80  });81});8283describe('Negative integration test: ext. burnItem():', () => {84  let donor: IKeyringPair;85  let alice: IKeyringPair;86  let bob: IKeyringPair;8788  before(async () => {89    await usingPlaygrounds(async (helper, privateKey) => {90      donor = await privateKey({filename: __filename});91      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);92    });93  });9495  itSub('Burn a token that was never created', async ({helper}) => {96    const collection = await helper.nft.mintCollection(alice);97    await expect(collection.burnToken(alice, 10)).to.be.rejectedWith('common.TokenNotFound');98  });99100  itSub('Burn a token using the address that does not own it', async ({helper}) => {101    const collection = await helper.nft.mintCollection(alice);102    const token = await collection.mintToken(alice);103104    await expect(token.burn(bob)).to.be.rejectedWith('common.NoPermission');105  });106107  itSub('Transfer a burned token', async ({helper}) => {108    const collection = await helper.nft.mintCollection(alice);109    const token = await collection.mintToken(alice);110    await token.burn(alice);111112    await expect(token.transfer(alice, {Substrate: bob.address})).to.be.rejectedWith('common.TokenNotFound');113  });114115  itSub('Burn more than owned in Fungible collection', async ({helper}) => {116    const collection = await helper.ft.mintCollection(alice, {}, 0);117    await collection.mint(alice, 10n);118119    await expect(collection.burnTokens(alice, 11n)).to.be.rejectedWith('common.TokenValueTooLow');120    expect(await collection.getBalance({Substrate: alice.address})).to.eq(10n);121  });122123  itSub('Zero burn NFT', async ({helper}) => {124    const collection = await helper.nft.mintCollection(alice, {name: 'Coll', description: 'Desc', tokenPrefix: 'T'});125    const tokenAlice = await collection.mintToken(alice, {Substrate: alice.address});126    const tokenBob = await collection.mintToken(alice, {Substrate: bob.address});127128    // 1. Zero burn of own tokens allowed:129    await helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, tokenAlice.tokenId, 0]);130    // 2. Zero burn of non-owned tokens not allowed:131    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, tokenBob.tokenId, 0])).to.be.rejectedWith('common.NoPermission');132    // 3. Zero burn of non-existing tokens not allowed:133    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, 9999, 0])).to.be.rejectedWith('common.TokenNotFound');134    expect(await tokenAlice.doesExist()).to.be.true;135    expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: alice.address});136    expect(await tokenBob.getOwner()).to.deep.eq({Substrate: bob.address});137    // 4. Storage is not corrupted:138    await tokenAlice.transfer(alice, {Substrate: bob.address});139    await tokenBob.transfer(bob, {Substrate: alice.address});140    expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: bob.address});141    expect(await tokenBob.getOwner()).to.deep.eq({Substrate: alice.address});142  });143144  itSub('zero burnFrom NFT', async ({helper}) => {145    const collection = await helper.nft.mintCollection(alice, {name: 'Zero', description: 'Zero transfer', tokenPrefix: 'TF'});146    const notApprovedNft = await collection.mintToken(alice, {Substrate: bob.address});147    const approvedNft = await collection.mintToken(alice, {Substrate: bob.address});148    await approvedNft.approve(bob, {Substrate: alice.address});149150    // 1. Zero burnFrom of non-existing tokens not allowed:151    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, 9999, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');152    // 2. Zero burnFrom of not approved tokens not allowed:153    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, notApprovedNft.tokenId, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');154    // 3. Zero burnFrom of approved tokens allowed:155    await helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, approvedNft.tokenId, 0]);156157    // 4.1 approvedNft still approved:158    expect(await approvedNft.isApproved({Substrate: alice.address})).to.be.true;159    // 4.2 bob is still the owner:160    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: bob.address});161    expect(await notApprovedNft.getOwner()).to.deep.eq({Substrate: bob.address});162    // 4.3 Alice can burn approved nft:163    await approvedNft.burnFrom(alice, {Substrate: bob.address});164    expect(await approvedNft.doesExist()).to.be.false;165  });166});