difftreelog
Nested rft pieces burn test
in: master
1 file changed
tests/src/sub/refungible/nesting.test.tsdiffbeforeafterboth1// 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, requirePalletsOrSkip, usingPlaygrounds} from '../../util';1920describe('Refungible nesting', () => {21 let alice: IKeyringPair;22 let charlie: IKeyringPair;2324 before(async function() {25 await usingPlaygrounds(async (helper, privateKey) => {26 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);27 const donor = await privateKey({filename: __filename});28 [alice, charlie] = await helper.arrange.createAccounts([50n, 10n], donor);29 });30 });3132 [33 {restrictedMode: true},34 {restrictedMode: false},35 ].map(testCase => {36 itSub(`Owner can nest their token${testCase.restrictedMode': Restricted mode'''}`, async ({helper}) => {37 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});38 const collectionRFT = await helper.rft.mintCollection(alice);39 const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});4041 await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionRFT.collectionId] : null}});42 await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});43 await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());4445 await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true});46 await collectionRFT.addToAllowList(alice, {Substrate: charlie.address});47 await collectionRFT.addToAllowList(alice, targetToken.nestingAccount());4849 // Create an immediately nested token50 const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount());51 expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n);5253 // Create a token to be nested and nest54 const newToken = await collectionRFT.mintToken(charlie, 5n);55 await newToken.transfer(charlie, targetToken.nestingAccount(), 2n);56 expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n);57 expect(await newToken.getBalance({Substrate: charlie.address})).to.be.equal(3n);58 });59 });6061 itSub('owner can unnest nested token', async ({helper}) => {62 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});63 const targetToken = await collection.mintToken(alice);6465 // Owner mints nested RFT token:66 const collectionRFT = await helper.rft.mintCollection(alice);67 const token = await collectionRFT.mintToken(alice, 10n, targetToken.nestingAccount());6869 // 1.1 Owner can partially unnest token pieces with transferFrom:70 await token.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 9n);71 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(9n);72 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(1n);7374 // 1.2 Owner can unnest all pieces:75 await token.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 1n);76 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(10n);77 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(0n);78 });7980 itSub('owner can burn nested token', async ({helper}) => {81 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});82 const targetToken = await collection.mintToken(alice);8384 // Owner mints nested RFT token:85 const collectionRFT = await helper.rft.mintCollection(alice);86 const token = await collectionRFT.mintToken(alice, 10n, targetToken.nestingAccount());8788 // 1.1 Owner can partially burnFrom nested pieces:89 await token.burnFrom(alice, targetToken.nestingAccount(), 6n);90 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);91 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(4n);92 expect(await targetToken.getChildren()).to.has.length(1);9394 // 1.1 Owner can burnFrom all nested pieces:95 await token.burnFrom(alice, targetToken.nestingAccount(), 4n);96 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(0n);97 expect(await targetToken.getChildren()).to.has.length(0);98 expect(await token.doesExist()).to.be.false;99 });100});101102describe('Refungible nesting negative tests', () => {103 let alice: IKeyringPair;104 let bob: IKeyringPair;105106 before(async function() {107 await usingPlaygrounds(async (helper, privateKey) => {108 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);109 const donor = await privateKey({filename: __filename});110 [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);111 });112 });113114 itSub('cannot nest token if nesting is disabled', async ({helper}) => {115 const collectionNFT = await helper.nft.mintCollection(alice);116 const collectionRFT = await helper.rft.mintCollection(alice);117 const targetToken = await collectionNFT.mintToken(alice);118119 // Try to create an immediately nested token120 await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))121 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);122123 // Try to create a token to be nested and nest124 const token = await collectionRFT.mintToken(alice, 5n);125 await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))126 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);127 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);128 });129130 [131 {restrictedMode: true},132 {restrictedMode: false},133 ].map(testCase => {134 itSub(`non-Owner cannot nest someone else's token${testCase.restrictedMode': Restricted mode'''}`, async ({helper}) => {135 const collectionNFT = await helper.nft.mintCollection(alice);136 const collectionRFT = await helper.rft.mintCollection(alice);137 const targetToken = await collectionNFT.mintToken(alice);138139 await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionRFT.collectionId] : null}});140 await collectionNFT.addToAllowList(alice, {Substrate: bob.address});141 await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());142143 // Try to create a token to be nested and nest144 const newToken = await collectionRFT.mintToken(alice);145 await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/);146147 expect(await targetToken.getChildren()).to.be.length(0);148 expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n);149150 // Nest some tokens as Alice into Bob's token151 await newToken.transfer(alice, targetToken.nestingAccount());152153 // Try to pull it out154 await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n))155 .to.be.rejectedWith(/common\.ApprovedValueTooLow/);156 expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);157 });158 });159160 itSub('ReFungible: disallows to nest token to an unlisted collection', async ({helper}) => {161 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}});162 const collectionRFT = await helper.rft.mintCollection(alice);163 const targetToken = await collectionNFT.mintToken(alice);164165 // Try to create an immediately nested token166 await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))167 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);168169 // Try to create a token to be nested and nest170 const token = await collectionRFT.mintToken(alice, 5n);171 await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))172 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);173 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);174 });175});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, requirePalletsOrSkip, usingPlaygrounds} from '../../util';1920describe('Refungible nesting', () => {21 let alice: IKeyringPair;22 let charlie: IKeyringPair;2324 before(async function() {25 await usingPlaygrounds(async (helper, privateKey) => {26 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);27 const donor = await privateKey({filename: __filename});28 [alice, charlie] = await helper.arrange.createAccounts([50n, 10n], donor);29 });30 });3132 [33 {restrictedMode: true},34 {restrictedMode: false},35 ].map(testCase => {36 itSub(`Owner can nest their token${testCase.restrictedMode': Restricted mode'''}`, async ({helper}) => {37 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});38 const collectionRFT = await helper.rft.mintCollection(alice);39 const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});4041 await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionRFT.collectionId] : null}});42 await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});43 await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());4445 await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true});46 await collectionRFT.addToAllowList(alice, {Substrate: charlie.address});47 await collectionRFT.addToAllowList(alice, targetToken.nestingAccount());4849 // Create an immediately nested token50 const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount());51 expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n);5253 // Create a token to be nested and nest54 const newToken = await collectionRFT.mintToken(charlie, 5n);55 await newToken.transfer(charlie, targetToken.nestingAccount(), 2n);56 expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n);57 expect(await newToken.getBalance({Substrate: charlie.address})).to.be.equal(3n);58 });59 });6061 itSub('Owner can unnest nested token', async ({helper}) => {62 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});63 const targetToken = await collection.mintToken(alice);6465 // Owner mints nested RFT token:66 const collectionRFT = await helper.rft.mintCollection(alice);67 const token = await collectionRFT.mintToken(alice, 10n, targetToken.nestingAccount());6869 // 1.1 Owner can partially unnest token pieces with transferFrom:70 await token.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 9n);71 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(9n);72 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(1n);7374 // 1.2 Owner can unnest all pieces:75 await token.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 1n);76 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(10n);77 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(0n);78 });7980 itSub('Owner can burn nested token pieces', async ({helper}) => {81 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});82 const targetToken = await collection.mintToken(alice);8384 // Owner mints nested RFT token:85 const collectionRFT = await helper.rft.mintCollection(alice);86 const token = await collectionRFT.mintToken(alice, 100n, {Substrate: alice.address});87 await token.transfer(alice, targetToken.nestingAccount(), 30n);8889 // 1.1 Owner can partially burnFrom nested pieces:90 await token.burnFrom(alice, targetToken.nestingAccount(), 10n);91 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(70n);92 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(20n);93 expect(await targetToken.getChildren()).to.has.length(1);9495 // 1.1 Owner can burnFrom all nested pieces:96 await token.burnFrom(alice, targetToken.nestingAccount(), 20n);97 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(0n);98 expect(await targetToken.getChildren()).to.has.length(0);99 expect(await token.doesExist()).to.be.true;100101 // 2. Target token does not contain any pieces and can be burnt:102 await targetToken.burn(alice);103 expect(await targetToken.doesExist()).to.be.false;104 });105});106107describe('Refungible nesting negative tests', () => {108 let alice: IKeyringPair;109 let bob: IKeyringPair;110111 before(async function() {112 await usingPlaygrounds(async (helper, privateKey) => {113 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);114 const donor = await privateKey({filename: __filename});115 [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);116 });117 });118119 itSub('cannot nest token if nesting is disabled', async ({helper}) => {120 const collectionNFT = await helper.nft.mintCollection(alice);121 const collectionRFT = await helper.rft.mintCollection(alice);122 const targetToken = await collectionNFT.mintToken(alice);123124 // Try to create an immediately nested token125 await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))126 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);127128 // Try to create a token to be nested and nest129 const token = await collectionRFT.mintToken(alice, 5n);130 await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))131 .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);132 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);133 });134135 [136 {restrictedMode: true},137 {restrictedMode: false},138 ].map(testCase => {139 itSub(`non-Owner cannot nest someone else's token${testCase.restrictedMode': Restricted mode'''}`, async ({helper}) => {140 const collectionNFT = await helper.nft.mintCollection(alice);141 const collectionRFT = await helper.rft.mintCollection(alice);142 const targetToken = await collectionNFT.mintToken(alice);143144 await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionRFT.collectionId] : null}});145 await collectionNFT.addToAllowList(alice, {Substrate: bob.address});146 await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());147148 // Try to create a token to be nested and nest149 const newToken = await collectionRFT.mintToken(alice);150 await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/);151152 expect(await targetToken.getChildren()).to.be.length(0);153 expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n);154155 // Nest some tokens as Alice into Bob's token156 await newToken.transfer(alice, targetToken.nestingAccount());157158 // Try to pull it out159 await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n))160 .to.be.rejectedWith(/common\.ApprovedValueTooLow/);161 expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);162 });163 });164165 itSub('ReFungible: disallows to nest token to an unlisted collection', async ({helper}) => {166 const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}});167 const collectionRFT = await helper.rft.mintCollection(alice);168 const targetToken = await collectionNFT.mintToken(alice);169170 // Try to create an immediately nested token171 await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))172 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);173174 // Try to create a token to be nested and nest175 const token = await collectionRFT.mintToken(alice, 5n);176 await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))177 .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);178 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);179 });180});