1234567891011121314151617import './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;3132333435function 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 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 169 const tolerance = 0.001;170171 expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);172 });173});