difftreelog
fix evm fees burn & test (#1079)
in: master
* fix evm fees burn & test * fix tips handling
3 files changed
js-packages/tests/creditFeesToTreasury.seqtest.tsdiffbeforeafterboth1// 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 type {IKeyringPair} from '@polkadot/types/types';18import {ApiPromise} from '@polkadot/api';19import {usingPlaygrounds, expect, itSub} from '@unique/test-utils/util.js';20import type {u32} from '@polkadot/types-codec';2122const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z';23const saneMinimumFee = 0.05;24const saneMaximumFee = 0.5;25const createCollectionDeposit = 100;2627// Skip the inflation block pauses if the block is close to inflation block28// until the inflation happens29/*eslint no-async-promise-executor: "off"*/30function skipInflationBlock(api: ApiPromise): Promise<void> {31 const promise = new Promise<void>(async (resolve) => {32 const inflationBlockInterval = api.consts.inflation.inflationBlockInterval as u32;33 const blockInterval = inflationBlockInterval.toNumber();34 const unsubscribe = await api.rpc.chain.subscribeNewHeads(head => {35 const currentBlock = head.number.toNumber();36 if(currentBlock % blockInterval < blockInterval - (blockInterval / 5)) {37 unsubscribe();38 resolve();39 } else {40 console.log(`Skipping inflation block, current block: ${currentBlock}`);41 }42 });43 });4445 return promise;46}4748describe('integration test: Fees must be credited to Treasury:', () => {49 let alice: IKeyringPair;50 let bob: IKeyringPair;5152 before(async () => {53 await usingPlaygrounds(async (helper, privateKey) => {54 const donor = await privateKey({url: import.meta.url});55 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);56 });57 });5859 itSub('Total issuance does not change', async ({helper}) => {60 const api = helper.getApi();61 await skipInflationBlock(api);62 await helper.wait.newBlocks(1);6364 const totalBefore = (await helper.callRpc('api.query.balances.totalIssuance', [])).toBigInt();6566 await helper.balance.transferToSubstrate(alice, bob.address, 1n);6768 const totalAfter = (await helper.callRpc('api.query.balances.totalIssuance', [])).toBigInt();6970 expect(totalAfter).to.be.equal(totalBefore);71 });7273 itSub('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async ({helper}) => {74 await skipInflationBlock(helper.getApi());75 await helper.wait.newBlocks(1);7677 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);78 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);7980 const amount = 1n;81 await helper.balance.transferToSubstrate(alice, bob.address, amount);8283 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);84 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);8586 const fee = aliceBalanceBefore - aliceBalanceAfter - amount;87 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;8889 expect(treasuryIncrease).to.be.equal(fee);90 });9192 itSub('Treasury balance increased by failed tx fee', async ({helper}) => {93 const api = helper.getApi();94 await helper.wait.newBlocks(1);9596 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);97 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);9899 await expect(helper.signTransaction(bob, api.tx.balances.forceSetBalance(alice.address, 0))).to.be.rejected;100101 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);102 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);103104 const fee = bobBalanceBefore - bobBalanceAfter;105 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;106107 expect(treasuryIncrease).to.be.equal(fee);108 });109110 itSub('NFT Transactions also send fees to Treasury', async ({helper}) => {111 await skipInflationBlock(helper.getApi());112 await helper.wait.newBlocks(1);113114 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);115 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);116117 await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});118119 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);120 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);121 const fee = aliceBalanceBefore - aliceBalanceAfter;122 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;123124 expect(treasuryIncrease).to.be.equal(fee);125 });126127 itSub('Fees are sane', async ({helper}) => {128 const unique = helper.balance.getOneTokenNominal();129 await skipInflationBlock(helper.getApi());130 await helper.wait.newBlocks(1);131132 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);133134 await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});135136 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);137 const fee = aliceBalanceBefore - aliceBalanceAfter;138139 expect(fee / unique < BigInt(Math.ceil(saneMaximumFee + createCollectionDeposit))).to.be.true;140 expect(fee / unique < BigInt(Math.ceil(saneMinimumFee + createCollectionDeposit))).to.be.true;141 });142143 itSub('NFT Transfer fee is close to 0.1 Unique', async ({helper}) => {144 await skipInflationBlock(helper.getApi());145 await helper.wait.newBlocks(1);146147 const collection = await helper.nft.mintCollection(alice, {148 name: 'test',149 description: 'test',150 tokenPrefix: 'test',151 });152 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');153 const token = await collection.mintToken(alice, {Substrate: alice.address});154155 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);156 await token.transfer(alice, {Substrate: bob.address});157 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);158159 const fee = Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal());160 const expectedTransferFee = 0.1;161 // fee drifts because of NextFeeMultiplier162 const tolerance = 0.001;163164 expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);165 });166});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 type {IKeyringPair} from '@polkadot/types/types';18import {ApiPromise} from '@polkadot/api';19import {usingPlaygrounds, expect, itSub} from '@unique/test-utils/util.js';20import type {u32} from '@polkadot/types-codec';21import { itEth } from '@unique/test-utils/eth/util.js';22import { ITransactionResult } from '@unique-nft/playgrounds/types';2324const TREASURY = '5EYCAe5ijiYfyeZ2JJCGq56LmPyNRAKzpG4QkoQkkQNB5e6Z';25const saneMinimumFee = 0.05;26const saneMaximumFee = 0.5;27const createCollectionDeposit = 100;2829// Skip the inflation block pauses if the block is close to inflation block30// until the inflation happens31/*eslint no-async-promise-executor: "off"*/32function skipInflationBlock(api: ApiPromise): Promise<void> {33 const promise = new Promise<void>(async (resolve) => {34 const inflationBlockInterval = api.consts.inflation.inflationBlockInterval as u32;35 const blockInterval = inflationBlockInterval.toNumber();36 const unsubscribe = await api.rpc.chain.subscribeNewHeads(head => {37 const currentBlock = head.number.toNumber();38 if(currentBlock % blockInterval < blockInterval - (blockInterval / 5)) {39 unsubscribe();40 resolve();41 } else {42 console.log(`Skipping inflation block, current block: ${currentBlock}`);43 }44 });45 });4647 return promise;48}4950describe('integration test: Fees must be credited to Treasury:', () => {51 let alice: IKeyringPair;52 let bob: IKeyringPair;5354 before(async () => {55 await usingPlaygrounds(async (helper, privateKey) => {56 const donor = await privateKey({url: import.meta.url});57 [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);58 });59 });6061 itSub('Total issuance does not change', async ({helper}) => {62 const api = helper.getApi();63 await skipInflationBlock(api);64 await helper.wait.newBlocks(1);6566 const totalBefore = (await helper.callRpc('api.query.balances.totalIssuance', [])).toBigInt();6768 await helper.balance.transferToSubstrate(alice, bob.address, 1n);6970 const totalAfter = (await helper.callRpc('api.query.balances.totalIssuance', [])).toBigInt();7172 expect(totalAfter).to.be.equal(totalBefore);73 });7475 itSub('Sender balance decreased by fee+sent amount, Treasury balance increased by fee', async ({helper}) => {76 await skipInflationBlock(helper.getApi());77 await helper.wait.newBlocks(1);7879 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);80 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);8182 const amount = 1n;83 await helper.balance.transferToSubstrate(alice, bob.address, amount);8485 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);86 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);8788 const fee = aliceBalanceBefore - aliceBalanceAfter - amount;89 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;9091 expect(treasuryIncrease).to.be.equal(fee);92 });9394 itSub('Treasury balance increased by failed tx fee', async ({helper}) => {95 const api = helper.getApi();96 await helper.wait.newBlocks(1);9798 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);99 const bobBalanceBefore = await helper.balance.getSubstrate(bob.address);100101 await expect(helper.signTransaction(bob, api.tx.balances.forceSetBalance(alice.address, 0))).to.be.rejected;102103 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);104 const bobBalanceAfter = await helper.balance.getSubstrate(bob.address);105106 const fee = bobBalanceBefore - bobBalanceAfter;107 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;108109 expect(treasuryIncrease).to.be.equal(fee);110 });111112 itSub('NFT Transactions also send fees to Treasury', async ({helper}) => {113 await skipInflationBlock(helper.getApi());114 await helper.wait.newBlocks(1);115116 const treasuryBalanceBefore = await helper.balance.getSubstrate(TREASURY);117 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);118119 await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});120121 const treasuryBalanceAfter = await helper.balance.getSubstrate(TREASURY);122 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);123 const fee = aliceBalanceBefore - aliceBalanceAfter;124 const treasuryIncrease = treasuryBalanceAfter - treasuryBalanceBefore;125126 expect(treasuryIncrease).to.be.equal(fee);127 });128129 itSub('Fees are sane', async ({helper}) => {130 const unique = helper.balance.getOneTokenNominal();131 await skipInflationBlock(helper.getApi());132 await helper.wait.newBlocks(1);133134 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);135136 await helper.nft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'});137138 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);139 const fee = aliceBalanceBefore - aliceBalanceAfter;140141 expect(fee / unique < BigInt(Math.ceil(saneMaximumFee + createCollectionDeposit))).to.be.true;142 expect(fee / unique < BigInt(Math.ceil(saneMinimumFee + createCollectionDeposit))).to.be.true;143 });144145 itSub('NFT Transfer fee is close to 0.1 Unique', async ({helper}) => {146 await skipInflationBlock(helper.getApi());147 await helper.wait.newBlocks(1);148149 const collection = await helper.nft.mintCollection(alice, {150 name: 'test',151 description: 'test',152 tokenPrefix: 'test',153 });154 // const tokenId = await createItemExpectSuccess(alice, collectionId, 'NFT');155 const token = await collection.mintToken(alice, {Substrate: alice.address});156157 const aliceBalanceBefore = await helper.balance.getSubstrate(alice.address);158 await token.transfer(alice, {Substrate: bob.address});159 const aliceBalanceAfter = await helper.balance.getSubstrate(alice.address);160161 const fee = Number(aliceBalanceBefore - aliceBalanceAfter) / Number(helper.balance.getOneTokenNominal());162 const expectedTransferFee = 0.1;163 // fee drifts because of NextFeeMultiplier164 const tolerance = 0.001;165166 expect(Math.abs(fee - expectedTransferFee)).to.be.lessThan(tolerance);167 });168 169 itEth('Evm Transactions send fees to Treasury', async ({helper}) => {170 const value = helper.balance.getOneTokenNominal();171 const gasPrice = await helper.getWeb3().eth.getGasPrice();172 let result = null;173 174 const lambda = async () => {175 result = await helper.executeExtrinsic(alice, 'api.tx.evm.call', [176 helper.address.substrateToEth(alice.address),177 helper.address.substrateToEth(bob.address),178 '0x',179 value,180 25_000,181 gasPrice,182 null,183 null,184 [],185 ]);186 };187 188 const totalPaid = await helper.arrange.calculcateFee({ Substrate: alice.address }, lambda);189 const evmFees = totalPaid - value;190 191 const treasuryDepoosited = (result as unknown as ITransactionResult).result.events192 .filter(({ event: { method, section } }) => section == 'treasury' && method == 'Deposit')193 .map(({ event: { data } }) => data[0].toJSON());194 195 const deposit = BigInt(treasuryDepoosited[0]);196 197 expect(deposit).to.be.equal(evmFees);198 });199 200});pallets/evm-transaction-payment/src/lib.rsdiffbeforeafterboth--- a/pallets/evm-transaction-payment/src/lib.rs
+++ b/pallets/evm-transaction-payment/src/lib.rs
@@ -287,8 +287,7 @@
}
fn pay_priority_fee(tip: Self::LiquidityInfo) {
- <pallet_evm::EVMCurrencyAdapter<C, OU> as OnChargeEVMTransaction<T>>::pay_priority_fee(
- tip.0,
- )
+ let Some(imbalance) = tip.0 else { return };
+ OU::on_unbalanced(imbalance)
}
}
runtime/common/mod.rsdiffbeforeafterboth--- a/runtime/common/mod.rs
+++ b/runtime/common/mod.rs
@@ -107,21 +107,7 @@
type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;
-pub struct DealWithFees;
-impl OnUnbalanced<NegativeImbalance> for DealWithFees {
- fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance>) {
- if let Some(fees) = fees_then_tips.next() {
- // for fees, 100% to treasury
- let mut split = fees.ration(100, 0);
- if let Some(tips) = fees_then_tips.next() {
- // for tips, if any, 100% to treasury
- tips.ration_merge_into(100, 0, &mut split);
- }
- Treasury::on_unbalanced(split.0);
- // Author::on_unbalanced(split.1);
- }
- }
-}
+pub(crate) type DealWithFees = Treasury;
pub struct RelayChainBlockNumberProvider<T>(sp_std::marker::PhantomData<T>);