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