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

difftreelog

source

tests/src/refungible.test.ts11.2 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 {default as usingApi, executeTransaction} from './substrate/substrate-api';18import {IKeyringPair} from '@polkadot/types/types';19import {20  createCollectionExpectSuccess,21  getBalance,22  createMultipleItemsExpectSuccess,23  isTokenExists,24  getLastTokenId,25  getAllowance,26  approve,27  transferFrom,28  createCollection,29  createRefungibleToken,30  transfer,31  burnItem,32  repartitionRFT,33  createCollectionWithPropsExpectSuccess,34  getDetailedCollectionInfo,35  normalizeAccountId,36  CrossAccountId,37} from './util/helpers';3839import chai from 'chai';40import chaiAsPromised from 'chai-as-promised';41chai.use(chaiAsPromised);42const expect = chai.expect;4344let alice: IKeyringPair;45let bob: IKeyringPair;4647describe('integration test: Refungible functionality:', () => {48  before(async () => {49    await usingApi(async (api, privateKeyWrapper) => {50      alice = privateKeyWrapper('//Alice');51      bob = privateKeyWrapper('//Bob');52    });53  });5455  it('Create refungible collection and token', async () => {56    await usingApi(async api => {57      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});58      expect(createCollectionResult.success).to.be.true;59      const collectionId  = createCollectionResult.collectionId;60      61      const itemCountBefore = await getLastTokenId(api, collectionId);62      const result = await createRefungibleToken(api, alice, collectionId, 100n);63      64      const itemCountAfter = await getLastTokenId(api, collectionId);65      66      // What to expect67      // tslint:disable-next-line:no-unused-expression68      expect(result.success).to.be.true;69      expect(itemCountAfter).to.be.equal(itemCountBefore + 1);70      expect(collectionId).to.be.equal(result.collectionId);71      expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());72    });73  });74  75  it('RPC method tokenOnewrs for refungible collection and token', async () => {76    await usingApi(async (api, privateKeyWrapper) => {77      const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};78      const facelessCrowd = Array.from(Array(7).keys()).map(i => normalizeAccountId(privateKeyWrapper(i.toString())));79      80      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});81      const collectionId = createCollectionResult.collectionId;82      83      const result = await createRefungibleToken(api, alice, collectionId, 10_000n);84      const aliceTokenId = result.itemId;85      86      87      await transfer(api, collectionId, aliceTokenId, alice, bob, 1000n);88      await transfer(api, collectionId, aliceTokenId, alice, ethAcc, 900n);89      90      for (let i = 0; i < 7; i++) {91        await transfer(api, collectionId, aliceTokenId, alice, facelessCrowd[i], 50*(i+1));92      } 93      94      const owners = await api.rpc.unique.tokenOwners(collectionId, aliceTokenId);95      const ids = (owners.toJSON() as CrossAccountId[]).map(s => normalizeAccountId(s));96      97      const aliceID = normalizeAccountId(alice);98      const bobId = normalizeAccountId(bob);99      100      // What to expect101      // tslint:disable-next-line:no-unused-expression102      expect(ids).to.deep.include.members([aliceID, ethAcc, bobId, ...facelessCrowd]);103      expect(owners.length).to.be.equal(10);104      105      const eleven = privateKeyWrapper('11');106      expect(await transfer(api, collectionId, aliceTokenId, alice, eleven, 10n)).to.be.true;107      expect((await api.rpc.unique.tokenOwners(collectionId, aliceTokenId)).length).to.be.equal(10);108    });109  });110  111  it('Transfer token pieces', async () => {112    await usingApi(async api => {113      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;114      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;115116      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);117      expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;118119      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);120      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);121      await expect(transfer(api, collectionId, tokenId, alice, bob, 41n)).to.eventually.be.rejected;122    });123  });124125  it('Create multiple tokens', async () => {126    const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});127    const args = [128      {ReFungible: {pieces: 1}},129      {ReFungible: {pieces: 2}},130      {ReFungible: {pieces: 100}},131    ];132    await createMultipleItemsExpectSuccess(alice, collectionId, args);133134    await usingApi(async api => {      135      const tokenId = await getLastTokenId(api, collectionId);136      expect(tokenId).to.be.equal(3);137      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);138    });139  });140141  it('Burn some pieces', async () => {142    await usingApi(async api => {   143      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;144      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;145      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;146      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);147      expect(await burnItem(api, alice, collectionId, tokenId, 99n)).to.be.true;148      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;149      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(1n);150    });151  });152153  it('Burn all pieces', async () => {154    await usingApi(async api => {   155      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;156      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;157      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;158      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);159      expect(await burnItem(api, alice, collectionId, tokenId, 100n)).to.be.true;160      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;161    });162  });163164  it('Burn some pieces for multiple users', async () => {165    await usingApi(async api => {   166      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;167      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;168      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;169170      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);171      expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;172173      174      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);175      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);176      expect(await burnItem(api, alice, collectionId, tokenId, 40n)).to.be.true;177178      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);179      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;180      expect(await burnItem(api, bob, collectionId, tokenId, 59n)).to.be.true;181182      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(1n);183      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;184      expect(await burnItem(api, bob, collectionId, tokenId, 1n)).to.be.true;185      186      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;187    });188  });189190  it('Set allowance for token', async () => {191    await usingApi(async api => {192      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;193      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;194195      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);196197      expect(await approve(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;198      expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(60n);199200      expect(await transferFrom(api, collectionId, tokenId, bob, alice, bob,  20n)).to.be.true;201      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(80n);202      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(20n);203      expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(40n);204    });205  });206207  it('Repartition', async () => {208    await usingApi(async api => {209      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;210      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;211212      expect(await repartitionRFT(api, collectionId, alice, tokenId, 200n)).to.be.true;213      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(200n);214215      expect(await transfer(api, collectionId, tokenId, alice, bob, 110n)).to.be.true;216      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(90n);217      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(110n);218219      await expect(repartitionRFT(api, collectionId, alice, tokenId, 80n)).to.eventually.be.rejected;220221      expect(await transfer(api, collectionId, tokenId, alice, bob, 90n)).to.be.true;222      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);223      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(200n);224225      expect(await repartitionRFT(api, collectionId, bob, tokenId, 150n)).to.be.true;226      await expect(transfer(api, collectionId, tokenId, bob, alice, 160n)).to.eventually.be.rejected;227    });228  });229});230231describe('Test Refungible properties:', () => {232  before(async () => {233    await usingApi(async (api, privateKeyWrapper) => {234      alice = privateKeyWrapper('//Alice');235      bob = privateKeyWrapper('//Bob');236    });237  });238  239  it('Сreate new collection with properties', async () => {240    await usingApi(async api => {241      const properties = [{key: 'key1', value: 'val1'}];242      const propertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];243      const collectionId = await createCollectionWithPropsExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'ReFungible'},244        properties: properties,245        propPerm: propertyPermissions, 246      });247      const collection = (await getDetailedCollectionInfo(api, collectionId))!;248      expect(collection.properties.toHuman()).to.be.deep.equal(properties);249      expect(collection.tokenPropertyPermissions.toHuman()).to.be.deep.equal(propertyPermissions);250    });251  });252});