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

difftreelog

source

tests/src/refungible.test.ts12.4 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 {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds} from './util/playgrounds';1920import chai from 'chai';21import chaiAsPromised from 'chai-as-promised';22chai.use(chaiAsPromised);23const expect = chai.expect;2425let alice: IKeyringPair;26let bob: IKeyringPair;27const MAX_REFUNGIBLE_PIECES = 1_000_000_000_000_000_000_000n;2829describe('integration test: Refungible functionality:', async () => {30  before(async function() {31    await usingPlaygrounds(async (helper, privateKey) => {32      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);3334      alice = privateKey('//Alice');35      bob = privateKey('//Bob');36    });37  });38  39  itSub('Create refungible collection and token', async ({helper}) => {40    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});4142    const itemCountBefore = await collection.getLastTokenId();43    const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);44    45    const itemCountAfter = await collection.getLastTokenId();46    47    // What to expect48    expect(token?.tokenId).to.be.gte(itemCountBefore);49    expect(itemCountAfter).to.be.equal(itemCountBefore + 1);50    expect(itemCountAfter.toString()).to.be.equal(token?.tokenId.toString());51  });52  53  itSub('Checking RPC methods when interacting with maximum allowed values (MAX_REFUNGIBLE_PIECES)', async ({helper}) => {54    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});55    56    const token = await collection.mintToken(alice, {Substrate: alice.address}, MAX_REFUNGIBLE_PIECES);57    58    expect(await collection.getTokenBalance(token.tokenId, {Substrate: alice.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);59    60    await collection.transferToken(alice, token.tokenId, {Substrate: bob.address}, MAX_REFUNGIBLE_PIECES);61    expect(await collection.getTokenBalance(token.tokenId, {Substrate: bob.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);62    expect(await token.getTotalPieces()).to.be.equal(MAX_REFUNGIBLE_PIECES);63    64    await expect(collection.mintToken(alice, {Substrate: alice.address}, MAX_REFUNGIBLE_PIECES + 1n))65      .to.eventually.be.rejectedWith(/refungible\.WrongRefungiblePieces/);66  });67  68  itSub('RPC method tokenOnewrs for refungible collection and token', async ({helper, privateKey}) => {69    const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};70    const facelessCrowd = Array(7).fill(0).map((_, i) => ({Substrate: privateKey(`//Alice+${i}`).address}));7172    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7374    const token = await collection.mintToken(alice, {Substrate: alice.address}, 10_000n);7576    await token.transfer(alice, {Substrate: bob.address}, 1000n);77    await token.transfer(alice, ethAcc, 900n);78    79    for (let i = 0; i < 7; i++) {80      await token.transfer(alice, facelessCrowd[i], 50n * BigInt(i + 1));81    } 8283    const owners = await token.getTop10Owners();8485    // What to expect86    expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);87    expect(owners.length).to.be.equal(10);88    89    const eleven = privateKey('//ALice+11');90    expect(await token.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;91    expect((await token.getTop10Owners()).length).to.be.equal(10);92  });93  94  itSub('Transfer token pieces', async ({helper}) => {95    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});96    const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);9798    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);99    expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;100    101    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);102    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);103    104    await expect(token.transfer(alice, {Substrate: bob.address}, 41n))105      .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);106  });107108  itSub('Create multiple tokens', async ({helper}) => {109    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});110    // TODO: fix mintMultipleTokens111    // await collection.mintMultipleTokens(alice, [112    //   {owner: {Substrate: alice.address}, pieces: 1n},113    //   {owner: {Substrate: alice.address}, pieces: 2n},114    //   {owner: {Substrate: alice.address}, pieces: 100n},115    // ]);116    await helper.rft.mintMultipleTokensWithOneOwner(alice, collection.collectionId, {Substrate: alice.address}, [117      {pieces: 1n}, 118      {pieces: 2n}, 119      {pieces: 100n},120    ]);121    const lastTokenId = await collection.getLastTokenId();122    expect(lastTokenId).to.be.equal(3);123    expect(await collection.getTokenBalance(lastTokenId, {Substrate: alice.address})).to.be.equal(100n);124  });125126  itSub('Burn some pieces', async ({helper}) => {127    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});128    const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);129    expect(await collection.isTokenExists(token.tokenId)).to.be.true;130    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);131    expect((await token.burn(alice, 99n)).success).to.be.true;132    expect(await collection.isTokenExists(token.tokenId)).to.be.true;133    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n);134  });135136  itSub('Burn all pieces', async ({helper}) => {137    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});138    const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);139    140    expect(await collection.isTokenExists(token.tokenId)).to.be.true;141    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);142143    expect((await token.burn(alice, 100n)).success).to.be.true;144    expect(await collection.isTokenExists(token.tokenId)).to.be.false;145  });146147  itSub('Burn some pieces for multiple users', async ({helper}) => {148    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});149    const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);150151    expect(await collection.isTokenExists(token.tokenId)).to.be.true;152    153    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);154    expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;155156    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);157    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);158159    expect((await token.burn(alice, 40n)).success).to.be.true;160161    expect(await collection.isTokenExists(token.tokenId)).to.be.true;162    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);163164    expect((await token.burn(bob, 59n)).success).to.be.true;165166    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n);167    expect(await collection.isTokenExists(token.tokenId)).to.be.true;168169    expect((await token.burn(bob, 1n)).success).to.be.true;170171    expect(await collection.isTokenExists(token.tokenId)).to.be.false;172  });173174  itSub('Set allowance for token', async ({helper}) => {175    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});176    const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);177    178    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);179180    expect(await token.approve(alice, {Substrate: bob.address}, 60n)).to.be.true;181    expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);182183    expect(await token.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;184    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(80n);185    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(20n);186    expect(await token.getApprovedPieces({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);187  });188189  itSub('Repartition', async ({helper}) => {190    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});191    const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);192193    expect(await token.repartition(alice, 200n)).to.be.true;194    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n);195    expect(await token.getTotalPieces()).to.be.equal(200n);196    197    expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true;198    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n);199    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n);200    201    await expect(token.repartition(alice, 80n))202      .to.eventually.be.rejectedWith(/refungible\.RepartitionWhileNotOwningAllPieces/);203    204    expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true;205    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);206    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n);207208    expect(await token.repartition(bob, 150n)).to.be.true;209    await expect(token.transfer(bob, {Substrate: alice.address}, 160n))210      .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);211  });212213  itSub('Repartition with increased amount', 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        helper.api!.createType('u32', collection.collectionId).toHuman(), 224        helper.api!.createType('u32', token.tokenId).toHuman(),225        {Substrate: alice.address}, 226        '100',227      ],228    }]);229  });230231  itSub('Repartition with decreased amount', async ({helper}) => {232    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});233    const token = await collection.mintToken(alice, {Substrate: alice.address}, 100n);234    await token.repartition(alice, 50n);235    const chainEvents = helper.chainLog.slice(-1)[0].events.map((x: any) => x.event);236    expect(chainEvents).to.include.deep.members([{237      method: 'ItemDestroyed',238      section: 'common',239      index: '0x4203',240      data: [ 241        helper.api!.createType('u32', collection.collectionId).toHuman(), 242        helper.api!.createType('u32', token.tokenId).toHuman(),243        {Substrate: alice.address}, 244        '50',245      ],246    }]);247  });248  249  itSub('Create new collection with properties', async ({helper}) => {250    const properties = [{key: 'key1', value: 'val1'}];251    const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];252    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test', properties, tokenPropertyPermissions});253    const info = await collection.getData();254    expect(info?.raw.properties).to.be.deep.equal(properties);255    expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions);256  });257});258