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

difftreelog

source

tests/src/fungible.test.ts7.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 {U128_MAX} from './util/helpers';19import {itSub, usingPlaygrounds} from './util/playgrounds';20import chai from 'chai';21import chaiAsPromised from 'chai-as-promised';2223chai.use(chaiAsPromised);24const expect = chai.expect;2526let alice: IKeyringPair;27let bob: IKeyringPair;2829describe('integration test: Fungible functionality:', () => {30  before(async () => {31    await usingPlaygrounds(async (helper, privateKey) => {32      alice = privateKey('//Alice');33      bob = privateKey('//Bob');34    });35  });3637  itSub('Create fungible collection and token', async ({helper}) => {38    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'trest'});39    const defaultTokenId = await collection.getLastTokenId();40    expect(defaultTokenId).to.be.equal(0);4142    await collection.mint(alice, {Substrate: alice.address}, U128_MAX);43    const aliceBalance = await collection.getBalance({Substrate: alice.address});44    const itemCountAfter = await collection.getLastTokenId();4546    expect(itemCountAfter).to.be.equal(defaultTokenId);47    expect(aliceBalance).to.be.equal(U128_MAX);48  });49  50  itSub('RPC method tokenOnewrs for fungible collection and token', async ({helper, privateKey}) => {51    const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};52    const facelessCrowd = Array(7).fill(0).map((_, i) => ({Substrate: privateKey(`//Alice+${i}`).address}));5354    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});5556    await collection.mint(alice, {Substrate: alice.address}, U128_MAX);5758    await collection.transfer(alice, {Substrate: bob.address}, 1000n);59    await collection.transfer(alice, ethAcc, 900n);60    61    for (let i = 0; i < 7; i++) {62      await collection.transfer(alice, facelessCrowd[i], 1n);63    } 6465    const owners = await collection.getTop10Owners();6667    // What to expect68    expect(owners).to.deep.include.members([{Substrate: alice.address}, ethAcc, {Substrate: bob.address}, ...facelessCrowd]);69    expect(owners.length).to.be.equal(10);70    71    const eleven = privateKey('//ALice+11');72    expect(await collection.transfer(alice, {Substrate: eleven.address}, 10n)).to.be.true;73    expect((await collection.getTop10Owners()).length).to.be.equal(10);74  });75  76  itSub('Transfer token', async ({helper}) => {77    const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};78    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});79    await collection.mint(alice, {Substrate: alice.address}, 500n);8081    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(500n);82    expect(await collection.transfer(alice, {Substrate: bob.address}, 60n)).to.be.true;83    expect(await collection.transfer(alice, ethAcc, 140n)).to.be.true;8485    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(300n);86    expect(await collection.getBalance({Substrate: bob.address})).to.be.equal(60n);87    expect(await collection.getBalance(ethAcc)).to.be.equal(140n);8889    await expect(collection.transfer(alice, {Substrate: bob.address}, 350n)).to.eventually.be.rejected;90  });9192  itSub('Tokens multiple creation', async ({helper}) => {93    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});9495    await collection.mintWithOneOwner(alice, {Substrate: alice.address}, [96      {value: 500n},97      {value: 400n},98      {value: 300n},99    ]);100101    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(1200n);102  });103104  itSub('Burn some tokens ', async ({helper}) => {105    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});106    await collection.mint(alice, {Substrate: alice.address}, 500n);107108    expect(await collection.isTokenExists(0)).to.be.true;109    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(500n);110    expect(await collection.burnTokens(alice, 499n)).to.be.true;111    expect(await collection.isTokenExists(0)).to.be.true;112    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(1n);113  });114  115  itSub('Burn all tokens ', async ({helper}) => {116    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});117    await collection.mint(alice, {Substrate: alice.address}, 500n);118119    expect(await collection.isTokenExists(0)).to.be.true;120    expect(await collection.burnTokens(alice, 500n)).to.be.true;121    expect(await collection.isTokenExists(0)).to.be.true;122123    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(0n);124    expect(await collection.getTotalPieces()).to.be.equal(0n);125  });126127  itSub('Set allowance for token', async ({helper}) => {128    const collection = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});129    const ethAcc = {Ethereum: '0x67fb3503a61b284dc83fa96dceec4192db47dc7c'};130    await collection.mint(alice, {Substrate: alice.address}, 100n);131132    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(100n);133    134    expect(await collection.approveTokens(alice, {Substrate: bob.address}, 60n)).to.be.true;135    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(60n);136    expect(await collection.getBalance({Substrate: bob.address})).to.be.equal(0n);137138    expect(await collection.transferFrom(bob, {Substrate: alice.address}, {Substrate: bob.address}, 20n)).to.be.true;139    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(80n);140    expect(await collection.getBalance({Substrate: bob.address})).to.be.equal(20n);141    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(40n);142143    await collection.burnTokensFrom(bob, {Substrate: alice.address}, 10n);144145    expect(await collection.getBalance({Substrate: alice.address})).to.be.equal(70n);146    expect(await collection.getApprovedTokens({Substrate: alice.address}, {Substrate: bob.address})).to.be.equal(30n);147    expect(await collection.transferFrom(bob, {Substrate: alice.address}, ethAcc, 10n)).to.be.true;148    expect(await collection.getBalance(ethAcc)).to.be.equal(10n);149  });150});