git.delta.rocks / unique-network / refs/commits / 0b16c5c25c9b

difftreelog

creditFeesToTreasury migrated

rkv2022-09-16parent: #d729c2f.patch.diff
in: master

1 file changed

modifiedtests/src/creditFeesToTreasury.test.tsdiffbeforeafterboth
before · tests/src/creditFeesToTreasury.test.ts
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 './interfaces/augment-api-consts';18import chai from 'chai';19import chaiAsPromised from 'chai-as-promised';20import {default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync} from './substrate/substrate-api';21import {IKeyringPair} from '@polkadot/types/types';22import {23  createCollectionExpectSuccess,24  createItemExpectSuccess,25  getGenericResult,26  transferExpectSuccess,27  UNIQUE,28} from './util/helpers';2930import {default as waitNewBlocks} from './substrate/wait-new-blocks';31import {ApiPromise} from '@polkadot/api';3233chai.use(chaiAsPromised);34const expect = chai.expect;3536const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z';37const saneMinimumFee = 0.05;38const saneMaximumFee = 0.5;39const createCollectionDeposit = 100;4041let alice: IKeyringPair;42let bob: IKeyringPair;4344// Skip the inflation block pauses if the block is close to inflation block45// until the inflation happens46/*eslint no-async-promise-executor: "off"*/47function skipInflationBlock(api: ApiPromise): Promise<void> {48  const promise = new Promise<void>(async (resolve) => {49    const blockInterval = (await api.consts.inflation.inflationBlockInterval).toNumber();50    const unsubscribe = await api.rpc.chain.subscribeNewHeads(head => {51      const currentBlock = head.number.toNumber();52      if (currentBlock % blockInterval < blockInterval - 10) {53        unsubscribe();54        resolve();55      } else {56        console.log(`Skipping inflation block, current block: ${currentBlock}`);57      }58    });59  });6061  return promise;62}6364describe('integration test: Fees must be credited to Treasury:', () => {65  before(async () => {66    await usingApi(async (api, privateKeyWrapper) => {67      alice = privateKeyWrapper('//Alice');68      bob = privateKeyWrapper('//Bob');69    });70  });7172  it('Total issuance does not change', async () => {73    await usingApi(async (api) => {74      await skipInflationBlock(api);75      await waitNewBlocks(api, 1);7677      const totalBefore = (await api.query.balances.totalIssuance()).toBigInt();7879      const amount = 1n;80      const transfer = api.tx.balances.transfer(bob.address, amount);8182      const result = getGenericResult(await submitTransactionAsync(alice, transfer));8384      const totalAfter = (await api.query.balances.totalIssuance()).toBigInt();8586      expect(result.success).to.be.true;87      expect(totalAfter).to.be.equal(totalBefore);88    });89  });9091  it('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async () => {92    await usingApi(async (api) => {93      await skipInflationBlock(api);94      await waitNewBlocks(api, 1);9596      const treasuryBalanceBefore: bigint = (await api.query.system.account(TREASURY)).data.free.toBigInt();97      const aliceBalanceBefore: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();9899      const amount = 1n;100      const transfer = api.tx.balances.transfer(bob.address, amount);101      const result = getGenericResult(await submitTransactionAsync(alice, transfer));102103      const treasuryBalanceAfter: bigint = (await api.query.system.account(TREASURY)).data.free.toBigInt();104      const aliceBalanceAfter: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();105      const fee = aliceBalanceBefore - aliceBalanceAfter - amount;106      const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;107108      expect(result.success).to.be.true;109      expect(treasuryIncrease).to.be.equal(fee);110    });111  });112113  it('Treasury balance increased by failed tx fee', async () => {114    await usingApi(async (api) => {115      //await skipInflationBlock(api);116      await waitNewBlocks(api, 1);117118      const treasuryBalanceBefore = (await api.query.system.account(TREASURY)).data.free.toBigInt();119      const bobBalanceBefore = (await api.query.system.account(bob.address)).data.free.toBigInt();120121      const badTx = api.tx.balances.setBalance(alice.address, 0, 0);122      await expect(submitTransactionExpectFailAsync(bob, badTx)).to.be.rejected;123124      const treasuryBalanceAfter = (await api.query.system.account(TREASURY)).data.free.toBigInt();125      const bobBalanceAfter = (await api.query.system.account(bob.address)).data.free.toBigInt();126      const fee = bobBalanceBefore - bobBalanceAfter;127      const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;128129      expect(treasuryIncrease).to.be.equal(fee);130    });131  });132133  it('NFT Transactions also send fees to Treasury', async () => {134    await usingApi(async (api) => {135      await skipInflationBlock(api);136      await waitNewBlocks(api, 1);137138      const treasuryBalanceBefore = (await api.query.system.account(TREASURY)).data.free.toBigInt();139      const aliceBalanceBefore = (await api.query.system.account(alice.address)).data.free.toBigInt();140141      await createCollectionExpectSuccess();142143      const treasuryBalanceAfter = (await api.query.system.account(TREASURY)).data.free.toBigInt();144      const aliceBalanceAfter = (await api.query.system.account(alice.address)).data.free.toBigInt();145      const fee = aliceBalanceBefore - aliceBalanceAfter;146      const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;147148      expect(treasuryIncrease).to.be.equal(fee);149    });150  });151152  it('Fees are sane', async () => {153    await usingApi(async (api) => {154      await skipInflationBlock(api);155      await waitNewBlocks(api, 1);156157      const aliceBalanceBefore: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();158159      await createCollectionExpectSuccess();160161      const aliceBalanceAfter: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();162      const fee = aliceBalanceBefore - aliceBalanceAfter;163164      expect(fee / UNIQUE < BigInt(Math.ceil(saneMaximumFee + createCollectionDeposit))).to.be.true;165      expect(fee / UNIQUE < BigInt(Math.ceil(saneMinimumFee  + createCollectionDeposit))).to.be.true;166    });167  });168169  it('NFT Transfer fee is close to 0.1 Unique', async () => {170    await usingApi(async (api) => {171      await skipInflationBlock(api);172      await waitNewBlocks(api, 1);173174      const collectionId = await createCollectionExpectSuccess();175      const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');176177      const aliceBalanceBefore: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();178      await transferExpectSuccess(collectionId, tokenId, alice, bob, 1, 'NFT');179      const aliceBalanceAfter: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();180181      const fee = Number(aliceBalanceBefore - aliceBalanceAfter) / Number(UNIQUE);182      const expectedTransferFee = 0.1;183      // fee drifts because of NextFeeMultiplier184      const tolerance = 0.001;185186      expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);187    });188  });189190});
after · tests/src/creditFeesToTreasury.test.ts
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 './interfaces/augment-api-consts';18import {IKeyringPair} from '@polkadot/types/types';19import {20  UNIQUE,21} from './util/helpers';2223import {default as waitNewBlocks} from './substrate/wait-new-blocks';24import {ApiPromise} from '@polkadot/api';25import {usingPlaygrounds, expect, itSub} from './util/playgrounds';2627const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z';28const saneMinimumFee = 0.05;29const saneMaximumFee = 0.5;30const createCollectionDeposit = 100;3132// Skip the inflation block pauses if the block is close to inflation block33// until the inflation happens34/*eslint no-async-promise-executor: "off"*/35function skipInflationBlock(api: ApiPromise): Promise<void> {36  const promise = new Promise<void>(async (resolve) => {37    const blockInterval = (await api.consts.inflation.inflationBlockInterval).toNumber();38    const unsubscribe = await api.rpc.chain.subscribeNewHeads(head => {39      const currentBlock = head.number.toNumber();40      if (currentBlock % blockInterval < blockInterval - 10) {41        unsubscribe();42        resolve();43      } else {44        console.log(`Skipping inflation block, current block: ${currentBlock}`);45      }46    });47  });4849  return promise;50}5152describe('integration test: Fees must be credited to Treasury:', () => {53  let alice: IKeyringPair;54  let bob: IKeyringPair;5556  before(async () => {57    await usingPlaygrounds(async (helper, privateKey) => {58      const donor = privateKey('//Alice');59      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);60    });61  });6263  itSub('Total issuance does not change', async ({helper}) => {64    const api = helper.api!;65    await skipInflationBlock(api);66    await waitNewBlocks(api, 1);6768    const totalBefore = (await api.query.balances.totalIssuance()).toBigInt();6970    await helper.balance.transferToSubstrate(alice, bob.address, 1n);7172    const totalAfter = (await api.query.balances.totalIssuance()).toBigInt();7374    expect(totalAfter).to.be.equal(totalBefore);75  });7677  itSub('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async ({helper}) => {78    const api = helper.api!;79    await skipInflationBlock(api);80    await waitNewBlocks(api, 1);8182    const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);83    const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);8485    const amount = 1n;86    await helper.balance.transferToSubstrate(alice, bob.address, amount);8788    const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);89    const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);9091    const fee = aliceBalanceBefore - aliceBalanceAfter - amount;92    const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;9394    expect(treasuryIncrease).to.be.equal(fee);95  });9697  itSub('Treasury balance increased by failed tx fee', async ({helper}) => {98    const api = helper.api!;99    await waitNewBlocks(api, 1);100101    const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);102    const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);103104    await expect(helper.signTransaction(bob, api.tx.balances.setBalance(alice.address, 0, 0))).to.be.rejected;105106    const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);107    const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);108109    const fee = bobBalanceBefore - bobBalanceAfter;110    const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;111112    expect(treasuryIncrease).to.be.equal(fee);113  });114115  itSub('NFT Transactions also send fees to Treasury', async ({helper}) => {116    const api = helper.api!;117    await skipInflationBlock(api);118    await waitNewBlocks(api, 1);119120    const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);121    const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);122123    await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});124125    const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);126    const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);127    const fee = aliceBalanceBefore - aliceBalanceAfter;128    const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;129130    expect(treasuryIncrease).to.be.equal(fee);131  });132133  itSub('Fees are sane', async ({helper}) => {134    const api = helper.api!;135    await skipInflationBlock(api);136    await waitNewBlocks(api, 1);137138    const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);139140    await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});141142    const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);143    const fee = aliceBalanceBefore - aliceBalanceAfter;144145    expect(fee / UNIQUE < BigInt(Math.ceil(saneMaximumFee + createCollectionDeposit))).to.be.true;146    expect(fee / UNIQUE < BigInt(Math.ceil(saneMinimumFee  + createCollectionDeposit))).to.be.true;147  });148149  itSub('NFT Transfer fee is close to 0.1 Unique', async ({helper}) => {150    const api = helper.api!;151    await skipInflationBlock(api);152    await waitNewBlocks(api, 1);153154    const collection = await helper.nft.mintCollection(alice, {155      name: 'test',156      description: 'test',157      tokenPrefix: 'test',158    });159    // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');160    const token = await collection.mintToken(alice, {Substrate: alice.address});161162    const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);163    await token.transfer(alice, {Substrate: bob.address});164    const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);165166    const fee = Number(aliceBalanceBefore - aliceBalanceAfter) / Number(UNIQUE);167    const expectedTransferFee = 0.1;168    // fee drifts because of NextFeeMultiplier169    const tolerance = 0.001;170171    expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);172  });173});