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

difftreelog

source

tests/src/creditFeesToTreasury.test.ts7.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 './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});