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

difftreelog

source

tests/src/burnItem.test.ts10.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  });4849  itSub.ifWithPallets('Burn item in ReFungible collection', [Pallets.ReFungible], async function({helper}) {50    const collection = await helper.rft.mintCollection(alice);51    const token = await collection.mintToken(alice, 100n);5253    await token.burn(alice, 90n);54    expect(await token.getBalance({Substrate: alice.address})).to.eq(10n);5556    await token.burn(alice, 10n);57    expect(await token.getBalance({Substrate: alice.address})).to.eq(0n);58  });5960  itSub.ifWithPallets('Burn owned portion of item in ReFungible collection', [Pallets.ReFungible], async function({helper}) {61    const collection = await helper.rft.mintCollection(alice);62    const token = await collection.mintToken(alice, 100n);6364    await token.transfer(alice, {Substrate: bob.address}, 1n);6566    expect(await token.getBalance({Substrate: alice.address})).to.eq(99n);67    expect(await token.getBalance({Substrate: bob.address})).to.eq(1n);6869    await token.burn(bob, 1n);7071    expect(await token.getBalance({Substrate: alice.address})).to.eq(99n);72    expect(await token.getBalance({Substrate: bob.address})).to.eq(0n);73  });74});7576describe('integration test: ext. burnItem() with admin permissions:', () => {77  let donor: IKeyringPair;78  let alice: IKeyringPair;79  let bob: IKeyringPair;8081  before(async () => {82    await usingPlaygrounds(async (helper, privateKey) => {83      donor = await privateKey({filename: __filename});84      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);85    });86  });8788  itSub('Burn item in NFT collection', async ({helper}) => {89    const collection = await helper.nft.mintCollection(alice);90    await collection.setLimits(alice, {ownerCanTransfer: true});91    await collection.addAdmin(alice, {Substrate: bob.address});92    const token = await collection.mintToken(alice);9394    await token.burnFrom(bob, {Substrate: alice.address});95    expect(await token.doesExist()).to.be.false;96  });9798  itSub('Burn item in Fungible collection', async ({helper}) => {99    const collection = await helper.ft.mintCollection(alice, {}, 0);100    await collection.setLimits(alice, {ownerCanTransfer: true});101    await collection.addAdmin(alice, {Substrate: bob.address});102    await collection.mint(alice, 10n);103104    await collection.burnTokensFrom(bob, {Substrate: alice.address}, 1n);105    expect(await collection.getBalance({Substrate: alice.address})).to.eq(9n);106  });107108  itSub.ifWithPallets('Burn item in ReFungible collection', [Pallets.ReFungible], async function({helper}) {109    const collection = await helper.rft.mintCollection(alice);110    await collection.setLimits(alice, {ownerCanTransfer: true});111    await collection.addAdmin(alice, {Substrate: bob.address});112    const token = await collection.mintToken(alice, 100n);113114    await token.burnFrom(bob, {Substrate: alice.address}, 100n);115    expect(await token.doesExist()).to.be.false;116  });117});118119describe('Negative integration test: ext. burnItem():', () => {120  let donor: IKeyringPair;121  let alice: IKeyringPair;122  let bob: IKeyringPair;123124  before(async () => {125    await usingPlaygrounds(async (helper, privateKey) => {126      donor = await privateKey({filename: __filename});127      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);128    });129  });130131  itSub('Burn a token that was never created', async ({helper}) => {132    const collection = await helper.nft.mintCollection(alice);133    await expect(collection.burnToken(alice, 10)).to.be.rejectedWith('common.TokenNotFound');134  });135136  itSub('Burn a token using the address that does not own it', async ({helper}) => {137    const collection = await helper.nft.mintCollection(alice);138    const token = await collection.mintToken(alice);139140    await expect(token.burn(bob)).to.be.rejectedWith('common.NoPermission');141  });142143  itSub.ifWithPallets('RFT: cannot burn non-owned token pieces', [Pallets.ReFungible], async ({helper}) => {144    const collection = await helper.rft.mintCollection(alice);145    const aliceToken = await collection.mintToken(alice, 10n, {Substrate: alice.address});146    const bobToken = await collection.mintToken(alice, 10n, {Substrate: bob.address});147148    // 1. Cannot burn non-owned token:149    await expect(bobToken.burn(alice, 0n)).to.be.rejectedWith('common.TokenValueTooLow');150    await expect(bobToken.burn(alice, 5n)).to.be.rejectedWith('common.TokenValueTooLow');151    // 2. Cannot burn non-existing token:152    await expect(helper.rft.burnToken(alice, 99999, 10)).to.be.rejectedWith('common.CollectionNotFound');153    await expect(helper.rft.burnToken(alice, collection.collectionId, 99999)).to.be.rejectedWith('common.TokenValueTooLow');154    // 3. Can burn zero amount of owned tokens (EIP-20)155    await aliceToken.burn(alice, 0n);156157    // 4. Storage is not corrupted:158    expect(await aliceToken.getTop10Owners()).to.deep.eq([{Substrate: alice.address}]);159    expect(await bobToken.getTop10Owners()).to.deep.eq([{Substrate: bob.address}]);160161    // 4.1 Tokens can be transfered:162    await aliceToken.transfer(alice, {Substrate: bob.address}, 10n);163    await bobToken.transfer(bob, {Substrate: alice.address}, 10n);164    expect(await aliceToken.getTop10Owners()).to.deep.eq([{Substrate: bob.address}]);165    expect(await bobToken.getTop10Owners()).to.deep.eq([{Substrate: alice.address}]);166  });167168  itSub('Transfer a burned token', async ({helper}) => {169    const collection = await helper.nft.mintCollection(alice);170    const token = await collection.mintToken(alice);171    await token.burn(alice);172173    await expect(token.transfer(alice, {Substrate: bob.address})).to.be.rejectedWith('common.TokenNotFound');174  });175176  itSub('Burn more than owned in Fungible collection', async ({helper}) => {177    const collection = await helper.ft.mintCollection(alice, {}, 0);178    await collection.mint(alice, 10n);179180    await expect(collection.burnTokens(alice, 11n)).to.be.rejectedWith('common.TokenValueTooLow');181    expect(await collection.getBalance({Substrate: alice.address})).to.eq(10n);182  });183184  itSub('Zero burn NFT', async ({helper}) => {185    const collection = await helper.nft.mintCollection(alice, {name: 'Coll', description: 'Desc', tokenPrefix: 'T'});186    const tokenAlice = await collection.mintToken(alice, {Substrate: alice.address});187    const tokenBob = await collection.mintToken(alice, {Substrate: bob.address});188    189    // 1. Zero burn of own tokens allowed:190    await helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, tokenAlice.tokenId, 0]);191    // 2. Zero burn of non-owned tokens not allowed:192    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, tokenBob.tokenId, 0])).to.be.rejectedWith('common.NoPermission');193    // 3. Zero burn of non-existing tokens not allowed:194    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnItem', [collection.collectionId, 9999, 0])).to.be.rejectedWith('common.TokenNotFound');195    expect(await tokenAlice.doesExist()).to.be.true;196    expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: alice.address});197    expect(await tokenBob.getOwner()).to.deep.eq({Substrate: bob.address});198    // 4. Storage is not corrupted:199    await tokenAlice.transfer(alice, {Substrate: bob.address});200    await tokenBob.transfer(bob, {Substrate: alice.address});201    expect(await tokenAlice.getOwner()).to.deep.eq({Substrate: bob.address});202    expect(await tokenBob.getOwner()).to.deep.eq({Substrate: alice.address});203  });204205  itSub('zero burnFrom NFT', async ({helper}) => {206    const collection = await helper.nft.mintCollection(alice, {name: 'Zero', description: 'Zero transfer', tokenPrefix: 'TF'});207    const notApprovedNft = await collection.mintToken(alice, {Substrate: bob.address});208    const approvedNft = await collection.mintToken(alice, {Substrate: bob.address});209    await approvedNft.approve(bob, {Substrate: alice.address});210211    // 1. Zero burnFrom of non-existing tokens not allowed:212    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, 9999, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');213    // 2. Zero burnFrom of not approved tokens not allowed:214    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, notApprovedNft.tokenId, 0])).to.be.rejectedWith('common.ApprovedValueTooLow');215    // 3. Zero burnFrom of approved tokens allowed:216    await helper.executeExtrinsic(alice, 'api.tx.unique.burnFrom', [collection.collectionId, {Substrate: bob.address}, approvedNft.tokenId, 0]);217218    // 4.1 approvedNft still approved:219    expect(await approvedNft.isApproved({Substrate: alice.address})).to.be.true;220    // 4.2 bob is still the owner:221    expect(await approvedNft.getOwner()).to.deep.eq({Substrate: bob.address});222    expect(await notApprovedNft.getOwner()).to.deep.eq({Substrate: bob.address});223    // 4.3 Alice can burn approved nft:224    await approvedNft.burnFrom(alice, {Substrate: bob.address});225    expect(await approvedNft.doesExist()).to.be.false;226  });227});