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

difftreelog

source

tests/src/refungible.test.ts10.9 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} from './util/helpers';3637import chai from 'chai';38import chaiAsPromised from 'chai-as-promised';39chai.use(chaiAsPromised);40const expect = chai.expect;4142let alice: IKeyringPair;43let bob: IKeyringPair;4445describe('integration test: Refungible functionality:', () => {46  before(async () => {47    await usingApi(async (api, privateKeyWrapper) => {48      alice = privateKeyWrapper('//Alice');49      bob = privateKeyWrapper('//Bob');50    });51  });5253  it('Create refungible collection and token', async () => {54    await usingApi(async api => {55      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});56      expect(createCollectionResult.success).to.be.true;57      const collectionId  = createCollectionResult.collectionId;58      59      const itemCountBefore = await getLastTokenId(api, collectionId);60      const result = await createRefungibleToken(api, alice, collectionId, 100n);61      62      const itemCountAfter = await getLastTokenId(api, collectionId);63      64      // What to expect65      // tslint:disable-next-line:no-unused-expression66      expect(result.success).to.be.true;67      expect(itemCountAfter).to.be.equal(itemCountBefore + 1);68      expect(collectionId).to.be.equal(result.collectionId);69      expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());70    });71  });72  73  it('RPC method tokenOnewrs for refungible collection and token', async () => {74    await usingApi(async (api, privateKeyWrapper) => {75      const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};76      const facelessCrowd = Array.from(Array(7).keys()).map(i => normalizeAccountId(privateKeyWrapper(i.toString())));77      78      const createCollectionResult = await createCollection(api, alice, {mode: {type: 'ReFungible'}});79      const collectionId = createCollectionResult.collectionId;80      81      const result = await createRefungibleToken(api, alice, collectionId, 10_000n);82      const aliceTokenId = result.itemId;83      84      85      await transfer(api, collectionId, aliceTokenId, alice, bob, 1000n);86      await transfer(api, collectionId, aliceTokenId, alice, ethAcc, 900n);87      88      for (let i = 0; i < 7; i++) {89        await transfer(api, collectionId, aliceTokenId, alice, facelessCrowd[i], 50*(i+1));90      } 91      92      const owners = await api.rpc.unique.tokenOwners(collectionId, aliceTokenId);93      const ids = (owners.toJSON() as CrossAccountId[]).map(s => normalizeAccountId(s));94      95      const aliceID = normalizeAccountId(alice);96      const bobId = normalizeAccountId(bob);97      98      // What to expect99      // tslint:disable-next-line:no-unused-expression100      expect(ids).to.deep.include.members([aliceID, ethAcc, bobId, ...facelessCrowd]);101      expect(owners.length == 10).to.be.true;102    });103  });104  105  it('Transfer token pieces', async () => {106    await usingApi(async api => {107      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;108      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;109110      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);111      expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;112113      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);114      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);115      await expect(transfer(api, collectionId, tokenId, alice, bob, 41n)).to.eventually.be.rejected;116    });117  });118119  it('Create multiple tokens', async () => {120    const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});121    const args = [122      {ReFungible: {pieces: 1}},123      {ReFungible: {pieces: 2}},124      {ReFungible: {pieces: 100}},125    ];126    await createMultipleItemsExpectSuccess(alice, collectionId, args);127128    await usingApi(async api => {      129      const tokenId = await getLastTokenId(api, collectionId);130      expect(tokenId).to.be.equal(3);131      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);132    });133  });134135  it('Burn some pieces', async () => {136    await usingApi(async api => {   137      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;138      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;139      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;140      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);141      expect(await burnItem(api, alice, collectionId, tokenId, 99n)).to.be.true;142      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;143      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(1n);144    });145  });146147  it('Burn all pieces', async () => {148    await usingApi(async api => {   149      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;150      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;151      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;152      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);153      expect(await burnItem(api, alice, collectionId, tokenId, 100n)).to.be.true;154      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;155    });156  });157158  it('Burn some pieces for multiple users', async () => {159    await usingApi(async api => {   160      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;161      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;162      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;163164      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);165      expect(await transfer(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;166167      168      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(40n);169      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(60n);170      expect(await burnItem(api, alice, collectionId, tokenId, 40n)).to.be.true;171172      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);173      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;174      expect(await burnItem(api, bob, collectionId, tokenId, 59n)).to.be.true;175176      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(1n);177      expect(await isTokenExists(api, collectionId, tokenId)).to.be.true;178      expect(await burnItem(api, bob, collectionId, tokenId, 1n)).to.be.true;179      180      expect(await isTokenExists(api, collectionId, tokenId)).to.be.false;181    });182  });183184  it('Set allowance for token', async () => {185    await usingApi(async api => {186      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;187      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;188189      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(100n);190191      expect(await approve(api, collectionId, tokenId, alice, bob, 60n)).to.be.true;192      expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(60n);193194      expect(await transferFrom(api, collectionId, tokenId, bob, alice, bob,  20n)).to.be.true;195      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(80n);196      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(20n);197      expect(await getAllowance(api, collectionId, alice, bob, tokenId)).to.be.equal(40n);198    });199  });200201  it('Repartition', async () => {202    await usingApi(async api => {203      const collectionId = (await createCollection(api, alice, {mode: {type: 'ReFungible'}})).collectionId;204      const tokenId = (await createRefungibleToken(api, alice, collectionId, 100n)).itemId;205206      expect(await repartitionRFT(api, collectionId, alice, tokenId, 200n)).to.be.true;207      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(200n);208209      expect(await transfer(api, collectionId, tokenId, alice, bob, 110n)).to.be.true;210      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(90n);211      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(110n);212213      await expect(repartitionRFT(api, collectionId, alice, tokenId, 80n)).to.eventually.be.rejected;214215      expect(await transfer(api, collectionId, tokenId, alice, bob, 90n)).to.be.true;216      expect(await getBalance(api, collectionId, alice, tokenId)).to.be.equal(0n);217      expect(await getBalance(api, collectionId, bob, tokenId)).to.be.equal(200n);218219      expect(await repartitionRFT(api, collectionId, bob, tokenId, 150n)).to.be.true;220      await expect(transfer(api, collectionId, tokenId, bob, alice, 160n)).to.eventually.be.rejected;221    });222  });223});224225describe('Test Refungible properties:', () => {226  before(async () => {227    await usingApi(async (api, privateKeyWrapper) => {228      alice = privateKeyWrapper('//Alice');229      bob = privateKeyWrapper('//Bob');230    });231  });232  233  it('Сreate new collection with properties', async () => {234    await usingApi(async api => {235      const properties = [{key: 'key1', value: 'val1'}];236      const propertyPermissions = [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}];237      const collectionId = await createCollectionWithPropsExpectSuccess({name: 'A', description: 'B', tokenPrefix: 'C', mode: {type: 'ReFungible'},238        properties: properties,239        propPerm: propertyPermissions, 240      });241      const collection = (await getDetailedCollectionInfo(api, collectionId))!;242      expect(collection.properties.toHuman()).to.be.deep.equal(properties);243      expect(collection.tokenPropertyPermissions.toHuman()).to.be.deep.equal(propertyPermissions);244    });245  });246});