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
50 changes: 44 additions & 6 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 @@ -49,6 +49,7 @@ pallet-ranked-collective = { version = "39.0.0", default-features = false }
pallet-preimage = { version = "39.0.0", default-features = false }
pallet-referenda = { version = "39.1.0", default-features = false }
pallet-scheduler = { version = "40.1.0", default-features = false }
pallet-treasury = { version = "38.1.0", default-features = false}
pallet-utility = { version = "39.1.0", default-features = false }
parking_lot = { version = "0.12.1", default-features = false }
pallet-vesting = { version = "39.1.0", default-features = false }
Expand Down
48 changes: 25 additions & 23 deletions pallets/mining-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,42 @@ publish = false
targets = ["x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "wasm32-unknown-unknown"]

[dependencies]
codec = { workspace=true, default-features = false, features = ["derive"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the formatting was still off here even after we did fmt...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did we run taplo as well?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was kinda expecting fmt to also format toml files. But we could just taplo everything.

codec = { workspace = true, default-features = false, features = ["derive"] }
scale-info = { workspace = true, default-features = false, features = ["derive"] }
frame-benchmarking = { optional = true, workspace = true, default-features = false }
frame-support = {workspace = true,default-features = false}
frame-system = {workspace = true, default-features = false}
sp-consensus-pow = {workspace = true,default-features = false}
sp-runtime = {workspace = true,default-features = false}
sp-std = {workspace = true, default-features = false}
frame-support = { workspace = true, default-features = false }
frame-system = { workspace = true, default-features = false }
sp-consensus-pow = { workspace = true, default-features = false }
sp-runtime = { workspace = true, default-features = false }
sp-std = { workspace = true, default-features = false }

log = {workspace = true, default-features = false}
pallet-balances = {workspace = true, default-features = false}
log = { workspace = true, default-features = false }
pallet-balances = { workspace = true, default-features = false }
pallet-treasury = { workspace = true, default-features = false }

[dev-dependencies]
sp-io = {workspace = true, default-features = false}
sp-core = {workspace = true, default-features = false}
sp-io = { workspace = true, default-features = false }
sp-core = { workspace = true, default-features = false }

[features]
default = ["std"]
std = [
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-consensus-pow/std",
"pallet-balances/std",
"sp-runtime/std",
"sp-std/std",
"codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"sp-consensus-pow/std",
"pallet-balances/std",
"pallet-treasury/std",
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-support/try-runtime",
]
129 changes: 96 additions & 33 deletions pallets/mining-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pub mod pallet {
use frame_system::pallet_prelude::*;
use sp_consensus_pow::POW_ENGINE_ID;
use sp_runtime::generic::DigestItem;
use sp_runtime::traits::Saturating;
use sp_runtime::traits::{AccountIdConversion, Saturating};
use sp_runtime::Permill;

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -54,6 +55,14 @@ pub mod pallet {
/// The base block reward given to miners
#[pallet::constant]
type BlockReward: Get<BalanceOf<Self>>;

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

/// The percentage of transaction fees that should go to the Treasury.
#[pallet::constant]
type FeesToTreasuryPermill: Get<Permill>;
}

#[pallet::event]
Expand All @@ -75,6 +84,16 @@ pub mod pallet {
/// Total fees waiting for distribution
total: BalanceOf<T>,
},
/// Rewards were sent to Treasury when no miner was specified
TreasuryRewarded {
/// Total reward (base + fees)
reward: BalanceOf<T>,
},
/// A portion of transaction fees was redirected to the Treasury.
FeesRedirectedToTreasury {
/// The amount of fees sent to the Treasury
amount: BalanceOf<T>,
},
}

#[pallet::hooks]
Expand All @@ -89,41 +108,85 @@ pub mod pallet {
if let Some(miner) = Self::extract_miner_from_digest() {
// Get the block reward
let base_reward = T::BlockReward::get();

let tx_fees = <CollectedFees<T>>::take();
let mut tx_fees = <CollectedFees<T>>::take();

log::info!("💰 Base reward: {:?}", base_reward);
log::info!("💰 Tx_fees: {:?}", tx_fees);

let total_reward = base_reward.saturating_add(tx_fees);

// Create imbalance for block reward
let reward_imbalance = T::Currency::issue(total_reward);

// We could do this in a more sophisticated way with OnUnbalanced<NegativeInbalance>
T::Currency::resolve_creating(&miner, reward_imbalance);

// Emit an event
Self::deposit_event(Event::MinerRewarded {
block: block_number,
miner: miner.clone(),
reward: total_reward,
});

log::info!(
target: "mining-rewards",
"💰 Miner rewarded: {:?}",
total_reward);
let miner_balance = T::Currency::free_balance(&miner);
log::info!(target: "mining-rewards",
"🏦 Miner balance: {:?}",
miner_balance);
log::info!("💰 Original Tx_fees: {:?}", tx_fees);

// Calculate fees for Treasury
let fees_to_treasury_percentage = T::FeesToTreasuryPermill::get();
let fees_for_treasury = fees_to_treasury_percentage.mul_floor(tx_fees);

// Get Treasury account
let treasury_account = T::TreasuryPalletId::get().into_account_truncating();

// Send fees to Treasury if any
if fees_for_treasury > Zero::zero() {
let treasury_imbalance = T::Currency::issue(fees_for_treasury);
T::Currency::resolve_creating(&treasury_account, treasury_imbalance);
Self::deposit_event(Event::FeesRedirectedToTreasury {
amount: fees_for_treasury,
});
log::info!(
target: "mining-rewards",
"💰 Fees sent to Treasury: {:?}",
fees_for_treasury
);
// Subtract fees sent to treasury from the total tx_fees
tx_fees = tx_fees.saturating_sub(fees_for_treasury);
}

let reward_for_miner = base_reward.saturating_add(tx_fees);

// Create imbalance for miner's reward
if reward_for_miner > Zero::zero() {
let miner_reward_imbalance = T::Currency::issue(reward_for_miner);
T::Currency::resolve_creating(&miner, miner_reward_imbalance);

// Emit an event for miner's reward
Self::deposit_event(Event::MinerRewarded {
block: block_number,
miner: miner.clone(),
reward: reward_for_miner, // Actual reward for miner
});

log::info!(
target: "mining-rewards",
"💰 Miner rewarded: {:?}",
reward_for_miner
);
let miner_balance = T::Currency::free_balance(&miner);
log::info!(target: "mining-rewards",
"🏦 Miner balance: {:?}",
miner_balance);
}
} else {
log::info!(
target: "mining-rewards",
"No rewards address provided for block {:?}",
block_number
);
// No miner specified, send all rewards (base + all fees) to Treasury
let base_reward = T::BlockReward::get();
let tx_fees = <CollectedFees<T>>::take();
let total_reward_for_treasury = base_reward.saturating_add(tx_fees);

if total_reward_for_treasury > BalanceOf::<T>::from(0u32) {
// Get Treasury account
let treasury_account = T::TreasuryPalletId::get().into_account_truncating();

// Create imbalance for block reward
let reward_imbalance = T::Currency::issue(total_reward_for_treasury);

// Send rewards to Treasury
T::Currency::resolve_creating(&treasury_account, reward_imbalance);

// Emit an event
Self::deposit_event(Event::TreasuryRewarded {
reward: total_reward_for_treasury,
});

log::info!(
target: "mining-rewards",
"💰 No miner specified, all rewards sent to Treasury: {:?}",
total_reward_for_treasury
);
}
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions pallets/mining-rewards/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use crate as pallet_mining_rewards;
use codec::Encode;
use frame_support::__private::sp_io;
use frame_support::traits::{Everything, Hooks};
use frame_support::{parameter_types, traits::ConstU32};
use frame_support::{parameter_types, traits::ConstU32, PalletId};
use sp_consensus_pow::POW_ENGINE_ID;
use sp_runtime::app_crypto::sp_core;
use sp_runtime::testing::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage, Digest, DigestItem,
BuildStorage, Digest, DigestItem, Permill,
};

// Configure a mock runtime to test the pallet
Expand All @@ -25,9 +25,11 @@ pub type Block = frame_system::mocking::MockBlock<Test>;

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const BlockReward: Balance = 50;
pub const ExistentialDeposit: Balance = 1;
pub const SS58Prefix: u8 = 42;
pub const BlockReward: u128 = 50;
pub const ExistentialDeposit: Balance = 1;
pub const TreasuryPalletId: PalletId = PalletId(*b"py/trsry");
pub FeesToTreasuryPermill: Permill = Permill::from_percent(10);
}

impl frame_system::Config for Test {
Expand Down Expand Up @@ -85,6 +87,8 @@ impl pallet_mining_rewards::Config for Test {
type WeightInfo = ();
type Currency = Balances;
type BlockReward = BlockReward;
type TreasuryPalletId = TreasuryPalletId;
type FeesToTreasuryPermill = FeesToTreasuryPermill;
}

// Configure a default miner account for tests
Expand Down
Loading