git.delta.rocks / unique-network / refs/commits / 1889107dcf3d

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';18import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect} from './util';1920const MAX_REFUNGIBLE_PIECES = 1_000_000_000_000_000_000_000n;2122describe('integration test: Refungible functionality:', async () => {23  let donor: IKeyringPair;24  let alice: IKeyringPair;25  let bob: IKeyringPair;2627  before(async function() {28    await usingPlaygrounds(async (helper, privateKey) => {29      requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);3031      donor = await privateKey({filename: __filename});32      [alice, bob] = await helper.arrange.createAccounts([100n, 10n], donor);33    });34  });35  36  itSub('Create refungible collection and token', async ({helper}) => {37    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});3839    const itemCountBefore = await collection.getLastTokenId();40    const token = await collection.mintToken(alice, 100n);41    42    const itemCountAfter = await collection.getLastTokenId();43    44    // What to expect45    expect(token?.tokenId).to.be.gte(itemCountBefore);46    expect(itemCountAfter).to.be.equal(itemCountBefore + 1);47    expect(itemCountAfter.toString()).to.be.equal(token?.tokenId.toString());48  });49  50  itSub('Checking RPC methods when interacting with maximum allowed values (MAX_REFUNGIBLE_PIECES)', async ({helper}) => {51    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});52    53    const token = await collection.mintToken(alice, MAX_REFUNGIBLE_PIECES);54    55    expect(await collection.getTokenBalance(token.tokenId, {Substrate: alice.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);56    57    await collection.transferToken(alice, token.tokenId, {Substrate: bob.address}, MAX_REFUNGIBLE_PIECES);58    expect(await collection.getTokenBalance(token.tokenId, {Substrate: bob.address})).to.be.equal(MAX_REFUNGIBLE_PIECES);59    expect(await token.getTotalPieces()).to.be.equal(MAX_REFUNGIBLE_PIECES);60    61    await expect(collection.mintToken(alice, MAX_REFUNGIBLE_PIECES + 1n))62      .to.eventually.be.rejectedWith(/refungible\.WrongRefungiblePieces/);63  });64  65  itSub('RPC method tokenOwners for refungible collection and token', async ({helper}) => {66    const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};67    const facelessCrowd = (await helper.arrange.createAccounts(Array(7).fill(0n), donor)).map(keyring => {return {Substrate: keyring.address};});6869    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});7071    const token = await collection.mintToken(alice, 10_000n);7273    await token.transfer(alice, {Substrate: bob.address}, 1000n);74    await token.transfer(alice, ethAcc, 900n);75    76    for (let i = 0; i < 7; i++) {77      await token.transfer(alice, facelessCrowd[i], 50n * BigInt(i + 1));78    } 7980    const owners = await token.getTop10Owners();8182    // What to expect83    expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);84    expect(owners.length).to.be.equal(10);85    86    const [eleven] = await helper.arrange.createAccounts([0n], donor);87    expect(await token.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;88    expect((await token.getTop10Owners()).length).to.be.equal(10);89  });90  91  itSub('Transfer token pieces', async ({helper}) => {92    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});93    const token = await collection.mintToken(alice, 100n);9495    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);96    expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;97    98    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);99    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);100    101    await expect(token.transfer(alice, {Substrate: bob.address}, 41n))102      .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);103  });104105  itSub('Create multiple tokens', 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  });122123  itSub('Burn some pieces', async ({helper}) => {124    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});125    const token = await collection.mintToken(alice, 100n);126    expect(await collection.doesTokenExist(token.tokenId)).to.be.true;127    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);128    expect(await token.burn(alice, 99n)).to.be.true;129    expect(await collection.doesTokenExist(token.tokenId)).to.be.true;130    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(1n);131  });132133  itSub('Burn all pieces', async ({helper}) => {134    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});135    const token = await collection.mintToken(alice, 100n);136    137    expect(await collection.doesTokenExist(token.tokenId)).to.be.true;138    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);139140    expect(await token.burn(alice, 100n)).to.be.true;141    expect(await collection.doesTokenExist(token.tokenId)).to.be.false;142  });143144  itSub('Burn some pieces for multiple users', async ({helper}) => {145    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});146    const token = await collection.mintToken(alice, 100n);147148    expect(await collection.doesTokenExist(token.tokenId)).to.be.true;149    150    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(100n);151    expect(await token.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;152153    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(40n);154    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(60n);155156    expect(await token.burn(alice, 40n)).to.be.true;157158    expect(await collection.doesTokenExist(token.tokenId)).to.be.true;159    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);160161    expect(await token.burn(bob, 59n)).to.be.true;162163    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(1n);164    expect(await collection.doesTokenExist(token.tokenId)).to.be.true;165166    expect(await token.burn(bob, 1n)).to.be.true;167168    expect(await collection.doesTokenExist(token.tokenId)).to.be.false;169  });170171  itSub('Set allowance for token', async ({helper}) => {172    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});173    const token = await collection.mintToken(alice, 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  });185186  itSub('Repartition', async ({helper}) => {187    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});188    const token = await collection.mintToken(alice, 100n);189190    expect(await token.repartition(alice, 200n)).to.be.true;191    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(200n);192    expect(await token.getTotalPieces()).to.be.equal(200n);193    194    expect(await token.transfer(alice, {Substrate: bob.address}, 110n)).to.be.true;195    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(90n);196    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(110n);197    198    await expect(token.repartition(alice, 80n))199      .to.eventually.be.rejectedWith(/refungible\.RepartitionWhileNotOwningAllPieces/);200    201    expect(await token.transfer(alice, {Substrate: bob.address}, 90n)).to.be.true;202    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(0n);203    expect(await token.getBalance({Substrate: bob.address})).to.be.equal(200n);204205    expect(await token.repartition(bob, 150n)).to.be.true;206    await expect(token.transfer(bob, {Substrate: alice.address}, 160n))207      .to.eventually.be.rejectedWith(/common\.TokenValueTooLow/);208  });209210  itSub('Repartition with increased amount', async ({helper}) => {211    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});212    const token = await collection.mintToken(alice, 100n);213    await token.repartition(alice, 200n);214    const chainEvents = helper.chainLog.slice(-1)[0].events;215    const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemCreated');216    expect(event).to.deep.include({217      section: 'common',218      method: 'ItemCreated',219      index: [66, 2],220      data: [221        collection.collectionId,222        token.tokenId,223        {substrate: alice.address}, 224        100n,225      ],226    });227  });228229  itSub('Repartition with decreased amount', async ({helper}) => {230    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});231    const token = await collection.mintToken(alice, 100n);232    await token.repartition(alice, 50n);233    const chainEvents = helper.chainLog.slice(-1)[0].events;234    const event = chainEvents.find((event: any) => event.section === 'common' && event.method === 'ItemDestroyed');235    expect(event).to.deep.include({236      section: 'common',237      method: 'ItemDestroyed',238      index: [66, 3],239      data: [240        collection.collectionId,241        token.tokenId,242        {substrate: alice.address}, 243        50n,244      ],245    });246  });247  248  itSub('Create new collection with properties', async ({helper}) => {249    const properties = [{key: 'key1', value: 'val1'}];250    const tokenPropertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];251    const collection = await helper.rft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test', properties, tokenPropertyPermissions});252    const info = await collection.getData();253    expect(info?.raw.properties).to.be.deep.equal(properties);254    expect(info?.raw.tokenPropertyPermissions).to.be.deep.equal(tokenPropertyPermissions);255  });256});257