referrerpolicy=no-referrer-when-downgrade

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