git.delta.rocks / unique-network / refs/commits / 0335e511ccbf

difftreelog

source

tests/src/refungible.test.ts13.0 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';1819import {usingPlaygrounds} from './util/playgrounds';20import {21  getModuleNames,22  Pallets,23  requirePallets,24} from './util/helpers';2526import chai from 'chai';27import chaiAsPromised from 'chai-as-promised';28chai.use(chaiAsPromised);29const expect = chai.expect;3031let alice: IKeyringPair;32let bob: IKeyringPair;33const MAX_REFUNGIBLE_PIECES = 1_000_000_000_000_000_000_000n;3435describe('integration test: Refungible functionality:', async () => {36  before(async function() {37    await requirePallets(this, [Pallets.ReFungible]);3839    await usingPlaygrounds(async (helper, privateKey) => {40      alice = privateKey('//Alice');41      bob = privateKey('//Bob');42      if (!getModuleNames(helper.api!).includes(Pallets.ReFungible)) this.skip();43    });44  });45  46  it('Create refungible collection and token', async () => {47    await usingPlaygrounds(async helper => {48      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});4950      const itemCountBefore = await collection.getLastTokenId();51      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);52      53      const itemCountAfter = await collection.getLastTokenId();54      55      // What to expect56      expect(token?.tokenId).to.be.gte(itemCountBefore);57      expect(itemCountAfter).to.be.equal(itemCountBefore + 1);58      expect(itemCountAfter.toString()).to.be.equal(token?.tokenId.toString());59    });60  });61  62  it('Checking RPC methods when interacting with maximum allowed values (MAX_REFUNGIBLE_PIECES)', async () => {63    await usingPlaygrounds(async helper => {64      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});65      66      const token = await collection.mintToken(alice, {Substrate: alice.address}, MAX_REFUNGIBLE_PIECES);67      68      expect(await collection.getTokenBalance(token.tokenId, {Substrate: alice.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);69      70      await collection.transferToken(alice, token.tokenId, {Substrate: bob.address}, MAX_REFUNGIBLE_PIECES);71      expect(await collection.getTokenBalance(token.tokenId, {Substrate: bob.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);72      expect(await token.getTotalPieces()).to.be.equal(MAX_REFUNGIBLE_PIECES);73      74      await expect(collection.mintToken(alice, {Substrate: alice.address}, MAX_REFUNGIBLE_PIECES + 1n)).to.eventually.be.rejected;75    });76  });77  78  it('RPC method tokenOnewrs for refungible collection and token', async () => {79    await usingPlaygrounds(async (helper, privateKey) => {80      const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};81      const facelessCrowd = Array(7).fill(0).map((_, i) => ({Substrate: privateKey(`//Alice+${i}`).address}));8283      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});8485      const token = await collection.mintToken(alice, {Substrate: alice.address}, 10_000n);8687      await token.transfer(alice, {Substrate: bob.address}, 1000n);88      await token.transfer(alice, ethAcc, 900n);89      90      for (let i = 0; i < 7; i++) {91        await token.transfer(alice, facelessCrowd[i], 50n * BigInt(i + 1));92      } 9394      const owners = await token.getTop10Owners();9596      // What to expect97      expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);98      expect(owners.length).to.be.equal(10);99      100      const eleven = privateKey('//ALice+11');101      expect(await token.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;102      expect((await token.getTop10Owners()).length).to.be.equal(10);103    });104  });105  106  it('Transfer token pieces', async () => {107    await usingPlaygrounds(async helper => {108      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});109      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);110111      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);112      expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;113      114      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);115      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);116      117      await expect(token.transfer(alice, {Substrate: bob.address}, 41n)).to.eventually.be.rejected;118    });119  });120121  it('Create multiple tokens', async () => {122    await usingPlaygrounds(async helper => {123      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});124      // TODO: fix mintMultipleTokens125      // await collection.mintMultipleTokens(alice, [126      //   {owner: {Substrate: alice.address}, pieces: 1n},127      //   {owner: {Substrate: alice.address}, pieces: 2n},128      //   {owner: {Substrate: alice.address}, pieces: 100n},129      // ]);130      await helper.rft.mintMultipleTokensWithOneOwner(alice, collection.collectionId, {Substrate: alice.address}, [131        {pieces: 1n}, 132        {pieces: 2n}, 133        {pieces: 100n},134      ]);135      const lastTokenId = await collection.getLastTokenId();136      expect(lastTokenId).to.be.equal(3);137      expect(await collection.getTokenBalance(lastTokenId, {Substrate: alice.address})).to.be.equal(100n);138    });139  });140141  it('Burn some pieces', async () => {142    await usingPlaygrounds(async helper => {143      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});144      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);145      expect(await collection.isTokenExists(token.tokenId)).to.be.true;146      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);147      expect((await token.burn(alice, 99n)).success).to.be.true;148      expect(await collection.isTokenExists(token.tokenId)).to.be.true;149      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n);150    });151  });152153  it('Burn all pieces', async () => {154    await usingPlaygrounds(async helper => {   155      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});156      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);157      158      expect(await collection.isTokenExists(token.tokenId)).to.be.true;159      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);160161      expect((await token.burn(alice, 100n)).success).to.be.true;162      expect(await collection.isTokenExists(token.tokenId)).to.be.false;163    });164  });165166  it('Burn some pieces for multiple users', async () => {167    await usingPlaygrounds(async helper => {168      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});169      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);170171      expect(await collection.isTokenExists(token.tokenId)).to.be.true;172      173      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);174      expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;175176      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);177      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);178179      expect((await token.burn(alice, 40n)).success).to.be.true;180181      expect(await collection.isTokenExists(token.tokenId)).to.be.true;182      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);183184      expect((await token.burn(bob, 59n)).success).to.be.true;185186      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n);187      expect(await collection.isTokenExists(token.tokenId)).to.be.true;188189      expect((await token.burn(bob, 1n)).success).to.be.true;190191      expect(await collection.isTokenExists(token.tokenId)).to.be.false;192    });193  });194195  it('Set allowance for token', async () => {196    await usingPlaygrounds(async helper => {197      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});198      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);199      200      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);201202      expect(await token.approve(alice, {Substrate: bob.address}, 60n)).to.be.true;203      expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);204205      expect(await token.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;206      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(80n);207      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(20n);208      expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);209    });210  });211212  it('Repartition', async () => {213    await usingPlaygrounds(async helper => {214      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});215      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);216217      expect(await token.repartition(alice, 200n)).to.be.true;218      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n);219      expect(await token.getTotalPieces()).to.be.equal(200n);220      221      expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true;222      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n);223      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n);224      225      await expect(token.repartition(alice, 80n)).to.eventually.be.rejected;226      227      expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true;228      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);229      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n);230231      expect(await token.repartition(bob, 150n)).to.be.true;232      await expect(token.transfer(bob, {Substrate: alice.address}, 160n)).to.eventually.be.rejected;233234    });235  });236237  it('Repartition with increased amount', async () => {238    await usingPlaygrounds(async helper => {239      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});240      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);241      await token.repartition(alice, 200n);242      const chainEvents = helper.chainLog.slice(-1)[0].events.map((x: any) => x.event);243      expect(chainEvents).to.include.deep.members([{244        method: 'ItemCreated',245        section: 'common',246        index: '0x4202',247        data: [ 248          collection.collectionId.toString(), 249          token.tokenId.toString(), 250          {Substrate: alice.address}, 251          '100',252        ],253      }]);254    });255  });256257  it('Repartition with decreased amount', async () => {258    await usingPlaygrounds(async helper => {259      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});260      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);261      await token.repartition(alice, 50n);262      const chainEvents = helper.chainLog.slice(-1)[0].events.map((x: any) => x.event);263      expect(chainEvents).to.include.deep.members([{264        method: 'ItemDestroyed',265        section: 'common',266        index: '0x4203',267        data: [ 268          collection.collectionId.toString(), 269          token.tokenId.toString(), 270          {Substrate: alice.address}, 271          '50',272        ],273      }]);274    });275  });276  277  it('Create new collection with properties', async () => {278    await usingPlaygrounds(async helper => {279      const properties = [{key: 'key1', value: 'val1'}];280      const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];281      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test', properties, tokenPropertyPermissions});282      const info = await collection.getData();283      expect(info?.raw.properties).to.be.deep.equal(properties);284      expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions);285    });286  });287});288