git.delta.rocks / unique-network / refs/commits / 9fe4c05e564b

difftreelog

source

tests/src/refungible.test.ts12.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;3334describe('integration test: Refungible functionality:', async () => {35  before(async function() {36    await requirePallets(this, [Pallets.ReFungible]);3738    await usingPlaygrounds(async (helper, privateKey) => {39      alice = privateKey('//Alice');40      bob = privateKey('//Bob');41      if (!getModuleNames(helper.api!).includes(Pallets.ReFungible)) this.skip();42    });43  });44  45  it('Create refungible collection and token', async () => {46    await usingPlaygrounds(async helper => {47      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});4849      const itemCountBefore = await collection.getLastTokenId();50      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);51      52      const itemCountAfter = await collection.getLastTokenId();53      54      // What to expect55      expect(token?.tokenId).to.be.gte(itemCountBefore);56      expect(itemCountAfter).to.be.equal(itemCountBefore + 1);57      expect(itemCountAfter.toString()).to.be.equal(token?.tokenId.toString());58    });59  });60  61  it('RPC method tokenOnewrs for refungible collection and token', async () => {62    await usingPlaygrounds(async (helper, privateKey) => {63      const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};64      const facelessCrowd = Array(7).fill(0).map((_, i) => ({Substrate: privateKey(`//Alice+${i}`).address}));6566      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});6768      const token = await collection.mintToken(alice, {Substrate: alice.address}, 10_000n);6970      await token.transfer(alice, {Substrate: bob.address}, 1000n);71      await token.transfer(alice, ethAcc, 900n);72      73      for (let i = 0; i < 7; i++) {74        await token.transfer(alice, facelessCrowd[i], 50n * BigInt(i + 1));75      } 7677      const owners = await token.getTop10Owners();7879      // What to expect80      expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);81      expect(owners.length).to.be.equal(10);82      83      const eleven = privateKey('//ALice+11');84      expect(await token.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;85      expect((await token.getTop10Owners()).length).to.be.equal(10);86    });87  });88  89  it('Transfer token pieces', async () => {90    await usingPlaygrounds(async helper => {91      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});92      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);9394      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);95      expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;96      97      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);98      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);99      100      await expect(token.transfer(alice, {Substrate: bob.address}, 41n)).to.eventually.be.rejected;101    });102  });103104  it('Create multiple tokens', async () => {105    await usingPlaygrounds(async helper => {106      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});107      // TODO: fix mintMultipleTokens108      // await collection.mintMultipleTokens(alice, [109      //   {owner: {Substrate: alice.address}, pieces: 1n},110      //   {owner: {Substrate: alice.address}, pieces: 2n},111      //   {owner: {Substrate: alice.address}, pieces: 100n},112      // ]);113      await helper.rft.mintMultipleTokensWithOneOwner(alice, collection.collectionId, {Substrate: alice.address}, [114        {pieces: 1n}, 115        {pieces: 2n}, 116        {pieces: 100n},117      ]);118      const lastTokenId = await collection.getLastTokenId();119      expect(lastTokenId).to.be.equal(3);120      expect(await collection.getTokenBalance(lastTokenId, {Substrate: alice.address})).to.be.equal(100n);121    });122  });123124  it('Burn some pieces', async () => {125    await usingPlaygrounds(async helper => {126      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});127      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);128      expect(await collection.isTokenExists(token.tokenId)).to.be.true;129      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);130      expect((await token.burn(alice, 99n)).success).to.be.true;131      expect(await collection.isTokenExists(token.tokenId)).to.be.true;132      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n);133    });134  });135136  it('Burn all pieces', async () => {137    await usingPlaygrounds(async helper => {   138      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});139      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);140      141      expect(await collection.isTokenExists(token.tokenId)).to.be.true;142      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);143144      expect((await token.burn(alice, 100n)).success).to.be.true;145      expect(await collection.isTokenExists(token.tokenId)).to.be.false;146    });147  });148149  it('Burn some pieces for multiple users', async () => {150    await usingPlaygrounds(async helper => {151      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});152      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);153154      expect(await collection.isTokenExists(token.tokenId)).to.be.true;155      156      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);157      expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;158159      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);160      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);161162      expect((await token.burn(alice, 40n)).success).to.be.true;163164      expect(await collection.isTokenExists(token.tokenId)).to.be.true;165      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);166167      expect((await token.burn(bob, 59n)).success).to.be.true;168169      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n);170      expect(await collection.isTokenExists(token.tokenId)).to.be.true;171172      expect((await token.burn(bob, 1n)).success).to.be.true;173174      expect(await collection.isTokenExists(token.tokenId)).to.be.false;175    });176  });177178  it('Set allowance for token', async () => {179    await usingPlaygrounds(async helper => {180      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});181      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);182      183      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);184185      expect(await token.approve(alice, {Substrate: bob.address}, 60n)).to.be.true;186      expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);187188      expect(await token.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;189      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(80n);190      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(20n);191      expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);192    });193  });194195  it('Repartition', 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);199200      expect(await token.repartition(alice, 200n)).to.be.true;201      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n);202      expect(await token.getTotalPieces()).to.be.equal(200n);203      204      expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true;205      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n);206      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n);207      208      await expect(token.repartition(alice, 80n)).to.eventually.be.rejected;209      210      expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true;211      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);212      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n);213214      expect(await token.repartition(bob, 150n)).to.be.true;215      await expect(token.transfer(bob, {Substrate: alice.address}, 160n)).to.eventually.be.rejected;216217    });218  });219220  it('Repartition with increased amount', async () => {221    await usingPlaygrounds(async helper => {222      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});223      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);224      await token.repartition(alice, 200n);225      const chainEvents = helper.chainLog.slice(-1)[0].events.map((x: any) => x.event);226      expect(chainEvents).to.include.deep.members([{227        method: 'ItemCreated',228        section: 'common',229        index: '0x4202',230        data: [ 231          collection.collectionId.toString(), 232          token.tokenId.toString(), 233          {Substrate: alice.address}, 234          '100',235        ],236      }]);237    });238  });239240  it('Repartition with decreased amount', async () => {241    await usingPlaygrounds(async helper => {242      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});243      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);244      await token.repartition(alice, 50n);245      const chainEvents = helper.chainLog.slice(-1)[0].events.map((x: any) => x.event);246      expect(chainEvents).to.include.deep.members([{247        method: 'ItemDestroyed',248        section: 'common',249        index: '0x4203',250        data: [ 251          collection.collectionId.toString(), 252          token.tokenId.toString(), 253          {Substrate: alice.address}, 254          '50',255        ],256      }]);257    });258  });259  260  it('Create new collection with properties', async () => {261    await usingPlaygrounds(async helper => {262      const properties = [{key: 'key1', value: 'val1'}];263      const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];264      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test', properties, tokenPropertyPermissions});265      const info = await collection.getData();266      expect(info?.raw.properties).to.be.deep.equal(properties);267      expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions);268    });269  });270});271