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

difftreelog

source

tests/src/refungible.test.ts8.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 {default as usingApi} 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} from './util/helpers';3435import chai from 'chai';36import chaiAsPromised from 'chai-as-promised';37chai.use(chaiAsPromised);38const expect = chai.expect;3940let alice: IKeyringPair;41let bob: IKeyringPair;4243describe('integration test: Refungible functionality:', () => {44  before(async () => {45    await usingApi(async (api, privateKeyWrapper) => {46      alice = privateKeyWrapper('//Alice');47      bob = privateKeyWrapper('//Bob');48    });49  });5051  it('Create refungible collection and token', async () => {52    await usingApi(async api => {53      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});54      expect(createCollectionResult.success).to.be.true;    55      const collectionId  = createCollectionResult.collectionId;5657      const itemCountBefore = await getLastTokenId(api, collectionId);58      const result = await createRefungibleToken(api, alice, collectionId, 100n);5960      const itemCountAfter = await getLastTokenId(api, collectionId);6162      // What to expect63      // tslint:disable-next-line:no-unused-expression64      expect(result.success).to.be.true;65      expect(itemCountAfter).to.be.equal(itemCountBefore + 1);66      expect(collectionId).to.be.equal(result.collectionId);67      expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());68    });69  });7071  it('Transfer token pieces', async () => {72    await usingApi(async api => {73      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;74      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;7576      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);77      expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;7879      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);80      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);81      await expect(transfer(api, collectionId, tokenId, alice, bob, 41n)).to.eventually.be.rejected;82    });83  });8485  it('Create multiple tokens', async () => {86    const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});87    const args = [88      {ReFungible: {pieces: 1}},89      {ReFungible: {pieces: 2}},90      {ReFungible: {pieces: 100}},91    ];92    await createMultipleItemsExpectSuccess(alice, collectionId, args);9394    await usingApi(async api => {      95      const tokenId = await getLastTokenId(api, collectionId);96      expect(tokenId).to.be.equal(3);97      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);98    });99  });100101  it('Burn some pieces', async () => {102    await usingApi(async api => {   103      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;104      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;105      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;106      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);107      expect(await burnItem(api, alice, collectionId, tokenId, 99n)).to.be.true;108      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;109      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(1n);110    });111  });112113  it('Burn all pieces', async () => {114    await usingApi(async api => {   115      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;116      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;117      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;118      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);119      expect(await burnItem(api, alice, collectionId, tokenId, 100n)).to.be.true;120      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;121    });122  });123124  it('Burn some pieces for multiple users', async () => {125    await usingApi(async api => {   126      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;127      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;128      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;129130      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);131      expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;132133      134      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);135      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);136      expect(await burnItem(api, alice, collectionId, tokenId, 40n)).to.be.true;137138      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);139      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;140      expect(await burnItem(api, bob, collectionId, tokenId, 59n)).to.be.true;141142      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(1n);143      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;144      expect(await burnItem(api, bob, collectionId, tokenId, 1n)).to.be.true;145      146      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;147    });148  });149150  it('Set allowance for token', async () => {151    await usingApi(async api => {152      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;153      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;154155      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);156157      expect(await approve(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;158      expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(60n);159160      expect(await transferFrom(api, collectionId, tokenId, bob, alice, bob,  20n)).to.be.true;161      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(80n);162      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(20n);163      expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(40n);164    });165  });166167  it('Repartition', async () => {168    await usingApi(async api => {169      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;170      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;171172      expect(await repartitionRFT(api, collectionId, alice, tokenId, 200n)).to.be.true;173      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(200n);174175      expect(await transfer(api, collectionId, tokenId, alice, bob, 110n)).to.be.true;176      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(90n);177      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(110n);178179      await expect(repartitionRFT(api, collectionId, alice, tokenId, 80n)).to.eventually.be.rejected;180181      expect(await transfer(api, collectionId, tokenId, alice, bob, 90n)).to.be.true;182      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);183      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(200n);184185      expect(await repartitionRFT(api, collectionId, bob, tokenId, 150n)).to.be.true;186      await expect(transfer(api, collectionId, tokenId, bob, alice, 160n)).to.eventually.be.rejected;187    });188  });189});