referrerpolicy=no-referrer-when-downgrade

pallet_child_bounties/
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//! # Child Bounties Pallet ( `pallet-child-bounties` )
19//!
20//! ## Child Bounty
21//!
22//! > NOTE: This pallet is tightly coupled with `pallet-treasury` and `pallet-bounties`.
23//!
24//! With child bounties, a large bounty proposal can be divided into smaller chunks,
25//! for parallel execution, and for efficient governance and tracking of spent funds.
26//! A child bounty is a smaller piece of work, extracted from a parent bounty.
27//! A curator is assigned after the child bounty is created by the parent bounty curator,
28//! to be delegated with the responsibility of assigning a payout address once the specified
29//! set of tasks is completed.
30//!
31//! ## Interface
32//!
33//! ### Dispatchable Functions
34//!
35//! Child Bounty protocol:
36//! - `add_child_bounty` - Add a child bounty for a parent bounty to for dividing the work in
37//!   smaller tasks.
38//! - `propose_curator` - Assign an account to a child bounty as candidate curator.
39//! - `accept_curator` - Accept a child bounty assignment from the parent bounty curator, setting a
40//!   curator deposit.
41//! - `award_child_bounty` - Close and pay out the specified amount for the completed work.
42//! - `claim_child_bounty` - Claim a specific child bounty amount from the payout address.
43//! - `unassign_curator` - Unassign an accepted curator from a specific child bounty.
44//! - `close_child_bounty` - Cancel the child bounty for a specific treasury amount and close the
45//!   bounty.
46
47// Most of the business logic in this pallet has been
48// originally contributed by "https://github.com/shamb0",
49// as part of the PR - https://github.com/paritytech/substrate/pull/7965.
50// The code has been moved here and then refactored in order to
51// extract child bounties as a separate pallet.
52
53#![cfg_attr(not(feature = "std"), no_std)]
54
55mod benchmarking;
56pub mod migration;
57mod tests;
58pub mod weights;
59
60extern crate alloc;
61
62/// The log target for this pallet.
63const LOG_TARGET: &str = "runtime::child-bounties";
64
65use alloc::vec::Vec;
66
67use frame_support::traits::{
68	Currency,
69	ExistenceRequirement::{AllowDeath, KeepAlive},
70	Get, OnUnbalanced, ReservableCurrency, WithdrawReasons,
71};
72
73use sp_runtime::{
74	traits::{
75		AccountIdConversion, BadOrigin, BlockNumberProvider, CheckedSub, Saturating, StaticLookup,
76		Zero,
77	},
78	DispatchResult, RuntimeDebug,
79};
80
81use frame_support::pallet_prelude::*;
82use frame_system::pallet_prelude::{
83	ensure_signed, BlockNumberFor as SystemBlockNumberFor, OriginFor,
84};
85use pallet_bounties::BountyStatus;
86use scale_info::TypeInfo;
87pub use weights::WeightInfo;
88
89pub use pallet::*;
90
91pub type BalanceOf<T> = pallet_treasury::BalanceOf<T>;
92pub type BountiesError<T> = pallet_bounties::Error<T>;
93pub type BountyIndex = pallet_bounties::BountyIndex;
94pub type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
95pub type BlockNumberFor<T> =
96	<<T as pallet_treasury::Config>::BlockNumberProvider as BlockNumberProvider>::BlockNumber;
97
98/// A child bounty proposal.
99#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
100pub struct ChildBounty<AccountId, Balance, BlockNumber> {
101	/// The parent of this child-bounty.
102	pub parent_bounty: BountyIndex,
103	/// The (total) amount that should be paid if this child-bounty is rewarded.
104	pub value: Balance,
105	/// The child bounty curator fee.
106	pub fee: Balance,
107	/// The deposit of child-bounty curator.
108	pub curator_deposit: Balance,
109	/// The status of this child-bounty.
110	pub status: ChildBountyStatus<AccountId, BlockNumber>,
111}
112
113/// The status of a child-bounty.
114#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
115pub enum ChildBountyStatus<AccountId, BlockNumber> {
116	/// The child-bounty is added and waiting for curator assignment.
117	Added,
118	/// A curator has been proposed by the parent bounty curator. Waiting for
119	/// acceptance from the child-bounty curator.
120	CuratorProposed {
121		/// The assigned child-bounty curator of this bounty.
122		curator: AccountId,
123	},
124	/// The child-bounty is active and waiting to be awarded.
125	Active {
126		/// The curator of this child-bounty.
127		curator: AccountId,
128	},
129	/// The child-bounty is awarded and waiting to released after a delay.
130	PendingPayout {
131		/// The curator of this child-bounty.
132		curator: AccountId,
133		/// The beneficiary of the child-bounty.
134		beneficiary: AccountId,
135		/// When the child-bounty can be claimed.
136		unlock_at: BlockNumber,
137	},
138}
139
140#[frame_support::pallet]
141pub mod pallet {
142
143	use super::*;
144
145	/// The in-code storage version.
146	const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
147
148	#[pallet::pallet]
149	#[pallet::storage_version(STORAGE_VERSION)]
150	pub struct Pallet<T>(_);
151
152	#[pallet::config]
153	pub trait Config:
154		frame_system::Config + pallet_treasury::Config + pallet_bounties::Config
155	{
156		/// Maximum number of child bounties that can be added to a parent bounty.
157		#[pallet::constant]
158		type MaxActiveChildBountyCount: Get<u32>;
159
160		/// Minimum value for a child-bounty.
161		#[pallet::constant]
162		type ChildBountyValueMinimum: Get<BalanceOf<Self>>;
163
164		/// The overarching event type.
165		#[allow(deprecated)]
166		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
167
168		/// Weight information for extrinsics in this pallet.
169		type WeightInfo: WeightInfo;
170	}
171
172	#[pallet::error]
173	pub enum Error<T> {
174		/// The parent bounty is not in active state.
175		ParentBountyNotActive,
176		/// The bounty balance is not enough to add new child-bounty.
177		InsufficientBountyBalance,
178		/// Number of child bounties exceeds limit `MaxActiveChildBountyCount`.
179		TooManyChildBounties,
180	}
181
182	#[pallet::event]
183	#[pallet::generate_deposit(pub(super) fn deposit_event)]
184	pub enum Event<T: Config> {
185		/// A child-bounty is added.
186		Added { index: BountyIndex, child_index: BountyIndex },
187		/// A child-bounty is awarded to a beneficiary.
188		Awarded { index: BountyIndex, child_index: BountyIndex, beneficiary: T::AccountId },
189		/// A child-bounty is claimed by beneficiary.
190		Claimed {
191			index: BountyIndex,
192			child_index: BountyIndex,
193			payout: BalanceOf<T>,
194			beneficiary: T::AccountId,
195		},
196		/// A child-bounty is cancelled.
197		Canceled { index: BountyIndex, child_index: BountyIndex },
198	}
199
200	/// DEPRECATED: Replaced with `ParentTotalChildBounties` storage item keeping dedicated counts
201	/// for each parent bounty. Number of total child bounties. Will be removed in May 2025.
202	#[pallet::storage]
203	pub type ChildBountyCount<T: Config> = StorageValue<_, BountyIndex, ValueQuery>;
204
205	/// Number of active child bounties per parent bounty.
206	/// Map of parent bounty index to number of child bounties.
207	#[pallet::storage]
208	pub type ParentChildBounties<T: Config> =
209		StorageMap<_, Twox64Concat, BountyIndex, u32, ValueQuery>;
210
211	/// Number of total child bounties per parent bounty, including completed bounties.
212	#[pallet::storage]
213	pub type ParentTotalChildBounties<T: Config> =
214		StorageMap<_, Twox64Concat, BountyIndex, u32, ValueQuery>;
215
216	/// Child bounties that have been added.
217	#[pallet::storage]
218	pub type ChildBounties<T: Config> = StorageDoubleMap<
219		_,
220		Twox64Concat,
221		BountyIndex,
222		Twox64Concat,
223		BountyIndex,
224		ChildBounty<T::AccountId, BalanceOf<T>, BlockNumberFor<T>>,
225	>;
226
227	/// The description of each child-bounty. Indexed by `(parent_id, child_id)`.
228	///
229	/// This item replaces the `ChildBountyDescriptions` storage item from the V0 storage version.
230	#[pallet::storage]
231	pub type ChildBountyDescriptionsV1<T: Config> = StorageDoubleMap<
232		_,
233		Twox64Concat,
234		BountyIndex,
235		Twox64Concat,
236		BountyIndex,
237		BoundedVec<u8, T::MaximumReasonLength>,
238	>;
239
240	/// The mapping of the child bounty ids from storage version `V0` to the new `V1` version.
241	///
242	/// The `V0` ids based on total child bounty count [`ChildBountyCount`]`. The `V1` version ids
243	/// based on the child bounty count per parent bounty [`ParentTotalChildBounties`].
244	/// The item intended solely for client convenience and not used in the pallet's core logic.
245	#[pallet::storage]
246	pub type V0ToV1ChildBountyIds<T: Config> =
247		StorageMap<_, Twox64Concat, BountyIndex, (BountyIndex, BountyIndex)>;
248
249	/// The cumulative child-bounty curator fee for each parent bounty.
250	#[pallet::storage]
251	pub type ChildrenCuratorFees<T: Config> =
252		StorageMap<_, Twox64Concat, BountyIndex, BalanceOf<T>, ValueQuery>;
253
254	#[pallet::call]
255	impl<T: Config> Pallet<T> {
256		/// Add a new child-bounty.
257		///
258		/// The dispatch origin for this call must be the curator of parent
259		/// bounty and the parent bounty must be in "active" state.
260		///
261		/// Child-bounty gets added successfully & fund gets transferred from
262		/// parent bounty to child-bounty account, if parent bounty has enough
263		/// funds, else the call fails.
264		///
265		/// Upper bound to maximum number of active  child bounties that can be
266		/// added are managed via runtime trait config
267		/// [`Config::MaxActiveChildBountyCount`].
268		///
269		/// If the call is success, the status of child-bounty is updated to
270		/// "Added".
271		///
272		/// - `parent_bounty_id`: Index of parent bounty for which child-bounty is being added.
273		/// - `value`: Value for executing the proposal.
274		/// - `description`: Text description for the child-bounty.
275		#[pallet::call_index(0)]
276		#[pallet::weight(<T as Config>::WeightInfo::add_child_bounty(description.len() as u32))]
277		pub fn add_child_bounty(
278			origin: OriginFor<T>,
279			#[pallet::compact] parent_bounty_id: BountyIndex,
280			#[pallet::compact] value: BalanceOf<T>,
281			description: Vec<u8>,
282		) -> DispatchResult {
283			let signer = ensure_signed(origin)?;
284
285			// Verify the arguments.
286			let bounded_description =
287				description.try_into().map_err(|_| BountiesError::<T>::ReasonTooBig)?;
288			ensure!(value >= T::ChildBountyValueMinimum::get(), BountiesError::<T>::InvalidValue);
289			ensure!(
290				ParentChildBounties::<T>::get(parent_bounty_id) <=
291					T::MaxActiveChildBountyCount::get() as u32,
292				Error::<T>::TooManyChildBounties,
293			);
294
295			let (curator, _) = Self::ensure_bounty_active(parent_bounty_id)?;
296			ensure!(signer == curator, BountiesError::<T>::RequireCurator);
297
298			// Read parent bounty account info.
299			let parent_bounty_account =
300				pallet_bounties::Pallet::<T>::bounty_account_id(parent_bounty_id);
301
302			// Ensure parent bounty has enough balance after adding child-bounty.
303			let bounty_balance = T::Currency::free_balance(&parent_bounty_account);
304			let new_bounty_balance = bounty_balance
305				.checked_sub(&value)
306				.ok_or(Error::<T>::InsufficientBountyBalance)?;
307			T::Currency::ensure_can_withdraw(
308				&parent_bounty_account,
309				value,
310				WithdrawReasons::TRANSFER,
311				new_bounty_balance,
312			)?;
313
314			// Get child-bounty ID.
315			let child_bounty_id = ParentTotalChildBounties::<T>::get(parent_bounty_id);
316			let child_bounty_account =
317				Self::child_bounty_account_id(parent_bounty_id, child_bounty_id);
318
319			// Transfer funds from parent bounty to child-bounty.
320			T::Currency::transfer(&parent_bounty_account, &child_bounty_account, value, KeepAlive)?;
321
322			// Increment the active child-bounty count.
323			ParentChildBounties::<T>::mutate(parent_bounty_id, |count| count.saturating_inc());
324			ParentTotalChildBounties::<T>::insert(
325				parent_bounty_id,
326				child_bounty_id.saturating_add(1),
327			);
328
329			// Create child-bounty instance.
330			Self::create_child_bounty(
331				parent_bounty_id,
332				child_bounty_id,
333				value,
334				bounded_description,
335			);
336			Ok(())
337		}
338
339		/// Propose curator for funded child-bounty.
340		///
341		/// The dispatch origin for this call must be curator of parent bounty.
342		///
343		/// Parent bounty must be in active state, for this child-bounty call to
344		/// work.
345		///
346		/// Child-bounty must be in "Added" state, for processing the call. And
347		/// state of child-bounty is moved to "CuratorProposed" on successful
348		/// call completion.
349		///
350		/// - `parent_bounty_id`: Index of parent bounty.
351		/// - `child_bounty_id`: Index of child bounty.
352		/// - `curator`: Address of child-bounty curator.
353		/// - `fee`: payment fee to child-bounty curator for execution.
354		#[pallet::call_index(1)]
355		#[pallet::weight(<T as Config>::WeightInfo::propose_curator())]
356		pub fn propose_curator(
357			origin: OriginFor<T>,
358			#[pallet::compact] parent_bounty_id: BountyIndex,
359			#[pallet::compact] child_bounty_id: BountyIndex,
360			curator: AccountIdLookupOf<T>,
361			#[pallet::compact] fee: BalanceOf<T>,
362		) -> DispatchResult {
363			let signer = ensure_signed(origin)?;
364			let child_bounty_curator = T::Lookup::lookup(curator)?;
365
366			let (curator, _) = Self::ensure_bounty_active(parent_bounty_id)?;
367			ensure!(signer == curator, BountiesError::<T>::RequireCurator);
368
369			// Mutate the child-bounty instance.
370			ChildBounties::<T>::try_mutate_exists(
371				parent_bounty_id,
372				child_bounty_id,
373				|maybe_child_bounty| -> DispatchResult {
374					let child_bounty =
375						maybe_child_bounty.as_mut().ok_or(BountiesError::<T>::InvalidIndex)?;
376
377					// Ensure child-bounty is in expected state.
378					ensure!(
379						child_bounty.status == ChildBountyStatus::Added,
380						BountiesError::<T>::UnexpectedStatus,
381					);
382
383					// Ensure child-bounty curator fee is less than child-bounty value.
384					ensure!(fee < child_bounty.value, BountiesError::<T>::InvalidFee);
385
386					// Add child-bounty curator fee to the cumulative sum. To be
387					// subtracted from the parent bounty curator when claiming
388					// bounty.
389					ChildrenCuratorFees::<T>::mutate(parent_bounty_id, |value| {
390						*value = value.saturating_add(fee)
391					});
392
393					// Update the child-bounty curator fee.
394					child_bounty.fee = fee;
395
396					// Update the child-bounty state.
397					child_bounty.status =
398						ChildBountyStatus::CuratorProposed { curator: child_bounty_curator };
399
400					Ok(())
401				},
402			)
403		}
404
405		/// Accept the curator role for the child-bounty.
406		///
407		/// The dispatch origin for this call must be the curator of this
408		/// child-bounty.
409		///
410		/// A deposit will be reserved from the curator and refund upon
411		/// successful payout or cancellation.
412		///
413		/// Fee for curator is deducted from curator fee of parent bounty.
414		///
415		/// Parent bounty must be in active state, for this child-bounty call to
416		/// work.
417		///
418		/// Child-bounty must be in "CuratorProposed" state, for processing the
419		/// call. And state of child-bounty is moved to "Active" on successful
420		/// call completion.
421		///
422		/// - `parent_bounty_id`: Index of parent bounty.
423		/// - `child_bounty_id`: Index of child bounty.
424		#[pallet::call_index(2)]
425		#[pallet::weight(<T as Config>::WeightInfo::accept_curator())]
426		pub fn accept_curator(
427			origin: OriginFor<T>,
428			#[pallet::compact] parent_bounty_id: BountyIndex,
429			#[pallet::compact] child_bounty_id: BountyIndex,
430		) -> DispatchResult {
431			let signer = ensure_signed(origin)?;
432
433			let (parent_curator, _) = Self::ensure_bounty_active(parent_bounty_id)?;
434			// Mutate child-bounty.
435			ChildBounties::<T>::try_mutate_exists(
436				parent_bounty_id,
437				child_bounty_id,
438				|maybe_child_bounty| -> DispatchResult {
439					let child_bounty =
440						maybe_child_bounty.as_mut().ok_or(BountiesError::<T>::InvalidIndex)?;
441
442					// Ensure child-bounty is in expected state.
443					if let ChildBountyStatus::CuratorProposed { ref curator } = child_bounty.status
444					{
445						ensure!(signer == *curator, BountiesError::<T>::RequireCurator);
446
447						// Reserve child-bounty curator deposit.
448						let deposit = Self::calculate_curator_deposit(
449							&parent_curator,
450							curator,
451							&child_bounty.fee,
452						);
453
454						T::Currency::reserve(curator, deposit)?;
455						child_bounty.curator_deposit = deposit;
456
457						child_bounty.status =
458							ChildBountyStatus::Active { curator: curator.clone() };
459						Ok(())
460					} else {
461						Err(BountiesError::<T>::UnexpectedStatus.into())
462					}
463				},
464			)
465		}
466
467		/// Unassign curator from a child-bounty.
468		///
469		/// The dispatch origin for this call can be either `RejectOrigin`, or
470		/// the curator of the parent bounty, or any signed origin.
471		///
472		/// For the origin other than T::RejectOrigin and the child-bounty
473		/// curator, parent bounty must be in active state, for this call to
474		/// work. We allow child-bounty curator and T::RejectOrigin to execute
475		/// this call irrespective of the parent bounty state.
476		///
477		/// If this function is called by the `RejectOrigin` or the
478		/// parent bounty curator, we assume that the child-bounty curator is
479		/// malicious or inactive. As a result, child-bounty curator deposit is
480		/// slashed.
481		///
482		/// If the origin is the child-bounty curator, we take this as a sign
483		/// that they are unable to do their job, and are willingly giving up.
484		/// We could slash the deposit, but for now we allow them to unreserve
485		/// their deposit and exit without issue. (We may want to change this if
486		/// it is abused.)
487		///
488		/// Finally, the origin can be anyone iff the child-bounty curator is
489		/// "inactive". Expiry update due of parent bounty is used to estimate
490		/// inactive state of child-bounty curator.
491		///
492		/// This allows anyone in the community to call out that a child-bounty
493		/// curator is not doing their due diligence, and we should pick a new
494		/// one. In this case the child-bounty curator deposit is slashed.
495		///
496		/// State of child-bounty is moved to Added state on successful call
497		/// completion.
498		///
499		/// - `parent_bounty_id`: Index of parent bounty.
500		/// - `child_bounty_id`: Index of child bounty.
501		#[pallet::call_index(3)]
502		#[pallet::weight(<T as Config>::WeightInfo::unassign_curator())]
503		pub fn unassign_curator(
504			origin: OriginFor<T>,
505			#[pallet::compact] parent_bounty_id: BountyIndex,
506			#[pallet::compact] child_bounty_id: BountyIndex,
507		) -> DispatchResult {
508			let maybe_sender = ensure_signed(origin.clone())
509				.map(Some)
510				.or_else(|_| T::RejectOrigin::ensure_origin(origin).map(|_| None))?;
511
512			ChildBounties::<T>::try_mutate_exists(
513				parent_bounty_id,
514				child_bounty_id,
515				|maybe_child_bounty| -> DispatchResult {
516					let child_bounty =
517						maybe_child_bounty.as_mut().ok_or(BountiesError::<T>::InvalidIndex)?;
518
519					let slash_curator =
520						|curator: &T::AccountId, curator_deposit: &mut BalanceOf<T>| {
521							let imbalance =
522								T::Currency::slash_reserved(curator, *curator_deposit).0;
523							T::OnSlash::on_unbalanced(imbalance);
524							*curator_deposit = Zero::zero();
525						};
526
527					match child_bounty.status {
528						ChildBountyStatus::Added => {
529							// No curator to unassign at this point.
530							return Err(BountiesError::<T>::UnexpectedStatus.into())
531						},
532						ChildBountyStatus::CuratorProposed { ref curator } => {
533							// A child-bounty curator has been proposed, but not accepted yet.
534							// Either `RejectOrigin`, parent bounty curator or the proposed
535							// child-bounty curator can unassign the child-bounty curator.
536							ensure!(
537								maybe_sender.map_or(true, |sender| {
538									sender == *curator ||
539										Self::ensure_bounty_active(parent_bounty_id)
540											.map_or(false, |(parent_curator, _)| {
541												sender == parent_curator
542											})
543								}),
544								BadOrigin
545							);
546							// Continue to change bounty status below.
547						},
548						ChildBountyStatus::Active { ref curator } => {
549							// The child-bounty is active.
550							match maybe_sender {
551								// If the `RejectOrigin` is calling this function, slash the curator
552								// deposit.
553								None => {
554									slash_curator(curator, &mut child_bounty.curator_deposit);
555									// Continue to change child-bounty status below.
556								},
557								Some(sender) if sender == *curator => {
558									// This is the child-bounty curator, willingly giving up their
559									// role. Give back their deposit.
560									T::Currency::unreserve(curator, child_bounty.curator_deposit);
561									// Reset curator deposit.
562									child_bounty.curator_deposit = Zero::zero();
563									// Continue to change bounty status below.
564								},
565								Some(sender) => {
566									let (parent_curator, update_due) =
567										Self::ensure_bounty_active(parent_bounty_id)?;
568									if sender == parent_curator ||
569										update_due < Self::treasury_block_number()
570									{
571										// Slash the child-bounty curator if
572										// + the call is made by the parent bounty curator.
573										// + or the curator is inactive.
574										slash_curator(curator, &mut child_bounty.curator_deposit);
575									// Continue to change bounty status below.
576									} else {
577										// Curator has more time to give an update.
578										return Err(BountiesError::<T>::Premature.into())
579									}
580								},
581							}
582						},
583						ChildBountyStatus::PendingPayout { ref curator, .. } => {
584							let (parent_curator, _) = Self::ensure_bounty_active(parent_bounty_id)?;
585							ensure!(
586								maybe_sender.map_or(true, |sender| parent_curator == sender),
587								BadOrigin,
588							);
589							slash_curator(curator, &mut child_bounty.curator_deposit);
590							// Continue to change child-bounty status below.
591						},
592					};
593					// Move the child-bounty state to Added.
594					child_bounty.status = ChildBountyStatus::Added;
595					Ok(())
596				},
597			)
598		}
599
600		/// Award child-bounty to a beneficiary.
601		///
602		/// The beneficiary will be able to claim the funds after a delay.
603		///
604		/// The dispatch origin for this call must be the parent curator or
605		/// curator of this child-bounty.
606		///
607		/// Parent bounty must be in active state, for this child-bounty call to
608		/// work.
609		///
610		/// Child-bounty must be in active state, for processing the call. And
611		/// state of child-bounty is moved to "PendingPayout" on successful call
612		/// completion.
613		///
614		/// - `parent_bounty_id`: Index of parent bounty.
615		/// - `child_bounty_id`: Index of child bounty.
616		/// - `beneficiary`: Beneficiary account.
617		#[pallet::call_index(4)]
618		#[pallet::weight(<T as Config>::WeightInfo::award_child_bounty())]
619		pub fn award_child_bounty(
620			origin: OriginFor<T>,
621			#[pallet::compact] parent_bounty_id: BountyIndex,
622			#[pallet::compact] child_bounty_id: BountyIndex,
623			beneficiary: AccountIdLookupOf<T>,
624		) -> DispatchResult {
625			let signer = ensure_signed(origin)?;
626			let beneficiary = T::Lookup::lookup(beneficiary)?;
627
628			// Ensure parent bounty exists, and is active.
629			let (parent_curator, _) = Self::ensure_bounty_active(parent_bounty_id)?;
630
631			ChildBounties::<T>::try_mutate_exists(
632				parent_bounty_id,
633				child_bounty_id,
634				|maybe_child_bounty| -> DispatchResult {
635					let child_bounty =
636						maybe_child_bounty.as_mut().ok_or(BountiesError::<T>::InvalidIndex)?;
637
638					// Ensure child-bounty is in active state.
639					if let ChildBountyStatus::Active { ref curator } = child_bounty.status {
640						ensure!(
641							signer == *curator || signer == parent_curator,
642							BountiesError::<T>::RequireCurator,
643						);
644						// Move the child-bounty state to pending payout.
645						child_bounty.status = ChildBountyStatus::PendingPayout {
646							curator: signer,
647							beneficiary: beneficiary.clone(),
648							unlock_at: Self::treasury_block_number() +
649								T::BountyDepositPayoutDelay::get(),
650						};
651						Ok(())
652					} else {
653						Err(BountiesError::<T>::UnexpectedStatus.into())
654					}
655				},
656			)?;
657
658			// Trigger the event Awarded.
659			Self::deposit_event(Event::<T>::Awarded {
660				index: parent_bounty_id,
661				child_index: child_bounty_id,
662				beneficiary,
663			});
664
665			Ok(())
666		}
667
668		/// Claim the payout from an awarded child-bounty after payout delay.
669		///
670		/// The dispatch origin for this call may be any signed origin.
671		///
672		/// Call works independent of parent bounty state, No need for parent
673		/// bounty to be in active state.
674		///
675		/// The Beneficiary is paid out with agreed bounty value. Curator fee is
676		/// paid & curator deposit is unreserved.
677		///
678		/// Child-bounty must be in "PendingPayout" state, for processing the
679		/// call. And instance of child-bounty is removed from the state on
680		/// successful call completion.
681		///
682		/// - `parent_bounty_id`: Index of parent bounty.
683		/// - `child_bounty_id`: Index of child bounty.
684		#[pallet::call_index(5)]
685		#[pallet::weight(<T as Config>::WeightInfo::claim_child_bounty())]
686		pub fn claim_child_bounty(
687			origin: OriginFor<T>,
688			#[pallet::compact] parent_bounty_id: BountyIndex,
689			#[pallet::compact] child_bounty_id: BountyIndex,
690		) -> DispatchResult {
691			ensure_signed(origin)?;
692
693			// Ensure child-bounty is in expected state.
694			ChildBounties::<T>::try_mutate_exists(
695				parent_bounty_id,
696				child_bounty_id,
697				|maybe_child_bounty| -> DispatchResult {
698					let child_bounty =
699						maybe_child_bounty.as_mut().ok_or(BountiesError::<T>::InvalidIndex)?;
700
701					if let ChildBountyStatus::PendingPayout {
702						ref curator,
703						ref beneficiary,
704						ref unlock_at,
705					} = child_bounty.status
706					{
707						// Ensure block number is elapsed for processing the
708						// claim.
709						ensure!(
710							Self::treasury_block_number() >= *unlock_at,
711							BountiesError::<T>::Premature,
712						);
713
714						// Make curator fee payment.
715						let child_bounty_account =
716							Self::child_bounty_account_id(parent_bounty_id, child_bounty_id);
717						let balance = T::Currency::free_balance(&child_bounty_account);
718						let curator_fee = child_bounty.fee.min(balance);
719						let payout = balance.saturating_sub(curator_fee);
720
721						// Unreserve the curator deposit. Should not fail
722						// because the deposit is always reserved when curator is
723						// assigned.
724						let _ = T::Currency::unreserve(curator, child_bounty.curator_deposit);
725
726						// Make payout to child-bounty curator.
727						// Should not fail because curator fee is always less than bounty value.
728						let fee_transfer_result = T::Currency::transfer(
729							&child_bounty_account,
730							curator,
731							curator_fee,
732							AllowDeath,
733						);
734						debug_assert!(fee_transfer_result.is_ok());
735
736						// Make payout to beneficiary.
737						// Should not fail.
738						let payout_transfer_result = T::Currency::transfer(
739							&child_bounty_account,
740							beneficiary,
741							payout,
742							AllowDeath,
743						);
744						debug_assert!(payout_transfer_result.is_ok());
745
746						// Trigger the Claimed event.
747						Self::deposit_event(Event::<T>::Claimed {
748							index: parent_bounty_id,
749							child_index: child_bounty_id,
750							payout,
751							beneficiary: beneficiary.clone(),
752						});
753
754						// Update the active child-bounty tracking count.
755						ParentChildBounties::<T>::mutate(parent_bounty_id, |count| {
756							count.saturating_dec()
757						});
758
759						// Remove the child-bounty description.
760						ChildBountyDescriptionsV1::<T>::remove(parent_bounty_id, child_bounty_id);
761
762						// Remove the child-bounty instance from the state.
763						*maybe_child_bounty = None;
764
765						Ok(())
766					} else {
767						Err(BountiesError::<T>::UnexpectedStatus.into())
768					}
769				},
770			)
771		}
772
773		/// Cancel a proposed or active child-bounty. Child-bounty account funds
774		/// are transferred to parent bounty account. The child-bounty curator
775		/// deposit may be unreserved if possible.
776		///
777		/// The dispatch origin for this call must be either parent curator or
778		/// `T::RejectOrigin`.
779		///
780		/// If the state of child-bounty is `Active`, curator deposit is
781		/// unreserved.
782		///
783		/// If the state of child-bounty is `PendingPayout`, call fails &
784		/// returns `PendingPayout` error.
785		///
786		/// For the origin other than T::RejectOrigin, parent bounty must be in
787		/// active state, for this child-bounty call to work. For origin
788		/// T::RejectOrigin execution is forced.
789		///
790		/// Instance of child-bounty is removed from the state on successful
791		/// call completion.
792		///
793		/// - `parent_bounty_id`: Index of parent bounty.
794		/// - `child_bounty_id`: Index of child bounty.
795		#[pallet::call_index(6)]
796		#[pallet::weight(<T as Config>::WeightInfo::close_child_bounty_added()
797			.max(<T as Config>::WeightInfo::close_child_bounty_active()))]
798		pub fn close_child_bounty(
799			origin: OriginFor<T>,
800			#[pallet::compact] parent_bounty_id: BountyIndex,
801			#[pallet::compact] child_bounty_id: BountyIndex,
802		) -> DispatchResult {
803			let maybe_sender = ensure_signed(origin.clone())
804				.map(Some)
805				.or_else(|_| T::RejectOrigin::ensure_origin(origin).map(|_| None))?;
806
807			// Ensure parent bounty exist, get parent curator.
808			let (parent_curator, _) = Self::ensure_bounty_active(parent_bounty_id)?;
809
810			ensure!(maybe_sender.map_or(true, |sender| parent_curator == sender), BadOrigin);
811
812			Self::impl_close_child_bounty(parent_bounty_id, child_bounty_id)?;
813			Ok(())
814		}
815	}
816
817	#[pallet::hooks]
818	impl<T: Config> Hooks<SystemBlockNumberFor<T>> for Pallet<T> {
819		fn integrity_test() {
820			let parent_bounty_id: BountyIndex = 1;
821			let child_bounty_id: BountyIndex = 2;
822			let _: T::AccountId = T::PalletId::get()
823				.try_into_sub_account(("cb", parent_bounty_id, child_bounty_id))
824				.expect(
825					"The `AccountId` type must be large enough to fit the child bounty account ID.",
826				);
827		}
828	}
829}
830
831impl<T: Config> Pallet<T> {
832	/// Get the block number used in the treasury pallet.
833	///
834	/// It may be configured to use the relay chain block number on a parachain.
835	pub fn treasury_block_number() -> BlockNumberFor<T> {
836		<T as pallet_treasury::Config>::BlockNumberProvider::current_block_number()
837	}
838
839	// This function will calculate the deposit of a curator.
840	fn calculate_curator_deposit(
841		parent_curator: &T::AccountId,
842		child_curator: &T::AccountId,
843		bounty_fee: &BalanceOf<T>,
844	) -> BalanceOf<T> {
845		if parent_curator == child_curator {
846			return Zero::zero()
847		}
848
849		// We just use the same logic from the parent bounties pallet.
850		pallet_bounties::Pallet::<T>::calculate_curator_deposit(bounty_fee)
851	}
852
853	/// The account ID of a child-bounty account.
854	pub fn child_bounty_account_id(
855		parent_bounty_id: BountyIndex,
856		child_bounty_id: BountyIndex,
857	) -> T::AccountId {
858		// This function is taken from the parent (bounties) pallet, but the
859		// prefix is changed to have different AccountId when the index of
860		// parent and child is same.
861		T::PalletId::get().into_sub_account_truncating(("cb", parent_bounty_id, child_bounty_id))
862	}
863
864	fn create_child_bounty(
865		parent_bounty_id: BountyIndex,
866		child_bounty_id: BountyIndex,
867		child_bounty_value: BalanceOf<T>,
868		description: BoundedVec<u8, T::MaximumReasonLength>,
869	) {
870		let child_bounty = ChildBounty {
871			parent_bounty: parent_bounty_id,
872			value: child_bounty_value,
873			fee: 0u32.into(),
874			curator_deposit: 0u32.into(),
875			status: ChildBountyStatus::Added,
876		};
877		ChildBounties::<T>::insert(parent_bounty_id, child_bounty_id, &child_bounty);
878		ChildBountyDescriptionsV1::<T>::insert(parent_bounty_id, child_bounty_id, description);
879		Self::deposit_event(Event::Added { index: parent_bounty_id, child_index: child_bounty_id });
880	}
881
882	fn ensure_bounty_active(
883		bounty_id: BountyIndex,
884	) -> Result<(T::AccountId, BlockNumberFor<T>), DispatchError> {
885		let parent_bounty = pallet_bounties::Bounties::<T>::get(bounty_id)
886			.ok_or(BountiesError::<T>::InvalidIndex)?;
887		if let BountyStatus::Active { curator, update_due } = parent_bounty.get_status() {
888			Ok((curator, update_due))
889		} else {
890			Err(Error::<T>::ParentBountyNotActive.into())
891		}
892	}
893
894	fn impl_close_child_bounty(
895		parent_bounty_id: BountyIndex,
896		child_bounty_id: BountyIndex,
897	) -> DispatchResult {
898		ChildBounties::<T>::try_mutate_exists(
899			parent_bounty_id,
900			child_bounty_id,
901			|maybe_child_bounty| -> DispatchResult {
902				let child_bounty =
903					maybe_child_bounty.as_mut().ok_or(BountiesError::<T>::InvalidIndex)?;
904
905				match &child_bounty.status {
906					ChildBountyStatus::Added | ChildBountyStatus::CuratorProposed { .. } => {
907						// Nothing extra to do besides the removal of the child-bounty below.
908					},
909					ChildBountyStatus::Active { curator } => {
910						// Cancelled by parent curator or RejectOrigin,
911						// refund deposit of the working child-bounty curator.
912						let _ = T::Currency::unreserve(curator, child_bounty.curator_deposit);
913						// Then execute removal of the child-bounty below.
914					},
915					ChildBountyStatus::PendingPayout { .. } => {
916						// Child-bounty is already in pending payout. If parent
917						// curator or RejectOrigin wants to close this
918						// child-bounty, it should mean the child-bounty curator
919						// was acting maliciously. So first unassign the
920						// child-bounty curator, slashing their deposit.
921						return Err(BountiesError::<T>::PendingPayout.into())
922					},
923				}
924
925				// Revert the curator fee back to parent bounty curator &
926				// reduce the active child-bounty count.
927				ChildrenCuratorFees::<T>::mutate(parent_bounty_id, |value| {
928					*value = value.saturating_sub(child_bounty.fee)
929				});
930				ParentChildBounties::<T>::mutate(parent_bounty_id, |count| {
931					*count = count.saturating_sub(1)
932				});
933
934				// Transfer fund from child-bounty to parent bounty.
935				let parent_bounty_account =
936					pallet_bounties::Pallet::<T>::bounty_account_id(parent_bounty_id);
937				let child_bounty_account =
938					Self::child_bounty_account_id(parent_bounty_id, child_bounty_id);
939				let balance = T::Currency::free_balance(&child_bounty_account);
940				let transfer_result = T::Currency::transfer(
941					&child_bounty_account,
942					&parent_bounty_account,
943					balance,
944					AllowDeath,
945				); // Should not fail; child bounty account gets this balance during creation.
946				debug_assert!(transfer_result.is_ok());
947
948				// Remove the child-bounty description.
949				ChildBountyDescriptionsV1::<T>::remove(parent_bounty_id, child_bounty_id);
950
951				*maybe_child_bounty = None;
952
953				Self::deposit_event(Event::<T>::Canceled {
954					index: parent_bounty_id,
955					child_index: child_bounty_id,
956				});
957				Ok(())
958			},
959		)
960	}
961}
962
963/// Implement ChildBountyManager to connect with the bounties pallet. This is
964/// where we pass the active child bounties and child curator fees to the parent
965/// bounty.
966///
967/// Function `children_curator_fees` not only returns the fee but also removes cumulative curator
968/// fees during call.
969impl<T: Config> pallet_bounties::ChildBountyManager<BalanceOf<T>> for Pallet<T> {
970	/// Returns number of active child bounties for `bounty_id`
971	fn child_bounties_count(
972		bounty_id: pallet_bounties::BountyIndex,
973	) -> pallet_bounties::BountyIndex {
974		ParentChildBounties::<T>::get(bounty_id)
975	}
976
977	/// Returns cumulative child bounty curator fees for `bounty_id` also removing the associated
978	/// storage item. This function is assumed to be called when parent bounty is claimed.
979	fn children_curator_fees(bounty_id: pallet_bounties::BountyIndex) -> BalanceOf<T> {
980		// This is asked for when the parent bounty is being claimed. No use of
981		// keeping it in state after that. Hence removing.
982		let children_fee_total = ChildrenCuratorFees::<T>::get(bounty_id);
983		ChildrenCuratorFees::<T>::remove(bounty_id);
984		children_fee_total
985	}
986
987	/// Clean up the storage on a parent bounty removal.
988	fn bounty_removed(bounty_id: BountyIndex) {
989		debug_assert!(ParentChildBounties::<T>::get(bounty_id).is_zero());
990		debug_assert!(ChildrenCuratorFees::<T>::get(bounty_id).is_zero());
991		debug_assert!(ChildBounties::<T>::iter_key_prefix(bounty_id).count().is_zero());
992		debug_assert!(ChildBountyDescriptionsV1::<T>::iter_key_prefix(bounty_id).count().is_zero());
993		ParentChildBounties::<T>::remove(bounty_id);
994		ParentTotalChildBounties::<T>::remove(bounty_id);
995	}
996}