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
#![deny(warnings)]
#![cfg_attr(test, feature(proc_macro_hygiene))]
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
use codec::{Decode, Encode, EncodeLike};
use frame_support::{
pallet_prelude::DispatchResult,
traits::{Currency, Get, ReservableCurrency},
transactional,
weights::Weight,
PalletId,
};
use frame_system::{ensure_root, pallet_prelude::BlockNumberFor};
use primitives::TruncateFixedPointToInt;
use scale_info::TypeInfo;
use sp_arithmetic::ArithmeticError;
use sp_runtime::{traits::AccountIdConversion, FixedPointNumber};
mod default_weights;
pub use default_weights::WeightInfo;
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
#[pallet::config]
pub trait Config: frame_system::Config {
#[pallet::constant]
type SupplyPalletId: Get<PalletId>;
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
type UnsignedFixedPoint: FixedPointNumber<Inner = BalanceOf<Self>>
+ TruncateFixedPointToInt
+ Encode
+ EncodeLike
+ Decode
+ MaybeSerializeDeserialize
+ TypeInfo
+ MaxEncodedLen;
type Currency: ReservableCurrency<Self::AccountId>;
#[pallet::constant]
type InflationPeriod: Get<BlockNumberFor<Self>>;
type OnInflation: OnInflation<Self::AccountId, Currency = Self::Currency>;
type WeightInfo: WeightInfo;
}
#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
Inflation { total_inflation: BalanceOf<T> },
}
#[pallet::error]
pub enum Error<T> {}
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
if let Err(e) = Self::begin_block(n) {
sp_runtime::print(e);
}
T::WeightInfo::on_initialize()
}
}
#[pallet::storage]
#[pallet::whitelist_storage]
#[pallet::getter(fn start_height)]
pub type StartHeight<T: Config> = StorageValue<_, BlockNumberFor<T>, OptionQuery>;
#[pallet::storage]
#[pallet::getter(fn last_emission)]
pub type LastEmission<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn inflation)]
pub type Inflation<T: Config> = StorageValue<_, T::UnsignedFixedPoint, ValueQuery>;
#[pallet::genesis_config]
#[derive(frame_support::DefaultNoBound)]
pub struct GenesisConfig<T: Config> {
pub initial_supply: BalanceOf<T>,
pub start_height: BlockNumberFor<T>,
pub inflation: T::UnsignedFixedPoint,
}
#[pallet::genesis_build]
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
fn build(&self) {
T::Currency::deposit_creating(&T::SupplyPalletId::get().into_account_truncating(), self.initial_supply);
StartHeight::<T>::put(self.start_height);
Inflation::<T>::put(self.inflation);
}
}
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::set_start_height_and_inflation())]
#[transactional]
pub fn set_start_height_and_inflation(
origin: OriginFor<T>,
start_height: BlockNumberFor<T>,
inflation: T::UnsignedFixedPoint,
) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
StartHeight::<T>::put(start_height);
Inflation::<T>::put(inflation);
Ok(().into())
}
}
}
impl<T: Config> Pallet<T> {
pub fn account_id() -> T::AccountId {
T::SupplyPalletId::get().into_account_truncating()
}
pub(crate) fn begin_block(height: BlockNumberFor<T>) -> DispatchResult {
if let Some(start_height) = <StartHeight<T>>::get().filter(|&start_height| height == start_height) {
let end_height = start_height + T::InflationPeriod::get();
<StartHeight<T>>::put(end_height);
let total_supply = T::Currency::total_issuance();
let total_inflation = <Inflation<T>>::get()
.checked_mul_int(total_supply)
.ok_or(ArithmeticError::Overflow)?;
<LastEmission<T>>::put(total_inflation);
let supply_account_id = Self::account_id();
T::Currency::deposit_creating(&supply_account_id, total_inflation);
T::OnInflation::on_inflation(&supply_account_id, total_inflation);
Self::deposit_event(Event::<T>::Inflation { total_inflation });
}
Ok(())
}
}
pub trait OnInflation<AccountId> {
type Currency: ReservableCurrency<AccountId>;
fn on_inflation(from: &AccountId, amount: <Self::Currency as Currency<AccountId>>::Balance);
}