Skip to content

Commit

Permalink
fix: FeesTreasuryProportion is only applied to substrate based tx only (
Browse files Browse the repository at this point in the history
#3043)

* fix: FeesTreasuryProportion is only applied to substrate based tx only

* refactor: split test into 2 files

* test: test 0% FeesTreasuryProportion

* add substrate based tests

* test: test for different cases

* test: add extra check

* fix: test calculation

* style: fix formatting

* fix: fix calculation for moonriver and moonbeam

* fix: fix calculation

* test: add with tip case

---------

Co-authored-by: Rodrigo Quelhas <[email protected]>
  • Loading branch information
TarekkMA and RomarQ authored Nov 15, 2024
1 parent 4a87b2c commit 2faa570
Show file tree
Hide file tree
Showing 6 changed files with 343 additions and 69 deletions.
12 changes: 8 additions & 4 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,9 @@ where
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
let treasury_perbill =
let treasury_proportion =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_perbill.deconstruct();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = fees.ration(burn_part, treasury_part);
// Balances pallet automatically burns dropped Credits by decreasing
Expand All @@ -378,7 +378,7 @@ where
// handle tip if there is one
if let Some(tip) = fees_then_tips.next() {
// for now we use the same burn/treasury strategy used for regular fees
let (_, to_treasury) = tip.ration(80, 20);
let (_, to_treasury) = tip.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(
to_treasury,
);
Expand All @@ -391,7 +391,11 @@ where
fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
let (_, to_treasury) = amount.ration(80, 20);
let treasury_proportion =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = amount.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
}
}
Expand Down
12 changes: 8 additions & 4 deletions runtime/moonbeam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ where
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
let treasury_perbill =
let treasury_proportion =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_perbill.deconstruct();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = fees.ration(burn_part, treasury_part);
// Balances pallet automatically burns dropped Credits by decreasing
Expand All @@ -366,7 +366,7 @@ where
// handle tip if there is one
if let Some(tip) = fees_then_tips.next() {
// for now we use the same burn/treasury strategy used for regular fees
let (_, to_treasury) = tip.ration(80, 20);
let (_, to_treasury) = tip.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(
to_treasury,
);
Expand All @@ -379,7 +379,11 @@ where
fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
let (_, to_treasury) = amount.ration(80, 20);
let treasury_proportion =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = amount.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
}
}
Expand Down
12 changes: 8 additions & 4 deletions runtime/moonriver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,9 @@ where
mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
) {
if let Some(fees) = fees_then_tips.next() {
let treasury_perbill =
let treasury_proportion =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_perbill.deconstruct();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = fees.ration(burn_part, treasury_part);
// Balances pallet automatically burns dropped Credits by decreasing
Expand All @@ -368,7 +368,7 @@ where
// handle tip if there is one
if let Some(tip) = fees_then_tips.next() {
// for now we use the same burn/treasury strategy used for regular fees
let (_, to_treasury) = tip.ration(80, 20);
let (_, to_treasury) = tip.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(
to_treasury,
);
Expand All @@ -381,7 +381,11 @@ where
fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
// Balances pallet automatically burns dropped Credits by decreasing
// total_supply accordingly
let (_, to_treasury) = amount.ration(80, 20);
let treasury_proportion =
runtime_params::dynamic_params::runtime_config::FeesTreasuryProportion::get();
let treasury_part = treasury_proportion.deconstruct();
let burn_part = Perbill::one().deconstruct() - treasury_part;
let (_, to_treasury) = amount.ration(burn_part, treasury_part);
ResolveTo::<TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(to_treasury);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describeSuite, expect } from "@moonwall/cli";
import { alith } from "@moonwall/util";
import { parameterType, UNIT } from "./test-parameters";

describeSuite({
id: "DTemp02",
title: "Parameters - Pallet Randomness",
foundationMethods: "dev",
testCases: ({ it, context, log }) => {
it({
id: `T01 - PalletRandomness - Deposit - CustomTests`,
title: "Deposit parameter should only be accepted in bounds",
test: async () => {
const MIN = 1n * UNIT;
const MAX = 1000n * UNIT;

// used as an acceptable value
const AVG = (MIN + MAX) / 2n;

const param1 = parameterType(context, "PalletRandomness", "Deposit", MIN - 1n);
try {
await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param1.toU8a()))
.signAsync(alith),
{ allowFailures: false }
);
expect.fail("An extrinsic should not be created, since the parameter is invalid");
} catch (error) {
expect(error.toString().toLowerCase()).to.contain("value out of bounds");
}

const param2 = parameterType(context, "PalletRandomness", "Deposit", MAX + 1n);
try {
await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param2.toU8a()))
.signAsync(alith),
{ allowFailures: false }
);
expect.fail("An extrinsic should not be created, since the parameter is invalid");
} catch (error) {
expect(error.toString().toLowerCase()).to.contain("value out of bounds");
}

const param3 = parameterType(context, "PalletRandomness", "Deposit", AVG);
const res3 = await context.createBlock(
context
.polkadotJs()
.tx.sudo.sudo(context.polkadotJs().tx.parameters.setParameter(param3.toU8a()))
.signAsync(alith),
{ allowFailures: false }
);
expect(
res3.result?.successful,
"An extrinsic should be created, since the parameter is valid"
).to.be.true;
},
});
},
});
Loading

0 comments on commit 2faa570

Please sign in to comment.