-
Notifications
You must be signed in to change notification settings - Fork 382
/
lib.rs
347 lines (300 loc) · 12.2 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// This file is part of Astar.
// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later
// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.
//! # Block Reward Distribution Pallet
//!
//! - [`Config`]
//!
//! ## Overview
//!
//! Pallet that implements block reward issuance and distribution mechanics.
//!
//! After issuing a block reward, pallet will calculate how to distribute the reward
//! based on configurable parameters and chain state.
//!
//! Major on-chain factors which can influence reward distribution are total issuance and total value locked by dapps staking.
//!
//! ## Interface
//!
//! ### Dispatchable Function
//!
//! - `set_configuration` - used to change reward distribution configuration parameters
//!
//! ### Other
//!
//! - `on_timestamp_set` - This pallet implements the `OnTimestampSet` trait to handle block production.
//! Note: We assume that it's impossible to set timestamp two times in a block.
//!
//! ## Usage
//!
//! 1. Pallet should be set as a handler of `OnTimestampSet`.
//! 2. `DappsStakingTvlProvider` handler should be defined as an impl of `TvlProvider` trait. For example:
//! ```nocompile
//! pub struct TvlProvider();
//! impl Get<Balance> for TvlProvider {
//! fn tvl() -> Balance {
//! DappsStaking::total_locked_value()
//! }
//! }
//! ```
//! 3. `BeneficiaryPayout` handler should be defined as an impl of `BeneficiaryPayout` trait. For example:
//! ```nocompile
//! pub struct BeneficiaryPayout();
//! impl BeneficiaryPayout<NegativeImbalanceOf<T>> for BeneficiaryPayout {
//!
//! fn treasury(reward: NegativeImbalanceOf<T>) {
//! Balances::resolve_creating(&TREASURY_POT.into_account(), reward);
//! }
//!
//! fn collators(reward: NegativeImbalanceOf<T>) {
//! Balances::resolve_creating(&COLLATOR_POT.into_account(), reward);
//! }
//!
//! fn dapps_staking(stakers: NegativeImbalanceOf<T>, dapps: NegativeImbalanceOf<T>) {
//! DappsStaking::rewards(stakers, dapps);
//! }
//! }
//! ```
//! 4. Set `RewardAmount` to desired block reward value in native currency.
//!
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
use astar_primitives::Balance;
use frame_support::pallet_prelude::*;
use frame_support::{
log,
traits::{Currency, Get, Imbalance, OnTimestampSet},
};
use frame_system::{ensure_root, pallet_prelude::*};
use sp_runtime::{
traits::{CheckedAdd, Zero},
Perbill,
};
use sp_std::vec;
#[cfg(any(feature = "runtime-benchmarks"))]
pub mod benchmarking;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
pub mod weights;
pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
#[pallet::pallet]
pub struct Pallet<T>(PhantomData<T>);
// Negative imbalance type of this pallet.
pub(crate) type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
#[pallet::config]
pub trait Config: frame_system::Config {
/// The currency trait.
type Currency: Currency<Self::AccountId, Balance = Balance>;
/// Provides information about how much value is locked by dapps staking
type DappsStakingTvlProvider: Get<Balance>;
/// Used to payout rewards
type BeneficiaryPayout: BeneficiaryPayout<NegativeImbalanceOf<Self>>;
/// The amount of issuance for each block.
#[pallet::constant]
type RewardAmount: Get<Balance>;
/// The overarching event type.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
#[pallet::storage]
#[pallet::getter(fn reward_config)]
pub type RewardDistributionConfigStorage<T: Config> =
StorageValue<_, RewardDistributionConfig, ValueQuery>;
#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// Distribution configuration has been updated.
DistributionConfigurationChanged(RewardDistributionConfig),
}
#[pallet::error]
pub enum Error<T> {
/// Sum of all rations must be one whole (100%)
InvalidDistributionConfiguration,
}
#[pallet::genesis_config]
#[cfg_attr(feature = "std", derive(Default))]
pub struct GenesisConfig {
pub reward_config: RewardDistributionConfig,
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
assert!(self.reward_config.is_consistent());
RewardDistributionConfigStorage::<T>::put(self.reward_config.clone())
}
}
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Sets the reward distribution configuration parameters which will be used from next block reward distribution.
///
/// It is mandatory that all components of configuration sum up to one whole (**100%**),
/// otherwise an error `InvalidDistributionConfiguration` will be raised.
///
/// - `reward_distro_params` - reward distribution params
///
/// Emits `DistributionConfigurationChanged` with config embeded into event itself.
///
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::set_configuration())]
pub fn set_configuration(
origin: OriginFor<T>,
reward_distro_params: RewardDistributionConfig,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
ensure!(
reward_distro_params.is_consistent(),
Error::<T>::InvalidDistributionConfiguration
);
RewardDistributionConfigStorage::<T>::put(reward_distro_params.clone());
Self::deposit_event(Event::<T>::DistributionConfigurationChanged(
reward_distro_params,
));
Ok(().into())
}
}
impl<Moment, T: Config> OnTimestampSet<Moment> for Pallet<T> {
fn on_timestamp_set(_moment: Moment) {
let inflation = T::Currency::issue(T::RewardAmount::get());
Self::distribute_rewards(inflation);
}
}
impl<T: Config> Pallet<T> {
/// Distribute reward between beneficiaries.
///
/// # Arguments
/// * `reward` - reward that will be split and distributed
///
fn distribute_rewards(block_reward: NegativeImbalanceOf<T>) {
let distro_params = Self::reward_config();
// Pre-calculate balance which will be deposited for each beneficiary
let base_staker_balance = distro_params.base_staker_percent * block_reward.peek();
let dapps_balance = distro_params.dapps_percent * block_reward.peek();
let collator_balance = distro_params.collators_percent * block_reward.peek();
// This is part that's distributed between stakers and treasury
let adjustable_balance = distro_params.adjustable_percent * block_reward.peek();
// Calculate total staker and treasury reward balance
let adjustable_staker_part = if distro_params.ideal_dapps_staking_tvl.is_zero() {
adjustable_balance
} else {
Self::tvl_percentage() / distro_params.ideal_dapps_staking_tvl * adjustable_balance
};
let total_staker_balance = base_staker_balance + adjustable_staker_part;
// Prepare imbalances
let (dapps_imbalance, remainder) = block_reward.split(dapps_balance);
let (stakers_imbalance, remainder) = remainder.split(total_staker_balance);
let (collator_imbalance, treasury_imbalance) = remainder.split(collator_balance);
// Payout beneficiaries
T::BeneficiaryPayout::treasury(treasury_imbalance);
T::BeneficiaryPayout::collators(collator_imbalance);
T::BeneficiaryPayout::dapps_staking(stakers_imbalance, dapps_imbalance);
}
/// Provides TVL as percentage of total issuance
fn tvl_percentage() -> Perbill {
let total_issuance = T::Currency::total_issuance();
if total_issuance.is_zero() {
log::warn!("Total issuance is zero - this should be impossible.");
Zero::zero()
} else {
Perbill::from_rational(T::DappsStakingTvlProvider::get(), total_issuance)
}
}
}
}
/// List of configuration parameters used to calculate reward distribution portions for all the beneficiaries.
///
/// Note that if `ideal_dapps_staking_tvl` is set to `Zero`, entire `adjustable_percent` goes to the stakers.
///
#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct RewardDistributionConfig {
/// Base percentage of reward that goes to treasury
#[codec(compact)]
pub base_treasury_percent: Perbill,
/// Base percentage of reward that goes to stakers
#[codec(compact)]
pub base_staker_percent: Perbill,
/// Percentage of rewards that goes to dApps
#[codec(compact)]
pub dapps_percent: Perbill,
/// Percentage of reward that goes to collators
#[codec(compact)]
pub collators_percent: Perbill,
/// Adjustable reward percentage that either goes to treasury or to stakers
#[codec(compact)]
pub adjustable_percent: Perbill,
/// Target dapps-staking TVL percentage at which adjustable inflation towards stakers becomes saturated
#[codec(compact)]
pub ideal_dapps_staking_tvl: Perbill,
}
impl Default for RewardDistributionConfig {
/// `default` values based on configuration at the time of writing this code.
/// Should be overriden by desired params.
fn default() -> Self {
RewardDistributionConfig {
base_treasury_percent: Perbill::from_percent(40),
base_staker_percent: Perbill::from_percent(25),
dapps_percent: Perbill::from_percent(25),
collators_percent: Perbill::from_percent(10),
adjustable_percent: Zero::zero(),
ideal_dapps_staking_tvl: Zero::zero(),
}
}
}
impl RewardDistributionConfig {
/// `true` if sum of all percentages is `one whole`, `false` otherwise.
pub fn is_consistent(&self) -> bool {
// TODO: perhaps this can be writen in a more cleaner way?
// experimental-only `try_reduce` could be used but it's not available
// https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.try_reduce
let variables = vec![
&self.base_treasury_percent,
&self.base_staker_percent,
&self.dapps_percent,
&self.collators_percent,
&self.adjustable_percent,
];
let mut accumulator = Perbill::zero();
for config_param in variables {
let result = accumulator.checked_add(config_param);
if let Some(mid_result) = result {
accumulator = mid_result;
} else {
return false;
}
}
Perbill::one() == accumulator
}
}
/// Defines functions used to payout the beneficiaries of block rewards
pub trait BeneficiaryPayout<Imbalance> {
/// Payout reward to the treasury
fn treasury(reward: Imbalance);
/// Payout reward to the collators
fn collators(reward: Imbalance);
/// Payout reward to dapps staking
///
/// # Arguments
///
/// * `stakers` - reward that goes towards staker reward pot
/// * `dapps` - reward that goes towards dapps reward pot
///
fn dapps_staking(stakers: Imbalance, dapps: Imbalance);
}