Skip to content

Commit

Permalink
MD 306 - add pallet treasury (#400)
Browse files Browse the repository at this point in the history
* pallet treasury added & compiling

* 20% of Fees & Tips go to treasury - working

* Test fees and tips and reserves on proposal creation

* slash on rejection

* Approval tests

* Added pallet to flashbox

* prettier

* even prettier

* lint

* missing references in flashbox runtime

* pallet added to flashbox

* api augment

* Transaction payment - added missing fees policy

* fmt

* clippy

* undo fmt

* assignment.rs - doesn't pass fmt check as it is in master

* api augment

* TODO: enable different assets in the treasury

* Added treasury to the benchmarks list
  • Loading branch information
augustocollado authored Feb 7, 2024
1 parent 7b170cd commit a77f1e5
Show file tree
Hide file tree
Showing 29 changed files with 2,171 additions and 670 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pallet-staking = { git = "https://github.com/moondance-labs/polkadot-sdk", branc
pallet-sudo = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-timestamp = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-transaction-payment = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-treasury = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-tx-pause = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
pallet-utility = { git = "https://github.com/moondance-labs/polkadot-sdk", branch = "tanssi-polkadot-v1.3.0", default-features = false }
parity-scale-codec = { version = "3.0.0", default-features = false, features = [ "derive", "max-encoded-len" ] }
Expand Down
1 change: 1 addition & 0 deletions node/src/chain_spec/dancebox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ fn testnet_genesis(
polkadot_xcm: PolkadotXcmConfig::default(),
transaction_payment: Default::default(),
tx_pause: Default::default(),
treasury: Default::default(),
}
}

Expand Down
1 change: 1 addition & 0 deletions node/src/chain_spec/flashbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ fn testnet_genesis(
},
transaction_payment: Default::default(),
tx_pause: Default::default(),
treasury: Default::default(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/collator-assignment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

use dp_collator_assignment::AssignedCollators;
use {
crate::{mock::*, CollatorContainerChain, Event, PendingCollatorContainerChain},
dp_collator_assignment::AssignedCollators,
std::collections::BTreeMap,
};

Expand Down
6 changes: 4 additions & 2 deletions pallets/collator-assignment/src/tests/assign_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Tanssi. If not, see <http://www.gnu.org/licenses/>

use crate::assignment::AssignmentError;
use {
crate::{assignment::Assignment, tests::Test},
crate::{
assignment::{Assignment, AssignmentError},
tests::Test,
},
sp_std::collections::btree_map::BTreeMap,
};

Expand Down
4 changes: 4 additions & 0 deletions runtime/dancebox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pallet-sudo = { workspace = true }
pallet-timestamp = { workspace = true }
pallet-transaction-payment = { workspace = true }
pallet-transaction-payment-rpc-runtime-api = { workspace = true }
pallet-treasury = { workspace = true }
pallet-tx-pause = { workspace = true }
pallet-utility = { workspace = true }
sp-api = { workspace = true }
Expand Down Expand Up @@ -193,6 +194,7 @@ std = [
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
"pallet-treasury/std",
"pallet-tx-pause/std",
"pallet-utility/std",
"pallet-xcm-benchmarks?/std",
Expand Down Expand Up @@ -276,6 +278,7 @@ runtime-benchmarks = [
"pallet-staking/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-tx-pause/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-xcm-benchmarks/runtime-benchmarks",
Expand Down Expand Up @@ -339,6 +342,7 @@ try-runtime = [
"pallet-sudo/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-treasury/try-runtime",
"pallet-tx-pause/try-runtime",
"pallet-utility/try-runtime",
"pallet-xcm/try-runtime",
Expand Down
89 changes: 85 additions & 4 deletions runtime/dancebox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ use {
parameter_types,
traits::{
fungible::{Balanced, Credit, Inspect},
tokens::{PayFromAccount, UnityAssetBalanceConversion},
ConstBool, ConstU128, ConstU32, ConstU64, ConstU8, Contains, EitherOfDiverse,
InsideBoth, InstanceFilter, OffchainWorker, OnFinalize, OnIdle, OnInitialize,
OnRuntimeUpgrade, ValidatorRegistration,
Imbalance, InsideBoth, InstanceFilter, OffchainWorker, OnFinalize, OnIdle,
OnInitialize, OnRuntimeUpgrade, OnUnbalanced, ValidatorRegistration,
},
weights::{
constants::{
Expand All @@ -68,6 +69,7 @@ use {
EnsureRoot,
},
nimbus_primitives::NimbusId,
pallet_balances::NegativeImbalance,
pallet_collator_assignment::{GetRandomnessForNextBlock, RotateCollatorsEveryNSessions},
pallet_invulnerables::InvulnerableRewardDistribution,
pallet_pooled_staking::traits::{IsCandidateEligible, Timer},
Expand All @@ -86,6 +88,7 @@ use {
create_runtime_str, generic, impl_opaque_keys,
traits::{
AccountIdConversion, AccountIdLookup, BlakeTwo256, Block as BlockT, Hash as HashT,
IdentityLookup,
},
transaction_validity::{TransactionSource, TransactionValidity},
AccountId32, ApplyExtrinsicResult,
Expand Down Expand Up @@ -417,15 +420,53 @@ impl pallet_balances::Config for Runtime {
type WeightInfo = pallet_balances::weights::SubstrateWeight<Runtime>;
}

pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<NegativeImbalance<R>> for DealWithFees<R>
where
R: pallet_balances::Config + pallet_treasury::Config,
pallet_treasury::Pallet<R>: OnUnbalanced<NegativeImbalance<R>>,
{
// this seems to be called for substrate-based transactions
fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance<R>>) {
if let Some(fees) = fees_then_tips.next() {
// 80% is burned, 20% goes to the treasury
// Same policy applies for tips as well
let burn_percentage = 80;
let treasury_percentage = 20;

let (_, to_treasury) = fees.ration(burn_percentage, treasury_percentage);
// Balances pallet automatically burns dropped Negative Imbalances by decreasing total_supply accordingly
<pallet_treasury::Pallet<R> as OnUnbalanced<_>>::on_unbalanced(to_treasury);

// handle tip if there is one
if let Some(tip) = fees_then_tips.next() {
let (_, to_treasury) = tip.ration(burn_percentage, treasury_percentage);
<pallet_treasury::Pallet<R> as OnUnbalanced<_>>::on_unbalanced(to_treasury);
}
}
}

// this is called from pallet_evm for Ethereum-based transactions
// (technically, it calls on_unbalanced, which calls this when non-zero)
fn on_nonzero_unbalanced(amount: NegativeImbalance<R>) {
// 80% is burned, 20% goes to the treasury
let burn_percentage = 80;
let treasury_percentage = 20;

let (_, to_treasury) = amount.ration(burn_percentage, treasury_percentage);
<pallet_treasury::Pallet<R> as OnUnbalanced<_>>::on_unbalanced(to_treasury);
}
}

parameter_types! {
pub const TransactionByteFee: Balance = 1;
pub const FeeMultiplier: Multiplier = Multiplier::from_u32(1);
}

impl pallet_transaction_payment::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
// This will burn the fees
type OnChargeTransaction = CurrencyAdapter<Balances, ()>;
// This will burn 80% from fees & tips and deposit the remainder into the treasury
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees<Runtime>>;
type OperationalFeeMultiplier = ConstU8<5>;
type WeightToFee = WeightToFee;
type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
Expand Down Expand Up @@ -1392,6 +1433,42 @@ impl pallet_identity::Config for Runtime {
type WeightInfo = pallet_identity::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub const TreasuryId: PalletId = PalletId(*b"tns/tsry");
pub const ProposalBond: Permill = Permill::from_percent(5);
pub TreasuryAccount: AccountId = Treasury::account_id();
}

impl pallet_treasury::Config for Runtime {
type PalletId = TreasuryId;
type Currency = Balances;

type ApproveOrigin = EnsureRoot<AccountId>;
type RejectOrigin = EnsureRoot<AccountId>;
type RuntimeEvent = RuntimeEvent;
// If proposal gets rejected, bond goes to treasury
type OnSlash = Treasury;
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ConstU128<{ 1 * currency::DANCE * currency::SUPPLY_FACTOR }>;
type SpendPeriod = ConstU32<{ 6 * DAYS }>;
type Burn = ();
type BurnDestination = ();
type MaxApprovals = ConstU32<100>;
type WeightInfo = pallet_treasury::weights::SubstrateWeight<Runtime>;
type SpendFunds = ();
type ProposalBondMaximum = ();
type SpendOrigin = frame_support::traits::NeverEnsureOrigin<Balance>; // Same as Polkadot
type AssetKind = ();
type Beneficiary = AccountId;
type BeneficiaryLookup = IdentityLookup<AccountId>;
type Paymaster = PayFromAccount<Balances, TreasuryAccount>;
// TODO: implement pallet-asset-rate to allow the treasury to spend other assets
type BalanceConverter = UnityAssetBalanceConversion;
type PayoutPeriod = ConstU32<0>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub enum Runtime
Expand Down Expand Up @@ -1434,6 +1511,9 @@ construct_runtime!(
// InflationRewards must be after Session and AuthorInherent
InflationRewards: pallet_inflation_rewards = 35,

// Treasury stuff.
Treasury: pallet_treasury::{Pallet, Storage, Config<T>, Event<T>, Call} = 40,

//XCM
XcmpQueue: cumulus_pallet_xcmp_queue::{Pallet, Call, Storage, Event<T>} = 50,
CumulusXcm: cumulus_pallet_xcm::{Pallet, Event<T>, Origin} = 51,
Expand All @@ -1459,6 +1539,7 @@ mod benches {
[pallet_sudo, Sudo]
[pallet_proxy, Proxy]
[pallet_utility, Utility]
[pallet_treasury, Treasury]
[pallet_tx_pause, TxPause]
[pallet_balances, Balances]
[pallet_identity, Identity]
Expand Down
11 changes: 6 additions & 5 deletions runtime/dancebox/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,12 @@ impl pallet_asset_rate::Config for Runtime {
type BenchmarkHelper = ForeignAssetBenchmarkHelper;
}

use crate::ForeignAssets;
use sp_runtime::{traits::CheckedDiv, FixedPointNumber};
use staging_xcm_builder::FungiblesAdapter;
use staging_xcm_builder::NoChecking;
use staging_xcm_executor::traits::JustTry;
use {
crate::ForeignAssets,
sp_runtime::{traits::CheckedDiv, FixedPointNumber},
staging_xcm_builder::{FungiblesAdapter, NoChecking},
staging_xcm_executor::traits::JustTry,
};

/// Means for transacting foreign assets from different global consensus.
pub type ForeignFungiblesTransactor = FungiblesAdapter<
Expand Down
4 changes: 4 additions & 0 deletions runtime/flashbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pallet-sudo = { workspace = true }
pallet-timestamp = { workspace = true }
pallet-transaction-payment = { workspace = true }
pallet-transaction-payment-rpc-runtime-api = { workspace = true }
pallet-treasury = { workspace = true }
pallet-tx-pause = { workspace = true }
pallet-utility = { workspace = true }
sp-api = { workspace = true }
Expand Down Expand Up @@ -160,6 +161,7 @@ std = [
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
"pallet-treasury/std",
"pallet-tx-pause/std",
"pallet-utility/std",
"parachain-info/std",
Expand Down Expand Up @@ -225,6 +227,7 @@ runtime-benchmarks = [
"pallet-services-payment/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-tx-pause/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"polkadot-parachain-primitives/runtime-benchmarks",
Expand Down Expand Up @@ -266,6 +269,7 @@ try-runtime = [
"pallet-sudo/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-treasury/try-runtime",
"pallet-tx-pause/try-runtime",
"pallet-utility/try-runtime",
"parachain-info/try-runtime",
Expand Down
Loading

0 comments on commit a77f1e5

Please sign in to comment.