1234567891011121314151617import './interfaces/augment-api-consts';18import {IKeyringPair} from '@polkadot/types/types';19import {ApiPromise} from '@polkadot/api';20import {usingPlaygrounds, expect, itSub} from './util';2122const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z';23const saneMinimumFee = 0.05;24const saneMaximumFee = 0.5;25const createCollectionDeposit = 100;2627282930function skipInflationBlock(api: ApiPromise): Promise<void> {31 const promise = new Promise<void>(async (resolve) => {32 const blockInterval = api.consts.inflation.inflationBlockInterval.toNumber();33 const unsubscribe = await api.rpc.chain.subscribeNewHeads(head => {34 const currentBlock = head.number.toNumber();35 if(currentBlock % blockInterval < blockInterval - 10) {36 unsubscribe();37 resolve();38 } else {39 console.log(`Skipping inflation block, current block: ${currentBlock}`);40 }41 });42 });4344 return promise;45}4647describe('integration test: Fees must be credited to Treasury:', () => {48 let alice: IKeyringPair;49 let bob: IKeyringPair;5051 before(async () => {52 await usingPlaygrounds(async (helper, privateKey) => {53 const donor = await privateKey({url: import.meta.url});54 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);55 });56 });5758 itSub('Total issuance does not change', async ({helper}) => {59 const api = helper.getApi();60 await skipInflationBlock(api);61 await helper.wait.newBlocks(1);6263 const totalBefore = (await helper.callRpc('api.query.balances.totalIssuance', [])).toBigInt();6465 await helper.balance.transferToSubstrate(alice, bob.address, 1n);6667 const totalAfter = (await helper.callRpc('api.query.balances.totalIssuance', [])).toBigInt();6869 expect(totalAfter).to.be.equal(totalBefore);70 });7172 itSub('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async ({helper}) => {73 await skipInflationBlock(helper.getApi());74 await helper.wait.newBlocks(1);7576 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);77 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);7879 const amount = 1n;80 await helper.balance.transferToSubstrate(alice, bob.address, amount);8182 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);83 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);8485 const fee = aliceBalanceBefore - aliceBalanceAfter - amount;86 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;8788 expect(treasuryIncrease).to.be.equal(fee);89 });9091 itSub('Treasury balance increased by failed tx fee', async ({helper}) => {92 const api = helper.getApi();93 await helper.wait.newBlocks(1);9495 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);96 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);9798 await expect(helper.signTransaction(bob, api.tx.balances.forceSetBalance(alice.address, 0))).to.be.rejected;99100 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);101 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);102103 const fee = bobBalanceBefore - bobBalanceAfter;104 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;105106 expect(treasuryIncrease).to.be.equal(fee);107 });108109 itSub('NFT Transactions also send fees to Treasury', async ({helper}) => {110 await skipInflationBlock(helper.getApi());111 await helper.wait.newBlocks(1);112113 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);114 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);115116 await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});117118 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);119 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);120 const fee = aliceBalanceBefore - aliceBalanceAfter;121 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;122123 expect(treasuryIncrease).to.be.equal(fee);124 });125126 itSub('Fees are sane', async ({helper}) => {127 const unique = helper.balance.getOneTokenNominal();128 await skipInflationBlock(helper.getApi());129 await helper.wait.newBlocks(1);130131 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);132133 await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});134135 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);136 const fee = aliceBalanceBefore - aliceBalanceAfter;137138 expect(fee / unique < BigInt(Math.ceil(saneMaximumFee + createCollectionDeposit))).to.be.true;139 expect(fee / unique < BigInt(Math.ceil(saneMinimumFee + createCollectionDeposit))).to.be.true;140 });141142 itSub('NFT Transfer fee is close to 0.1 Unique', async ({helper}) => {143 await skipInflationBlock(helper.getApi());144 await helper.wait.newBlocks(1);145146 const collection = await helper.nft.mintCollection(alice, {147 name: 'test',148 description: 'test',149 tokenPrefix: 'test',150 });151 152 const token = await collection.mintToken(alice, {Substrate: alice.address});153154 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);155 await token.transfer(alice, {Substrate: bob.address});156 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);157158 const fee = Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal());159 const expectedTransferFee = 0.1;160 161 const tolerance = 0.001;162163 expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);164 });165});