Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"pallets/qpow",
"pallets/reversible-transfers",
"pallets/scheduler",
"pallets/treasury",
"pallets/wormhole",
"primitives/consensus/pow",
"primitives/consensus/qpow",
Expand Down Expand Up @@ -184,7 +185,7 @@ pallet-timestamp = { version = "40.0.0", default-features = false }
pallet-transaction-payment = { version = "41.0.0", default-features = false }
pallet-transaction-payment-rpc = { version = "44.0.0", default-features = false }
pallet-transaction-payment-rpc-runtime-api = { version = "41.0.0", default-features = false }
pallet-treasury = { version = "40.0.0", default-features = false }
pallet-treasury = { path = "pallets/treasury", default-features = false }
pallet-utility = { version = "41.0.0", default-features = false }
prometheus-endpoint = { version = "0.17.2", default-features = false, package = "substrate-prometheus-endpoint" }
sc-basic-authorship = { version = "0.50.0", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions pallets/mining-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ frame-benchmarking = { optional = true, workspace = true, default-features = fal
frame-support.workspace = true
frame-system.workspace = true
log.workspace = true
pallet-treasury = { path = "../treasury", default-features = false }
qp-poseidon.workspace = true
qp-wormhole.workspace = true
scale-info = { workspace = true, default-features = false, features = ["derive"] }
Expand All @@ -47,6 +48,7 @@ std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"pallet-treasury/std",
"qp-poseidon/std",
"qp-wormhole/std",
"scale-info/std",
Expand Down
10 changes: 4 additions & 6 deletions pallets/mining-rewards/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ use frame_benchmarking::{account, v2::*, BenchmarkError};
use frame_support::traits::fungible::{Inspect, Mutate};
use frame_system::{pallet_prelude::BlockNumberFor, Pallet as SystemPallet};
use sp_consensus_pow::POW_ENGINE_ID;
use sp_runtime::{
generic::{Digest, DigestItem},
traits::AccountIdConversion,
};
use sp_runtime::generic::{Digest, DigestItem};

#[benchmarks]
mod benchmarks {
use super::*;
use codec::Encode;
use frame_support::traits::{Get, OnFinalize};
use frame_support::traits::OnFinalize;
use pallet_treasury::TreasuryProvider;
use sp_runtime::Saturating;

#[benchmark]
Expand All @@ -37,7 +35,7 @@ mod benchmarks {
);

// Pre-fund Treasury account to ensure it exists
let treasury_account = T::TreasuryPalletId::get().into_account_truncating();
let treasury_account = T::Treasury::account_id();
let ed = T::Currency::minimum_balance();
let _ = T::Currency::mint_into(&treasury_account, ed.saturating_mul(1000u32.into()));
let _ = T::Currency::mint_into(&miner, ed.saturating_mul(1000u32.into()));
Expand Down
24 changes: 9 additions & 15 deletions pallets/mining-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ pub mod pallet {
},
};
use frame_system::pallet_prelude::*;
use pallet_treasury::TreasuryProvider;
use qp_poseidon::PoseidonHasher;
use qp_wormhole::TransferProofRecorder;
use sp_consensus_pow::POW_ENGINE_ID;
use sp_runtime::{
generic::DigestItem,
traits::{AccountIdConversion, Saturating},
Permill,
};
use sp_runtime::{generic::DigestItem, traits::Saturating, Permill};

pub(crate) type BalanceOf<T> =
<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -71,18 +68,13 @@ pub mod pallet {
#[pallet::constant]
type EmissionDivisor: Get<BalanceOf<Self>>;

/// The portion of rewards that goes to treasury
#[pallet::constant]
type TreasuryPortion: Get<Permill>;
/// Provides treasury account and portion (from treasury pallet).
type Treasury: TreasuryProvider<AccountId = Self::AccountId>;

/// The base unit for token amounts (e.g., 1e12 for 12 decimals)
#[pallet::constant]
type Unit: Get<BalanceOf<Self>>;

/// The treasury pallet ID
#[pallet::constant]
type TreasuryPalletId: Get<frame_support::PalletId>;

/// Account ID used as the "from" account when creating transfer proofs for minted tokens
#[pallet::constant]
type MintingAccount: Get<Self::AccountId>;
Expand Down Expand Up @@ -138,8 +130,10 @@ pub mod pallet {
.checked_div(&emission_divisor)
.unwrap_or_else(BalanceOf::<T>::zero);

// Split the reward between treasury and miner
let treasury_reward = T::TreasuryPortion::get().mul_floor(total_reward);
// Split the reward between treasury and miner (portion is 0-100)
let treasury_portion = T::Treasury::portion();
let treasury_reward =
Permill::from_percent(u32::from(treasury_portion)).mul_floor(total_reward);
let miner_reward = total_reward.saturating_sub(treasury_reward);

let tx_fees = <CollectedFees<T>>::take();
Expand Down Expand Up @@ -247,7 +241,7 @@ pub mod pallet {
Self::deposit_event(Event::MinerRewarded { miner: miner.clone(), reward });
},
None => {
let treasury = T::TreasuryPalletId::get().into_account_truncating();
let treasury = T::Treasury::account_id();
let _ = T::Currency::mint_into(&treasury, reward).defensive();

let _ = T::ProofRecorder::record_transfer_proof(
Expand Down
28 changes: 21 additions & 7 deletions pallets/mining-rewards/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ use crate as pallet_mining_rewards;
use frame_support::{
parameter_types,
traits::{ConstU32, Everything, Hooks},
PalletId,
};
use pallet_treasury;
use qp_poseidon::PoseidonHasher;
use sp_consensus_pow::POW_ENGINE_ID;
use sp_runtime::{
app_crypto::sp_core,
testing::H256,
traits::{BlakeTwo256, IdentityLookup},
BuildStorage, DigestItem, Permill,
BuildStorage, DigestItem,
};

// Configure a mock runtime to test the pallet
frame_support::construct_runtime!(
pub enum Test {
System: frame_system,
Balances: pallet_balances,
Treasury: pallet_treasury,
MiningRewards: pallet_mining_rewards,
}
);
Expand All @@ -33,7 +34,6 @@ parameter_types! {
pub const MaxSupply: u128 = 21_000_000 * UNIT;
pub const EmissionDivisor: u128 = 26_280_000;
pub const ExistentialDeposit: Balance = 1;
pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
}

impl frame_system::Config for Test {
Expand Down Expand Up @@ -87,11 +87,15 @@ impl pallet_balances::Config for Test {
}

parameter_types! {
pub const TreasuryPortion: Permill = Permill::from_percent(50); // 50% goes to treasury in tests (matching runtime)
pub const MintingAccount: sp_core::crypto::AccountId32 = sp_core::crypto::AccountId32::new([99u8; 32]);
pub const MintingAccount: sp_core::crypto::AccountId32 =
sp_core::crypto::AccountId32::new([99u8; 32]);
pub const Unit: u128 = UNIT;
}

impl pallet_treasury::Config for Test {
type WeightInfo = ();
}

// Mock proof recorder that does nothing
pub struct MockProofRecorder;
impl qp_wormhole::TransferProofRecorder<sp_core::crypto::AccountId32, u32, u128>
Expand All @@ -116,8 +120,7 @@ impl pallet_mining_rewards::Config for Test {
type WeightInfo = ();
type MaxSupply = MaxSupply;
type EmissionDivisor = EmissionDivisor;
type TreasuryPortion = TreasuryPortion;
type TreasuryPalletId = TreasuryPalletId;
type Treasury = Treasury;
type MintingAccount = MintingAccount;
type Unit = Unit;
}
Expand Down Expand Up @@ -150,6 +153,10 @@ pub fn miner2() -> sp_core::crypto::AccountId32 {
wormhole_address_from_preimage(miner_preimage_2())
}

fn treasury_account() -> sp_core::crypto::AccountId32 {
sp_core::crypto::AccountId32::new([1u8; 32])
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
Expand All @@ -161,6 +168,13 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
.assimilate_storage(&mut t)
.unwrap();

pallet_treasury::GenesisConfig::<Test> {
treasury_account: treasury_account(),
treasury_portion: 50,
}
.assimilate_storage(&mut t)
.unwrap();

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| System::set_block_number(1)); // Start at block 1
ext
Expand Down
Loading
Loading