pallet_nomination_pools/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Nomination Pools for Staking Delegation
19//!
20//! A pallet that allows members to delegate their stake to nominating pools. A nomination pool acts
21//! as nominator and nominates validators on the members' behalf.
22//!
23//! # Index
24//!
25//! * [Key terms](#key-terms)
26//! * [Usage](#usage)
27//! * [Implementor's Guide](#implementors-guide)
28//! * [Design](#design)
29//!
30//! ## Key Terms
31//!
32//!  * pool id: A unique identifier of each pool. Set to u32.
33//!  * bonded pool: Tracks the distribution of actively staked funds. See [`BondedPool`] and
34//! [`BondedPoolInner`].
35//! * reward pool: Tracks rewards earned by actively staked funds. See [`RewardPool`] and
36//!   [`RewardPools`].
37//! * unbonding sub pools: Collection of pools at different phases of the unbonding lifecycle. See
38//!   [`SubPools`] and [`SubPoolsStorage`].
39//! * members: Accounts that are members of pools. See [`PoolMember`] and [`PoolMembers`].
40//! * roles: Administrative roles of each pool, capable of controlling nomination, and the state of
41//!   the pool.
42//! * point: A unit of measure for a members portion of a pool's funds. Points initially have a
43//!   ratio of 1 (as set by `POINTS_TO_BALANCE_INIT_RATIO`) to balance, but as slashing happens,
44//!   this can change.
45//! * kick: The act of a pool administrator forcibly ejecting a member.
46//! * bonded account: A key-less account id derived from the pool id that acts as the bonded
47//!   account. This account registers itself as a nominator in the staking system, and follows
48//!   exactly the same rules and conditions as a normal staker. Its bond increases or decreases as
49//!   members join, it can `nominate` or `chill`, and might not even earn staking rewards if it is
50//!   not nominating proper validators.
51//! * reward account: A similar key-less account, that is set as the `Payee` account for the bonded
52//!   account for all staking rewards.
53//! * change rate: The rate at which pool commission can be changed. A change rate consists of a
54//!   `max_increase` and `min_delay`, dictating the maximum percentage increase that can be applied
55//!   to the commission per number of blocks.
56//! * throttle: An attempted commission increase is throttled if the attempted change falls outside
57//!   the change rate bounds.
58//!
59//! ## Usage
60//!
61//! ### Join
62//!
63//! An account can stake funds with a nomination pool by calling [`Call::join`].
64//!
65//! ### Claim rewards
66//!
67//! After joining a pool, a member can claim rewards by calling [`Call::claim_payout`].
68//!
69//! A pool member can also set a `ClaimPermission` with [`Call::set_claim_permission`], to allow
70//! other members to permissionlessly bond or withdraw their rewards by calling
71//! [`Call::bond_extra_other`] or [`Call::claim_payout_other`] respectively.
72//!
73//! For design docs see the [reward pool](#reward-pool) section.
74//!
75//! ### Leave
76//!
77//! In order to leave, a member must take two steps.
78//!
79//! First, they must call [`Call::unbond`]. The unbond extrinsic will start the unbonding process by
80//! unbonding all or a portion of the members funds.
81//!
82//! > A member can have up to [`Config::MaxUnbonding`] distinct active unbonding requests.
83//!
84//! Second, once [`sp_staking::StakingInterface::bonding_duration`] eras have passed, the member can
85//! call [`Call::withdraw_unbonded`] to withdraw any funds that are free.
86//!
87//! For design docs see the [bonded pool](#bonded-pool) and [unbonding sub
88//! pools](#unbonding-sub-pools) sections.
89//!
90//! ### Slashes
91//!
92//! Slashes are distributed evenly across the bonded pool and the unbonding pools from slash era+1
93//! through the slash apply era. Thus, any member who either
94//!
95//! 1. unbonded, or
96//! 2. was actively bonded
97//
98//! in the aforementioned range of eras will be affected by the slash. A member is slashed pro-rata
99//! based on its stake relative to the total slash amount.
100//!
101//! Slashing does not change any single member's balance. Instead, the slash will only reduce the
102//! balance associated with a particular pool. But, we never change the total *points* of a pool
103//! because of slashing. Therefore, when a slash happens, the ratio of points to balance changes in
104//! a pool. In other words, the value of one point, which is initially 1-to-1 against a unit of
105//! balance, is now less than one balance because of the slash.
106//!
107//! ### Administration
108//!
109//! A pool can be created with the [`Call::create`] call. Once created, the pools nominator or root
110//! user must call [`Call::nominate`] to start nominating. [`Call::nominate`] can be called at
111//! anytime to update validator selection.
112//!
113//! Similar to [`Call::nominate`], [`Call::chill`] will chill to pool in the staking system, and
114//! [`Call::pool_withdraw_unbonded`] will withdraw any unbonding chunks of the pool bonded account.
115//! The latter call is permissionless and can be called by anyone at any time.
116//!
117//! To help facilitate pool administration the pool has one of three states (see [`PoolState`]):
118//!
119//! * Open: Anyone can join the pool and no members can be permissionlessly removed.
120//! * Blocked: No members can join and some admin roles can kick members. Kicking is not instant,
121//!   and follows the same process of `unbond` and then `withdraw_unbonded`. In other words,
122//!   administrators can permissionlessly unbond other members.
123//! * Destroying: No members can join and all members can be permissionlessly removed with
124//!   [`Call::unbond`] and [`Call::withdraw_unbonded`]. Once a pool is in destroying state, it
125//!   cannot be reverted to another state.
126//!
127//! A pool has 4 administrative roles (see [`PoolRoles`]):
128//!
129//! * Depositor: creates the pool and is the initial member. They can only leave the pool once all
130//!   other members have left. Once they fully withdraw their funds, the pool is destroyed.
131//! * Nominator: can select which validators the pool nominates.
132//! * Bouncer: can change the pools state and kick members if the pool is blocked.
133//! * Root: can change the nominator, bouncer, or itself, manage and claim commission, and can
134//!   perform any of the actions the nominator or bouncer can.
135//!
136//! ### Commission
137//!
138//! A pool can optionally have a commission configuration, via the `root` role, set with
139//! [`Call::set_commission`] and claimed with [`Call::claim_commission`]. A payee account must be
140//! supplied with the desired commission percentage. Beyond the commission itself, a pool can have a
141//! maximum commission and a change rate.
142//!
143//! Importantly, both max commission  [`Call::set_commission_max`] and change rate
144//! [`Call::set_commission_change_rate`] can not be removed once set, and can only be set to more
145//! restrictive values (i.e. a lower max commission or a slower change rate) in subsequent updates.
146//!
147//! If set, a pool's commission is bound to [`GlobalMaxCommission`] at the time it is applied to
148//! pending rewards. [`GlobalMaxCommission`] is intended to be updated only via governance.
149//!
150//! When a pool is dissolved, any outstanding pending commission that has not been claimed will be
151//! transferred to the depositor.
152//!
153//! Implementation note: Commission is analogous to a separate member account of the pool, with its
154//! own reward counter in the form of `current_pending_commission`.
155//!
156//! Crucially, commission is applied to rewards based on the current commission in effect at the
157//! time rewards are transferred into the reward pool. This is to prevent the malicious behaviour of
158//! changing the commission rate to a very high value after rewards are accumulated, and thus claim
159//! an unexpectedly high chunk of the reward.
160//!
161//! ### Dismantling
162//!
163//! As noted, a pool is destroyed once
164//!
165//! 1. First, all members need to fully unbond and withdraw. If the pool state is set to
166//!    `Destroying`, this can happen permissionlessly.
167//! 2. The depositor itself fully unbonds and withdraws.
168//!
169//! > Note that at this point, based on the requirements of the staking system, the pool's bonded
170//! > account's stake might not be able to ge below a certain threshold as a nominator. At this
171//! > point, the pool should `chill` itself to allow the depositor to leave. See [`Call::chill`].
172//!
173//! ## Implementor's Guide
174//!
175//! Some notes and common mistakes that wallets/apps wishing to implement this pallet should be
176//! aware of:
177//!
178//!
179//! ### Pool Members
180//!
181//! * In general, whenever a pool member changes their total points, the chain will automatically
182//!   claim all their pending rewards for them. This is not optional, and MUST happen for the reward
183//!   calculation to remain correct (see the documentation of `bond` as an example). So, make sure
184//!   you are warning your users about it. They might be surprised if they see that they bonded an
185//!   extra 100 DOTs, and now suddenly their 5.23 DOTs in pending reward is gone. It is not gone, it
186//!   has been paid out to you!
187//! * Joining a pool implies transferring funds to the pool account. So it might be (based on which
188//!   wallet that you are using) that you no longer see the funds that are moved to the pool in your
189//!   “free balance” section. Make sure the user is aware of this, and not surprised by seeing this.
190//!   Also, the transfer that happens here is configured to to never accidentally destroy the sender
191//!   account. So to join a Pool, your sender account must remain alive with 1 DOT left in it. This
192//!   means, with 1 DOT as existential deposit, and 1 DOT as minimum to join a pool, you need at
193//!   least 2 DOT to join a pool. Consequently, if you are suggesting members to join a pool with
194//!   “Maximum possible value”, you must subtract 1 DOT to remain in the sender account to not
195//!   accidentally kill it.
196//! * Points and balance are not the same! Any pool member, at any point in time, can have points in
197//!   either the bonded pool or any of the unbonding pools. The crucial fact is that in any of these
198//!   pools, the ratio of point to balance is different and might not be 1. Each pool starts with a
199//!   ratio of 1, but as time goes on, for reasons such as slashing, the ratio gets broken. Over
200//!   time, 100 points in a bonded pool can be worth 90 DOTs. Make sure you are either representing
201//!   points as points (not as DOTs), or even better, always display both: “You have x points in
202//!   pool y which is worth z DOTs”. See here and here for examples of how to calculate point to
203//!   balance ratio of each pool (it is almost trivial ;))
204//!
205//! ### Pool Management
206//!
207//! * The pool will be seen from the perspective of the rest of the system as a single nominator.
208//!   Ergo, This nominator must always respect the `staking.minNominatorBond` limit. Similar to a
209//!   normal nominator, who has to first `chill` before fully unbonding, the pool must also do the
210//!   same. The pool’s bonded account will be fully unbonded only when the depositor wants to leave
211//!   and dismantle the pool. All that said, the message is: the depositor can only leave the chain
212//!   when they chill the pool first.
213//!
214//! ## Design
215//!
216//! _Notes_: this section uses pseudo code to explain general design and does not necessarily
217//! reflect the exact implementation. Additionally, a working knowledge of `pallet-staking`'s api is
218//! assumed.
219//!
220//! ### Goals
221//!
222//! * Maintain network security by upholding integrity of slashing events, sufficiently penalizing
223//!   members that where in the pool while it was backing a validator that got slashed.
224//! * Maximize scalability in terms of member count.
225//!
226//! In order to maintain scalability, all operations are independent of the number of members. To do
227//! this, delegation specific information is stored local to the member while the pool data
228//! structures have bounded datum.
229//!
230//! ### Bonded pool
231//!
232//! A bonded pool nominates with its total balance, excluding that which has been withdrawn for
233//! unbonding. The total points of a bonded pool are always equal to the sum of points of the
234//! delegation members. A bonded pool tracks its points and reads its bonded balance.
235//!
236//! When a member joins a pool, `amount_transferred` is transferred from the members account to the
237//! bonded pools account. Then the pool calls `staking::bond_extra(amount_transferred)` and issues
238//! new points which are tracked by the member and added to the bonded pool's points.
239//!
240//! When the pool already has some balance, we want the value of a point before the transfer to
241//! equal the value of a point after the transfer. So, when a member joins a bonded pool with a
242//! given `amount_transferred`, we maintain the ratio of bonded balance to points such that:
243//!
244//! ```text
245//! balance_after_transfer / points_after_transfer == balance_before_transfer / points_before_transfer;
246//! ```
247//!
248//! To achieve this, we issue points based on the following:
249//!
250//! ```text
251//! points_issued = (points_before_transfer / balance_before_transfer) * amount_transferred;
252//! ```
253//!
254//! For new bonded pools we can set the points issued per balance arbitrarily. In this
255//! implementation we use a 1 points to 1 balance ratio for pool creation (see
256//! [`POINTS_TO_BALANCE_INIT_RATIO`]).
257//!
258//! **Relevant extrinsics:**
259//!
260//! * [`Call::create`]
261//! * [`Call::join`]
262//!
263//! ### Reward pool
264//!
265//! When a pool is first bonded it sets up a deterministic, inaccessible account as its reward
266//! destination. This reward account combined with `RewardPool` compose a reward pool.
267//!
268//! Reward pools are completely separate entities to bonded pools. Along with its account, a reward
269//! pool also tracks its outstanding and claimed rewards as counters, in addition to pending and
270//! claimed commission. These counters are updated with `RewardPool::update_records`. The current
271//! reward counter of the pool (the total outstanding rewards, in points) is also callable with the
272//! `RewardPool::current_reward_counter` method.
273//!
274//! See [this link](https://hackmd.io/PFGn6wI5TbCmBYoEA_f2Uw) for an in-depth explanation of the
275//! reward pool mechanism.
276//!
277//! **Relevant extrinsics:**
278//!
279//! * [`Call::claim_payout`]
280//!
281//! ### Unbonding sub pools
282//!
283//! When a member unbonds, it's balance is unbonded in the bonded pool's account and tracked in an
284//! unbonding pool associated with the active era. If no such pool exists, one is created. To track
285//! which unbonding sub pool a member belongs too, a member tracks it's `unbonding_era`.
286//!
287//! When a member initiates unbonding it's claim on the bonded pool (`balance_to_unbond`) is
288//! computed as:
289//!
290//! ```text
291//! balance_to_unbond = (bonded_pool.balance / bonded_pool.points) * member.points;
292//! ```
293//!
294//! If this is the first transfer into an unbonding pool arbitrary amount of points can be issued
295//! per balance. In this implementation unbonding pools are initialized with a 1 point to 1 balance
296//! ratio (see [`POINTS_TO_BALANCE_INIT_RATIO`]). Otherwise, the unbonding pools hold the same
297//! points to balance ratio properties as the bonded pool, so member points in the unbonding pool
298//! are issued based on
299//!
300//! ```text
301//! new_points_issued = (points_before_transfer / balance_before_transfer) * balance_to_unbond;
302//! ```
303//!
304//! For scalability, a bound is maintained on the number of unbonding sub pools (see
305//! [`TotalUnbondingPools`]). An unbonding pool is removed once its older than `current_era -
306//! TotalUnbondingPools`. An unbonding pool is merged into the unbonded pool with
307//!
308//! ```text
309//! unbounded_pool.balance = unbounded_pool.balance + unbonding_pool.balance;
310//! unbounded_pool.points = unbounded_pool.points + unbonding_pool.points;
311//! ```
312//!
313//! This scheme "averages" out the points value in the unbonded pool.
314//!
315//! Once a members `unbonding_era` is older than `current_era -
316//! [sp_staking::StakingInterface::bonding_duration]`, it can can cash it's points out of the
317//! corresponding unbonding pool. If it's `unbonding_era` is older than `current_era -
318//! TotalUnbondingPools`, it can cash it's points from the unbonded pool.
319//!
320//! **Relevant extrinsics:**
321//!
322//! * [`Call::unbond`]
323//! * [`Call::withdraw_unbonded`]
324//!
325//! ### Slashing
326//!
327//! This section assumes that the slash computation is executed by
328//! `pallet_staking::StakingLedger::slash`, which passes the information to this pallet via
329//! [`sp_staking::OnStakingUpdate::on_slash`].
330//!
331//! Unbonding pools need to be slashed to ensure all nominators whom where in the bonded pool while
332//! it was backing a validator that equivocated are punished. Without these measures a member could
333//! unbond right after a validator equivocated with no consequences.
334//!
335//! This strategy is unfair to members who joined after the slash, because they get slashed as well,
336//! but spares members who unbond. The latter is much more important for security: if a pool's
337//! validators are attacking the network, their members need to unbond fast! Avoiding slashes gives
338//! them an incentive to do that if validators get repeatedly slashed.
339//!
340//! To be fair to joiners, this implementation also need joining pools, which are actively staking,
341//! in addition to the unbonding pools. For maintenance simplicity these are not implemented.
342//! Related: <https://github.com/paritytech/substrate/issues/10860>
343//!
344//! ### Limitations
345//!
346//! * PoolMembers cannot vote with their staked funds because they are transferred into the pools
347//!   account. In the future this can be overcome by allowing the members to vote with their bonded
348//!   funds via vote splitting.
349//! * PoolMembers cannot quickly transfer to another pool if they do no like nominations, instead
350//!   they must wait for the unbonding duration.
351
352#![cfg_attr(not(feature = "std"), no_std)]
353
354extern crate alloc;
355
356use adapter::{Member, Pool, StakeStrategy};
357use alloc::{collections::btree_map::BTreeMap, vec::Vec};
358use codec::{Codec, DecodeWithMemTracking};
359use core::{fmt::Debug, ops::Div};
360use frame_support::{
361	defensive, defensive_assert, ensure,
362	pallet_prelude::{MaxEncodedLen, *},
363	storage::bounded_btree_map::BoundedBTreeMap,
364	traits::{
365		fungible::{Inspect, InspectFreeze, Mutate, MutateFreeze},
366		tokens::{Fortitude, Preservation},
367		Contains, Defensive, DefensiveOption, DefensiveResult, DefensiveSaturating, Get,
368	},
369	DefaultNoBound, PalletError,
370};
371use scale_info::TypeInfo;
372use sp_core::U256;
373use sp_runtime::{
374	traits::{
375		AccountIdConversion, Bounded, CheckedAdd, CheckedSub, Convert, Saturating, StaticLookup,
376		Zero,
377	},
378	FixedPointNumber, Perbill,
379};
380use sp_staking::{EraIndex, StakingInterface};
381
382#[cfg(any(feature = "try-runtime", feature = "fuzzing", test, debug_assertions))]
383use sp_runtime::TryRuntimeError;
384
385/// The log target of this pallet.
386pub const LOG_TARGET: &str = "runtime::nomination-pools";
387// syntactic sugar for logging.
388#[macro_export]
389macro_rules! log {
390	($level:tt, $patter:expr $(, $values:expr)* $(,)?) => {
391		log::$level!(
392			target: $crate::LOG_TARGET,
393			concat!("[{:?}] 🏊‍♂️ ", $patter), <frame_system::Pallet<T>>::block_number() $(, $values)*
394		)
395	};
396}
397
398#[cfg(any(test, feature = "fuzzing"))]
399pub mod mock;
400#[cfg(test)]
401mod tests;
402
403pub mod adapter;
404pub mod migration;
405pub mod weights;
406
407pub use pallet::*;
408use sp_runtime::traits::BlockNumberProvider;
409pub use weights::WeightInfo;
410
411/// The balance type used by the currency system.
412pub type BalanceOf<T> =
413	<<T as Config>::Currency as Inspect<<T as frame_system::Config>::AccountId>>::Balance;
414/// Type used for unique identifier of each pool.
415pub type PoolId = u32;
416
417type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
418
419pub type BlockNumberFor<T> =
420	<<T as Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
421
422pub const POINTS_TO_BALANCE_INIT_RATIO: u32 = 1;
423
424/// Possible operations on the configuration values of this pallet.
425#[derive(
426	Encode,
427	Decode,
428	DecodeWithMemTracking,
429	MaxEncodedLen,
430	TypeInfo,
431	RuntimeDebugNoBound,
432	PartialEq,
433	Clone,
434)]
435pub enum ConfigOp<T: Codec + Debug> {
436	/// Don't change.
437	Noop,
438	/// Set the given value.
439	Set(T),
440	/// Remove from storage.
441	Remove,
442}
443
444/// The type of bonding that can happen to a pool.
445pub enum BondType {
446	/// Someone is bonding into the pool upon creation.
447	Create,
448	/// Someone is adding more funds later to this pool.
449	Extra,
450}
451
452/// How to increase the bond of a member.
453#[derive(Encode, Decode, DecodeWithMemTracking, Clone, Copy, Debug, PartialEq, Eq, TypeInfo)]
454pub enum BondExtra<Balance> {
455	/// Take from the free balance.
456	FreeBalance(Balance),
457	/// Take the entire amount from the accumulated rewards.
458	Rewards,
459}
460
461/// The type of account being created.
462#[derive(Encode, Decode)]
463enum AccountType {
464	Bonded,
465	Reward,
466}
467
468/// The permission a pool member can set for other accounts to claim rewards on their behalf.
469#[derive(
470	Encode,
471	Decode,
472	DecodeWithMemTracking,
473	MaxEncodedLen,
474	Clone,
475	Copy,
476	Debug,
477	PartialEq,
478	Eq,
479	TypeInfo,
480)]
481pub enum ClaimPermission {
482	/// Only the pool member themselves can claim their rewards.
483	Permissioned,
484	/// Anyone can compound rewards on a pool member's behalf.
485	PermissionlessCompound,
486	/// Anyone can withdraw rewards on a pool member's behalf.
487	PermissionlessWithdraw,
488	/// Anyone can withdraw and compound rewards on a pool member's behalf.
489	PermissionlessAll,
490}
491
492impl Default for ClaimPermission {
493	fn default() -> Self {
494		Self::PermissionlessWithdraw
495	}
496}
497
498impl ClaimPermission {
499	/// Permissionless compounding of pool rewards is allowed if the current permission is
500	/// `PermissionlessCompound`, or permissionless.
501	fn can_bond_extra(&self) -> bool {
502		matches!(self, ClaimPermission::PermissionlessAll | ClaimPermission::PermissionlessCompound)
503	}
504
505	/// Permissionless payout claiming is allowed if the current permission is
506	/// `PermissionlessWithdraw`, or permissionless.
507	fn can_claim_payout(&self) -> bool {
508		matches!(self, ClaimPermission::PermissionlessAll | ClaimPermission::PermissionlessWithdraw)
509	}
510}
511
512/// A member in a pool.
513#[derive(
514	Encode,
515	Decode,
516	DecodeWithMemTracking,
517	MaxEncodedLen,
518	TypeInfo,
519	RuntimeDebugNoBound,
520	CloneNoBound,
521	PartialEqNoBound,
522	EqNoBound,
523)]
524#[cfg_attr(feature = "std", derive(DefaultNoBound))]
525#[scale_info(skip_type_params(T))]
526pub struct PoolMember<T: Config> {
527	/// The identifier of the pool to which `who` belongs.
528	pub pool_id: PoolId,
529	/// The quantity of points this member has in the bonded pool or in a sub pool if
530	/// `Self::unbonding_era` is some.
531	pub points: BalanceOf<T>,
532	/// The reward counter at the time of this member's last payout claim.
533	pub last_recorded_reward_counter: T::RewardCounter,
534	/// The eras in which this member is unbonding, mapped from era index to the number of
535	/// points scheduled to unbond in the given era.
536	pub unbonding_eras: BoundedBTreeMap<EraIndex, BalanceOf<T>, T::MaxUnbonding>,
537}
538
539impl<T: Config> PoolMember<T> {
540	/// The pending rewards of this member.
541	fn pending_rewards(
542		&self,
543		current_reward_counter: T::RewardCounter,
544	) -> Result<BalanceOf<T>, Error<T>> {
545		// accuracy note: Reward counters are `FixedU128` with base of 10^18. This value is being
546		// multiplied by a point. The worse case of a point is 10x the granularity of the balance
547		// (10x is the common configuration of `MaxPointsToBalance`).
548		//
549		// Assuming roughly the current issuance of polkadot (12,047,781,394,999,601,455, which is
550		// 1.2 * 10^9 * 10^10 = 1.2 * 10^19), the worse case point value is around 10^20.
551		//
552		// The final multiplication is:
553		//
554		// rc * 10^20 / 10^18 = rc * 100
555		//
556		// the implementation of `multiply_by_rational_with_rounding` shows that it will only fail
557		// if the final division is not enough to fit in u128. In other words, if `rc * 100` is more
558		// than u128::max. Given that RC is interpreted as reward per unit of point, and unit of
559		// point is equal to balance (normally), and rewards are usually a proportion of the points
560		// in the pool, the likelihood of rc reaching near u128::MAX is near impossible.
561
562		(current_reward_counter.defensive_saturating_sub(self.last_recorded_reward_counter))
563			.checked_mul_int(self.active_points())
564			.ok_or(Error::<T>::OverflowRisk)
565	}
566
567	/// Active balance of the member.
568	///
569	/// This is derived from the ratio of points in the pool to which the member belongs to.
570	/// Might return different values based on the pool state for the same member and points.
571	fn active_balance(&self) -> BalanceOf<T> {
572		if let Some(pool) = BondedPool::<T>::get(self.pool_id).defensive() {
573			pool.points_to_balance(self.points)
574		} else {
575			Zero::zero()
576		}
577	}
578
579	/// Total balance of the member, both active and unbonding.
580	/// Doesn't mutate state.
581	///
582	/// Worst case, iterates over [`TotalUnbondingPools`] member unbonding pools to calculate member
583	/// balance.
584	pub fn total_balance(&self) -> BalanceOf<T> {
585		let pool = match BondedPool::<T>::get(self.pool_id) {
586			Some(pool) => pool,
587			None => {
588				// this internal function is always called with a valid pool id.
589				defensive!("pool should exist; qed");
590				return Zero::zero();
591			},
592		};
593
594		let active_balance = pool.points_to_balance(self.active_points());
595
596		let sub_pools = match SubPoolsStorage::<T>::get(self.pool_id) {
597			Some(sub_pools) => sub_pools,
598			None => return active_balance,
599		};
600
601		let unbonding_balance = self.unbonding_eras.iter().fold(
602			BalanceOf::<T>::zero(),
603			|accumulator, (era, unlocked_points)| {
604				// if the `SubPools::with_era` has already been merged into the
605				// `SubPools::no_era` use this pool instead.
606				let era_pool = sub_pools.with_era.get(era).unwrap_or(&sub_pools.no_era);
607				accumulator + (era_pool.point_to_balance(*unlocked_points))
608			},
609		);
610
611		active_balance + unbonding_balance
612	}
613
614	/// Total points of this member, both active and unbonding.
615	fn total_points(&self) -> BalanceOf<T> {
616		self.active_points().saturating_add(self.unbonding_points())
617	}
618
619	/// Active points of the member.
620	fn active_points(&self) -> BalanceOf<T> {
621		self.points
622	}
623
624	/// Inactive points of the member, waiting to be withdrawn.
625	fn unbonding_points(&self) -> BalanceOf<T> {
626		self.unbonding_eras
627			.as_ref()
628			.iter()
629			.fold(BalanceOf::<T>::zero(), |acc, (_, v)| acc.saturating_add(*v))
630	}
631
632	/// Try and unbond `points_dissolved` from self, and in return mint `points_issued` into the
633	/// corresponding `era`'s unlock schedule.
634	///
635	/// In the absence of slashing, these two points are always the same. In the presence of
636	/// slashing, the value of points in different pools varies.
637	///
638	/// Returns `Ok(())` and updates `unbonding_eras` and `points` if success, `Err(_)` otherwise.
639	fn try_unbond(
640		&mut self,
641		points_dissolved: BalanceOf<T>,
642		points_issued: BalanceOf<T>,
643		unbonding_era: EraIndex,
644	) -> Result<(), Error<T>> {
645		if let Some(new_points) = self.points.checked_sub(&points_dissolved) {
646			match self.unbonding_eras.get_mut(&unbonding_era) {
647				Some(already_unbonding_points) =>
648					*already_unbonding_points =
649						already_unbonding_points.saturating_add(points_issued),
650				None => self
651					.unbonding_eras
652					.try_insert(unbonding_era, points_issued)
653					.map(|old| {
654						if old.is_some() {
655							defensive!("value checked to not exist in the map; qed");
656						}
657					})
658					.map_err(|_| Error::<T>::MaxUnbondingLimit)?,
659			}
660			self.points = new_points;
661			Ok(())
662		} else {
663			Err(Error::<T>::MinimumBondNotMet)
664		}
665	}
666
667	/// Withdraw any funds in [`Self::unbonding_eras`] who's deadline in reached and is fully
668	/// unlocked.
669	///
670	/// Returns a a subset of [`Self::unbonding_eras`] that got withdrawn.
671	///
672	/// Infallible, noop if no unbonding eras exist.
673	fn withdraw_unlocked(
674		&mut self,
675		current_era: EraIndex,
676	) -> BoundedBTreeMap<EraIndex, BalanceOf<T>, T::MaxUnbonding> {
677		// NOTE: if only drain-filter was stable..
678		let mut removed_points =
679			BoundedBTreeMap::<EraIndex, BalanceOf<T>, T::MaxUnbonding>::default();
680		self.unbonding_eras.retain(|e, p| {
681			if *e > current_era {
682				true
683			} else {
684				removed_points
685					.try_insert(*e, *p)
686					.expect("source map is bounded, this is a subset, will be bounded; qed");
687				false
688			}
689		});
690		removed_points
691	}
692}
693
694/// A pool's possible states.
695#[derive(
696	Encode,
697	Decode,
698	DecodeWithMemTracking,
699	MaxEncodedLen,
700	TypeInfo,
701	PartialEq,
702	RuntimeDebugNoBound,
703	Clone,
704	Copy,
705)]
706pub enum PoolState {
707	/// The pool is open to be joined, and is working normally.
708	Open,
709	/// The pool is blocked. No one else can join.
710	Blocked,
711	/// The pool is in the process of being destroyed.
712	///
713	/// All members can now be permissionlessly unbonded, and the pool can never go back to any
714	/// other state other than being dissolved.
715	Destroying,
716}
717
718/// Pool administration roles.
719///
720/// Any pool has a depositor, which can never change. But, all the other roles are optional, and
721/// cannot exist. Note that if `root` is set to `None`, it basically means that the roles of this
722/// pool can never change again (except via governance).
723#[derive(
724	Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, TypeInfo, Debug, PartialEq, Clone,
725)]
726pub struct PoolRoles<AccountId> {
727	/// Creates the pool and is the initial member. They can only leave the pool once all other
728	/// members have left. Once they fully leave, the pool is destroyed.
729	pub depositor: AccountId,
730	/// Can change the nominator, bouncer, or itself and can perform any of the actions the
731	/// nominator or bouncer can.
732	pub root: Option<AccountId>,
733	/// Can select which validators the pool nominates.
734	pub nominator: Option<AccountId>,
735	/// Can change the pools state and kick members if the pool is blocked.
736	pub bouncer: Option<AccountId>,
737}
738
739// A pool's possible commission claiming permissions.
740#[derive(
741	PartialEq,
742	Eq,
743	Copy,
744	Clone,
745	Encode,
746	Decode,
747	DecodeWithMemTracking,
748	RuntimeDebug,
749	TypeInfo,
750	MaxEncodedLen,
751)]
752pub enum CommissionClaimPermission<AccountId> {
753	Permissionless,
754	Account(AccountId),
755}
756
757/// Pool commission.
758///
759/// The pool `root` can set commission configuration after pool creation. By default, all commission
760/// values are `None`. Pool `root` can also set `max` and `change_rate` configurations before
761/// setting an initial `current` commission.
762///
763/// `current` is a tuple of the commission percentage and payee of commission. `throttle_from`
764/// keeps track of which block `current` was last updated. A `max` commission value can only be
765/// decreased after the initial value is set, to prevent commission from repeatedly increasing.
766///
767/// An optional commission `change_rate` allows the pool to set strict limits to how much commission
768/// can change in each update, and how often updates can take place.
769#[derive(
770	Encode,
771	Decode,
772	DecodeWithMemTracking,
773	DefaultNoBound,
774	MaxEncodedLen,
775	TypeInfo,
776	DebugNoBound,
777	PartialEq,
778	Copy,
779	Clone,
780)]
781#[codec(mel_bound(T: Config))]
782#[scale_info(skip_type_params(T))]
783pub struct Commission<T: Config> {
784	/// Optional commission rate of the pool along with the account commission is paid to.
785	pub current: Option<(Perbill, T::AccountId)>,
786	/// Optional maximum commission that can be set by the pool `root`. Once set, this value can
787	/// only be updated to a decreased value.
788	pub max: Option<Perbill>,
789	/// Optional configuration around how often commission can be updated, and when the last
790	/// commission update took place.
791	pub change_rate: Option<CommissionChangeRate<BlockNumberFor<T>>>,
792	/// The block from where throttling should be checked from. This value will be updated on all
793	/// commission updates and when setting an initial `change_rate`.
794	pub throttle_from: Option<BlockNumberFor<T>>,
795	// Whether commission can be claimed permissionlessly, or whether an account can claim
796	// commission. `Root` role can always claim.
797	pub claim_permission: Option<CommissionClaimPermission<T::AccountId>>,
798}
799
800impl<T: Config> Commission<T> {
801	/// Returns true if the current commission updating to `to` would exhaust the change rate
802	/// limits.
803	///
804	/// A commission update will be throttled (disallowed) if:
805	/// 1. not enough blocks have passed since the `throttle_from` block, if exists, or
806	/// 2. the new commission is greater than the maximum allowed increase.
807	fn throttling(&self, to: &Perbill) -> bool {
808		if let Some(t) = self.change_rate.as_ref() {
809			let commission_as_percent =
810				self.current.as_ref().map(|(x, _)| *x).unwrap_or(Perbill::zero());
811
812			// do not throttle if `to` is the same or a decrease in commission.
813			if *to <= commission_as_percent {
814				return false
815			}
816			// Test for `max_increase` throttling.
817			//
818			// Throttled if the attempted increase in commission is greater than `max_increase`.
819			if (*to).saturating_sub(commission_as_percent) > t.max_increase {
820				return true
821			}
822
823			// Test for `min_delay` throttling.
824			//
825			// Note: matching `None` is defensive only. `throttle_from` should always exist where
826			// `change_rate` has already been set, so this scenario should never happen.
827			return self.throttle_from.map_or_else(
828				|| {
829					defensive!("throttle_from should exist if change_rate is set");
830					true
831				},
832				|f| {
833					// if `min_delay` is zero (no delay), not throttling.
834					if t.min_delay == Zero::zero() {
835						false
836					} else {
837						// throttling if blocks passed is less than `min_delay`.
838						let blocks_surpassed =
839							T::BlockNumberProvider::current_block_number().saturating_sub(f);
840						blocks_surpassed < t.min_delay
841					}
842				},
843			)
844		}
845		false
846	}
847
848	/// Gets the pool's current commission, or returns Perbill::zero if none is set.
849	/// Bounded to global max if current is greater than `GlobalMaxCommission`.
850	fn current(&self) -> Perbill {
851		self.current
852			.as_ref()
853			.map_or(Perbill::zero(), |(c, _)| *c)
854			.min(GlobalMaxCommission::<T>::get().unwrap_or(Bounded::max_value()))
855	}
856
857	/// Set the pool's commission.
858	///
859	/// Update commission based on `current`. If a `None` is supplied, allow the commission to be
860	/// removed without any change rate restrictions. Updates `throttle_from` to the current block.
861	/// If the supplied commission is zero, `None` will be inserted and `payee` will be ignored.
862	fn try_update_current(&mut self, current: &Option<(Perbill, T::AccountId)>) -> DispatchResult {
863		self.current = match current {
864			None => None,
865			Some((commission, payee)) => {
866				ensure!(!self.throttling(commission), Error::<T>::CommissionChangeThrottled);
867				ensure!(
868					commission <= &GlobalMaxCommission::<T>::get().unwrap_or(Bounded::max_value()),
869					Error::<T>::CommissionExceedsGlobalMaximum
870				);
871				ensure!(
872					self.max.map_or(true, |m| commission <= &m),
873					Error::<T>::CommissionExceedsMaximum
874				);
875				if commission.is_zero() {
876					None
877				} else {
878					Some((*commission, payee.clone()))
879				}
880			},
881		};
882		self.register_update();
883		Ok(())
884	}
885
886	/// Set the pool's maximum commission.
887	///
888	/// The pool's maximum commission can initially be set to any value, and only smaller values
889	/// thereafter. If larger values are attempted, this function will return a dispatch error.
890	///
891	/// If `current.0` is larger than the updated max commission value, `current.0` will also be
892	/// updated to the new maximum. This will also register a `throttle_from` update.
893	/// A `PoolCommissionUpdated` event is triggered if `current.0` is updated.
894	fn try_update_max(&mut self, pool_id: PoolId, new_max: Perbill) -> DispatchResult {
895		ensure!(
896			new_max <= GlobalMaxCommission::<T>::get().unwrap_or(Bounded::max_value()),
897			Error::<T>::CommissionExceedsGlobalMaximum
898		);
899		if let Some(old) = self.max.as_mut() {
900			if new_max > *old {
901				return Err(Error::<T>::MaxCommissionRestricted.into())
902			}
903			*old = new_max;
904		} else {
905			self.max = Some(new_max)
906		};
907		let updated_current = self
908			.current
909			.as_mut()
910			.map(|(c, _)| {
911				let u = *c > new_max;
912				*c = (*c).min(new_max);
913				u
914			})
915			.unwrap_or(false);
916
917		if updated_current {
918			if let Some((_, payee)) = self.current.as_ref() {
919				Pallet::<T>::deposit_event(Event::<T>::PoolCommissionUpdated {
920					pool_id,
921					current: Some((new_max, payee.clone())),
922				});
923			}
924			self.register_update();
925		}
926		Ok(())
927	}
928
929	/// Set the pool's commission `change_rate`.
930	///
931	/// Once a change rate configuration has been set, only more restrictive values can be set
932	/// thereafter. These restrictions translate to increased `min_delay` values and decreased
933	/// `max_increase` values.
934	///
935	/// Update `throttle_from` to the current block upon setting change rate for the first time, so
936	/// throttling can be checked from this block.
937	fn try_update_change_rate(
938		&mut self,
939		change_rate: CommissionChangeRate<BlockNumberFor<T>>,
940	) -> DispatchResult {
941		ensure!(!&self.less_restrictive(&change_rate), Error::<T>::CommissionChangeRateNotAllowed);
942
943		if self.change_rate.is_none() {
944			self.register_update();
945		}
946		self.change_rate = Some(change_rate);
947		Ok(())
948	}
949
950	/// Updates a commission's `throttle_from` field to the current block.
951	fn register_update(&mut self) {
952		self.throttle_from = Some(T::BlockNumberProvider::current_block_number());
953	}
954
955	/// Checks whether a change rate is less restrictive than the current change rate, if any.
956	///
957	/// No change rate will always be less restrictive than some change rate, so where no
958	/// `change_rate` is currently set, `false` is returned.
959	fn less_restrictive(&self, new: &CommissionChangeRate<BlockNumberFor<T>>) -> bool {
960		self.change_rate
961			.as_ref()
962			.map(|c| new.max_increase > c.max_increase || new.min_delay < c.min_delay)
963			.unwrap_or(false)
964	}
965}
966
967/// Pool commission change rate preferences.
968///
969/// The pool root is able to set a commission change rate for their pool. A commission change rate
970/// consists of 2 values; (1) the maximum allowed commission change, and (2) the minimum amount of
971/// blocks that must elapse before commission updates are allowed again.
972///
973/// Commission change rates are not applied to decreases in commission.
974#[derive(
975	Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, TypeInfo, Debug, PartialEq, Copy, Clone,
976)]
977pub struct CommissionChangeRate<BlockNumber> {
978	/// The maximum amount the commission can be updated by per `min_delay` period.
979	pub max_increase: Perbill,
980	/// How often an update can take place.
981	pub min_delay: BlockNumber,
982}
983
984/// Pool permissions and state
985#[derive(
986	Encode, Decode, DecodeWithMemTracking, MaxEncodedLen, TypeInfo, DebugNoBound, PartialEq, Clone,
987)]
988#[codec(mel_bound(T: Config))]
989#[scale_info(skip_type_params(T))]
990pub struct BondedPoolInner<T: Config> {
991	/// The commission rate of the pool.
992	pub commission: Commission<T>,
993	/// Count of members that belong to the pool.
994	pub member_counter: u32,
995	/// Total points of all the members in the pool who are actively bonded.
996	pub points: BalanceOf<T>,
997	/// See [`PoolRoles`].
998	pub roles: PoolRoles<T::AccountId>,
999	/// The current state of the pool.
1000	pub state: PoolState,
1001}
1002
1003/// A wrapper for bonded pools, with utility functions.
1004///
1005/// The main purpose of this is to wrap a [`BondedPoolInner`], with the account
1006/// + id of the pool, for easier access.
1007#[derive(RuntimeDebugNoBound)]
1008#[cfg_attr(feature = "std", derive(Clone, PartialEq))]
1009pub struct BondedPool<T: Config> {
1010	/// The identifier of the pool.
1011	id: PoolId,
1012	/// The inner fields.
1013	inner: BondedPoolInner<T>,
1014}
1015
1016impl<T: Config> core::ops::Deref for BondedPool<T> {
1017	type Target = BondedPoolInner<T>;
1018	fn deref(&self) -> &Self::Target {
1019		&self.inner
1020	}
1021}
1022
1023impl<T: Config> core::ops::DerefMut for BondedPool<T> {
1024	fn deref_mut(&mut self) -> &mut Self::Target {
1025		&mut self.inner
1026	}
1027}
1028
1029impl<T: Config> BondedPool<T> {
1030	/// Create a new bonded pool with the given roles and identifier.
1031	fn new(id: PoolId, roles: PoolRoles<T::AccountId>) -> Self {
1032		Self {
1033			id,
1034			inner: BondedPoolInner {
1035				commission: Commission::default(),
1036				member_counter: Zero::zero(),
1037				points: Zero::zero(),
1038				roles,
1039				state: PoolState::Open,
1040			},
1041		}
1042	}
1043
1044	/// Get [`Self`] from storage. Returns `None` if no entry for `pool_account` exists.
1045	pub fn get(id: PoolId) -> Option<Self> {
1046		BondedPools::<T>::try_get(id).ok().map(|inner| Self { id, inner })
1047	}
1048
1049	/// Get the bonded account id of this pool.
1050	fn bonded_account(&self) -> T::AccountId {
1051		Pallet::<T>::generate_bonded_account(self.id)
1052	}
1053
1054	/// Get the reward account id of this pool.
1055	fn reward_account(&self) -> T::AccountId {
1056		Pallet::<T>::generate_reward_account(self.id)
1057	}
1058
1059	/// Consume self and put into storage.
1060	fn put(self) {
1061		BondedPools::<T>::insert(self.id, self.inner);
1062	}
1063
1064	/// Consume self and remove from storage.
1065	fn remove(self) {
1066		BondedPools::<T>::remove(self.id);
1067	}
1068
1069	/// Convert the given amount of balance to points given the current pool state.
1070	///
1071	/// This is often used for bonding and issuing new funds into the pool.
1072	fn balance_to_point(&self, new_funds: BalanceOf<T>) -> BalanceOf<T> {
1073		let bonded_balance = T::StakeAdapter::active_stake(Pool::from(self.bonded_account()));
1074		Pallet::<T>::balance_to_point(bonded_balance, self.points, new_funds)
1075	}
1076
1077	/// Convert the given number of points to balance given the current pool state.
1078	///
1079	/// This is often used for unbonding.
1080	fn points_to_balance(&self, points: BalanceOf<T>) -> BalanceOf<T> {
1081		let bonded_balance = T::StakeAdapter::active_stake(Pool::from(self.bonded_account()));
1082		Pallet::<T>::point_to_balance(bonded_balance, self.points, points)
1083	}
1084
1085	/// Issue points to [`Self`] for `new_funds`.
1086	fn issue(&mut self, new_funds: BalanceOf<T>) -> BalanceOf<T> {
1087		let points_to_issue = self.balance_to_point(new_funds);
1088		self.points = self.points.saturating_add(points_to_issue);
1089		points_to_issue
1090	}
1091
1092	/// Dissolve some points from the pool i.e. unbond the given amount of points from this pool.
1093	/// This is the opposite of issuing some funds into the pool.
1094	///
1095	/// Mutates self in place, but does not write anything to storage.
1096	///
1097	/// Returns the equivalent balance amount that actually needs to get unbonded.
1098	fn dissolve(&mut self, points: BalanceOf<T>) -> BalanceOf<T> {
1099		// NOTE: do not optimize by removing `balance`. it must be computed before mutating
1100		// `self.point`.
1101		let balance = self.points_to_balance(points);
1102		self.points = self.points.saturating_sub(points);
1103		balance
1104	}
1105
1106	/// Increment the member counter. Ensures that the pool and system member limits are
1107	/// respected.
1108	fn try_inc_members(&mut self) -> Result<(), DispatchError> {
1109		ensure!(
1110			MaxPoolMembersPerPool::<T>::get()
1111				.map_or(true, |max_per_pool| self.member_counter < max_per_pool),
1112			Error::<T>::MaxPoolMembers
1113		);
1114		ensure!(
1115			MaxPoolMembers::<T>::get().map_or(true, |max| PoolMembers::<T>::count() < max),
1116			Error::<T>::MaxPoolMembers
1117		);
1118		self.member_counter = self.member_counter.checked_add(1).ok_or(Error::<T>::OverflowRisk)?;
1119		Ok(())
1120	}
1121
1122	/// Decrement the member counter.
1123	fn dec_members(mut self) -> Self {
1124		self.member_counter = self.member_counter.defensive_saturating_sub(1);
1125		self
1126	}
1127
1128	fn is_root(&self, who: &T::AccountId) -> bool {
1129		self.roles.root.as_ref().map_or(false, |root| root == who)
1130	}
1131
1132	fn is_bouncer(&self, who: &T::AccountId) -> bool {
1133		self.roles.bouncer.as_ref().map_or(false, |bouncer| bouncer == who)
1134	}
1135
1136	fn can_update_roles(&self, who: &T::AccountId) -> bool {
1137		self.is_root(who)
1138	}
1139
1140	fn can_nominate(&self, who: &T::AccountId) -> bool {
1141		self.is_root(who) ||
1142			self.roles.nominator.as_ref().map_or(false, |nominator| nominator == who)
1143	}
1144
1145	fn can_kick(&self, who: &T::AccountId) -> bool {
1146		self.state == PoolState::Blocked && (self.is_root(who) || self.is_bouncer(who))
1147	}
1148
1149	fn can_toggle_state(&self, who: &T::AccountId) -> bool {
1150		(self.is_root(who) || self.is_bouncer(who)) && !self.is_destroying()
1151	}
1152
1153	fn can_set_metadata(&self, who: &T::AccountId) -> bool {
1154		self.is_root(who) || self.is_bouncer(who)
1155	}
1156
1157	fn can_manage_commission(&self, who: &T::AccountId) -> bool {
1158		self.is_root(who)
1159	}
1160
1161	fn can_claim_commission(&self, who: &T::AccountId) -> bool {
1162		if let Some(permission) = self.commission.claim_permission.as_ref() {
1163			match permission {
1164				CommissionClaimPermission::Permissionless => true,
1165				CommissionClaimPermission::Account(account) => account == who || self.is_root(who),
1166			}
1167		} else {
1168			self.is_root(who)
1169		}
1170	}
1171
1172	fn is_destroying(&self) -> bool {
1173		matches!(self.state, PoolState::Destroying)
1174	}
1175
1176	fn is_destroying_and_only_depositor(&self, alleged_depositor_points: BalanceOf<T>) -> bool {
1177		// we need to ensure that `self.member_counter == 1` as well, because the depositor's
1178		// initial `MinCreateBond` (or more) is what guarantees that the ledger of the pool does not
1179		// get killed in the staking system, and that it does not fall below `MinimumNominatorBond`,
1180		// which could prevent other non-depositor members from fully leaving. Thus, all members
1181		// must withdraw, then depositor can unbond, and finally withdraw after waiting another
1182		// cycle.
1183		self.is_destroying() && self.points == alleged_depositor_points && self.member_counter == 1
1184	}
1185
1186	/// Whether or not the pool is ok to be in `PoolSate::Open`. If this returns an `Err`, then the
1187	/// pool is unrecoverable and should be in the destroying state.
1188	fn ok_to_be_open(&self) -> Result<(), DispatchError> {
1189		ensure!(!self.is_destroying(), Error::<T>::CanNotChangeState);
1190
1191		let bonded_balance = T::StakeAdapter::active_stake(Pool::from(self.bonded_account()));
1192		ensure!(!bonded_balance.is_zero(), Error::<T>::OverflowRisk);
1193
1194		let points_to_balance_ratio_floor = self
1195			.points
1196			// We checked for zero above
1197			.div(bonded_balance);
1198
1199		let max_points_to_balance = T::MaxPointsToBalance::get();
1200
1201		// Pool points can inflate relative to balance, but only if the pool is slashed.
1202		// If we cap the ratio of points:balance so one cannot join a pool that has been slashed
1203		// by `max_points_to_balance`%, if not zero.
1204		ensure!(
1205			points_to_balance_ratio_floor < max_points_to_balance.into(),
1206			Error::<T>::OverflowRisk
1207		);
1208
1209		// then we can be decently confident the bonding pool points will not overflow
1210		// `BalanceOf<T>`. Note that these are just heuristics.
1211
1212		Ok(())
1213	}
1214
1215	/// Check that the pool can accept a member with `new_funds`.
1216	fn ok_to_join(&self) -> Result<(), DispatchError> {
1217		ensure!(self.state == PoolState::Open, Error::<T>::NotOpen);
1218		self.ok_to_be_open()?;
1219		Ok(())
1220	}
1221
1222	fn ok_to_unbond_with(
1223		&self,
1224		caller: &T::AccountId,
1225		target_account: &T::AccountId,
1226		target_member: &PoolMember<T>,
1227		unbonding_points: BalanceOf<T>,
1228	) -> Result<(), DispatchError> {
1229		let is_permissioned = caller == target_account;
1230		let is_depositor = *target_account == self.roles.depositor;
1231		let is_full_unbond = unbonding_points == target_member.active_points();
1232
1233		let balance_after_unbond = {
1234			let new_depositor_points =
1235				target_member.active_points().saturating_sub(unbonding_points);
1236			let mut target_member_after_unbond = (*target_member).clone();
1237			target_member_after_unbond.points = new_depositor_points;
1238			target_member_after_unbond.active_balance()
1239		};
1240
1241		// any partial unbonding is only ever allowed if this unbond is permissioned.
1242		ensure!(
1243			is_permissioned || is_full_unbond,
1244			Error::<T>::PartialUnbondNotAllowedPermissionlessly
1245		);
1246
1247		// any unbond must comply with the balance condition:
1248		ensure!(
1249			is_full_unbond ||
1250				balance_after_unbond >=
1251					if is_depositor {
1252						Pallet::<T>::depositor_min_bond()
1253					} else {
1254						MinJoinBond::<T>::get()
1255					},
1256			Error::<T>::MinimumBondNotMet
1257		);
1258
1259		// additional checks:
1260		match (is_permissioned, is_depositor) {
1261			(true, false) => (),
1262			(true, true) => {
1263				// permission depositor unbond: if destroying and pool is empty, always allowed,
1264				// with no additional limits.
1265				if self.is_destroying_and_only_depositor(target_member.active_points()) {
1266					// everything good, let them unbond anything.
1267				} else {
1268					// depositor cannot fully unbond yet.
1269					ensure!(!is_full_unbond, Error::<T>::MinimumBondNotMet);
1270				}
1271			},
1272			(false, false) => {
1273				// If the pool is blocked, then an admin with kicking permissions can remove a
1274				// member. If the pool is being destroyed, anyone can remove a member
1275				debug_assert!(is_full_unbond);
1276				ensure!(
1277					self.can_kick(caller) || self.is_destroying(),
1278					Error::<T>::NotKickerOrDestroying
1279				)
1280			},
1281			(false, true) => {
1282				// the depositor can simply not be unbonded permissionlessly, period.
1283				return Err(Error::<T>::DoesNotHavePermission.into())
1284			},
1285		};
1286
1287		Ok(())
1288	}
1289
1290	/// # Returns
1291	///
1292	/// * Ok(()) if [`Call::withdraw_unbonded`] can be called, `Err(DispatchError)` otherwise.
1293	fn ok_to_withdraw_unbonded_with(
1294		&self,
1295		caller: &T::AccountId,
1296		target_account: &T::AccountId,
1297	) -> Result<(), DispatchError> {
1298		// This isn't a depositor
1299		let is_permissioned = caller == target_account;
1300		ensure!(
1301			is_permissioned || self.can_kick(caller) || self.is_destroying(),
1302			Error::<T>::NotKickerOrDestroying
1303		);
1304		Ok(())
1305	}
1306
1307	/// Bond exactly `amount` from `who`'s funds into this pool. Increases the [`TotalValueLocked`]
1308	/// by `amount`.
1309	///
1310	/// If the bond is [`BondType::Create`], [`Staking::bond`] is called, and `who` is allowed to be
1311	/// killed. Otherwise, [`Staking::bond_extra`] is called and `who` cannot be killed.
1312	///
1313	/// Returns `Ok(points_issues)`, `Err` otherwise.
1314	fn try_bond_funds(
1315		&mut self,
1316		who: &T::AccountId,
1317		amount: BalanceOf<T>,
1318		ty: BondType,
1319	) -> Result<BalanceOf<T>, DispatchError> {
1320		// We must calculate the points issued *before* we bond who's funds, else points:balance
1321		// ratio will be wrong.
1322		let points_issued = self.issue(amount);
1323
1324		T::StakeAdapter::pledge_bond(
1325			Member::from(who.clone()),
1326			Pool::from(self.bonded_account()),
1327			&self.reward_account(),
1328			amount,
1329			ty,
1330		)?;
1331		TotalValueLocked::<T>::mutate(|tvl| {
1332			tvl.saturating_accrue(amount);
1333		});
1334
1335		Ok(points_issued)
1336	}
1337
1338	// Set the state of `self`, and deposit an event if the state changed. State should never be set
1339	// directly in in order to ensure a state change event is always correctly deposited.
1340	fn set_state(&mut self, state: PoolState) {
1341		if self.state != state {
1342			self.state = state;
1343			Pallet::<T>::deposit_event(Event::<T>::StateChanged {
1344				pool_id: self.id,
1345				new_state: state,
1346			});
1347		};
1348	}
1349}
1350
1351/// A reward pool.
1352///
1353/// A reward pool is not so much a pool anymore, since it does not contain any shares or points.
1354/// Rather, simply to fit nicely next to bonded pool and unbonding pools in terms of terminology. In
1355/// reality, a reward pool is just a container for a few pool-dependent data related to the rewards.
1356#[derive(
1357	Encode,
1358	Decode,
1359	MaxEncodedLen,
1360	TypeInfo,
1361	CloneNoBound,
1362	PartialEqNoBound,
1363	EqNoBound,
1364	RuntimeDebugNoBound,
1365)]
1366#[cfg_attr(feature = "std", derive(DefaultNoBound))]
1367#[codec(mel_bound(T: Config))]
1368#[scale_info(skip_type_params(T))]
1369pub struct RewardPool<T: Config> {
1370	/// The last recorded value of the reward counter.
1371	///
1372	/// This is updated ONLY when the points in the bonded pool change, which means `join`,
1373	/// `bond_extra` and `unbond`, all of which is done through `update_recorded`.
1374	pub last_recorded_reward_counter: T::RewardCounter,
1375	/// The last recorded total payouts of the reward pool.
1376	///
1377	/// Payouts is essentially income of the pool.
1378	///
1379	/// Update criteria is same as that of `last_recorded_reward_counter`.
1380	pub last_recorded_total_payouts: BalanceOf<T>,
1381	/// Total amount that this pool has paid out so far to the members.
1382	pub total_rewards_claimed: BalanceOf<T>,
1383	/// The amount of commission pending to be claimed.
1384	pub total_commission_pending: BalanceOf<T>,
1385	/// The amount of commission that has been claimed.
1386	pub total_commission_claimed: BalanceOf<T>,
1387}
1388
1389impl<T: Config> RewardPool<T> {
1390	/// Getter for [`RewardPool::last_recorded_reward_counter`].
1391	pub(crate) fn last_recorded_reward_counter(&self) -> T::RewardCounter {
1392		self.last_recorded_reward_counter
1393	}
1394
1395	/// Register some rewards that are claimed from the pool by the members.
1396	fn register_claimed_reward(&mut self, reward: BalanceOf<T>) {
1397		self.total_rewards_claimed = self.total_rewards_claimed.saturating_add(reward);
1398	}
1399
1400	/// Update the recorded values of the reward pool.
1401	///
1402	/// This function MUST be called whenever the points in the bonded pool change, AND whenever the
1403	/// the pools commission is updated. The reason for the former is that a change in pool points
1404	/// will alter the share of the reward balance among pool members, and the reason for the latter
1405	/// is that a change in commission will alter the share of the reward balance among the pool.
1406	fn update_records(
1407		&mut self,
1408		id: PoolId,
1409		bonded_points: BalanceOf<T>,
1410		commission: Perbill,
1411	) -> Result<(), Error<T>> {
1412		let balance = Self::current_balance(id);
1413
1414		let (current_reward_counter, new_pending_commission) =
1415			self.current_reward_counter(id, bonded_points, commission)?;
1416
1417		// Store the reward counter at the time of this update. This is used in subsequent calls to
1418		// `current_reward_counter`, whereby newly pending rewards (in points) are added to this
1419		// value.
1420		self.last_recorded_reward_counter = current_reward_counter;
1421
1422		// Add any new pending commission that has been calculated from `current_reward_counter` to
1423		// determine the total pending commission at the time of this update.
1424		self.total_commission_pending =
1425			self.total_commission_pending.saturating_add(new_pending_commission);
1426
1427		// Total payouts are essentially the entire historical balance of the reward pool, equating
1428		// to the current balance + the total rewards that have left the pool + the total commission
1429		// that has left the pool.
1430		let last_recorded_total_payouts = balance
1431			.checked_add(&self.total_rewards_claimed.saturating_add(self.total_commission_claimed))
1432			.ok_or(Error::<T>::OverflowRisk)?;
1433
1434		// Store the total payouts at the time of this update.
1435		//
1436		// An increase in ED could cause `last_recorded_total_payouts` to decrease but we should not
1437		// allow that to happen since an already paid out reward cannot decrease. The reward account
1438		// might go in deficit temporarily in this exceptional case but it will be corrected once
1439		// new rewards are added to the pool.
1440		self.last_recorded_total_payouts =
1441			self.last_recorded_total_payouts.max(last_recorded_total_payouts);
1442
1443		Ok(())
1444	}
1445
1446	/// Get the current reward counter, based on the given `bonded_points` being the state of the
1447	/// bonded pool at this time.
1448	fn current_reward_counter(
1449		&self,
1450		id: PoolId,
1451		bonded_points: BalanceOf<T>,
1452		commission: Perbill,
1453	) -> Result<(T::RewardCounter, BalanceOf<T>), Error<T>> {
1454		let balance = Self::current_balance(id);
1455
1456		// Calculate the current payout balance. The first 3 values of this calculation added
1457		// together represent what the balance would be if no payouts were made. The
1458		// `last_recorded_total_payouts` is then subtracted from this value to cancel out previously
1459		// recorded payouts, leaving only the remaining payouts that have not been claimed.
1460		let current_payout_balance = balance
1461			.saturating_add(self.total_rewards_claimed)
1462			.saturating_add(self.total_commission_claimed)
1463			.saturating_sub(self.last_recorded_total_payouts);
1464
1465		// Split the `current_payout_balance` into claimable rewards and claimable commission
1466		// according to the current commission rate.
1467		let new_pending_commission = commission * current_payout_balance;
1468		let new_pending_rewards = current_payout_balance.saturating_sub(new_pending_commission);
1469
1470		// * accuracy notes regarding the multiplication in `checked_from_rational`:
1471		// `current_payout_balance` is a subset of the total_issuance at the very worse.
1472		// `bonded_points` are similarly, in a non-slashed pool, have the same granularity as
1473		// balance, and are thus below within the range of total_issuance. In the worse case
1474		// scenario, for `saturating_from_rational`, we have:
1475		//
1476		// dot_total_issuance * 10^18 / `minJoinBond`
1477		//
1478		// assuming `MinJoinBond == ED`
1479		//
1480		// dot_total_issuance * 10^18 / 10^10 = dot_total_issuance * 10^8
1481		//
1482		// which, with the current numbers, is a miniscule fraction of the u128 capacity.
1483		//
1484		// Thus, adding two values of type reward counter should be safe for ages in a chain like
1485		// Polkadot. The important note here is that `reward_pool.last_recorded_reward_counter` only
1486		// ever accumulates, but its semantics imply that it is less than total_issuance, when
1487		// represented as `FixedU128`, which means it is less than `total_issuance * 10^18`.
1488		//
1489		// * accuracy notes regarding `checked_from_rational` collapsing to zero, meaning that no
1490		//   reward can be claimed:
1491		//
1492		// largest `bonded_points`, such that the reward counter is non-zero, with `FixedU128` will
1493		// be when the payout is being computed. This essentially means `payout/bonded_points` needs
1494		// to be more than 1/1^18. Thus, assuming that `bonded_points` will always be less than `10
1495		// * dot_total_issuance`, if the reward_counter is the smallest possible value, the value of
1496		//   the
1497		// reward being calculated is:
1498		//
1499		// x / 10^20 = 1/ 10^18
1500		//
1501		// x = 100
1502		//
1503		// which is basically 10^-8 DOTs. See `smallest_claimable_reward` for an example of this.
1504		let current_reward_counter =
1505			T::RewardCounter::checked_from_rational(new_pending_rewards, bonded_points)
1506				.and_then(|ref r| self.last_recorded_reward_counter.checked_add(r))
1507				.ok_or(Error::<T>::OverflowRisk)?;
1508
1509		Ok((current_reward_counter, new_pending_commission))
1510	}
1511
1512	/// Current free balance of the reward pool.
1513	///
1514	/// This is sum of all the rewards that are claimable by pool members.
1515	fn current_balance(id: PoolId) -> BalanceOf<T> {
1516		T::Currency::reducible_balance(
1517			&Pallet::<T>::generate_reward_account(id),
1518			Preservation::Expendable,
1519			Fortitude::Polite,
1520		)
1521	}
1522}
1523
1524/// An unbonding pool. This is always mapped with an era.
1525#[derive(
1526	Encode,
1527	Decode,
1528	MaxEncodedLen,
1529	TypeInfo,
1530	DefaultNoBound,
1531	RuntimeDebugNoBound,
1532	CloneNoBound,
1533	PartialEqNoBound,
1534	EqNoBound,
1535)]
1536#[codec(mel_bound(T: Config))]
1537#[scale_info(skip_type_params(T))]
1538pub struct UnbondPool<T: Config> {
1539	/// The points in this pool.
1540	pub points: BalanceOf<T>,
1541	/// The funds in the pool.
1542	pub balance: BalanceOf<T>,
1543}
1544
1545impl<T: Config> UnbondPool<T> {
1546	fn balance_to_point(&self, new_funds: BalanceOf<T>) -> BalanceOf<T> {
1547		Pallet::<T>::balance_to_point(self.balance, self.points, new_funds)
1548	}
1549
1550	fn point_to_balance(&self, points: BalanceOf<T>) -> BalanceOf<T> {
1551		Pallet::<T>::point_to_balance(self.balance, self.points, points)
1552	}
1553
1554	/// Issue the equivalent points of `new_funds` into self.
1555	///
1556	/// Returns the actual amounts of points issued.
1557	fn issue(&mut self, new_funds: BalanceOf<T>) -> BalanceOf<T> {
1558		let new_points = self.balance_to_point(new_funds);
1559		self.points = self.points.saturating_add(new_points);
1560		self.balance = self.balance.saturating_add(new_funds);
1561		new_points
1562	}
1563
1564	/// Dissolve some points from the unbonding pool, reducing the balance of the pool
1565	/// proportionally. This is the opposite of `issue`.
1566	///
1567	/// Returns the actual amount of `Balance` that was removed from the pool.
1568	fn dissolve(&mut self, points: BalanceOf<T>) -> BalanceOf<T> {
1569		let balance_to_unbond = self.point_to_balance(points);
1570		self.points = self.points.saturating_sub(points);
1571		self.balance = self.balance.saturating_sub(balance_to_unbond);
1572
1573		balance_to_unbond
1574	}
1575}
1576
1577#[derive(
1578	Encode,
1579	Decode,
1580	MaxEncodedLen,
1581	TypeInfo,
1582	DefaultNoBound,
1583	RuntimeDebugNoBound,
1584	CloneNoBound,
1585	PartialEqNoBound,
1586	EqNoBound,
1587)]
1588#[codec(mel_bound(T: Config))]
1589#[scale_info(skip_type_params(T))]
1590pub struct SubPools<T: Config> {
1591	/// A general, era agnostic pool of funds that have fully unbonded. The pools
1592	/// of `Self::with_era` will lazily be merged into into this pool if they are
1593	/// older then `current_era - TotalUnbondingPools`.
1594	pub no_era: UnbondPool<T>,
1595	/// Map of era in which a pool becomes unbonded in => unbond pools.
1596	pub with_era: BoundedBTreeMap<EraIndex, UnbondPool<T>, TotalUnbondingPools<T>>,
1597}
1598
1599impl<T: Config> SubPools<T> {
1600	/// Merge the oldest `with_era` unbond pools into the `no_era` unbond pool.
1601	///
1602	/// This is often used whilst getting the sub-pool from storage, thus it consumes and returns
1603	/// `Self` for ergonomic purposes.
1604	fn maybe_merge_pools(mut self, current_era: EraIndex) -> Self {
1605		// Ex: if `TotalUnbondingPools` is 5 and current era is 10, we only want to retain pools
1606		// 6..=10. Note that in the first few eras where `checked_sub` is `None`, we don't remove
1607		// anything.
1608		if let Some(newest_era_to_remove) =
1609			current_era.checked_sub(T::PostUnbondingPoolsWindow::get())
1610		{
1611			self.with_era.retain(|k, v| {
1612				if *k > newest_era_to_remove {
1613					// keep
1614					true
1615				} else {
1616					// merge into the no-era pool
1617					self.no_era.points = self.no_era.points.saturating_add(v.points);
1618					self.no_era.balance = self.no_era.balance.saturating_add(v.balance);
1619					false
1620				}
1621			});
1622		}
1623
1624		self
1625	}
1626
1627	/// The sum of all unbonding balance, regardless of whether they are actually unlocked or not.
1628	#[cfg(any(feature = "try-runtime", feature = "fuzzing", test, debug_assertions))]
1629	fn sum_unbonding_balance(&self) -> BalanceOf<T> {
1630		self.no_era.balance.saturating_add(
1631			self.with_era
1632				.values()
1633				.fold(BalanceOf::<T>::zero(), |acc, pool| acc.saturating_add(pool.balance)),
1634		)
1635	}
1636}
1637
1638/// The maximum amount of eras an unbonding pool can exist prior to being merged with the
1639/// `no_era` pool. This is guaranteed to at least be equal to the staking `UnbondingDuration`. For
1640/// improved UX [`Config::PostUnbondingPoolsWindow`] should be configured to a non-zero value.
1641pub struct TotalUnbondingPools<T: Config>(PhantomData<T>);
1642
1643impl<T: Config> Get<u32> for TotalUnbondingPools<T> {
1644	fn get() -> u32 {
1645		// NOTE: this may be dangerous in the scenario bonding_duration gets decreased because
1646		// we would no longer be able to decode `BoundedBTreeMap::<EraIndex, UnbondPool<T>,
1647		// TotalUnbondingPools<T>>`, which uses `TotalUnbondingPools` as the bound
1648		T::StakeAdapter::bonding_duration() + T::PostUnbondingPoolsWindow::get()
1649	}
1650}
1651
1652#[frame_support::pallet]
1653pub mod pallet {
1654	use super::*;
1655	use frame_support::traits::StorageVersion;
1656	use frame_system::pallet_prelude::{
1657		ensure_root, ensure_signed, BlockNumberFor as SystemBlockNumberFor, OriginFor,
1658	};
1659	use sp_runtime::Perbill;
1660
1661	/// The in-code storage version.
1662	const STORAGE_VERSION: StorageVersion = StorageVersion::new(8);
1663
1664	#[pallet::pallet]
1665	#[pallet::storage_version(STORAGE_VERSION)]
1666	pub struct Pallet<T>(_);
1667
1668	#[pallet::config]
1669	pub trait Config: frame_system::Config {
1670		/// The overarching event type.
1671		#[allow(deprecated)]
1672		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
1673
1674		/// Weight information for extrinsics in this pallet.
1675		type WeightInfo: weights::WeightInfo;
1676
1677		/// The currency type used for nomination pool.
1678		type Currency: Mutate<Self::AccountId>
1679			+ MutateFreeze<Self::AccountId, Id = Self::RuntimeFreezeReason>;
1680
1681		/// The overarching freeze reason.
1682		type RuntimeFreezeReason: From<FreezeReason>;
1683
1684		/// The type that is used for reward counter.
1685		///
1686		/// The arithmetic of the reward counter might saturate based on the size of the
1687		/// `Currency::Balance`. If this happens, operations fails. Nonetheless, this type should be
1688		/// chosen such that this failure almost never happens, as if it happens, the pool basically
1689		/// needs to be dismantled (or all pools migrated to a larger `RewardCounter` type, which is
1690		/// a PITA to do).
1691		///
1692		/// See the inline code docs of `Member::pending_rewards` and `RewardPool::update_recorded`
1693		/// for example analysis. A [`sp_runtime::FixedU128`] should be fine for chains with balance
1694		/// types similar to that of Polkadot and Kusama, in the absence of severe slashing (or
1695		/// prevented via a reasonable `MaxPointsToBalance`), for many many years to come.
1696		type RewardCounter: FixedPointNumber + MaxEncodedLen + TypeInfo + Default + codec::FullCodec;
1697
1698		/// The nomination pool's pallet id.
1699		#[pallet::constant]
1700		type PalletId: Get<frame_support::PalletId>;
1701
1702		/// The maximum pool points-to-balance ratio that an `open` pool can have.
1703		///
1704		/// This is important in the event slashing takes place and the pool's points-to-balance
1705		/// ratio becomes disproportional.
1706		///
1707		/// Moreover, this relates to the `RewardCounter` type as well, as the arithmetic operations
1708		/// are a function of number of points, and by setting this value to e.g. 10, you ensure
1709		/// that the total number of points in the system are at most 10 times the total_issuance of
1710		/// the chain, in the absolute worse case.
1711		///
1712		/// For a value of 10, the threshold would be a pool points-to-balance ratio of 10:1.
1713		/// Such a scenario would also be the equivalent of the pool being 90% slashed.
1714		#[pallet::constant]
1715		type MaxPointsToBalance: Get<u8>;
1716
1717		/// The maximum number of simultaneous unbonding chunks that can exist per member.
1718		#[pallet::constant]
1719		type MaxUnbonding: Get<u32>;
1720
1721		/// Infallible method for converting `Currency::Balance` to `U256`.
1722		type BalanceToU256: Convert<BalanceOf<Self>, U256>;
1723
1724		/// Infallible method for converting `U256` to `Currency::Balance`.
1725		type U256ToBalance: Convert<U256, BalanceOf<Self>>;
1726
1727		/// The interface for nominating.
1728		///
1729		/// Note: Switching to a new [`StakeStrategy`] might require a migration of the storage.
1730		type StakeAdapter: StakeStrategy<AccountId = Self::AccountId, Balance = BalanceOf<Self>>;
1731
1732		/// The amount of eras a `SubPools::with_era` pool can exist before it gets merged into the
1733		/// `SubPools::no_era` pool. In other words, this is the amount of eras a member will be
1734		/// able to withdraw from an unbonding pool which is guaranteed to have the correct ratio of
1735		/// points to balance; once the `with_era` pool is merged into the `no_era` pool, the ratio
1736		/// can become skewed due to some slashed ratio getting merged in at some point.
1737		type PostUnbondingPoolsWindow: Get<u32>;
1738
1739		/// The maximum length, in bytes, that a pools metadata maybe.
1740		type MaxMetadataLen: Get<u32>;
1741
1742		/// The origin that can manage pool configurations.
1743		type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;
1744
1745		/// Provider for the block number. Normally this is the `frame_system` pallet.
1746		type BlockNumberProvider: BlockNumberProvider;
1747
1748		/// Restrict some accounts from participating in a nomination pool.
1749		type Filter: Contains<Self::AccountId>;
1750	}
1751
1752	/// The sum of funds across all pools.
1753	///
1754	/// This might be lower but never higher than the sum of `total_balance` of all [`PoolMembers`]
1755	/// because calling `pool_withdraw_unbonded` might decrease the total stake of the pool's
1756	/// `bonded_account` without adjusting the pallet-internal `UnbondingPool`'s.
1757	#[pallet::storage]
1758	pub type TotalValueLocked<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
1759
1760	/// Minimum amount to bond to join a pool.
1761	#[pallet::storage]
1762	pub type MinJoinBond<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
1763
1764	/// Minimum bond required to create a pool.
1765	///
1766	/// This is the amount that the depositor must put as their initial stake in the pool, as an
1767	/// indication of "skin in the game".
1768	///
1769	/// This is the value that will always exist in the staking ledger of the pool bonded account
1770	/// while all other accounts leave.
1771	#[pallet::storage]
1772	pub type MinCreateBond<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
1773
1774	/// Maximum number of nomination pools that can exist. If `None`, then an unbounded number of
1775	/// pools can exist.
1776	#[pallet::storage]
1777	pub type MaxPools<T: Config> = StorageValue<_, u32, OptionQuery>;
1778
1779	/// Maximum number of members that can exist in the system. If `None`, then the count
1780	/// members are not bound on a system wide basis.
1781	#[pallet::storage]
1782	pub type MaxPoolMembers<T: Config> = StorageValue<_, u32, OptionQuery>;
1783
1784	/// Maximum number of members that may belong to pool. If `None`, then the count of
1785	/// members is not bound on a per pool basis.
1786	#[pallet::storage]
1787	pub type MaxPoolMembersPerPool<T: Config> = StorageValue<_, u32, OptionQuery>;
1788
1789	/// The maximum commission that can be charged by a pool. Used on commission payouts to bound
1790	/// pool commissions that are > `GlobalMaxCommission`, necessary if a future
1791	/// `GlobalMaxCommission` is lower than some current pool commissions.
1792	#[pallet::storage]
1793	pub type GlobalMaxCommission<T: Config> = StorageValue<_, Perbill, OptionQuery>;
1794
1795	/// Active members.
1796	///
1797	/// TWOX-NOTE: SAFE since `AccountId` is a secure hash.
1798	#[pallet::storage]
1799	pub type PoolMembers<T: Config> =
1800		CountedStorageMap<_, Twox64Concat, T::AccountId, PoolMember<T>>;
1801
1802	/// Storage for bonded pools.
1803	// To get or insert a pool see [`BondedPool::get`] and [`BondedPool::put`]
1804	#[pallet::storage]
1805	pub type BondedPools<T: Config> =
1806		CountedStorageMap<_, Twox64Concat, PoolId, BondedPoolInner<T>>;
1807
1808	/// Reward pools. This is where there rewards for each pool accumulate. When a members payout is
1809	/// claimed, the balance comes out of the reward pool. Keyed by the bonded pools account.
1810	#[pallet::storage]
1811	pub type RewardPools<T: Config> = CountedStorageMap<_, Twox64Concat, PoolId, RewardPool<T>>;
1812
1813	/// Groups of unbonding pools. Each group of unbonding pools belongs to a
1814	/// bonded pool, hence the name sub-pools. Keyed by the bonded pools account.
1815	#[pallet::storage]
1816	pub type SubPoolsStorage<T: Config> = CountedStorageMap<_, Twox64Concat, PoolId, SubPools<T>>;
1817
1818	/// Metadata for the pool.
1819	#[pallet::storage]
1820	pub type Metadata<T: Config> =
1821		CountedStorageMap<_, Twox64Concat, PoolId, BoundedVec<u8, T::MaxMetadataLen>, ValueQuery>;
1822
1823	/// Ever increasing number of all pools created so far.
1824	#[pallet::storage]
1825	pub type LastPoolId<T: Config> = StorageValue<_, u32, ValueQuery>;
1826
1827	/// A reverse lookup from the pool's account id to its id.
1828	///
1829	/// This is only used for slashing and on automatic withdraw update. In all other instances, the
1830	/// pool id is used, and the accounts are deterministically derived from it.
1831	#[pallet::storage]
1832	pub type ReversePoolIdLookup<T: Config> =
1833		CountedStorageMap<_, Twox64Concat, T::AccountId, PoolId, OptionQuery>;
1834
1835	/// Map from a pool member account to their opted claim permission.
1836	#[pallet::storage]
1837	pub type ClaimPermissions<T: Config> =
1838		StorageMap<_, Twox64Concat, T::AccountId, ClaimPermission, ValueQuery>;
1839
1840	#[pallet::genesis_config]
1841	pub struct GenesisConfig<T: Config> {
1842		pub min_join_bond: BalanceOf<T>,
1843		pub min_create_bond: BalanceOf<T>,
1844		pub max_pools: Option<u32>,
1845		pub max_members_per_pool: Option<u32>,
1846		pub max_members: Option<u32>,
1847		pub global_max_commission: Option<Perbill>,
1848	}
1849
1850	impl<T: Config> Default for GenesisConfig<T> {
1851		fn default() -> Self {
1852			Self {
1853				min_join_bond: Zero::zero(),
1854				min_create_bond: Zero::zero(),
1855				max_pools: Some(16),
1856				max_members_per_pool: Some(32),
1857				max_members: Some(16 * 32),
1858				global_max_commission: None,
1859			}
1860		}
1861	}
1862
1863	#[pallet::genesis_build]
1864	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
1865		fn build(&self) {
1866			MinJoinBond::<T>::put(self.min_join_bond);
1867			MinCreateBond::<T>::put(self.min_create_bond);
1868
1869			if let Some(max_pools) = self.max_pools {
1870				MaxPools::<T>::put(max_pools);
1871			}
1872			if let Some(max_members_per_pool) = self.max_members_per_pool {
1873				MaxPoolMembersPerPool::<T>::put(max_members_per_pool);
1874			}
1875			if let Some(max_members) = self.max_members {
1876				MaxPoolMembers::<T>::put(max_members);
1877			}
1878			if let Some(global_max_commission) = self.global_max_commission {
1879				GlobalMaxCommission::<T>::put(global_max_commission);
1880			}
1881		}
1882	}
1883
1884	/// Events of this pallet.
1885	#[pallet::event]
1886	#[pallet::generate_deposit(pub(crate) fn deposit_event)]
1887	pub enum Event<T: Config> {
1888		/// A pool has been created.
1889		Created { depositor: T::AccountId, pool_id: PoolId },
1890		/// A member has became bonded in a pool.
1891		Bonded { member: T::AccountId, pool_id: PoolId, bonded: BalanceOf<T>, joined: bool },
1892		/// A payout has been made to a member.
1893		PaidOut { member: T::AccountId, pool_id: PoolId, payout: BalanceOf<T> },
1894		/// A member has unbonded from their pool.
1895		///
1896		/// - `balance` is the corresponding balance of the number of points that has been
1897		///   requested to be unbonded (the argument of the `unbond` transaction) from the bonded
1898		///   pool.
1899		/// - `points` is the number of points that are issued as a result of `balance` being
1900		/// dissolved into the corresponding unbonding pool.
1901		/// - `era` is the era in which the balance will be unbonded.
1902		/// In the absence of slashing, these values will match. In the presence of slashing, the
1903		/// number of points that are issued in the unbonding pool will be less than the amount
1904		/// requested to be unbonded.
1905		Unbonded {
1906			member: T::AccountId,
1907			pool_id: PoolId,
1908			balance: BalanceOf<T>,
1909			points: BalanceOf<T>,
1910			era: EraIndex,
1911		},
1912		/// A member has withdrawn from their pool.
1913		///
1914		/// The given number of `points` have been dissolved in return of `balance`.
1915		///
1916		/// Similar to `Unbonded` event, in the absence of slashing, the ratio of point to balance
1917		/// will be 1.
1918		Withdrawn {
1919			member: T::AccountId,
1920			pool_id: PoolId,
1921			balance: BalanceOf<T>,
1922			points: BalanceOf<T>,
1923		},
1924		/// A pool has been destroyed.
1925		Destroyed { pool_id: PoolId },
1926		/// The state of a pool has changed
1927		StateChanged { pool_id: PoolId, new_state: PoolState },
1928		/// A member has been removed from a pool.
1929		///
1930		/// The removal can be voluntary (withdrawn all unbonded funds) or involuntary (kicked).
1931		/// Any funds that are still delegated (i.e. dangling delegation) are released and are
1932		/// represented by `released_balance`.
1933		MemberRemoved { pool_id: PoolId, member: T::AccountId, released_balance: BalanceOf<T> },
1934		/// The roles of a pool have been updated to the given new roles. Note that the depositor
1935		/// can never change.
1936		RolesUpdated {
1937			root: Option<T::AccountId>,
1938			bouncer: Option<T::AccountId>,
1939			nominator: Option<T::AccountId>,
1940		},
1941		/// The active balance of pool `pool_id` has been slashed to `balance`.
1942		PoolSlashed { pool_id: PoolId, balance: BalanceOf<T> },
1943		/// The unbond pool at `era` of pool `pool_id` has been slashed to `balance`.
1944		UnbondingPoolSlashed { pool_id: PoolId, era: EraIndex, balance: BalanceOf<T> },
1945		/// A pool's commission setting has been changed.
1946		PoolCommissionUpdated { pool_id: PoolId, current: Option<(Perbill, T::AccountId)> },
1947		/// A pool's maximum commission setting has been changed.
1948		PoolMaxCommissionUpdated { pool_id: PoolId, max_commission: Perbill },
1949		/// A pool's commission `change_rate` has been changed.
1950		PoolCommissionChangeRateUpdated {
1951			pool_id: PoolId,
1952			change_rate: CommissionChangeRate<BlockNumberFor<T>>,
1953		},
1954		/// Pool commission claim permission has been updated.
1955		PoolCommissionClaimPermissionUpdated {
1956			pool_id: PoolId,
1957			permission: Option<CommissionClaimPermission<T::AccountId>>,
1958		},
1959		/// Pool commission has been claimed.
1960		PoolCommissionClaimed { pool_id: PoolId, commission: BalanceOf<T> },
1961		/// Topped up deficit in frozen ED of the reward pool.
1962		MinBalanceDeficitAdjusted { pool_id: PoolId, amount: BalanceOf<T> },
1963		/// Claimed excess frozen ED of af the reward pool.
1964		MinBalanceExcessAdjusted { pool_id: PoolId, amount: BalanceOf<T> },
1965		/// A pool member's claim permission has been updated.
1966		MemberClaimPermissionUpdated { member: T::AccountId, permission: ClaimPermission },
1967		/// A pool's metadata was updated.
1968		MetadataUpdated { pool_id: PoolId, caller: T::AccountId },
1969		/// A pool's nominating account (or the pool's root account) has nominated a validator set
1970		/// on behalf of the pool.
1971		PoolNominationMade { pool_id: PoolId, caller: T::AccountId },
1972		/// The pool is chilled i.e. no longer nominating.
1973		PoolNominatorChilled { pool_id: PoolId, caller: T::AccountId },
1974		/// Global parameters regulating nomination pools have been updated.
1975		GlobalParamsUpdated {
1976			min_join_bond: BalanceOf<T>,
1977			min_create_bond: BalanceOf<T>,
1978			max_pools: Option<u32>,
1979			max_members: Option<u32>,
1980			max_members_per_pool: Option<u32>,
1981			global_max_commission: Option<Perbill>,
1982		},
1983	}
1984
1985	#[pallet::error]
1986	#[cfg_attr(test, derive(PartialEq))]
1987	pub enum Error<T> {
1988		/// A (bonded) pool id does not exist.
1989		PoolNotFound,
1990		/// An account is not a member.
1991		PoolMemberNotFound,
1992		/// A reward pool does not exist. In all cases this is a system logic error.
1993		RewardPoolNotFound,
1994		/// A sub pool does not exist.
1995		SubPoolsNotFound,
1996		/// An account is already delegating in another pool. An account may only belong to one
1997		/// pool at a time.
1998		AccountBelongsToOtherPool,
1999		/// The member is fully unbonded (and thus cannot access the bonded and reward pool
2000		/// anymore to, for example, collect rewards).
2001		FullyUnbonding,
2002		/// The member cannot unbond further chunks due to reaching the limit.
2003		MaxUnbondingLimit,
2004		/// None of the funds can be withdrawn yet because the bonding duration has not passed.
2005		CannotWithdrawAny,
2006		/// The amount does not meet the minimum bond to either join or create a pool.
2007		///
2008		/// The depositor can never unbond to a value less than `Pallet::depositor_min_bond`. The
2009		/// caller does not have nominating permissions for the pool. Members can never unbond to a
2010		/// value below `MinJoinBond`.
2011		MinimumBondNotMet,
2012		/// The transaction could not be executed due to overflow risk for the pool.
2013		OverflowRisk,
2014		/// A pool must be in [`PoolState::Destroying`] in order for the depositor to unbond or for
2015		/// other members to be permissionlessly unbonded.
2016		NotDestroying,
2017		/// The caller does not have nominating permissions for the pool.
2018		NotNominator,
2019		/// Either a) the caller cannot make a valid kick or b) the pool is not destroying.
2020		NotKickerOrDestroying,
2021		/// The pool is not open to join
2022		NotOpen,
2023		/// The system is maxed out on pools.
2024		MaxPools,
2025		/// Too many members in the pool or system.
2026		MaxPoolMembers,
2027		/// The pools state cannot be changed.
2028		CanNotChangeState,
2029		/// The caller does not have adequate permissions.
2030		DoesNotHavePermission,
2031		/// Metadata exceeds [`Config::MaxMetadataLen`]
2032		MetadataExceedsMaxLen,
2033		/// Some error occurred that should never happen. This should be reported to the
2034		/// maintainers.
2035		Defensive(DefensiveError),
2036		/// Partial unbonding now allowed permissionlessly.
2037		PartialUnbondNotAllowedPermissionlessly,
2038		/// The pool's max commission cannot be set higher than the existing value.
2039		MaxCommissionRestricted,
2040		/// The supplied commission exceeds the max allowed commission.
2041		CommissionExceedsMaximum,
2042		/// The supplied commission exceeds global maximum commission.
2043		CommissionExceedsGlobalMaximum,
2044		/// Not enough blocks have surpassed since the last commission update.
2045		CommissionChangeThrottled,
2046		/// The submitted changes to commission change rate are not allowed.
2047		CommissionChangeRateNotAllowed,
2048		/// There is no pending commission to claim.
2049		NoPendingCommission,
2050		/// No commission current has been set.
2051		NoCommissionCurrentSet,
2052		/// Pool id currently in use.
2053		PoolIdInUse,
2054		/// Pool id provided is not correct/usable.
2055		InvalidPoolId,
2056		/// Bonding extra is restricted to the exact pending reward amount.
2057		BondExtraRestricted,
2058		/// No imbalance in the ED deposit for the pool.
2059		NothingToAdjust,
2060		/// No slash pending that can be applied to the member.
2061		NothingToSlash,
2062		/// The slash amount is too low to be applied.
2063		SlashTooLow,
2064		/// The pool or member delegation has already migrated to delegate stake.
2065		AlreadyMigrated,
2066		/// The pool or member delegation has not migrated yet to delegate stake.
2067		NotMigrated,
2068		/// This call is not allowed in the current state of the pallet.
2069		NotSupported,
2070		/// Account is restricted from participation in pools. This may happen if the account is
2071		/// staking in another way already.
2072		Restricted,
2073	}
2074
2075	#[derive(
2076		Encode, Decode, DecodeWithMemTracking, PartialEq, TypeInfo, PalletError, RuntimeDebug,
2077	)]
2078	pub enum DefensiveError {
2079		/// There isn't enough space in the unbond pool.
2080		NotEnoughSpaceInUnbondPool,
2081		/// A (bonded) pool id does not exist.
2082		PoolNotFound,
2083		/// A reward pool does not exist. In all cases this is a system logic error.
2084		RewardPoolNotFound,
2085		/// A sub pool does not exist.
2086		SubPoolsNotFound,
2087		/// The bonded account should only be killed by the staking system when the depositor is
2088		/// withdrawing
2089		BondedStashKilledPrematurely,
2090		/// The delegation feature is unsupported.
2091		DelegationUnsupported,
2092		/// Unable to slash to the member of the pool.
2093		SlashNotApplied,
2094	}
2095
2096	impl<T> From<DefensiveError> for Error<T> {
2097		fn from(e: DefensiveError) -> Error<T> {
2098			Error::<T>::Defensive(e)
2099		}
2100	}
2101
2102	/// A reason for freezing funds.
2103	#[pallet::composite_enum]
2104	pub enum FreezeReason {
2105		/// Pool reward account is restricted from going below Existential Deposit.
2106		#[codec(index = 0)]
2107		PoolMinBalance,
2108	}
2109
2110	#[pallet::call]
2111	impl<T: Config> Pallet<T> {
2112		/// Stake funds with a pool. The amount to bond is delegated (or transferred based on
2113		/// [`adapter::StakeStrategyType`]) from the member to the pool account and immediately
2114		/// increases the pool's bond.
2115		///
2116		/// The method of transferring the amount to the pool account is determined by
2117		/// [`adapter::StakeStrategyType`]. If the pool is configured to use
2118		/// [`adapter::StakeStrategyType::Delegate`], the funds remain in the account of
2119		/// the `origin`, while the pool gains the right to use these funds for staking.
2120		///
2121		/// # Note
2122		///
2123		/// * An account can only be a member of a single pool.
2124		/// * An account cannot join the same pool multiple times.
2125		/// * This call will *not* dust the member account, so the member must have at least
2126		///   `existential deposit + amount` in their account.
2127		/// * Only a pool with [`PoolState::Open`] can be joined
2128		#[pallet::call_index(0)]
2129		#[pallet::weight(T::WeightInfo::join())]
2130		pub fn join(
2131			origin: OriginFor<T>,
2132			#[pallet::compact] amount: BalanceOf<T>,
2133			pool_id: PoolId,
2134		) -> DispatchResult {
2135			let who = ensure_signed(origin)?;
2136			// ensure pool is not in an un-migrated state.
2137			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
2138
2139			// ensure account is not restricted from joining the pool.
2140			ensure!(!T::Filter::contains(&who), Error::<T>::Restricted);
2141
2142			ensure!(amount >= MinJoinBond::<T>::get(), Error::<T>::MinimumBondNotMet);
2143			// If a member already exists that means they already belong to a pool
2144			ensure!(!PoolMembers::<T>::contains_key(&who), Error::<T>::AccountBelongsToOtherPool);
2145
2146			let mut bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
2147			bonded_pool.ok_to_join()?;
2148
2149			let mut reward_pool = RewardPools::<T>::get(pool_id)
2150				.defensive_ok_or::<Error<T>>(DefensiveError::RewardPoolNotFound.into())?;
2151			// IMPORTANT: reward pool records must be updated with the old points.
2152			reward_pool.update_records(
2153				pool_id,
2154				bonded_pool.points,
2155				bonded_pool.commission.current(),
2156			)?;
2157
2158			bonded_pool.try_inc_members()?;
2159			let points_issued = bonded_pool.try_bond_funds(&who, amount, BondType::Extra)?;
2160
2161			PoolMembers::insert(
2162				who.clone(),
2163				PoolMember::<T> {
2164					pool_id,
2165					points: points_issued,
2166					// we just updated `last_known_reward_counter` to the current one in
2167					// `update_recorded`.
2168					last_recorded_reward_counter: reward_pool.last_recorded_reward_counter(),
2169					unbonding_eras: Default::default(),
2170				},
2171			);
2172
2173			Self::deposit_event(Event::<T>::Bonded {
2174				member: who,
2175				pool_id,
2176				bonded: amount,
2177				joined: true,
2178			});
2179
2180			bonded_pool.put();
2181			RewardPools::<T>::insert(pool_id, reward_pool);
2182
2183			Ok(())
2184		}
2185
2186		/// Bond `extra` more funds from `origin` into the pool to which they already belong.
2187		///
2188		/// Additional funds can come from either the free balance of the account, of from the
2189		/// accumulated rewards, see [`BondExtra`].
2190		///
2191		/// Bonding extra funds implies an automatic payout of all pending rewards as well.
2192		/// See `bond_extra_other` to bond pending rewards of `other` members.
2193		// NOTE: this transaction is implemented with the sole purpose of readability and
2194		// correctness, not optimization. We read/write several storage items multiple times instead
2195		// of just once, in the spirit reusing code.
2196		#[pallet::call_index(1)]
2197		#[pallet::weight(
2198			T::WeightInfo::bond_extra_transfer()
2199			.max(T::WeightInfo::bond_extra_other())
2200		)]
2201		pub fn bond_extra(origin: OriginFor<T>, extra: BondExtra<BalanceOf<T>>) -> DispatchResult {
2202			let who = ensure_signed(origin)?;
2203
2204			// ensure who is not in an un-migrated state.
2205			ensure!(
2206				!Self::api_member_needs_delegate_migration(who.clone()),
2207				Error::<T>::NotMigrated
2208			);
2209
2210			Self::do_bond_extra(who.clone(), who, extra)
2211		}
2212
2213		/// A bonded member can use this to claim their payout based on the rewards that the pool
2214		/// has accumulated since their last claimed payout (OR since joining if this is their first
2215		/// time claiming rewards). The payout will be transferred to the member's account.
2216		///
2217		/// The member will earn rewards pro rata based on the members stake vs the sum of the
2218		/// members in the pools stake. Rewards do not "expire".
2219		///
2220		/// See `claim_payout_other` to claim rewards on behalf of some `other` pool member.
2221		#[pallet::call_index(2)]
2222		#[pallet::weight(T::WeightInfo::claim_payout())]
2223		pub fn claim_payout(origin: OriginFor<T>) -> DispatchResult {
2224			let signer = ensure_signed(origin)?;
2225			// ensure signer is not in an un-migrated state.
2226			ensure!(
2227				!Self::api_member_needs_delegate_migration(signer.clone()),
2228				Error::<T>::NotMigrated
2229			);
2230
2231			Self::do_claim_payout(signer.clone(), signer)
2232		}
2233
2234		/// Unbond up to `unbonding_points` of the `member_account`'s funds from the pool. It
2235		/// implicitly collects the rewards one last time, since not doing so would mean some
2236		/// rewards would be forfeited.
2237		///
2238		/// Under certain conditions, this call can be dispatched permissionlessly (i.e. by any
2239		/// account).
2240		///
2241		/// # Conditions for a permissionless dispatch.
2242		///
2243		/// * The pool is blocked and the caller is either the root or bouncer. This is refereed to
2244		///   as a kick.
2245		/// * The pool is destroying and the member is not the depositor.
2246		/// * The pool is destroying, the member is the depositor and no other members are in the
2247		///   pool.
2248		///
2249		/// ## Conditions for permissioned dispatch (i.e. the caller is also the
2250		/// `member_account`):
2251		///
2252		/// * The caller is not the depositor.
2253		/// * The caller is the depositor, the pool is destroying and no other members are in the
2254		///   pool.
2255		///
2256		/// # Note
2257		///
2258		/// If there are too many unlocking chunks to unbond with the pool account,
2259		/// [`Call::pool_withdraw_unbonded`] can be called to try and minimize unlocking chunks.
2260		/// The [`StakingInterface::unbond`] will implicitly call [`Call::pool_withdraw_unbonded`]
2261		/// to try to free chunks if necessary (ie. if unbound was called and no unlocking chunks
2262		/// are available). However, it may not be possible to release the current unlocking chunks,
2263		/// in which case, the result of this call will likely be the `NoMoreChunks` error from the
2264		/// staking system.
2265		#[pallet::call_index(3)]
2266		#[pallet::weight(T::WeightInfo::unbond())]
2267		pub fn unbond(
2268			origin: OriginFor<T>,
2269			member_account: AccountIdLookupOf<T>,
2270			#[pallet::compact] unbonding_points: BalanceOf<T>,
2271		) -> DispatchResult {
2272			let who = ensure_signed(origin)?;
2273			let member_account = T::Lookup::lookup(member_account)?;
2274			// ensure member is not in an un-migrated state.
2275			ensure!(
2276				!Self::api_member_needs_delegate_migration(member_account.clone()),
2277				Error::<T>::NotMigrated
2278			);
2279
2280			let (mut member, mut bonded_pool, mut reward_pool) =
2281				Self::get_member_with_pools(&member_account)?;
2282
2283			bonded_pool.ok_to_unbond_with(&who, &member_account, &member, unbonding_points)?;
2284
2285			// Claim the the payout prior to unbonding. Once the user is unbonding their points no
2286			// longer exist in the bonded pool and thus they can no longer claim their payouts. It
2287			// is not strictly necessary to claim the rewards, but we do it here for UX.
2288			reward_pool.update_records(
2289				bonded_pool.id,
2290				bonded_pool.points,
2291				bonded_pool.commission.current(),
2292			)?;
2293			Self::do_reward_payout(
2294				&member_account,
2295				&mut member,
2296				&mut bonded_pool,
2297				&mut reward_pool,
2298			)?;
2299
2300			let current_era = T::StakeAdapter::current_era();
2301			let unbond_era = T::StakeAdapter::bonding_duration().saturating_add(current_era);
2302
2303			// Unbond in the actual underlying nominator.
2304			let unbonding_balance = bonded_pool.dissolve(unbonding_points);
2305			T::StakeAdapter::unbond(Pool::from(bonded_pool.bonded_account()), unbonding_balance)?;
2306
2307			// Note that we lazily create the unbonding pools here if they don't already exist
2308			let mut sub_pools = SubPoolsStorage::<T>::get(member.pool_id)
2309				.unwrap_or_default()
2310				.maybe_merge_pools(current_era);
2311
2312			// Update the unbond pool associated with the current era with the unbonded funds. Note
2313			// that we lazily create the unbond pool if it does not yet exist.
2314			if !sub_pools.with_era.contains_key(&unbond_era) {
2315				sub_pools
2316					.with_era
2317					.try_insert(unbond_era, UnbondPool::default())
2318					// The above call to `maybe_merge_pools` should ensure there is
2319					// always enough space to insert.
2320					.defensive_map_err::<Error<T>, _>(|_| {
2321						DefensiveError::NotEnoughSpaceInUnbondPool.into()
2322					})?;
2323			}
2324
2325			let points_unbonded = sub_pools
2326				.with_era
2327				.get_mut(&unbond_era)
2328				// The above check ensures the pool exists.
2329				.defensive_ok_or::<Error<T>>(DefensiveError::PoolNotFound.into())?
2330				.issue(unbonding_balance);
2331
2332			// Try and unbond in the member map.
2333			member.try_unbond(unbonding_points, points_unbonded, unbond_era)?;
2334
2335			Self::deposit_event(Event::<T>::Unbonded {
2336				member: member_account.clone(),
2337				pool_id: member.pool_id,
2338				points: points_unbonded,
2339				balance: unbonding_balance,
2340				era: unbond_era,
2341			});
2342
2343			// Now that we know everything has worked write the items to storage.
2344			SubPoolsStorage::insert(member.pool_id, sub_pools);
2345			Self::put_member_with_pools(&member_account, member, bonded_pool, reward_pool);
2346			Ok(())
2347		}
2348
2349		/// Call `withdraw_unbonded` for the pools account. This call can be made by any account.
2350		///
2351		/// This is useful if there are too many unlocking chunks to call `unbond`, and some
2352		/// can be cleared by withdrawing. In the case there are too many unlocking chunks, the user
2353		/// would probably see an error like `NoMoreChunks` emitted from the staking system when
2354		/// they attempt to unbond.
2355		#[pallet::call_index(4)]
2356		#[pallet::weight(T::WeightInfo::pool_withdraw_unbonded(*num_slashing_spans))]
2357		pub fn pool_withdraw_unbonded(
2358			origin: OriginFor<T>,
2359			pool_id: PoolId,
2360			num_slashing_spans: u32,
2361		) -> DispatchResult {
2362			ensure_signed(origin)?;
2363			// ensure pool is not in an un-migrated state.
2364			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
2365
2366			let pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
2367
2368			// For now we only allow a pool to withdraw unbonded if its not destroying. If the pool
2369			// is destroying then `withdraw_unbonded` can be used.
2370			ensure!(pool.state != PoolState::Destroying, Error::<T>::NotDestroying);
2371			T::StakeAdapter::withdraw_unbonded(
2372				Pool::from(pool.bonded_account()),
2373				num_slashing_spans,
2374			)?;
2375
2376			Ok(())
2377		}
2378
2379		/// Withdraw unbonded funds from `member_account`. If no bonded funds can be unbonded, an
2380		/// error is returned.
2381		///
2382		/// Under certain conditions, this call can be dispatched permissionlessly (i.e. by any
2383		/// account).
2384		///
2385		/// # Conditions for a permissionless dispatch
2386		///
2387		/// * The pool is in destroy mode and the target is not the depositor.
2388		/// * The target is the depositor and they are the only member in the sub pools.
2389		/// * The pool is blocked and the caller is either the root or bouncer.
2390		///
2391		/// # Conditions for permissioned dispatch
2392		///
2393		/// * The caller is the target and they are not the depositor.
2394		///
2395		/// # Note
2396		///
2397		/// - If the target is the depositor, the pool will be destroyed.
2398		/// - If the pool has any pending slash, we also try to slash the member before letting them
2399		/// withdraw. This calculation adds some weight overhead and is only defensive. In reality,
2400		/// pool slashes must have been already applied via permissionless [`Call::apply_slash`].
2401		#[pallet::call_index(5)]
2402		#[pallet::weight(
2403			T::WeightInfo::withdraw_unbonded_kill(*num_slashing_spans)
2404		)]
2405		pub fn withdraw_unbonded(
2406			origin: OriginFor<T>,
2407			member_account: AccountIdLookupOf<T>,
2408			num_slashing_spans: u32,
2409		) -> DispatchResultWithPostInfo {
2410			let caller = ensure_signed(origin)?;
2411			let member_account = T::Lookup::lookup(member_account)?;
2412			// ensure member is not in an un-migrated state.
2413			ensure!(
2414				!Self::api_member_needs_delegate_migration(member_account.clone()),
2415				Error::<T>::NotMigrated
2416			);
2417
2418			let mut member =
2419				PoolMembers::<T>::get(&member_account).ok_or(Error::<T>::PoolMemberNotFound)?;
2420			let current_era = T::StakeAdapter::current_era();
2421
2422			let bonded_pool = BondedPool::<T>::get(member.pool_id)
2423				.defensive_ok_or::<Error<T>>(DefensiveError::PoolNotFound.into())?;
2424			let mut sub_pools =
2425				SubPoolsStorage::<T>::get(member.pool_id).ok_or(Error::<T>::SubPoolsNotFound)?;
2426
2427			let slash_weight =
2428				// apply slash if any before withdraw.
2429				match Self::do_apply_slash(&member_account, None, false) {
2430					Ok(_) => T::WeightInfo::apply_slash(),
2431					Err(e) => {
2432						let no_pending_slash: DispatchResult = Err(Error::<T>::NothingToSlash.into());
2433						// This is an expected error. We add appropriate fees and continue withdrawal.
2434						if Err(e) == no_pending_slash {
2435							T::WeightInfo::apply_slash_fail()
2436						} else {
2437							// defensive: if we can't apply slash for some reason, we abort.
2438							return Err(Error::<T>::Defensive(DefensiveError::SlashNotApplied).into());
2439						}
2440					}
2441
2442				};
2443
2444			bonded_pool.ok_to_withdraw_unbonded_with(&caller, &member_account)?;
2445			let pool_account = bonded_pool.bonded_account();
2446
2447			// NOTE: must do this after we have done the `ok_to_withdraw_unbonded_other_with` check.
2448			let withdrawn_points = member.withdraw_unlocked(current_era);
2449			ensure!(!withdrawn_points.is_empty(), Error::<T>::CannotWithdrawAny);
2450
2451			// Before calculating the `balance_to_unbond`, we call withdraw unbonded to ensure the
2452			// `transferable_balance` is correct.
2453			let stash_killed = T::StakeAdapter::withdraw_unbonded(
2454				Pool::from(bonded_pool.bonded_account()),
2455				num_slashing_spans,
2456			)?;
2457
2458			// defensive-only: the depositor puts enough funds into the stash so that it will only
2459			// be destroyed when they are leaving.
2460			ensure!(
2461				!stash_killed || caller == bonded_pool.roles.depositor,
2462				Error::<T>::Defensive(DefensiveError::BondedStashKilledPrematurely)
2463			);
2464
2465			if stash_killed {
2466				// Maybe an extra consumer left on the pool account, if so, remove it.
2467				if frame_system::Pallet::<T>::consumers(&pool_account) == 1 {
2468					frame_system::Pallet::<T>::dec_consumers(&pool_account);
2469				}
2470
2471				// Note: This is not pretty, but we have to do this because of a bug where old pool
2472				// accounts might have had an extra consumer increment. We know at this point no
2473				// other pallet should depend on pool account so safe to do this.
2474				// Refer to following issues:
2475				// - https://github.com/paritytech/polkadot-sdk/issues/4440
2476				// - https://github.com/paritytech/polkadot-sdk/issues/2037
2477			}
2478
2479			let mut sum_unlocked_points: BalanceOf<T> = Zero::zero();
2480			let balance_to_unbond = withdrawn_points
2481				.iter()
2482				.fold(BalanceOf::<T>::zero(), |accumulator, (era, unlocked_points)| {
2483					sum_unlocked_points = sum_unlocked_points.saturating_add(*unlocked_points);
2484					if let Some(era_pool) = sub_pools.with_era.get_mut(era) {
2485						let balance_to_unbond = era_pool.dissolve(*unlocked_points);
2486						if era_pool.points.is_zero() {
2487							sub_pools.with_era.remove(era);
2488						}
2489						accumulator.saturating_add(balance_to_unbond)
2490					} else {
2491						// A pool does not belong to this era, so it must have been merged to the
2492						// era-less pool.
2493						accumulator.saturating_add(sub_pools.no_era.dissolve(*unlocked_points))
2494					}
2495				})
2496				// A call to this transaction may cause the pool's stash to get dusted. If this
2497				// happens before the last member has withdrawn, then all subsequent withdraws will
2498				// be 0. However the unbond pools do no get updated to reflect this. In the
2499				// aforementioned scenario, this check ensures we don't try to withdraw funds that
2500				// don't exist. This check is also defensive in cases where the unbond pool does not
2501				// update its balance (e.g. a bug in the slashing hook.) We gracefully proceed in
2502				// order to ensure members can leave the pool and it can be destroyed.
2503				.min(T::StakeAdapter::transferable_balance(
2504					Pool::from(bonded_pool.bonded_account()),
2505					Member::from(member_account.clone()),
2506				));
2507
2508			// this can fail if the pool uses `DelegateStake` strategy and the member delegation
2509			// is not claimed yet. See `Call::migrate_delegation()`.
2510			T::StakeAdapter::member_withdraw(
2511				Member::from(member_account.clone()),
2512				Pool::from(bonded_pool.bonded_account()),
2513				balance_to_unbond,
2514				num_slashing_spans,
2515			)?;
2516
2517			Self::deposit_event(Event::<T>::Withdrawn {
2518				member: member_account.clone(),
2519				pool_id: member.pool_id,
2520				points: sum_unlocked_points,
2521				balance: balance_to_unbond,
2522			});
2523
2524			let post_info_weight = if member.total_points().is_zero() {
2525				// remove any `ClaimPermission` associated with the member.
2526				ClaimPermissions::<T>::remove(&member_account);
2527
2528				// member being reaped.
2529				PoolMembers::<T>::remove(&member_account);
2530
2531				// Ensure any dangling delegation is withdrawn.
2532				let dangling_withdrawal = match T::StakeAdapter::member_delegation_balance(
2533					Member::from(member_account.clone()),
2534				) {
2535					Some(dangling_delegation) => {
2536						T::StakeAdapter::member_withdraw(
2537							Member::from(member_account.clone()),
2538							Pool::from(bonded_pool.bonded_account()),
2539							dangling_delegation,
2540							num_slashing_spans,
2541						)?;
2542						dangling_delegation
2543					},
2544					None => Zero::zero(),
2545				};
2546
2547				Self::deposit_event(Event::<T>::MemberRemoved {
2548					pool_id: member.pool_id,
2549					member: member_account.clone(),
2550					released_balance: dangling_withdrawal,
2551				});
2552
2553				if member_account == bonded_pool.roles.depositor {
2554					Pallet::<T>::dissolve_pool(bonded_pool);
2555					Weight::default()
2556				} else {
2557					bonded_pool.dec_members().put();
2558					SubPoolsStorage::<T>::insert(member.pool_id, sub_pools);
2559					T::WeightInfo::withdraw_unbonded_update(num_slashing_spans)
2560				}
2561			} else {
2562				// we certainly don't need to delete any pools, because no one is being removed.
2563				SubPoolsStorage::<T>::insert(member.pool_id, sub_pools);
2564				PoolMembers::<T>::insert(&member_account, member);
2565				T::WeightInfo::withdraw_unbonded_update(num_slashing_spans)
2566			};
2567
2568			Ok(Some(post_info_weight.saturating_add(slash_weight)).into())
2569		}
2570
2571		/// Create a new delegation pool.
2572		///
2573		/// # Arguments
2574		///
2575		/// * `amount` - The amount of funds to delegate to the pool. This also acts of a sort of
2576		///   deposit since the pools creator cannot fully unbond funds until the pool is being
2577		///   destroyed.
2578		/// * `index` - A disambiguation index for creating the account. Likely only useful when
2579		///   creating multiple pools in the same extrinsic.
2580		/// * `root` - The account to set as [`PoolRoles::root`].
2581		/// * `nominator` - The account to set as the [`PoolRoles::nominator`].
2582		/// * `bouncer` - The account to set as the [`PoolRoles::bouncer`].
2583		///
2584		/// # Note
2585		///
2586		/// In addition to `amount`, the caller will transfer the existential deposit; so the caller
2587		/// needs at have at least `amount + existential_deposit` transferable.
2588		#[pallet::call_index(6)]
2589		#[pallet::weight(T::WeightInfo::create())]
2590		pub fn create(
2591			origin: OriginFor<T>,
2592			#[pallet::compact] amount: BalanceOf<T>,
2593			root: AccountIdLookupOf<T>,
2594			nominator: AccountIdLookupOf<T>,
2595			bouncer: AccountIdLookupOf<T>,
2596		) -> DispatchResult {
2597			let depositor = ensure_signed(origin)?;
2598
2599			let pool_id = LastPoolId::<T>::try_mutate::<_, Error<T>, _>(|id| {
2600				*id = id.checked_add(1).ok_or(Error::<T>::OverflowRisk)?;
2601				Ok(*id)
2602			})?;
2603
2604			Self::do_create(depositor, amount, root, nominator, bouncer, pool_id)
2605		}
2606
2607		/// Create a new delegation pool with a previously used pool id
2608		///
2609		/// # Arguments
2610		///
2611		/// same as `create` with the inclusion of
2612		/// * `pool_id` - `A valid PoolId.
2613		#[pallet::call_index(7)]
2614		#[pallet::weight(T::WeightInfo::create())]
2615		pub fn create_with_pool_id(
2616			origin: OriginFor<T>,
2617			#[pallet::compact] amount: BalanceOf<T>,
2618			root: AccountIdLookupOf<T>,
2619			nominator: AccountIdLookupOf<T>,
2620			bouncer: AccountIdLookupOf<T>,
2621			pool_id: PoolId,
2622		) -> DispatchResult {
2623			let depositor = ensure_signed(origin)?;
2624
2625			ensure!(!BondedPools::<T>::contains_key(pool_id), Error::<T>::PoolIdInUse);
2626			ensure!(pool_id < LastPoolId::<T>::get(), Error::<T>::InvalidPoolId);
2627
2628			Self::do_create(depositor, amount, root, nominator, bouncer, pool_id)
2629		}
2630
2631		/// Nominate on behalf of the pool.
2632		///
2633		/// The dispatch origin of this call must be signed by the pool nominator or the pool
2634		/// root role.
2635		///
2636		/// This directly forwards the call to an implementation of `StakingInterface` (e.g.,
2637		/// `pallet-staking`) through [`Config::StakeAdapter`], on behalf of the bonded pool.
2638		///
2639		/// # Note
2640		///
2641		/// In addition to a `root` or `nominator` role of `origin`, the pool's depositor needs to
2642		/// have at least `depositor_min_bond` in the pool to start nominating.
2643		#[pallet::call_index(8)]
2644		#[pallet::weight(T::WeightInfo::nominate(validators.len() as u32))]
2645		pub fn nominate(
2646			origin: OriginFor<T>,
2647			pool_id: PoolId,
2648			validators: Vec<T::AccountId>,
2649		) -> DispatchResult {
2650			let who = ensure_signed(origin)?;
2651			let bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
2652			// ensure pool is not in an un-migrated state.
2653			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
2654			ensure!(bonded_pool.can_nominate(&who), Error::<T>::NotNominator);
2655
2656			let depositor_points = PoolMembers::<T>::get(&bonded_pool.roles.depositor)
2657				.ok_or(Error::<T>::PoolMemberNotFound)?
2658				.active_points();
2659
2660			ensure!(
2661				bonded_pool.points_to_balance(depositor_points) >= Self::depositor_min_bond(),
2662				Error::<T>::MinimumBondNotMet
2663			);
2664
2665			T::StakeAdapter::nominate(Pool::from(bonded_pool.bonded_account()), validators).map(
2666				|_| Self::deposit_event(Event::<T>::PoolNominationMade { pool_id, caller: who }),
2667			)
2668		}
2669
2670		/// Set a new state for the pool.
2671		///
2672		/// If a pool is already in the `Destroying` state, then under no condition can its state
2673		/// change again.
2674		///
2675		/// The dispatch origin of this call must be either:
2676		///
2677		/// 1. signed by the bouncer, or the root role of the pool,
2678		/// 2. if the pool conditions to be open are NOT met (as described by `ok_to_be_open`), and
2679		///    then the state of the pool can be permissionlessly changed to `Destroying`.
2680		#[pallet::call_index(9)]
2681		#[pallet::weight(T::WeightInfo::set_state())]
2682		pub fn set_state(
2683			origin: OriginFor<T>,
2684			pool_id: PoolId,
2685			state: PoolState,
2686		) -> DispatchResult {
2687			let who = ensure_signed(origin)?;
2688			let mut bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
2689			ensure!(bonded_pool.state != PoolState::Destroying, Error::<T>::CanNotChangeState);
2690			// ensure pool is not in an un-migrated state.
2691			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
2692
2693			if bonded_pool.can_toggle_state(&who) {
2694				bonded_pool.set_state(state);
2695			} else if bonded_pool.ok_to_be_open().is_err() && state == PoolState::Destroying {
2696				// If the pool has bad properties, then anyone can set it as destroying
2697				bonded_pool.set_state(PoolState::Destroying);
2698			} else {
2699				Err(Error::<T>::CanNotChangeState)?;
2700			}
2701
2702			bonded_pool.put();
2703
2704			Ok(())
2705		}
2706
2707		/// Set a new metadata for the pool.
2708		///
2709		/// The dispatch origin of this call must be signed by the bouncer, or the root role of the
2710		/// pool.
2711		#[pallet::call_index(10)]
2712		#[pallet::weight(T::WeightInfo::set_metadata(metadata.len() as u32))]
2713		pub fn set_metadata(
2714			origin: OriginFor<T>,
2715			pool_id: PoolId,
2716			metadata: Vec<u8>,
2717		) -> DispatchResult {
2718			let who = ensure_signed(origin)?;
2719			let metadata: BoundedVec<_, _> =
2720				metadata.try_into().map_err(|_| Error::<T>::MetadataExceedsMaxLen)?;
2721			ensure!(
2722				BondedPool::<T>::get(pool_id)
2723					.ok_or(Error::<T>::PoolNotFound)?
2724					.can_set_metadata(&who),
2725				Error::<T>::DoesNotHavePermission
2726			);
2727			// ensure pool is not in an un-migrated state.
2728			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
2729
2730			Metadata::<T>::mutate(pool_id, |pool_meta| *pool_meta = metadata);
2731
2732			Self::deposit_event(Event::<T>::MetadataUpdated { pool_id, caller: who });
2733
2734			Ok(())
2735		}
2736
2737		/// Update configurations for the nomination pools. The origin for this call must be
2738		/// [`Config::AdminOrigin`].
2739		///
2740		/// # Arguments
2741		///
2742		/// * `min_join_bond` - Set [`MinJoinBond`].
2743		/// * `min_create_bond` - Set [`MinCreateBond`].
2744		/// * `max_pools` - Set [`MaxPools`].
2745		/// * `max_members` - Set [`MaxPoolMembers`].
2746		/// * `max_members_per_pool` - Set [`MaxPoolMembersPerPool`].
2747		/// * `global_max_commission` - Set [`GlobalMaxCommission`].
2748		#[pallet::call_index(11)]
2749		#[pallet::weight(T::WeightInfo::set_configs())]
2750		pub fn set_configs(
2751			origin: OriginFor<T>,
2752			min_join_bond: ConfigOp<BalanceOf<T>>,
2753			min_create_bond: ConfigOp<BalanceOf<T>>,
2754			max_pools: ConfigOp<u32>,
2755			max_members: ConfigOp<u32>,
2756			max_members_per_pool: ConfigOp<u32>,
2757			global_max_commission: ConfigOp<Perbill>,
2758		) -> DispatchResult {
2759			T::AdminOrigin::ensure_origin(origin)?;
2760
2761			macro_rules! config_op_exp {
2762				($storage:ty, $op:ident) => {
2763					match $op {
2764						ConfigOp::Noop => (),
2765						ConfigOp::Set(v) => <$storage>::put(v),
2766						ConfigOp::Remove => <$storage>::kill(),
2767					}
2768				};
2769			}
2770
2771			config_op_exp!(MinJoinBond::<T>, min_join_bond);
2772			config_op_exp!(MinCreateBond::<T>, min_create_bond);
2773			config_op_exp!(MaxPools::<T>, max_pools);
2774			config_op_exp!(MaxPoolMembers::<T>, max_members);
2775			config_op_exp!(MaxPoolMembersPerPool::<T>, max_members_per_pool);
2776			config_op_exp!(GlobalMaxCommission::<T>, global_max_commission);
2777
2778			Self::deposit_event(Event::<T>::GlobalParamsUpdated {
2779				min_join_bond: MinJoinBond::<T>::get(),
2780				min_create_bond: MinCreateBond::<T>::get(),
2781				max_pools: MaxPools::<T>::get(),
2782				max_members: MaxPoolMembers::<T>::get(),
2783				max_members_per_pool: MaxPoolMembersPerPool::<T>::get(),
2784				global_max_commission: GlobalMaxCommission::<T>::get(),
2785			});
2786
2787			Ok(())
2788		}
2789
2790		/// Update the roles of the pool.
2791		///
2792		/// The root is the only entity that can change any of the roles, including itself,
2793		/// excluding the depositor, who can never change.
2794		///
2795		/// It emits an event, notifying UIs of the role change. This event is quite relevant to
2796		/// most pool members and they should be informed of changes to pool roles.
2797		#[pallet::call_index(12)]
2798		#[pallet::weight(T::WeightInfo::update_roles())]
2799		pub fn update_roles(
2800			origin: OriginFor<T>,
2801			pool_id: PoolId,
2802			new_root: ConfigOp<T::AccountId>,
2803			new_nominator: ConfigOp<T::AccountId>,
2804			new_bouncer: ConfigOp<T::AccountId>,
2805		) -> DispatchResult {
2806			let mut bonded_pool = match ensure_root(origin.clone()) {
2807				Ok(()) => BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?,
2808				Err(sp_runtime::traits::BadOrigin) => {
2809					let who = ensure_signed(origin)?;
2810					let bonded_pool =
2811						BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
2812					ensure!(bonded_pool.can_update_roles(&who), Error::<T>::DoesNotHavePermission);
2813					bonded_pool
2814				},
2815			};
2816
2817			// ensure pool is not in an un-migrated state.
2818			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
2819
2820			match new_root {
2821				ConfigOp::Noop => (),
2822				ConfigOp::Remove => bonded_pool.roles.root = None,
2823				ConfigOp::Set(v) => bonded_pool.roles.root = Some(v),
2824			};
2825			match new_nominator {
2826				ConfigOp::Noop => (),
2827				ConfigOp::Remove => bonded_pool.roles.nominator = None,
2828				ConfigOp::Set(v) => bonded_pool.roles.nominator = Some(v),
2829			};
2830			match new_bouncer {
2831				ConfigOp::Noop => (),
2832				ConfigOp::Remove => bonded_pool.roles.bouncer = None,
2833				ConfigOp::Set(v) => bonded_pool.roles.bouncer = Some(v),
2834			};
2835
2836			Self::deposit_event(Event::<T>::RolesUpdated {
2837				root: bonded_pool.roles.root.clone(),
2838				nominator: bonded_pool.roles.nominator.clone(),
2839				bouncer: bonded_pool.roles.bouncer.clone(),
2840			});
2841
2842			bonded_pool.put();
2843			Ok(())
2844		}
2845
2846		/// Chill on behalf of the pool.
2847		///
2848		/// The dispatch origin of this call can be signed by the pool nominator or the pool
2849		/// root role, same as [`Pallet::nominate`].
2850		///
2851		/// This directly forwards the call to an implementation of `StakingInterface` (e.g.,
2852		/// `pallet-staking`) through [`Config::StakeAdapter`], on behalf of the bonded pool.
2853		///
2854		/// Under certain conditions, this call can be dispatched permissionlessly (i.e. by any
2855		/// account).
2856		///
2857		/// # Conditions for a permissionless dispatch:
2858		/// * When pool depositor has less than `MinNominatorBond` staked, otherwise pool members
2859		///   are unable to unbond.
2860		///
2861		/// # Conditions for permissioned dispatch:
2862		/// * The caller is the pool's nominator or root.
2863		#[pallet::call_index(13)]
2864		#[pallet::weight(T::WeightInfo::chill())]
2865		pub fn chill(origin: OriginFor<T>, pool_id: PoolId) -> DispatchResult {
2866			let who = ensure_signed(origin)?;
2867			let bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
2868			// ensure pool is not in an un-migrated state.
2869			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
2870
2871			let depositor_points = PoolMembers::<T>::get(&bonded_pool.roles.depositor)
2872				.ok_or(Error::<T>::PoolMemberNotFound)?
2873				.active_points();
2874
2875			if bonded_pool.points_to_balance(depositor_points) >=
2876				T::StakeAdapter::minimum_nominator_bond()
2877			{
2878				ensure!(bonded_pool.can_nominate(&who), Error::<T>::NotNominator);
2879			}
2880
2881			T::StakeAdapter::chill(Pool::from(bonded_pool.bonded_account())).map(|_| {
2882				Self::deposit_event(Event::<T>::PoolNominatorChilled { pool_id, caller: who })
2883			})
2884		}
2885
2886		/// `origin` bonds funds from `extra` for some pool member `member` into their respective
2887		/// pools.
2888		///
2889		/// `origin` can bond extra funds from free balance or pending rewards when `origin ==
2890		/// other`.
2891		///
2892		/// In the case of `origin != other`, `origin` can only bond extra pending rewards of
2893		/// `other` members assuming set_claim_permission for the given member is
2894		/// `PermissionlessCompound` or `PermissionlessAll`.
2895		#[pallet::call_index(14)]
2896		#[pallet::weight(
2897			T::WeightInfo::bond_extra_transfer()
2898			.max(T::WeightInfo::bond_extra_other())
2899		)]
2900		pub fn bond_extra_other(
2901			origin: OriginFor<T>,
2902			member: AccountIdLookupOf<T>,
2903			extra: BondExtra<BalanceOf<T>>,
2904		) -> DispatchResult {
2905			let who = ensure_signed(origin)?;
2906			let member_account = T::Lookup::lookup(member)?;
2907			// ensure member is not in an un-migrated state.
2908			ensure!(
2909				!Self::api_member_needs_delegate_migration(member_account.clone()),
2910				Error::<T>::NotMigrated
2911			);
2912
2913			Self::do_bond_extra(who, member_account, extra)
2914		}
2915
2916		/// Allows a pool member to set a claim permission to allow or disallow permissionless
2917		/// bonding and withdrawing.
2918		///
2919		/// # Arguments
2920		///
2921		/// * `origin` - Member of a pool.
2922		/// * `permission` - The permission to be applied.
2923		#[pallet::call_index(15)]
2924		#[pallet::weight(T::DbWeight::get().reads_writes(1, 1))]
2925		pub fn set_claim_permission(
2926			origin: OriginFor<T>,
2927			permission: ClaimPermission,
2928		) -> DispatchResult {
2929			let who = ensure_signed(origin)?;
2930			ensure!(PoolMembers::<T>::contains_key(&who), Error::<T>::PoolMemberNotFound);
2931
2932			// ensure member is not in an un-migrated state.
2933			ensure!(
2934				!Self::api_member_needs_delegate_migration(who.clone()),
2935				Error::<T>::NotMigrated
2936			);
2937
2938			ClaimPermissions::<T>::mutate(who.clone(), |source| {
2939				*source = permission;
2940			});
2941
2942			Self::deposit_event(Event::<T>::MemberClaimPermissionUpdated {
2943				member: who,
2944				permission,
2945			});
2946
2947			Ok(())
2948		}
2949
2950		/// `origin` can claim payouts on some pool member `other`'s behalf.
2951		///
2952		/// Pool member `other` must have a `PermissionlessWithdraw` or `PermissionlessAll` claim
2953		/// permission for this call to be successful.
2954		#[pallet::call_index(16)]
2955		#[pallet::weight(T::WeightInfo::claim_payout())]
2956		pub fn claim_payout_other(origin: OriginFor<T>, other: T::AccountId) -> DispatchResult {
2957			let signer = ensure_signed(origin)?;
2958			// ensure member is not in an un-migrated state.
2959			ensure!(
2960				!Self::api_member_needs_delegate_migration(other.clone()),
2961				Error::<T>::NotMigrated
2962			);
2963
2964			Self::do_claim_payout(signer, other)
2965		}
2966
2967		/// Set the commission of a pool.
2968		//
2969		/// Both a commission percentage and a commission payee must be provided in the `current`
2970		/// tuple. Where a `current` of `None` is provided, any current commission will be removed.
2971		///
2972		/// - If a `None` is supplied to `new_commission`, existing commission will be removed.
2973		#[pallet::call_index(17)]
2974		#[pallet::weight(T::WeightInfo::set_commission())]
2975		pub fn set_commission(
2976			origin: OriginFor<T>,
2977			pool_id: PoolId,
2978			new_commission: Option<(Perbill, T::AccountId)>,
2979		) -> DispatchResult {
2980			let who = ensure_signed(origin)?;
2981			let mut bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
2982			// ensure pool is not in an un-migrated state.
2983			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
2984
2985			ensure!(bonded_pool.can_manage_commission(&who), Error::<T>::DoesNotHavePermission);
2986
2987			let mut reward_pool = RewardPools::<T>::get(pool_id)
2988				.defensive_ok_or::<Error<T>>(DefensiveError::RewardPoolNotFound.into())?;
2989			// IMPORTANT: make sure that everything up to this point is using the current commission
2990			// before it updates. Note that `try_update_current` could still fail at this point.
2991			reward_pool.update_records(
2992				pool_id,
2993				bonded_pool.points,
2994				bonded_pool.commission.current(),
2995			)?;
2996			RewardPools::insert(pool_id, reward_pool);
2997
2998			bonded_pool.commission.try_update_current(&new_commission)?;
2999			bonded_pool.put();
3000			Self::deposit_event(Event::<T>::PoolCommissionUpdated {
3001				pool_id,
3002				current: new_commission,
3003			});
3004			Ok(())
3005		}
3006
3007		/// Set the maximum commission of a pool.
3008		///
3009		/// - Initial max can be set to any `Perbill`, and only smaller values thereafter.
3010		/// - Current commission will be lowered in the event it is higher than a new max
3011		///   commission.
3012		#[pallet::call_index(18)]
3013		#[pallet::weight(T::WeightInfo::set_commission_max())]
3014		pub fn set_commission_max(
3015			origin: OriginFor<T>,
3016			pool_id: PoolId,
3017			max_commission: Perbill,
3018		) -> DispatchResult {
3019			let who = ensure_signed(origin)?;
3020			let mut bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
3021			// ensure pool is not in an un-migrated state.
3022			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
3023
3024			ensure!(bonded_pool.can_manage_commission(&who), Error::<T>::DoesNotHavePermission);
3025
3026			bonded_pool.commission.try_update_max(pool_id, max_commission)?;
3027			bonded_pool.put();
3028
3029			Self::deposit_event(Event::<T>::PoolMaxCommissionUpdated { pool_id, max_commission });
3030			Ok(())
3031		}
3032
3033		/// Set the commission change rate for a pool.
3034		///
3035		/// Initial change rate is not bounded, whereas subsequent updates can only be more
3036		/// restrictive than the current.
3037		#[pallet::call_index(19)]
3038		#[pallet::weight(T::WeightInfo::set_commission_change_rate())]
3039		pub fn set_commission_change_rate(
3040			origin: OriginFor<T>,
3041			pool_id: PoolId,
3042			change_rate: CommissionChangeRate<BlockNumberFor<T>>,
3043		) -> DispatchResult {
3044			let who = ensure_signed(origin)?;
3045			let mut bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
3046			// ensure pool is not in an un-migrated state.
3047			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
3048			ensure!(bonded_pool.can_manage_commission(&who), Error::<T>::DoesNotHavePermission);
3049
3050			bonded_pool.commission.try_update_change_rate(change_rate)?;
3051			bonded_pool.put();
3052
3053			Self::deposit_event(Event::<T>::PoolCommissionChangeRateUpdated {
3054				pool_id,
3055				change_rate,
3056			});
3057			Ok(())
3058		}
3059
3060		/// Claim pending commission.
3061		///
3062		/// The `root` role of the pool is _always_ allowed to claim the pool's commission.
3063		///
3064		/// If the pool has set `CommissionClaimPermission::Permissionless`, then any account can
3065		/// trigger the process of claiming the pool's commission.
3066		///
3067		/// If the pool has set its `CommissionClaimPermission` to `Account(acc)`, then only
3068		/// accounts
3069		/// * `acc`, and
3070		/// * the pool's root account
3071		///
3072		/// may call this extrinsic on behalf of the pool.
3073		///
3074		/// Pending commissions are paid out and added to the total claimed commission.
3075		/// The total pending commission is reset to zero.
3076		#[pallet::call_index(20)]
3077		#[pallet::weight(T::WeightInfo::claim_commission())]
3078		pub fn claim_commission(origin: OriginFor<T>, pool_id: PoolId) -> DispatchResult {
3079			let who = ensure_signed(origin)?;
3080			// ensure pool is not in an un-migrated state.
3081			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
3082
3083			Self::do_claim_commission(who, pool_id)
3084		}
3085
3086		/// Top up the deficit or withdraw the excess ED from the pool.
3087		///
3088		/// When a pool is created, the pool depositor transfers ED to the reward account of the
3089		/// pool. ED is subject to change and over time, the deposit in the reward account may be
3090		/// insufficient to cover the ED deficit of the pool or vice-versa where there is excess
3091		/// deposit to the pool. This call allows anyone to adjust the ED deposit of the
3092		/// pool by either topping up the deficit or claiming the excess.
3093		#[pallet::call_index(21)]
3094		#[pallet::weight(T::WeightInfo::adjust_pool_deposit())]
3095		pub fn adjust_pool_deposit(origin: OriginFor<T>, pool_id: PoolId) -> DispatchResult {
3096			let who = ensure_signed(origin)?;
3097			// ensure pool is not in an un-migrated state.
3098			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
3099
3100			Self::do_adjust_pool_deposit(who, pool_id)
3101		}
3102
3103		/// Set or remove a pool's commission claim permission.
3104		///
3105		/// Determines who can claim the pool's pending commission. Only the `Root` role of the pool
3106		/// is able to configure commission claim permissions.
3107		#[pallet::call_index(22)]
3108		#[pallet::weight(T::WeightInfo::set_commission_claim_permission())]
3109		pub fn set_commission_claim_permission(
3110			origin: OriginFor<T>,
3111			pool_id: PoolId,
3112			permission: Option<CommissionClaimPermission<T::AccountId>>,
3113		) -> DispatchResult {
3114			let who = ensure_signed(origin)?;
3115			let mut bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
3116			// ensure pool is not in an un-migrated state.
3117			ensure!(!Self::api_pool_needs_delegate_migration(pool_id), Error::<T>::NotMigrated);
3118			ensure!(bonded_pool.can_manage_commission(&who), Error::<T>::DoesNotHavePermission);
3119
3120			bonded_pool.commission.claim_permission = permission.clone();
3121			bonded_pool.put();
3122
3123			Self::deposit_event(Event::<T>::PoolCommissionClaimPermissionUpdated {
3124				pool_id,
3125				permission,
3126			});
3127
3128			Ok(())
3129		}
3130
3131		/// Apply a pending slash on a member.
3132		///
3133		/// Fails unless [`crate::pallet::Config::StakeAdapter`] is of strategy type:
3134		/// [`adapter::StakeStrategyType::Delegate`].
3135		///
3136		/// The pending slash amount of the member must be equal or more than `ExistentialDeposit`.
3137		/// This call can be dispatched permissionlessly (i.e. by any account). If the execution
3138		/// is successful, fee is refunded and caller may be rewarded with a part of the slash
3139		/// based on the [`crate::pallet::Config::StakeAdapter`] configuration.
3140		#[pallet::call_index(23)]
3141		#[pallet::weight(T::WeightInfo::apply_slash())]
3142		pub fn apply_slash(
3143			origin: OriginFor<T>,
3144			member_account: AccountIdLookupOf<T>,
3145		) -> DispatchResultWithPostInfo {
3146			ensure!(
3147				T::StakeAdapter::strategy_type() == adapter::StakeStrategyType::Delegate,
3148				Error::<T>::NotSupported
3149			);
3150
3151			let who = ensure_signed(origin)?;
3152			let member_account = T::Lookup::lookup(member_account)?;
3153			Self::do_apply_slash(&member_account, Some(who), true)?;
3154
3155			// If successful, refund the fees.
3156			Ok(Pays::No.into())
3157		}
3158
3159		/// Migrates delegated funds from the pool account to the `member_account`.
3160		///
3161		/// Fails unless [`crate::pallet::Config::StakeAdapter`] is of strategy type:
3162		/// [`adapter::StakeStrategyType::Delegate`].
3163		///
3164		/// This is a permission-less call and refunds any fee if claim is successful.
3165		///
3166		/// If the pool has migrated to delegation based staking, the staked tokens of pool members
3167		/// can be moved and held in their own account. See [`adapter::DelegateStake`]
3168		#[pallet::call_index(24)]
3169		#[pallet::weight(T::WeightInfo::migrate_delegation())]
3170		pub fn migrate_delegation(
3171			origin: OriginFor<T>,
3172			member_account: AccountIdLookupOf<T>,
3173		) -> DispatchResultWithPostInfo {
3174			let _caller = ensure_signed(origin)?;
3175
3176			// ensure `DelegateStake` strategy is used.
3177			ensure!(
3178				T::StakeAdapter::strategy_type() == adapter::StakeStrategyType::Delegate,
3179				Error::<T>::NotSupported
3180			);
3181
3182			// ensure member is not restricted from joining the pool.
3183			let member_account = T::Lookup::lookup(member_account)?;
3184			ensure!(!T::Filter::contains(&member_account), Error::<T>::Restricted);
3185
3186			let member =
3187				PoolMembers::<T>::get(&member_account).ok_or(Error::<T>::PoolMemberNotFound)?;
3188
3189			// ensure pool is migrated.
3190			ensure!(
3191				T::StakeAdapter::pool_strategy(Pool::from(Self::generate_bonded_account(
3192					member.pool_id
3193				))) == adapter::StakeStrategyType::Delegate,
3194				Error::<T>::NotMigrated
3195			);
3196
3197			let pool_contribution = member.total_balance();
3198			// ensure the pool contribution is greater than the existential deposit otherwise we
3199			// cannot transfer funds to member account.
3200			ensure!(
3201				pool_contribution >= T::Currency::minimum_balance(),
3202				Error::<T>::MinimumBondNotMet
3203			);
3204
3205			let delegation =
3206				T::StakeAdapter::member_delegation_balance(Member::from(member_account.clone()));
3207			// delegation should not exist.
3208			ensure!(delegation.is_none(), Error::<T>::AlreadyMigrated);
3209
3210			T::StakeAdapter::migrate_delegation(
3211				Pool::from(Pallet::<T>::generate_bonded_account(member.pool_id)),
3212				Member::from(member_account),
3213				pool_contribution,
3214			)?;
3215
3216			// if successful, we refund the fee.
3217			Ok(Pays::No.into())
3218		}
3219
3220		/// Migrate pool from [`adapter::StakeStrategyType::Transfer`] to
3221		/// [`adapter::StakeStrategyType::Delegate`].
3222		///
3223		/// Fails unless [`crate::pallet::Config::StakeAdapter`] is of strategy type:
3224		/// [`adapter::StakeStrategyType::Delegate`].
3225		///
3226		/// This call can be dispatched permissionlessly, and refunds any fee if successful.
3227		///
3228		/// If the pool has already migrated to delegation based staking, this call will fail.
3229		#[pallet::call_index(25)]
3230		#[pallet::weight(T::WeightInfo::pool_migrate())]
3231		pub fn migrate_pool_to_delegate_stake(
3232			origin: OriginFor<T>,
3233			pool_id: PoolId,
3234		) -> DispatchResultWithPostInfo {
3235			// gate this call to be called only if `DelegateStake` strategy is used.
3236			ensure!(
3237				T::StakeAdapter::strategy_type() == adapter::StakeStrategyType::Delegate,
3238				Error::<T>::NotSupported
3239			);
3240
3241			let _caller = ensure_signed(origin)?;
3242			// ensure pool exists.
3243			let bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
3244			ensure!(
3245				T::StakeAdapter::pool_strategy(Pool::from(bonded_pool.bonded_account())) ==
3246					adapter::StakeStrategyType::Transfer,
3247				Error::<T>::AlreadyMigrated
3248			);
3249
3250			Self::migrate_to_delegate_stake(pool_id)?;
3251			Ok(Pays::No.into())
3252		}
3253	}
3254
3255	#[pallet::hooks]
3256	impl<T: Config> Hooks<SystemBlockNumberFor<T>> for Pallet<T> {
3257		#[cfg(feature = "try-runtime")]
3258		fn try_state(_n: SystemBlockNumberFor<T>) -> Result<(), TryRuntimeError> {
3259			Self::do_try_state(u8::MAX)
3260		}
3261
3262		fn integrity_test() {
3263			assert!(
3264				T::MaxPointsToBalance::get() > 0,
3265				"Minimum points to balance ratio must be greater than 0"
3266			);
3267			assert!(
3268				T::StakeAdapter::bonding_duration() < TotalUnbondingPools::<T>::get(),
3269				"There must be more unbonding pools then the bonding duration /
3270				so a slash can be applied to relevant unbonding pools. (We assume /
3271				the bonding duration > slash deffer duration.",
3272			);
3273		}
3274	}
3275}
3276
3277impl<T: Config> Pallet<T> {
3278	/// The amount of bond that MUST REMAIN IN BONDED in ALL POOLS.
3279	///
3280	/// It is the responsibility of the depositor to put these funds into the pool initially. Upon
3281	/// unbond, they can never unbond to a value below this amount.
3282	///
3283	/// It is essentially `max { MinNominatorBond, MinCreateBond, MinJoinBond }`, where the former
3284	/// is coming from the staking pallet and the latter two are configured in this pallet.
3285	pub fn depositor_min_bond() -> BalanceOf<T> {
3286		T::StakeAdapter::minimum_nominator_bond()
3287			.max(MinCreateBond::<T>::get())
3288			.max(MinJoinBond::<T>::get())
3289			.max(T::Currency::minimum_balance())
3290	}
3291	/// Remove everything related to the given bonded pool.
3292	///
3293	/// Metadata and all of the sub-pools are also deleted. All accounts are dusted and the leftover
3294	/// of the reward account is returned to the depositor.
3295	pub fn dissolve_pool(bonded_pool: BondedPool<T>) {
3296		let reward_account = bonded_pool.reward_account();
3297		let bonded_account = bonded_pool.bonded_account();
3298
3299		ReversePoolIdLookup::<T>::remove(&bonded_account);
3300		RewardPools::<T>::remove(bonded_pool.id);
3301		SubPoolsStorage::<T>::remove(bonded_pool.id);
3302
3303		// remove the ED restriction from the pool reward account.
3304		let _ = Self::unfreeze_pool_deposit(&bonded_pool.reward_account()).defensive();
3305
3306		// Kill accounts from storage by making their balance go below ED. We assume that the
3307		// accounts have no references that would prevent destruction once we get to this point. We
3308		// don't work with the system pallet directly, but
3309		// 1. we drain the reward account and kill it. This account should never have any extra
3310		// consumers anyway.
3311		// 2. the bonded account should become a 'killed stash' in the staking system, and all of
3312		//    its consumers removed.
3313		defensive_assert!(
3314			frame_system::Pallet::<T>::consumers(&reward_account) == 0,
3315			"reward account of dissolving pool should have no consumers"
3316		);
3317		defensive_assert!(
3318			frame_system::Pallet::<T>::consumers(&bonded_account) == 0,
3319			"bonded account of dissolving pool should have no consumers"
3320		);
3321		defensive_assert!(
3322			T::StakeAdapter::total_stake(Pool::from(bonded_pool.bonded_account())) == Zero::zero(),
3323			"dissolving pool should not have any stake in the staking pallet"
3324		);
3325
3326		// This shouldn't fail, but if it does we don't really care. Remaining balance can consist
3327		// of unclaimed pending commission, erroneous transfers to the reward account, etc.
3328		let reward_pool_remaining = T::Currency::reducible_balance(
3329			&reward_account,
3330			Preservation::Expendable,
3331			Fortitude::Polite,
3332		);
3333		let _ = T::Currency::transfer(
3334			&reward_account,
3335			&bonded_pool.roles.depositor,
3336			reward_pool_remaining,
3337			Preservation::Expendable,
3338		);
3339
3340		defensive_assert!(
3341			T::Currency::total_balance(&reward_account) == Zero::zero(),
3342			"could not transfer all amount to depositor while dissolving pool"
3343		);
3344		// NOTE: Defensively force set balance to zero.
3345		T::Currency::set_balance(&reward_account, Zero::zero());
3346
3347		// dissolve pool account.
3348		let _ = T::StakeAdapter::dissolve(Pool::from(bonded_account)).defensive();
3349
3350		Self::deposit_event(Event::<T>::Destroyed { pool_id: bonded_pool.id });
3351		// Remove bonded pool metadata.
3352		Metadata::<T>::remove(bonded_pool.id);
3353
3354		bonded_pool.remove();
3355	}
3356
3357	/// Create the main, bonded account of a pool with the given id.
3358	pub fn generate_bonded_account(id: PoolId) -> T::AccountId {
3359		T::PalletId::get().into_sub_account_truncating((AccountType::Bonded, id))
3360	}
3361
3362	fn migrate_to_delegate_stake(id: PoolId) -> DispatchResult {
3363		T::StakeAdapter::migrate_nominator_to_agent(
3364			Pool::from(Self::generate_bonded_account(id)),
3365			&Self::generate_reward_account(id),
3366		)
3367	}
3368
3369	/// Create the reward account of a pool with the given id.
3370	pub fn generate_reward_account(id: PoolId) -> T::AccountId {
3371		// NOTE: in order to have a distinction in the test account id type (u128), we put
3372		// account_type first so it does not get truncated out.
3373		T::PalletId::get().into_sub_account_truncating((AccountType::Reward, id))
3374	}
3375
3376	/// Get the member with their associated bonded and reward pool.
3377	fn get_member_with_pools(
3378		who: &T::AccountId,
3379	) -> Result<(PoolMember<T>, BondedPool<T>, RewardPool<T>), Error<T>> {
3380		let member = PoolMembers::<T>::get(who).ok_or(Error::<T>::PoolMemberNotFound)?;
3381		let bonded_pool =
3382			BondedPool::<T>::get(member.pool_id).defensive_ok_or(DefensiveError::PoolNotFound)?;
3383		let reward_pool =
3384			RewardPools::<T>::get(member.pool_id).defensive_ok_or(DefensiveError::PoolNotFound)?;
3385		Ok((member, bonded_pool, reward_pool))
3386	}
3387
3388	/// Persist the member with their associated bonded and reward pool into storage, consuming
3389	/// all of them.
3390	fn put_member_with_pools(
3391		member_account: &T::AccountId,
3392		member: PoolMember<T>,
3393		bonded_pool: BondedPool<T>,
3394		reward_pool: RewardPool<T>,
3395	) {
3396		// The pool id of a member cannot change in any case, so we use it to make sure
3397		// `member_account` is the right one.
3398		debug_assert_eq!(PoolMembers::<T>::get(member_account).unwrap().pool_id, member.pool_id);
3399		debug_assert_eq!(member.pool_id, bonded_pool.id);
3400
3401		bonded_pool.put();
3402		RewardPools::insert(member.pool_id, reward_pool);
3403		PoolMembers::<T>::insert(member_account, member);
3404	}
3405
3406	/// Calculate the equivalent point of `new_funds` in a pool with `current_balance` and
3407	/// `current_points`.
3408	fn balance_to_point(
3409		current_balance: BalanceOf<T>,
3410		current_points: BalanceOf<T>,
3411		new_funds: BalanceOf<T>,
3412	) -> BalanceOf<T> {
3413		let u256 = T::BalanceToU256::convert;
3414		let balance = T::U256ToBalance::convert;
3415		match (current_balance.is_zero(), current_points.is_zero()) {
3416			(_, true) => new_funds.saturating_mul(POINTS_TO_BALANCE_INIT_RATIO.into()),
3417			(true, false) => {
3418				// The pool was totally slashed.
3419				// This is the equivalent of `(current_points / 1) * new_funds`.
3420				new_funds.saturating_mul(current_points)
3421			},
3422			(false, false) => {
3423				// Equivalent to (current_points / current_balance) * new_funds
3424				balance(
3425					u256(current_points)
3426						.saturating_mul(u256(new_funds))
3427						// We check for zero above
3428						.div(u256(current_balance)),
3429				)
3430			},
3431		}
3432	}
3433
3434	/// Calculate the equivalent balance of `points` in a pool with `current_balance` and
3435	/// `current_points`.
3436	fn point_to_balance(
3437		current_balance: BalanceOf<T>,
3438		current_points: BalanceOf<T>,
3439		points: BalanceOf<T>,
3440	) -> BalanceOf<T> {
3441		let u256 = T::BalanceToU256::convert;
3442		let balance = T::U256ToBalance::convert;
3443		if current_balance.is_zero() || current_points.is_zero() || points.is_zero() {
3444			// There is nothing to unbond
3445			return Zero::zero()
3446		}
3447
3448		// Equivalent of (current_balance / current_points) * points
3449		balance(
3450			u256(current_balance)
3451				.saturating_mul(u256(points))
3452				// We check for zero above
3453				.div(u256(current_points)),
3454		)
3455	}
3456
3457	/// If the member has some rewards, transfer a payout from the reward pool to the member.
3458	// Emits events and potentially modifies pool state if any arithmetic saturates, but does
3459	// not persist any of the mutable inputs to storage.
3460	fn do_reward_payout(
3461		member_account: &T::AccountId,
3462		member: &mut PoolMember<T>,
3463		bonded_pool: &mut BondedPool<T>,
3464		reward_pool: &mut RewardPool<T>,
3465	) -> Result<BalanceOf<T>, DispatchError> {
3466		debug_assert_eq!(member.pool_id, bonded_pool.id);
3467		debug_assert_eq!(&mut PoolMembers::<T>::get(member_account).unwrap(), member);
3468
3469		// a member who has no skin in the game anymore cannot claim any rewards.
3470		ensure!(!member.active_points().is_zero(), Error::<T>::FullyUnbonding);
3471
3472		let (current_reward_counter, _) = reward_pool.current_reward_counter(
3473			bonded_pool.id,
3474			bonded_pool.points,
3475			bonded_pool.commission.current(),
3476		)?;
3477
3478		// Determine the pending rewards. In scenarios where commission is 100%, `pending_rewards`
3479		// will be zero.
3480		let pending_rewards = member.pending_rewards(current_reward_counter)?;
3481		if pending_rewards.is_zero() {
3482			return Ok(pending_rewards)
3483		}
3484
3485		// IFF the reward is non-zero alter the member and reward pool info.
3486		member.last_recorded_reward_counter = current_reward_counter;
3487		reward_pool.register_claimed_reward(pending_rewards);
3488
3489		T::Currency::transfer(
3490			&bonded_pool.reward_account(),
3491			member_account,
3492			pending_rewards,
3493			// defensive: the depositor has put existential deposit into the pool and it stays
3494			// untouched, reward account shall not die.
3495			Preservation::Preserve,
3496		)?;
3497
3498		Self::deposit_event(Event::<T>::PaidOut {
3499			member: member_account.clone(),
3500			pool_id: member.pool_id,
3501			payout: pending_rewards,
3502		});
3503		Ok(pending_rewards)
3504	}
3505
3506	fn do_create(
3507		who: T::AccountId,
3508		amount: BalanceOf<T>,
3509		root: AccountIdLookupOf<T>,
3510		nominator: AccountIdLookupOf<T>,
3511		bouncer: AccountIdLookupOf<T>,
3512		pool_id: PoolId,
3513	) -> DispatchResult {
3514		// ensure depositor is not restricted from joining the pool.
3515		ensure!(!T::Filter::contains(&who), Error::<T>::Restricted);
3516
3517		let root = T::Lookup::lookup(root)?;
3518		let nominator = T::Lookup::lookup(nominator)?;
3519		let bouncer = T::Lookup::lookup(bouncer)?;
3520
3521		ensure!(amount >= Pallet::<T>::depositor_min_bond(), Error::<T>::MinimumBondNotMet);
3522		ensure!(
3523			MaxPools::<T>::get().map_or(true, |max_pools| BondedPools::<T>::count() < max_pools),
3524			Error::<T>::MaxPools
3525		);
3526		ensure!(!PoolMembers::<T>::contains_key(&who), Error::<T>::AccountBelongsToOtherPool);
3527		let mut bonded_pool = BondedPool::<T>::new(
3528			pool_id,
3529			PoolRoles {
3530				root: Some(root),
3531				nominator: Some(nominator),
3532				bouncer: Some(bouncer),
3533				depositor: who.clone(),
3534			},
3535		);
3536
3537		bonded_pool.try_inc_members()?;
3538		let points = bonded_pool.try_bond_funds(&who, amount, BondType::Create)?;
3539
3540		// Transfer the minimum balance for the reward account.
3541		T::Currency::transfer(
3542			&who,
3543			&bonded_pool.reward_account(),
3544			T::Currency::minimum_balance(),
3545			Preservation::Expendable,
3546		)?;
3547
3548		// Restrict reward account balance from going below ED.
3549		Self::freeze_pool_deposit(&bonded_pool.reward_account())?;
3550
3551		PoolMembers::<T>::insert(
3552			who.clone(),
3553			PoolMember::<T> {
3554				pool_id,
3555				points,
3556				last_recorded_reward_counter: Zero::zero(),
3557				unbonding_eras: Default::default(),
3558			},
3559		);
3560		RewardPools::<T>::insert(
3561			pool_id,
3562			RewardPool::<T> {
3563				last_recorded_reward_counter: Zero::zero(),
3564				last_recorded_total_payouts: Zero::zero(),
3565				total_rewards_claimed: Zero::zero(),
3566				total_commission_pending: Zero::zero(),
3567				total_commission_claimed: Zero::zero(),
3568			},
3569		);
3570		ReversePoolIdLookup::<T>::insert(bonded_pool.bonded_account(), pool_id);
3571
3572		Self::deposit_event(Event::<T>::Created { depositor: who.clone(), pool_id });
3573
3574		Self::deposit_event(Event::<T>::Bonded {
3575			member: who,
3576			pool_id,
3577			bonded: amount,
3578			joined: true,
3579		});
3580		bonded_pool.put();
3581
3582		Ok(())
3583	}
3584
3585	fn do_bond_extra(
3586		signer: T::AccountId,
3587		member_account: T::AccountId,
3588		extra: BondExtra<BalanceOf<T>>,
3589	) -> DispatchResult {
3590		// ensure account is not restricted from joining the pool.
3591		ensure!(!T::Filter::contains(&member_account), Error::<T>::Restricted);
3592
3593		if signer != member_account {
3594			ensure!(
3595				ClaimPermissions::<T>::get(&member_account).can_bond_extra(),
3596				Error::<T>::DoesNotHavePermission
3597			);
3598			ensure!(extra == BondExtra::Rewards, Error::<T>::BondExtraRestricted);
3599		}
3600
3601		let (mut member, mut bonded_pool, mut reward_pool) =
3602			Self::get_member_with_pools(&member_account)?;
3603
3604		// payout related stuff: we must claim the payouts, and updated recorded payout data
3605		// before updating the bonded pool points, similar to that of `join` transaction.
3606		reward_pool.update_records(
3607			bonded_pool.id,
3608			bonded_pool.points,
3609			bonded_pool.commission.current(),
3610		)?;
3611		let claimed = Self::do_reward_payout(
3612			&member_account,
3613			&mut member,
3614			&mut bonded_pool,
3615			&mut reward_pool,
3616		)?;
3617
3618		let (points_issued, bonded) = match extra {
3619			BondExtra::FreeBalance(amount) =>
3620				(bonded_pool.try_bond_funds(&member_account, amount, BondType::Extra)?, amount),
3621			BondExtra::Rewards =>
3622				(bonded_pool.try_bond_funds(&member_account, claimed, BondType::Extra)?, claimed),
3623		};
3624
3625		bonded_pool.ok_to_be_open()?;
3626		member.points =
3627			member.points.checked_add(&points_issued).ok_or(Error::<T>::OverflowRisk)?;
3628
3629		Self::deposit_event(Event::<T>::Bonded {
3630			member: member_account.clone(),
3631			pool_id: member.pool_id,
3632			bonded,
3633			joined: false,
3634		});
3635		Self::put_member_with_pools(&member_account, member, bonded_pool, reward_pool);
3636
3637		Ok(())
3638	}
3639
3640	fn do_claim_commission(who: T::AccountId, pool_id: PoolId) -> DispatchResult {
3641		let bonded_pool = BondedPool::<T>::get(pool_id).ok_or(Error::<T>::PoolNotFound)?;
3642		ensure!(bonded_pool.can_claim_commission(&who), Error::<T>::DoesNotHavePermission);
3643
3644		let mut reward_pool = RewardPools::<T>::get(pool_id)
3645			.defensive_ok_or::<Error<T>>(DefensiveError::RewardPoolNotFound.into())?;
3646
3647		// IMPORTANT: ensure newly pending commission not yet processed is added to
3648		// `total_commission_pending`.
3649		reward_pool.update_records(
3650			pool_id,
3651			bonded_pool.points,
3652			bonded_pool.commission.current(),
3653		)?;
3654
3655		let commission = reward_pool.total_commission_pending;
3656		ensure!(!commission.is_zero(), Error::<T>::NoPendingCommission);
3657
3658		let payee = bonded_pool
3659			.commission
3660			.current
3661			.as_ref()
3662			.map(|(_, p)| p.clone())
3663			.ok_or(Error::<T>::NoCommissionCurrentSet)?;
3664
3665		// Payout claimed commission.
3666		T::Currency::transfer(
3667			&bonded_pool.reward_account(),
3668			&payee,
3669			commission,
3670			Preservation::Preserve,
3671		)?;
3672
3673		// Add pending commission to total claimed counter.
3674		reward_pool.total_commission_claimed =
3675			reward_pool.total_commission_claimed.saturating_add(commission);
3676		// Reset total pending commission counter to zero.
3677		reward_pool.total_commission_pending = Zero::zero();
3678		RewardPools::<T>::insert(pool_id, reward_pool);
3679
3680		Self::deposit_event(Event::<T>::PoolCommissionClaimed { pool_id, commission });
3681		Ok(())
3682	}
3683
3684	pub(crate) fn do_claim_payout(
3685		signer: T::AccountId,
3686		member_account: T::AccountId,
3687	) -> DispatchResult {
3688		if signer != member_account {
3689			ensure!(
3690				ClaimPermissions::<T>::get(&member_account).can_claim_payout(),
3691				Error::<T>::DoesNotHavePermission
3692			);
3693		}
3694		let (mut member, mut bonded_pool, mut reward_pool) =
3695			Self::get_member_with_pools(&member_account)?;
3696
3697		Self::do_reward_payout(&member_account, &mut member, &mut bonded_pool, &mut reward_pool)?;
3698
3699		Self::put_member_with_pools(&member_account, member, bonded_pool, reward_pool);
3700		Ok(())
3701	}
3702
3703	fn do_adjust_pool_deposit(who: T::AccountId, pool: PoolId) -> DispatchResult {
3704		let bonded_pool = BondedPool::<T>::get(pool).ok_or(Error::<T>::PoolNotFound)?;
3705
3706		let reward_acc = &bonded_pool.reward_account();
3707		let pre_frozen_balance =
3708			T::Currency::balance_frozen(&FreezeReason::PoolMinBalance.into(), reward_acc);
3709		let min_balance = T::Currency::minimum_balance();
3710
3711		if pre_frozen_balance == min_balance {
3712			return Err(Error::<T>::NothingToAdjust.into())
3713		}
3714
3715		// Update frozen amount with current ED.
3716		Self::freeze_pool_deposit(reward_acc)?;
3717
3718		if pre_frozen_balance > min_balance {
3719			// Transfer excess back to depositor.
3720			let excess = pre_frozen_balance.saturating_sub(min_balance);
3721			T::Currency::transfer(reward_acc, &who, excess, Preservation::Preserve)?;
3722			Self::deposit_event(Event::<T>::MinBalanceExcessAdjusted {
3723				pool_id: pool,
3724				amount: excess,
3725			});
3726		} else {
3727			// Transfer ED deficit from depositor to the pool
3728			let deficit = min_balance.saturating_sub(pre_frozen_balance);
3729			T::Currency::transfer(&who, reward_acc, deficit, Preservation::Expendable)?;
3730			Self::deposit_event(Event::<T>::MinBalanceDeficitAdjusted {
3731				pool_id: pool,
3732				amount: deficit,
3733			});
3734		}
3735
3736		Ok(())
3737	}
3738
3739	/// Slash member against the pending slash for the pool.
3740	fn do_apply_slash(
3741		member_account: &T::AccountId,
3742		reporter: Option<T::AccountId>,
3743		enforce_min_slash: bool,
3744	) -> DispatchResult {
3745		let member = PoolMembers::<T>::get(member_account).ok_or(Error::<T>::PoolMemberNotFound)?;
3746
3747		let pending_slash =
3748			Self::member_pending_slash(Member::from(member_account.clone()), member.clone())?;
3749
3750		// ensure there is something to slash.
3751		ensure!(!pending_slash.is_zero(), Error::<T>::NothingToSlash);
3752
3753		if enforce_min_slash {
3754			// ensure slashed amount is at least the minimum balance.
3755			ensure!(pending_slash >= T::Currency::minimum_balance(), Error::<T>::SlashTooLow);
3756		}
3757
3758		T::StakeAdapter::member_slash(
3759			Member::from(member_account.clone()),
3760			Pool::from(Pallet::<T>::generate_bonded_account(member.pool_id)),
3761			pending_slash,
3762			reporter,
3763		)
3764	}
3765
3766	/// Pending slash for a member.
3767	///
3768	/// Takes the pool_member object corresponding to the `member_account`.
3769	fn member_pending_slash(
3770		member_account: Member<T::AccountId>,
3771		pool_member: PoolMember<T>,
3772	) -> Result<BalanceOf<T>, DispatchError> {
3773		// only executed in tests: ensure the member account is correct.
3774		debug_assert!(
3775			PoolMembers::<T>::get(member_account.clone().get()).expect("member must exist") ==
3776				pool_member
3777		);
3778
3779		let pool_account = Pallet::<T>::generate_bonded_account(pool_member.pool_id);
3780		// if the pool doesn't have any pending slash, it implies the member also does not have any
3781		// pending slash.
3782		if T::StakeAdapter::pending_slash(Pool::from(pool_account.clone())).is_zero() {
3783			return Ok(Zero::zero())
3784		}
3785
3786		// this is their actual held balance that may or may not have been slashed.
3787		let actual_balance = T::StakeAdapter::member_delegation_balance(member_account)
3788			// no delegation implies the member delegation is not migrated yet to `DelegateStake`.
3789			.ok_or(Error::<T>::NotMigrated)?;
3790
3791		// this is their balance in the pool
3792		let expected_balance = pool_member.total_balance();
3793
3794		// return the amount to be slashed.
3795		Ok(actual_balance.saturating_sub(expected_balance))
3796	}
3797
3798	/// Apply freeze on reward account to restrict it from going below ED.
3799	pub(crate) fn freeze_pool_deposit(reward_acc: &T::AccountId) -> DispatchResult {
3800		T::Currency::set_freeze(
3801			&FreezeReason::PoolMinBalance.into(),
3802			reward_acc,
3803			T::Currency::minimum_balance(),
3804		)
3805	}
3806
3807	/// Removes the ED freeze on the reward account of `pool_id`.
3808	pub fn unfreeze_pool_deposit(reward_acc: &T::AccountId) -> DispatchResult {
3809		T::Currency::thaw(&FreezeReason::PoolMinBalance.into(), reward_acc)
3810	}
3811
3812	/// Ensure the correctness of the state of this pallet.
3813	///
3814	/// This should be valid before or after each state transition of this pallet.
3815	///
3816	/// ## Invariants:
3817	///
3818	/// First, let's consider pools:
3819	///
3820	/// * `BondedPools` and `RewardPools` must all have the EXACT SAME key-set.
3821	/// * `SubPoolsStorage` must be a subset of the above superset.
3822	/// * `Metadata` keys must be a subset of the above superset.
3823	/// * the count of the above set must be less than `MaxPools`.
3824	///
3825	/// Then, considering members as well:
3826	///
3827	/// * each `BondedPool.member_counter` must be:
3828	///   - correct (compared to actual count of member who have `.pool_id` this pool)
3829	///   - less than `MaxPoolMembersPerPool`.
3830	/// * each `member.pool_id` must correspond to an existing `BondedPool.id` (which implies the
3831	///   existence of the reward pool as well).
3832	/// * count of all members must be less than `MaxPoolMembers`.
3833	/// * each `BondedPool.points` must never be lower than the pool's balance.
3834	///
3835	/// Then, considering unbonding members:
3836	///
3837	/// for each pool:
3838	///   * sum of the balance that's tracked in all unbonding pools must be the same as the
3839	///     unbonded balance of the main account, as reported by the staking interface.
3840	///   * sum of the balance that's tracked in all unbonding pools, plus the bonded balance of the
3841	///     main account should be less than or qual to the total balance of the main account.
3842	///
3843	/// ## Sanity check level
3844	///
3845	/// To cater for tests that want to escape parts of these checks, this function is split into
3846	/// multiple `level`s, where the higher the level, the more checks we performs. So,
3847	/// `try_state(255)` is the strongest sanity check, and `0` performs no checks.
3848	#[cfg(any(feature = "try-runtime", feature = "fuzzing", test, debug_assertions))]
3849	pub fn do_try_state(level: u8) -> Result<(), TryRuntimeError> {
3850		if level.is_zero() {
3851			return Ok(())
3852		}
3853		// note: while a bit wacky, since they have the same key, even collecting to vec should
3854		// result in the same set of keys, in the same order.
3855		let bonded_pools = BondedPools::<T>::iter_keys().collect::<Vec<_>>();
3856		let reward_pools = RewardPools::<T>::iter_keys().collect::<Vec<_>>();
3857		ensure!(
3858			bonded_pools == reward_pools,
3859			"`BondedPools` and `RewardPools` must all have the EXACT SAME key-set."
3860		);
3861
3862		ensure!(
3863			SubPoolsStorage::<T>::iter_keys().all(|k| bonded_pools.contains(&k)),
3864			"`SubPoolsStorage` must be a subset of the above superset."
3865		);
3866		ensure!(
3867			Metadata::<T>::iter_keys().all(|k| bonded_pools.contains(&k)),
3868			"`Metadata` keys must be a subset of the above superset."
3869		);
3870
3871		ensure!(
3872			MaxPools::<T>::get().map_or(true, |max| bonded_pools.len() <= (max as usize)),
3873			Error::<T>::MaxPools
3874		);
3875
3876		for id in reward_pools {
3877			let account = Self::generate_reward_account(id);
3878			if T::Currency::reducible_balance(&account, Preservation::Expendable, Fortitude::Polite) <
3879				T::Currency::minimum_balance()
3880			{
3881				log!(
3882					warn,
3883					"reward pool of {:?}: {:?} (ed = {:?}), should only happen because ED has \
3884					changed recently. Pool operators should be notified to top up the reward \
3885					account",
3886					id,
3887					T::Currency::reducible_balance(
3888						&account,
3889						Preservation::Expendable,
3890						Fortitude::Polite
3891					),
3892					T::Currency::minimum_balance(),
3893				)
3894			}
3895		}
3896
3897		let mut pools_members = BTreeMap::<PoolId, u32>::new();
3898		let mut pools_members_pending_rewards = BTreeMap::<PoolId, BalanceOf<T>>::new();
3899		let mut all_members = 0u32;
3900		let mut total_balance_members = Default::default();
3901		PoolMembers::<T>::iter().try_for_each(|(_, d)| -> Result<(), TryRuntimeError> {
3902			let bonded_pool = BondedPools::<T>::get(d.pool_id).unwrap();
3903			ensure!(!d.total_points().is_zero(), "No member should have zero points");
3904			*pools_members.entry(d.pool_id).or_default() += 1;
3905			all_members += 1;
3906
3907			let reward_pool = RewardPools::<T>::get(d.pool_id).unwrap();
3908			if !bonded_pool.points.is_zero() {
3909				let commission = bonded_pool.commission.current();
3910				let (current_rc, _) = reward_pool
3911					.current_reward_counter(d.pool_id, bonded_pool.points, commission)
3912					.unwrap();
3913				let pending_rewards = d.pending_rewards(current_rc).unwrap();
3914				*pools_members_pending_rewards.entry(d.pool_id).or_default() += pending_rewards;
3915			} // else this pool has been heavily slashed and cannot have any rewards anymore.
3916			total_balance_members += d.total_balance();
3917
3918			Ok(())
3919		})?;
3920
3921		RewardPools::<T>::iter_keys().try_for_each(|id| -> Result<(), TryRuntimeError> {
3922			// the sum of the pending rewards must be less than the leftover balance. Since the
3923			// reward math rounds down, we might accumulate some dust here.
3924			let pending_rewards_lt_leftover_bal = RewardPool::<T>::current_balance(id) >=
3925				pools_members_pending_rewards.get(&id).copied().unwrap_or_default();
3926
3927			// If this happens, this is most likely due to an old bug and not a recent code change.
3928			// We warn about this in try-runtime checks but do not panic.
3929			if !pending_rewards_lt_leftover_bal {
3930				log::warn!(
3931					"pool {:?}, sum pending rewards = {:?}, remaining balance = {:?}",
3932					id,
3933					pools_members_pending_rewards.get(&id),
3934					RewardPool::<T>::current_balance(id)
3935				);
3936			}
3937			Ok(())
3938		})?;
3939
3940		let mut expected_tvl: BalanceOf<T> = Default::default();
3941		BondedPools::<T>::iter().try_for_each(|(id, inner)| -> Result<(), TryRuntimeError> {
3942			let bonded_pool = BondedPool { id, inner };
3943			ensure!(
3944				pools_members.get(&id).copied().unwrap_or_default() ==
3945				bonded_pool.member_counter,
3946				"Each `BondedPool.member_counter` must be equal to the actual count of members of this pool"
3947			);
3948			ensure!(
3949				MaxPoolMembersPerPool::<T>::get()
3950					.map_or(true, |max| bonded_pool.member_counter <= max),
3951				Error::<T>::MaxPoolMembers
3952			);
3953
3954			let depositor = PoolMembers::<T>::get(&bonded_pool.roles.depositor).unwrap();
3955			ensure!(
3956				bonded_pool.is_destroying_and_only_depositor(depositor.active_points()) ||
3957					depositor.active_points() >= MinCreateBond::<T>::get(),
3958				"depositor must always have MinCreateBond stake in the pool, except for when the \
3959				pool is being destroyed and the depositor is the last member",
3960			);
3961
3962			ensure!(
3963				bonded_pool.points >= bonded_pool.points_to_balance(bonded_pool.points),
3964				"Each `BondedPool.points` must never be lower than the pool's balance"
3965			);
3966
3967			expected_tvl += T::StakeAdapter::total_stake(Pool::from(bonded_pool.bonded_account()));
3968
3969			Ok(())
3970		})?;
3971
3972		ensure!(
3973			MaxPoolMembers::<T>::get().map_or(true, |max| all_members <= max),
3974			Error::<T>::MaxPoolMembers
3975		);
3976
3977		ensure!(
3978			TotalValueLocked::<T>::get() == expected_tvl,
3979			"TVL deviates from the actual sum of funds of all Pools."
3980		);
3981
3982		ensure!(
3983			TotalValueLocked::<T>::get() <= total_balance_members,
3984			"TVL must be equal to or less than the total balance of all PoolMembers."
3985		);
3986
3987		if level <= 1 {
3988			return Ok(())
3989		}
3990
3991		for (pool_id, _pool) in BondedPools::<T>::iter() {
3992			let pool_account = Pallet::<T>::generate_bonded_account(pool_id);
3993			let subs = SubPoolsStorage::<T>::get(pool_id).unwrap_or_default();
3994
3995			let sum_unbonding_balance = subs.sum_unbonding_balance();
3996			let bonded_balance = T::StakeAdapter::active_stake(Pool::from(pool_account.clone()));
3997			// TODO: should be total_balance + unclaimed_withdrawals from delegated staking
3998			let total_balance = T::StakeAdapter::total_balance(Pool::from(pool_account.clone()))
3999				// At the time when StakeAdapter is changed to `DelegateStake` but pool is not yet
4000				// migrated, the total balance would be none.
4001				.unwrap_or(T::Currency::total_balance(&pool_account));
4002
4003			if total_balance < bonded_balance + sum_unbonding_balance {
4004				log!(
4005						warn,
4006						"possibly faulty pool: {:?} / {:?}, total_balance {:?} >= bonded_balance {:?} + sum_unbonding_balance {:?}",
4007						pool_id,
4008						_pool,
4009						total_balance,
4010						bonded_balance,
4011						sum_unbonding_balance
4012					)
4013			};
4014		}
4015
4016		// Warn if any pool has incorrect ED frozen. We don't want to fail hard as this could be a
4017		// result of an intentional ED change.
4018		Self::check_ed_imbalance()?;
4019
4020		Ok(())
4021	}
4022
4023	/// Check if any pool have an incorrect amount of ED frozen.
4024	///
4025	/// This can happen if the ED has changed since the pool was created.
4026	#[cfg(any(
4027		feature = "try-runtime",
4028		feature = "runtime-benchmarks",
4029		feature = "fuzzing",
4030		test,
4031		debug_assertions
4032	))]
4033	pub fn check_ed_imbalance() -> Result<(), DispatchError> {
4034		let mut failed: u32 = 0;
4035		BondedPools::<T>::iter_keys().for_each(|id| {
4036			let reward_acc = Self::generate_reward_account(id);
4037			let frozen_balance =
4038				T::Currency::balance_frozen(&FreezeReason::PoolMinBalance.into(), &reward_acc);
4039
4040			let expected_frozen_balance = T::Currency::minimum_balance();
4041			if frozen_balance != expected_frozen_balance {
4042				failed += 1;
4043				log::warn!(
4044					"pool {:?} has incorrect ED frozen that can result from change in ED. Expected  = {:?},  Actual = {:?}",
4045					id,
4046					expected_frozen_balance,
4047					frozen_balance,
4048				);
4049			}
4050		});
4051
4052		ensure!(failed == 0, "Some pools do not have correct ED frozen");
4053		Ok(())
4054	}
4055	/// Fully unbond the shares of `member`, when executed from `origin`.
4056	///
4057	/// This is useful for backwards compatibility with the majority of tests that only deal with
4058	/// full unbonding, not partial unbonding.
4059	#[cfg(any(feature = "runtime-benchmarks", test))]
4060	pub fn fully_unbond(
4061		origin: frame_system::pallet_prelude::OriginFor<T>,
4062		member: T::AccountId,
4063	) -> DispatchResult {
4064		let points = PoolMembers::<T>::get(&member).map(|d| d.active_points()).unwrap_or_default();
4065		let member_lookup = T::Lookup::unlookup(member);
4066		Self::unbond(origin, member_lookup, points)
4067	}
4068}
4069
4070impl<T: Config> Pallet<T> {
4071	/// Returns the pending rewards for the specified `who` account.
4072	///
4073	/// In the case of error, `None` is returned. Used by runtime API.
4074	pub fn api_pending_rewards(who: T::AccountId) -> Option<BalanceOf<T>> {
4075		if let Some(pool_member) = PoolMembers::<T>::get(who) {
4076			if let Some((reward_pool, bonded_pool)) = RewardPools::<T>::get(pool_member.pool_id)
4077				.zip(BondedPools::<T>::get(pool_member.pool_id))
4078			{
4079				let commission = bonded_pool.commission.current();
4080				let (current_reward_counter, _) = reward_pool
4081					.current_reward_counter(pool_member.pool_id, bonded_pool.points, commission)
4082					.ok()?;
4083				return pool_member.pending_rewards(current_reward_counter).ok()
4084			}
4085		}
4086
4087		None
4088	}
4089
4090	/// Returns the points to balance conversion for a specified pool.
4091	///
4092	/// If the pool ID does not exist, it returns 0 ratio points to balance. Used by runtime API.
4093	pub fn api_points_to_balance(pool_id: PoolId, points: BalanceOf<T>) -> BalanceOf<T> {
4094		if let Some(pool) = BondedPool::<T>::get(pool_id) {
4095			pool.points_to_balance(points)
4096		} else {
4097			Zero::zero()
4098		}
4099	}
4100
4101	/// Returns the equivalent `new_funds` balance to point conversion for a specified pool.
4102	///
4103	/// If the pool ID does not exist, returns 0 ratio balance to points. Used by runtime API.
4104	pub fn api_balance_to_points(pool_id: PoolId, new_funds: BalanceOf<T>) -> BalanceOf<T> {
4105		if let Some(pool) = BondedPool::<T>::get(pool_id) {
4106			let bonded_balance =
4107				T::StakeAdapter::active_stake(Pool::from(Self::generate_bonded_account(pool_id)));
4108			Pallet::<T>::balance_to_point(bonded_balance, pool.points, new_funds)
4109		} else {
4110			Zero::zero()
4111		}
4112	}
4113
4114	/// Returns the unapplied slash of the pool.
4115	///
4116	/// Pending slash is only applicable with [`adapter::DelegateStake`] strategy.
4117	pub fn api_pool_pending_slash(pool_id: PoolId) -> BalanceOf<T> {
4118		T::StakeAdapter::pending_slash(Pool::from(Self::generate_bonded_account(pool_id)))
4119	}
4120
4121	/// Returns the unapplied slash of a member.
4122	///
4123	/// Pending slash is only applicable with [`adapter::DelegateStake`] strategy.
4124	///
4125	/// If pending slash of the member exceeds `ExistentialDeposit`, it can be reported on
4126	/// chain via [`Call::apply_slash`].
4127	pub fn api_member_pending_slash(who: T::AccountId) -> BalanceOf<T> {
4128		PoolMembers::<T>::get(who.clone())
4129			.map(|pool_member| {
4130				Self::member_pending_slash(Member::from(who), pool_member).unwrap_or_default()
4131			})
4132			.unwrap_or_default()
4133	}
4134
4135	/// Checks whether pool needs to be migrated to [`adapter::StakeStrategyType::Delegate`]. Only
4136	/// applicable when the [`Config::StakeAdapter`] is [`adapter::DelegateStake`].
4137	///
4138	/// Useful to check this before calling [`Call::migrate_pool_to_delegate_stake`].
4139	pub fn api_pool_needs_delegate_migration(pool_id: PoolId) -> bool {
4140		// if the `Delegate` strategy is not used in the pallet, then no migration required.
4141		if T::StakeAdapter::strategy_type() != adapter::StakeStrategyType::Delegate {
4142			return false
4143		}
4144
4145		// if pool does not exist, return false.
4146		if !BondedPools::<T>::contains_key(pool_id) {
4147			return false
4148		}
4149
4150		let pool_account = Self::generate_bonded_account(pool_id);
4151
4152		// true if pool is still not migrated to `DelegateStake`.
4153		T::StakeAdapter::pool_strategy(Pool::from(pool_account)) !=
4154			adapter::StakeStrategyType::Delegate
4155	}
4156
4157	/// Checks whether member delegation needs to be migrated to
4158	/// [`adapter::StakeStrategyType::Delegate`]. Only applicable when the [`Config::StakeAdapter`]
4159	/// is [`adapter::DelegateStake`].
4160	///
4161	/// Useful to check this before calling [`Call::migrate_delegation`].
4162	pub fn api_member_needs_delegate_migration(who: T::AccountId) -> bool {
4163		// if the `Delegate` strategy is not used in the pallet, then no migration required.
4164		if T::StakeAdapter::strategy_type() != adapter::StakeStrategyType::Delegate {
4165			return false
4166		}
4167
4168		PoolMembers::<T>::get(who.clone())
4169			.map(|pool_member| {
4170				if Self::api_pool_needs_delegate_migration(pool_member.pool_id) {
4171					// the pool needs to be migrated before members can be migrated.
4172					return false
4173				}
4174
4175				let member_balance = pool_member.total_balance();
4176				let delegated_balance =
4177					T::StakeAdapter::member_delegation_balance(Member::from(who.clone()));
4178
4179				// if the member has no delegation but has some balance in the pool, then it needs
4180				// to be migrated.
4181				delegated_balance.is_none() && !member_balance.is_zero()
4182			})
4183			.unwrap_or_default()
4184	}
4185
4186	/// Contribution of the member in the pool.
4187	///
4188	/// Includes balance that is unbonded from staking but not claimed yet from the pool, therefore
4189	/// this balance can be higher than the staked funds.
4190	pub fn api_member_total_balance(who: T::AccountId) -> BalanceOf<T> {
4191		PoolMembers::<T>::get(who.clone())
4192			.map(|m| m.total_balance())
4193			.unwrap_or_default()
4194	}
4195
4196	/// Total balance contributed to the pool.
4197	pub fn api_pool_balance(pool_id: PoolId) -> BalanceOf<T> {
4198		T::StakeAdapter::total_balance(Pool::from(Self::generate_bonded_account(pool_id)))
4199			.unwrap_or_default()
4200	}
4201
4202	/// Returns the bonded account and reward account associated with the pool_id.
4203	pub fn api_pool_accounts(pool_id: PoolId) -> (T::AccountId, T::AccountId) {
4204		let bonded_account = Self::generate_bonded_account(pool_id);
4205		let reward_account = Self::generate_reward_account(pool_id);
4206		(bonded_account, reward_account)
4207	}
4208}
4209
4210impl<T: Config> sp_staking::OnStakingUpdate<T::AccountId, BalanceOf<T>> for Pallet<T> {
4211	/// Reduces the balances of the [`SubPools`], that belong to the pool involved in the
4212	/// slash, to the amount that is defined in the `slashed_unlocking` field of
4213	/// [`sp_staking::OnStakingUpdate::on_slash`]
4214	///
4215	/// Emits the `PoolsSlashed` event.
4216	fn on_slash(
4217		pool_account: &T::AccountId,
4218		// Bonded balance is always read directly from staking, therefore we don't need to update
4219		// anything here.
4220		slashed_bonded: BalanceOf<T>,
4221		slashed_unlocking: &BTreeMap<EraIndex, BalanceOf<T>>,
4222		total_slashed: BalanceOf<T>,
4223	) {
4224		let Some(pool_id) = ReversePoolIdLookup::<T>::get(pool_account) else { return };
4225		// As the slashed account belongs to a `BondedPool` the `TotalValueLocked` decreases and
4226		// an event is emitted.
4227		TotalValueLocked::<T>::mutate(|tvl| {
4228			tvl.defensive_saturating_reduce(total_slashed);
4229		});
4230
4231		if let Some(mut sub_pools) = SubPoolsStorage::<T>::get(pool_id) {
4232			// set the reduced balance for each of the `SubPools`
4233			slashed_unlocking.iter().for_each(|(era, slashed_balance)| {
4234				if let Some(pool) = sub_pools.with_era.get_mut(era).defensive() {
4235					pool.balance = *slashed_balance;
4236					Self::deposit_event(Event::<T>::UnbondingPoolSlashed {
4237						era: *era,
4238						pool_id,
4239						balance: *slashed_balance,
4240					});
4241				}
4242			});
4243			SubPoolsStorage::<T>::insert(pool_id, sub_pools);
4244		} else if !slashed_unlocking.is_empty() {
4245			defensive!("Expected SubPools were not found");
4246		}
4247		Self::deposit_event(Event::<T>::PoolSlashed { pool_id, balance: slashed_bonded });
4248	}
4249
4250	/// Reduces the overall `TotalValueLocked` if a withdrawal happened for a pool involved in the
4251	/// staking withdraw.
4252	fn on_withdraw(pool_account: &T::AccountId, amount: BalanceOf<T>) {
4253		if ReversePoolIdLookup::<T>::get(pool_account).is_some() {
4254			TotalValueLocked::<T>::mutate(|tvl| {
4255				tvl.saturating_reduce(amount);
4256			});
4257		}
4258	}
4259}
4260
4261/// A utility struct that provides a way to check if a given account is a pool member.
4262pub struct AllPoolMembers<T: Config>(PhantomData<T>);
4263impl<T: Config> Contains<T::AccountId> for AllPoolMembers<T> {
4264	fn contains(t: &T::AccountId) -> bool {
4265		PoolMembers::<T>::contains_key(t)
4266	}
4267}