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

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';2021import chai from 'chai';22import chaiAsPromised from 'chai-as-promised';23chai.use(chaiAsPromised);24const expect = chai.expect;2526let alice: IKeyringPair;27let bob: IKeyringPair;2829describe('integration test: Refungible functionality:', () => {30  before(async () => {31    await usingPlaygrounds(async (helper, privateKey) => {32      alice = privateKey('//Alice');33      bob = privateKey('//Bob');34    });35  });3637  it('Create refungible collection and token', async () => {38    await usingPlaygrounds(async helper => {39      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});4041      const itemCountBefore = await collection.getLastTokenId();42      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);43      44      const itemCountAfter = await collection.getLastTokenId();45      46      // What to expect47      expect(token?.tokenId).to.be.gte(itemCountBefore);48      expect(itemCountAfter).to.be.equal(itemCountBefore + 1);49      expect(itemCountAfter.toString()).to.be.equal(token?.tokenId.toString());50    });51  });52  53  it('RPC method tokenOnewrs for refungible collection and token', async () => {54    await usingPlaygrounds(async (helper, privateKey) => {55      const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};56      const facelessCrowd = Array(7).fill(0).map((_, i) => ({Substrate: privateKey(`//Alice+${i}`).address}));5758      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});5960      const token = await collection.mintToken(alice, {Substrate: alice.address}, 10_000n);6162      await token.transfer(alice, {Substrate: bob.address}, 1000n);63      await token.transfer(alice, ethAcc, 900n);64      65      for (let i = 0; i < 7; i++) {66        await token.transfer(alice, facelessCrowd[i], 50n * BigInt(i + 1));67      } 6869      const owners = await token.getTop10Owners();7071      // What to expect72      expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);73      expect(owners.length).to.be.equal(10);74      75      const eleven = privateKey('//ALice+11');76      expect(await token.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;77      expect((await token.getTop10Owners()).length).to.be.equal(10);78    });79  });80  81  it('Transfer token pieces', async () => {82    await usingPlaygrounds(async helper => {83      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});84      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);8586      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);87      expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;88      89      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);90      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);91      92      await expect(token.transfer(alice, {Substrate: bob.address}, 41n)).to.eventually.be.rejected;93    });94  });9596  it('Create multiple tokens', async () => {97    await usingPlaygrounds(async helper => {98      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});99      // TODO: fix mintMultipleTokens100      // await collection.mintMultipleTokens(alice, [101      //   {owner: {Substrate: alice.address}, pieces: 1n},102      //   {owner: {Substrate: alice.address}, pieces: 2n},103      //   {owner: {Substrate: alice.address}, pieces: 100n},104      // ]);105      await helper.rft.mintMultipleTokensWithOneOwner(alice, collection.collectionId, {Substrate: alice.address}, [106        {pieces: 1n}, 107        {pieces: 2n}, 108        {pieces: 100n},109      ]);110      const lastTokenId = await collection.getLastTokenId();111      expect(lastTokenId).to.be.equal(3);112      expect(await collection.getTokenBalance(lastTokenId, {Substrate: alice.address})).to.be.equal(100n);113    });114  });115116  it('Burn some pieces', async () => {117    await usingPlaygrounds(async helper => {118      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});119      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);120      expect(await collection.isTokenExists(token.tokenId)).to.be.true;121      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);122      expect((await token.burn(alice, 99n)).success).to.be.true;123      expect(await collection.isTokenExists(token.tokenId)).to.be.true;124      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n);125    });126  });127128  it('Burn all pieces', async () => {129    await usingPlaygrounds(async helper => {   130      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});131      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);132      133      expect(await collection.isTokenExists(token.tokenId)).to.be.true;134      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);135136      expect((await token.burn(alice, 100n)).success).to.be.true;137      expect(await collection.isTokenExists(token.tokenId)).to.be.false;138    });139  });140141  it('Burn some pieces for multiple users', 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);145146      expect(await collection.isTokenExists(token.tokenId)).to.be.true;147      148      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);149      expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;150151      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);152      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);153154      expect((await token.burn(alice, 40n)).success).to.be.true;155156      expect(await collection.isTokenExists(token.tokenId)).to.be.true;157      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);158159      expect((await token.burn(bob, 59n)).success).to.be.true;160161      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n);162      expect(await collection.isTokenExists(token.tokenId)).to.be.true;163164      expect((await token.burn(bob, 1n)).success).to.be.true;165166      expect(await collection.isTokenExists(token.tokenId)).to.be.false;167    });168  });169170  it('Set allowance for token', async () => {171    await usingPlaygrounds(async helper => {172      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});173      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);174      175      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);176177      expect(await token.approve(alice, {Substrate: bob.address}, 60n)).to.be.true;178      expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);179180      expect(await token.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;181      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(80n);182      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(20n);183      expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);184    });185  });186187  it('Repartition', async () => {188    await usingPlaygrounds(async helper => {189      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});190      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);191192      expect(await token.repartition(alice, 200n)).to.be.true;193      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n);194      expect(await token.getTotalPieces()).to.be.equal(200n);195      196      expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true;197      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n);198      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n);199      200      await expect(token.repartition(alice, 80n)).to.eventually.be.rejected;201      202      expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true;203      expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);204      expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n);205206      expect(await token.repartition(bob, 150n)).to.be.true;207      await expect(token.transfer(bob, {Substrate: alice.address}, 160n)).to.eventually.be.rejected;208209    });210  });211212  it('Repartition with increased amount', 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);216      await token.repartition(alice, 200n);217      const chainEvents = helper.chainLog.slice(-1)[0].events.map((x: any) => x.event);218      expect(chainEvents).to.include.deep.members([{219        method: 'ItemCreated',220        section: 'common',221        index: '0x4202',222        data: [ 223          collection.collectionId.toString(), 224          token.tokenId.toString(), 225          {Substrate: alice.address}, 226          '100',227        ],228      }]);229    });230  });231232  it('Repartition with decreased amount', async () => {233    await usingPlaygrounds(async helper => {234      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});235      const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);236      await token.repartition(alice, 50n);237      const chainEvents = helper.chainLog.slice(-1)[0].events.map((x: any) => x.event);238      expect(chainEvents).to.include.deep.members([{239        method: 'ItemDestroyed',240        section: 'common',241        index: '0x4203',242        data: [ 243          collection.collectionId.toString(), 244          token.tokenId.toString(), 245          {Substrate: alice.address}, 246          '50',247        ],248      }]);249    });250  });251});252253describe('Test Refungible properties:', () => {254  before(async () => {255    await usingPlaygrounds(async (helper, privateKey) => {256      alice = privateKey('//Alice');257      bob = privateKey('//Bob');258    });259  });260  261  it('Сreate new collection with properties', async () => {262    await usingPlaygrounds(async helper => {263      const properties = [{key: 'key1', value: 'val1'}];264      const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];265      const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test', properties, tokenPropertyPermissions});266      const info = await collection.getData();267      expect(info?.raw.properties).to.be.deep.equal(properties);268      expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions);269    });270  });271});