difftreelog
creditFeesToTreasury migrated
in: master
1 file changed
tests/src/creditFeesToTreasury.test.tsdiffbeforeafterboth15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.161617import './interfaces/augment-api-consts';17import './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';18import {IKeyringPair} from '@polkadot/types/types';22import {19import {23 createCollectionExpectSuccess,24 createItemExpectSuccess,25 getGenericResult,26 transferExpectSuccess,27 UNIQUE,20 UNIQUE,28} from './util/helpers';21} from './util/helpers';292230import {default as waitNewBlocks} from './substrate/wait-new-blocks';23import {default as waitNewBlocks} from './substrate/wait-new-blocks';31import {ApiPromise} from '@polkadot/api';24import {ApiPromise} from '@polkadot/api';3233chai.use(chaiAsPromised);34const expect = chai.expect;25import {usingPlaygrounds, expect, itSub} from './util/playgrounds';352636const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z';27const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z';37const saneMinimumFee = 0.05;28const saneMinimumFee = 0.05;38const saneMaximumFee = 0.5;29const saneMaximumFee = 0.5;39const createCollectionDeposit = 100;30const createCollectionDeposit = 100;4041let alice: IKeyringPair;42let bob: IKeyringPair;433144// Skip the inflation block pauses if the block is close to inflation block32// Skip the inflation block pauses if the block is close to inflation block45// until the inflation happens33// until the inflation happens62}50}635164describe('integration test: Fees must be credited to Treasury:', () => {52describe('integration test: Fees must be credited to Treasury:', () => {53 let alice: IKeyringPair;54 let bob: IKeyringPair;5565 before(async () => {56 before(async () => {66 await usingApi(async (api, privateKeyWrapper) => {57 await usingPlaygrounds(async (helper, privateKey) => {67 alice = privateKeyWrapper('//Alice');58 const donor = privateKey('//Alice');68 bob = privateKeyWrapper('//Bob');59 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);69 });60 });70 });61 });716272 it('Total issuance does not change', async () => {63 itSub('Total issuance does not change', async ({helper}) => {73 await usingApi(async (api) => {64 const api = helper.api!;74 await skipInflationBlock(api);65 await skipInflationBlock(api);75 await waitNewBlocks(api, 1);66 await waitNewBlocks(api, 1);766777 const totalBefore = (await api.query.balances.totalIssuance()).toBigInt();68 const totalBefore = (await api.query.balances.totalIssuance()).toBigInt();786979 const amount = 1n;80 const transfer = api.tx.balances.transfer(bob.address, amount);70 await helper.balance.transferToSubstrate(alice, bob.address, 1n);8182 const result = getGenericResult(await submitTransactionAsync(alice, transfer));837184 const totalAfter = (await api.query.balances.totalIssuance()).toBigInt();72 const totalAfter = (await api.query.balances.totalIssuance()).toBigInt();857386 expect(result.success).to.be.true;87 expect(totalAfter).to.be.equal(totalBefore);74 expect(totalAfter).to.be.equal(totalBefore);88 });89 });75 });907691 it('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async () => {77 itSub('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async ({helper}) => {92 await usingApi(async (api) => {78 const api = helper.api!;93 await skipInflationBlock(api);79 await skipInflationBlock(api);94 await waitNewBlocks(api, 1);80 await waitNewBlocks(api, 1);958196 const treasuryBalanceBefore: bigint = (await api.query.system.account(TREASURY)).data.free.toBigInt();82 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);97 const aliceBalanceBefore: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();83 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);988499 const amount = 1n;85 const amount = 1n;100 const transfer = api.tx.balances.transfer(bob.address, amount);86 await helper.balance.transferToSubstrate(alice, bob.address, amount);101 const result = getGenericResult(await submitTransactionAsync(alice, transfer));10287103 const treasuryBalanceAfter: bigint = (await api.query.system.account(TREASURY)).data.free.toBigInt();88 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);104 const aliceBalanceAfter: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();89 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);90105 const fee = aliceBalanceBefore - aliceBalanceAfter - amount;91 const fee = aliceBalanceBefore - aliceBalanceAfter - amount;106 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;92 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;10793108 expect(result.success).to.be.true;109 expect(treasuryIncrease).to.be.equal(fee);94 expect(treasuryIncrease).to.be.equal(fee);110 });111 });95 });11296113 it('Treasury balance increased by failed tx fee', async () => {97 itSub('Treasury balance increased by failed tx fee', async ({helper}) => {114 await usingApi(async (api) => {98 const api = helper.api!;115 //await skipInflationBlock(api);116 await waitNewBlocks(api, 1);99 await waitNewBlocks(api, 1);117100118 const treasuryBalanceBefore = (await api.query.system.account(TREASURY)).data.free.toBigInt();101 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);119 const bobBalanceBefore = (await api.query.system.account(bob.address)).data.free.toBigInt();102 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);120103121 const badTx = api.tx.balances.setBalance(alice.address, 0, 0);104 await expect(helper.signTransaction(bob, api.tx.balances.setBalance(alice.address, 0, 0))).to.be.rejected;122 await expect(submitTransactionExpectFailAsync(bob, badTx)).to.be.rejected;123105124 const treasuryBalanceAfter = (await api.query.system.account(TREASURY)).data.free.toBigInt();106 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);125 const bobBalanceAfter = (await api.query.system.account(bob.address)).data.free.toBigInt();107 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);108126 const fee = bobBalanceBefore - bobBalanceAfter;109 const fee = bobBalanceBefore - bobBalanceAfter;127 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;110 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;128111129 expect(treasuryIncrease).to.be.equal(fee);112 expect(treasuryIncrease).to.be.equal(fee);130 });131 });113 });132114133 it('NFT Transactions also send fees to Treasury', async () => {115 itSub('NFT Transactions also send fees to Treasury', async ({helper}) => {134 await usingApi(async (api) => {116 const api = helper.api!;135 await skipInflationBlock(api);117 await skipInflationBlock(api);136 await waitNewBlocks(api, 1);118 await waitNewBlocks(api, 1);137119138 const treasuryBalanceBefore = (await api.query.system.account(TREASURY)).data.free.toBigInt();120 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);139 const aliceBalanceBefore = (await api.query.system.account(alice.address)).data.free.toBigInt();121 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);140122141 await createCollectionExpectSuccess();123 await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});142124143 const treasuryBalanceAfter = (await api.query.system.account(TREASURY)).data.free.toBigInt();125 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);144 const aliceBalanceAfter = (await api.query.system.account(alice.address)).data.free.toBigInt();126 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);145 const fee = aliceBalanceBefore - aliceBalanceAfter;127 const fee = aliceBalanceBefore - aliceBalanceAfter;146 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;128 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;147129148 expect(treasuryIncrease).to.be.equal(fee);130 expect(treasuryIncrease).to.be.equal(fee);149 });150 });131 });151132152 it('Fees are sane', async () => {133 itSub('Fees are sane', async ({helper}) => {153 await usingApi(async (api) => {134 const api = helper.api!;154 await skipInflationBlock(api);135 await skipInflationBlock(api);155 await waitNewBlocks(api, 1);136 await waitNewBlocks(api, 1);156137157 const aliceBalanceBefore: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();138 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);158139159 await createCollectionExpectSuccess();140 await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});160141161 const aliceBalanceAfter: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();142 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);162 const fee = aliceBalanceBefore - aliceBalanceAfter;143 const fee = aliceBalanceBefore - aliceBalanceAfter;163144164 expect(fee / UNIQUE < BigInt(Math.ceil(saneMaximumFee + createCollectionDeposit))).to.be.true;145 expect(fee / UNIQUE < BigInt(Math.ceil(saneMaximumFee + createCollectionDeposit))).to.be.true;165 expect(fee / UNIQUE < BigInt(Math.ceil(saneMinimumFee + createCollectionDeposit))).to.be.true;146 expect(fee / UNIQUE < BigInt(Math.ceil(saneMinimumFee + createCollectionDeposit))).to.be.true;166 });167 });147 });168148169 it('NFT Transfer fee is close to 0.1 Unique', async () => {149 itSub('NFT Transfer fee is close to 0.1 Unique', async ({helper}) => {170 await usingApi(async (api) => {150 const api = helper.api!;171 await skipInflationBlock(api);151 await skipInflationBlock(api);172 await waitNewBlocks(api, 1);152 await waitNewBlocks(api, 1);173153174 const collectionId = await createCollectionExpectSuccess();154 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');175 const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');160 const token = await collection.mintToken(alice, {Substrate: alice.address});176161177 const aliceBalanceBefore: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();162 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);178 await transferExpectSuccess(collectionId, tokenId, alice, bob, 1, 'NFT');163 await token.transfer(alice, {Substrate: bob.address});179 const aliceBalanceAfter: bigint = (await api.query.system.account(alice.address)).data.free.toBigInt();164 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);180165181 const fee = Number(aliceBalanceBefore - aliceBalanceAfter) / Number(UNIQUE);166 const fee = Number(aliceBalanceBefore - aliceBalanceAfter) / Number(UNIQUE);182 const expectedTransferFee = 0.1;167 const expectedTransferFee = 0.1;183 // fee drifts because of NextFeeMultiplier168 // fee drifts because of NextFeeMultiplier184 const tolerance = 0.001;169 const tolerance = 0.001;185170186 expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);171 expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);187 });188 });172 });189190});173});