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
//! > Made with *Substrate*, for *Polkadot*.
19
//!
20
//! [![github]](https://github.com/paritytech/substrate/frame/fast-unstake) -
21
//! [![polkadot]](https://polkadot.network)
22
//!
23
//! [polkadot]: https://img.shields.io/badge/polkadot-E6007A?style=for-the-badge&logo=polkadot&logoColor=white
24
//! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
25
//!
26
//! # Treasury Pallet
27
//!
28
//! The Treasury pallet provides a "pot" of funds that can be managed by stakeholders in the system
29
//! and a structure for making spending proposals from this pot.
30
//!
31
//! ## Overview
32
//!
33
//! The Treasury Pallet itself provides the pot to store funds, and a means for stakeholders to
34
//! propose and claim expenditures (aka spends). The chain will need to provide a method to approve
35
//! spends (e.g. public referendum) and a method for collecting funds (e.g. inflation, fees).
36
//!
37
//! By way of example, stakeholders could vote to fund the Treasury with a portion of the block
38
//! reward and use the funds to pay developers.
39
//!
40
//! ### Terminology
41
//!
42
//! - **Proposal:** A suggestion to allocate funds from the pot to a beneficiary.
43
//! - **Beneficiary:** An account who will receive the funds from a proposal iff the proposal is
44
//!   approved.
45
//! - **Pot:** Unspent funds accumulated by the treasury pallet.
46
//! - **Spend** An approved proposal for transferring a specific amount of funds to a designated
47
//!   beneficiary.
48
//!
49
//! ### Example
50
//!
51
//! 1. Multiple local spends approved by spend origins and received by a beneficiary.
52
#![doc = docify::embed!("src/tests.rs", spend_local_origin_works)]
53
//!
54
//! 2. Approve a spend of some asset kind and claim it.
55
#![doc = docify::embed!("src/tests.rs", spend_payout_works)]
56
//!
57
//! ## Pallet API
58
//!
59
//! See the [`pallet`] module for more information about the interfaces this pallet exposes,
60
//! including its configuration trait, dispatchables, storage items, events and errors.
61
//!
62
//! ## Low Level / Implementation Details
63
//!
64
//! Spends can be initiated using either the `spend_local` or `spend` dispatchable. The
65
//! `spend_local` dispatchable enables the creation of spends using the native currency of the
66
//! chain, utilizing the funds stored in the pot. These spends are automatically paid out every
67
//! [`pallet::Config::SpendPeriod`]. On the other hand, the `spend` dispatchable allows spending of
68
//! any asset kind managed by the treasury, with payment facilitated by a designated
69
//! [`pallet::Config::Paymaster`]. To claim these spends, the `payout` dispatchable should be called
70
//! within some temporal bounds, starting from the moment they become valid and within one
71
//! [`pallet::Config::PayoutPeriod`].
72

            
73
#![cfg_attr(not(feature = "std"), no_std)]
74

            
75
mod benchmarking;
76
#[cfg(test)]
77
mod tests;
78
pub mod weights;
79
use core::marker::PhantomData;
80

            
81
#[cfg(feature = "runtime-benchmarks")]
82
pub use benchmarking::ArgumentsFactory;
83

            
84
extern crate alloc;
85

            
86
use codec::{Decode, Encode, MaxEncodedLen};
87
use scale_info::TypeInfo;
88

            
89
use alloc::{boxed::Box, collections::btree_map::BTreeMap};
90
use sp_runtime::{
91
	traits::{AccountIdConversion, CheckedAdd, Saturating, StaticLookup, Zero},
92
	Permill, RuntimeDebug,
93
};
94

            
95
use frame_support::{
96
	dispatch::{DispatchResult, DispatchResultWithPostInfo},
97
	ensure, print,
98
	traits::{
99
		tokens::Pay, Currency, ExistenceRequirement::KeepAlive, Get, Imbalance, OnUnbalanced,
100
		ReservableCurrency, WithdrawReasons,
101
	},
102
	weights::Weight,
103
	PalletId,
104
};
105

            
106
pub use pallet::*;
107
pub use weights::WeightInfo;
108

            
109
pub type BalanceOf<T, I = ()> =
110
	<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
111
pub type AssetBalanceOf<T, I> = <<T as Config<I>>::Paymaster as Pay>::Balance;
112
pub type PositiveImbalanceOf<T, I = ()> = <<T as Config<I>>::Currency as Currency<
113
	<T as frame_system::Config>::AccountId,
114
>>::PositiveImbalance;
115
pub type NegativeImbalanceOf<T, I = ()> = <<T as Config<I>>::Currency as Currency<
116
	<T as frame_system::Config>::AccountId,
117
>>::NegativeImbalance;
118
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
119
type BeneficiaryLookupOf<T, I> = <<T as Config<I>>::BeneficiaryLookup as StaticLookup>::Source;
120

            
121
/// A trait to allow the Treasury Pallet to spend it's funds for other purposes.
122
/// There is an expectation that the implementer of this trait will correctly manage
123
/// the mutable variables passed to it:
124
/// * `budget_remaining`: How much available funds that can be spent by the treasury. As funds are
125
///   spent, you must correctly deduct from this value.
126
/// * `imbalance`: Any imbalances that you create should be subsumed in here to maximize efficiency
127
///   of updating the total issuance. (i.e. `deposit_creating`)
128
/// * `total_weight`: Track any weight that your `spend_fund` implementation uses by updating this
129
///   value.
130
/// * `missed_any`: If there were items that you want to spend on, but there were not enough funds,
131
///   mark this value as `true`. This will prevent the treasury from burning the excess funds.
132
#[impl_trait_for_tuples::impl_for_tuples(30)]
133
pub trait SpendFunds<T: Config<I>, I: 'static = ()> {
134
	fn spend_funds(
135
		budget_remaining: &mut BalanceOf<T, I>,
136
		imbalance: &mut PositiveImbalanceOf<T, I>,
137
		total_weight: &mut Weight,
138
		missed_any: &mut bool,
139
	);
140
}
141

            
142
/// An index of a proposal. Just a `u32`.
143
pub type ProposalIndex = u32;
144

            
145
/// A spending proposal.
146
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
147
#[derive(Encode, Decode, Clone, PartialEq, Eq, MaxEncodedLen, RuntimeDebug, TypeInfo)]
148
pub struct Proposal<AccountId, Balance> {
149
	/// The account proposing it.
150
	proposer: AccountId,
151
	/// The (total) amount that should be paid if the proposal is accepted.
152
	value: Balance,
153
	/// The account to whom the payment should be made if the proposal is accepted.
154
	beneficiary: AccountId,
155
	/// The amount held on deposit (reserved) for making this proposal.
156
	bond: Balance,
157
}
158

            
159
/// The state of the payment claim.
160
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
161
#[derive(Encode, Decode, Clone, PartialEq, Eq, MaxEncodedLen, RuntimeDebug, TypeInfo)]
162
pub enum PaymentState<Id> {
163
	/// Pending claim.
164
	Pending,
165
	/// Payment attempted with a payment identifier.
166
	Attempted { id: Id },
167
	/// Payment failed.
168
	Failed,
169
}
170

            
171
/// Info regarding an approved treasury spend.
172
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
173
#[derive(Encode, Decode, Clone, PartialEq, Eq, MaxEncodedLen, RuntimeDebug, TypeInfo)]
174
pub struct SpendStatus<AssetKind, AssetBalance, Beneficiary, BlockNumber, PaymentId> {
175
	// The kind of asset to be spent.
176
	asset_kind: AssetKind,
177
	/// The asset amount of the spend.
178
	amount: AssetBalance,
179
	/// The beneficiary of the spend.
180
	beneficiary: Beneficiary,
181
	/// The block number from which the spend can be claimed.
182
	valid_from: BlockNumber,
183
	/// The block number by which the spend has to be claimed.
184
	expire_at: BlockNumber,
185
	/// The status of the payout/claim.
186
	status: PaymentState<PaymentId>,
187
}
188

            
189
/// Index of an approved treasury spend.
190
pub type SpendIndex = u32;
191

            
192
559
#[frame_support::pallet]
193
pub mod pallet {
194
	use super::*;
195
	use frame_support::{
196
		dispatch_context::with_context,
197
		pallet_prelude::*,
198
		traits::tokens::{ConversionFromAssetBalance, PaymentStatus},
199
	};
200
	use frame_system::pallet_prelude::*;
201

            
202
1698
	#[pallet::pallet]
203
	pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
204

            
205
	#[pallet::config]
206
	pub trait Config<I: 'static = ()>: frame_system::Config {
207
		/// The staking balance.
208
		type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
209

            
210
		/// Origin from which rejections must come.
211
		type RejectOrigin: EnsureOrigin<Self::RuntimeOrigin>;
212

            
213
		/// The overarching event type.
214
		type RuntimeEvent: From<Event<Self, I>>
215
			+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
216

            
217
		/// Period between successive spends.
218
		#[pallet::constant]
219
		type SpendPeriod: Get<BlockNumberFor<Self>>;
220

            
221
		/// Percentage of spare funds (if any) that are burnt per spend period.
222
		#[pallet::constant]
223
		type Burn: Get<Permill>;
224

            
225
		/// The treasury's pallet id, used for deriving its sovereign account ID.
226
		#[pallet::constant]
227
		type PalletId: Get<PalletId>;
228

            
229
		/// Handler for the unbalanced decrease when treasury funds are burned.
230
		type BurnDestination: OnUnbalanced<NegativeImbalanceOf<Self, I>>;
231

            
232
		/// Weight information for extrinsics in this pallet.
233
		type WeightInfo: WeightInfo;
234

            
235
		/// Runtime hooks to external pallet using treasury to compute spend funds.
236
		type SpendFunds: SpendFunds<Self, I>;
237

            
238
		/// The maximum number of approvals that can wait in the spending queue.
239
		///
240
		/// NOTE: This parameter is also used within the Bounties Pallet extension if enabled.
241
		#[pallet::constant]
242
		type MaxApprovals: Get<u32>;
243

            
244
		/// The origin required for approving spends from the treasury outside of the proposal
245
		/// process. The `Success` value is the maximum amount in a native asset that this origin
246
		/// is allowed to spend at a time.
247
		type SpendOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = BalanceOf<Self, I>>;
248

            
249
		/// Type parameter representing the asset kinds to be spent from the treasury.
250
		type AssetKind: Parameter + MaxEncodedLen;
251

            
252
		/// Type parameter used to identify the beneficiaries eligible to receive treasury spends.
253
		type Beneficiary: Parameter + MaxEncodedLen;
254

            
255
		/// Converting trait to take a source type and convert to [`Self::Beneficiary`].
256
		type BeneficiaryLookup: StaticLookup<Target = Self::Beneficiary>;
257

            
258
		/// Type for processing spends of [Self::AssetKind] in favor of [`Self::Beneficiary`].
259
		type Paymaster: Pay<Beneficiary = Self::Beneficiary, AssetKind = Self::AssetKind>;
260

            
261
		/// Type for converting the balance of an [Self::AssetKind] to the balance of the native
262
		/// asset, solely for the purpose of asserting the result against the maximum allowed spend
263
		/// amount of the [`Self::SpendOrigin`].
264
		type BalanceConverter: ConversionFromAssetBalance<
265
			<Self::Paymaster as Pay>::Balance,
266
			Self::AssetKind,
267
			BalanceOf<Self, I>,
268
		>;
269

            
270
		/// The period during which an approved treasury spend has to be claimed.
271
		#[pallet::constant]
272
		type PayoutPeriod: Get<BlockNumberFor<Self>>;
273

            
274
		/// Helper type for benchmarks.
275
		#[cfg(feature = "runtime-benchmarks")]
276
		type BenchmarkHelper: ArgumentsFactory<Self::AssetKind, Self::Beneficiary>;
277
	}
278

            
279
	/// Number of proposals that have been made.
280
107274
	#[pallet::storage]
281
	#[pallet::getter(fn proposal_count)]
282
	pub(crate) type ProposalCount<T, I = ()> = StorageValue<_, ProposalIndex, ValueQuery>;
283

            
284
	/// Proposals that have been made.
285
214548
	#[pallet::storage]
286
	#[pallet::getter(fn proposals)]
287
	pub type Proposals<T: Config<I>, I: 'static = ()> = StorageMap<
288
		_,
289
		Twox64Concat,
290
		ProposalIndex,
291
		Proposal<T::AccountId, BalanceOf<T, I>>,
292
		OptionQuery,
293
	>;
294

            
295
	/// The amount which has been reported as inactive to Currency.
296
390018
	#[pallet::storage]
297
	pub type Deactivated<T: Config<I>, I: 'static = ()> =
298
		StorageValue<_, BalanceOf<T, I>, ValueQuery>;
299

            
300
	/// Proposal indices that have been approved but not yet awarded.
301
107274
	#[pallet::storage]
302
	#[pallet::getter(fn approvals)]
303
	pub type Approvals<T: Config<I>, I: 'static = ()> =
304
		StorageValue<_, BoundedVec<ProposalIndex, T::MaxApprovals>, ValueQuery>;
305

            
306
	/// The count of spends that have been made.
307
107274
	#[pallet::storage]
308
	pub(crate) type SpendCount<T, I = ()> = StorageValue<_, SpendIndex, ValueQuery>;
309

            
310
	/// Spends that have been approved and being processed.
311
	// Hasher: Twox safe since `SpendIndex` is an internal count based index.
312
321882
	#[pallet::storage]
313
	pub type Spends<T: Config<I>, I: 'static = ()> = StorageMap<
314
		_,
315
		Twox64Concat,
316
		SpendIndex,
317
		SpendStatus<
318
			T::AssetKind,
319
			AssetBalanceOf<T, I>,
320
			T::Beneficiary,
321
			BlockNumberFor<T>,
322
			<T::Paymaster as Pay>::Id,
323
		>,
324
		OptionQuery,
325
	>;
326

            
327
	#[pallet::genesis_config]
328
	#[derive(frame_support::DefaultNoBound)]
329
	pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
330
		#[serde(skip)]
331
		_config: core::marker::PhantomData<(T, I)>,
332
	}
333

            
334
	#[pallet::genesis_build]
335
	impl<T: Config<I>, I: 'static> BuildGenesisConfig for GenesisConfig<T, I> {
336
3
		fn build(&self) {
337
3
			// Create Treasury account
338
3
			let account_id = <Pallet<T, I>>::account_id();
339
3
			let min = T::Currency::minimum_balance();
340
3
			if T::Currency::free_balance(&account_id) < min {
341
3
				let _ = T::Currency::make_free_balance_be(&account_id, min);
342
3
			}
343
3
		}
344
	}
345

            
346
	#[pallet::event]
347
246
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
348
	pub enum Event<T: Config<I>, I: 'static = ()> {
349
		/// We have ended a spend period and will now allocate funds.
350
		Spending { budget_remaining: BalanceOf<T, I> },
351
		/// Some funds have been allocated.
352
		Awarded { proposal_index: ProposalIndex, award: BalanceOf<T, I>, account: T::AccountId },
353
		/// Some of our funds have been burnt.
354
		Burnt { burnt_funds: BalanceOf<T, I> },
355
		/// Spending has finished; this is the amount that rolls over until next spend.
356
		Rollover { rollover_balance: BalanceOf<T, I> },
357
		/// Some funds have been deposited.
358
		Deposit { value: BalanceOf<T, I> },
359
		/// A new spend proposal has been approved.
360
		SpendApproved {
361
			proposal_index: ProposalIndex,
362
			amount: BalanceOf<T, I>,
363
			beneficiary: T::AccountId,
364
		},
365
		/// The inactive funds of the pallet have been updated.
366
		UpdatedInactive { reactivated: BalanceOf<T, I>, deactivated: BalanceOf<T, I> },
367
		/// A new asset spend proposal has been approved.
368
		AssetSpendApproved {
369
			index: SpendIndex,
370
			asset_kind: T::AssetKind,
371
			amount: AssetBalanceOf<T, I>,
372
			beneficiary: T::Beneficiary,
373
			valid_from: BlockNumberFor<T>,
374
			expire_at: BlockNumberFor<T>,
375
		},
376
		/// An approved spend was voided.
377
		AssetSpendVoided { index: SpendIndex },
378
		/// A payment happened.
379
		Paid { index: SpendIndex, payment_id: <T::Paymaster as Pay>::Id },
380
		/// A payment failed and can be retried.
381
		PaymentFailed { index: SpendIndex, payment_id: <T::Paymaster as Pay>::Id },
382
		/// A spend was processed and removed from the storage. It might have been successfully
383
		/// paid or it may have expired.
384
		SpendProcessed { index: SpendIndex },
385
	}
386

            
387
	/// Error for the treasury pallet.
388
120
	#[pallet::error]
389
	pub enum Error<T, I = ()> {
390
		/// No proposal, bounty or spend at that index.
391
		InvalidIndex,
392
		/// Too many approvals in the queue.
393
		TooManyApprovals,
394
		/// The spend origin is valid but the amount it is allowed to spend is lower than the
395
		/// amount to be spent.
396
		InsufficientPermission,
397
		/// Proposal has not been approved.
398
		ProposalNotApproved,
399
		/// The balance of the asset kind is not convertible to the balance of the native asset.
400
		FailedToConvertBalance,
401
		/// The spend has expired and cannot be claimed.
402
		SpendExpired,
403
		/// The spend is not yet eligible for payout.
404
		EarlyPayout,
405
		/// The payment has already been attempted.
406
		AlreadyAttempted,
407
		/// There was some issue with the mechanism of payment.
408
		PayoutError,
409
		/// The payout was not yet attempted/claimed.
410
		NotAttempted,
411
		/// The payment has neither failed nor succeeded yet.
412
		Inconclusive,
413
	}
414

            
415
554367
	#[pallet::hooks]
416
	impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
417
		/// ## Complexity
418
		/// - `O(A)` where `A` is the number of approvals
419
194763
		fn on_initialize(n: frame_system::pallet_prelude::BlockNumberFor<T>) -> Weight {
420
194763
			let pot = Self::pot();
421
194763
			let deactivated = Deactivated::<T, I>::get();
422
194763
			if pot != deactivated {
423
246
				T::Currency::reactivate(deactivated);
424
246
				T::Currency::deactivate(pot);
425
246
				Deactivated::<T, I>::put(&pot);
426
246
				Self::deposit_event(Event::<T, I>::UpdatedInactive {
427
246
					reactivated: deactivated,
428
246
					deactivated: pot,
429
246
				});
430
194517
			}
431

            
432
			// Check to see if we should spend some funds!
433
194763
			if (n % T::SpendPeriod::get()).is_zero() {
434
				Self::spend_funds()
435
			} else {
436
194763
				Weight::zero()
437
			}
438
194763
		}
439

            
440
		#[cfg(feature = "try-runtime")]
441
53637
		fn try_state(
442
53637
			_: frame_system::pallet_prelude::BlockNumberFor<T>,
443
53637
		) -> Result<(), sp_runtime::TryRuntimeError> {
444
53637
			Self::do_try_state()?;
445
53637
			Ok(())
446
53637
		}
447
	}
448

            
449
	#[derive(Default)]
450
	struct SpendContext<Balance> {
451
		spend_in_context: BTreeMap<Balance, Balance>,
452
	}
453

            
454
3912
	#[pallet::call]
455
	impl<T: Config<I>, I: 'static> Pallet<T, I> {
456
		/// Propose and approve a spend of treasury funds.
457
		///
458
		/// ## Dispatch Origin
459
		///
460
		/// Must be [`Config::SpendOrigin`] with the `Success` value being at least `amount`.
461
		///
462
		/// ### Details
463
		/// NOTE: For record-keeping purposes, the proposer is deemed to be equivalent to the
464
		/// beneficiary.
465
		///
466
		/// ### Parameters
467
		/// - `amount`: The amount to be transferred from the treasury to the `beneficiary`.
468
		/// - `beneficiary`: The destination account for the transfer.
469
		///
470
		/// ## Events
471
		///
472
		/// Emits [`Event::SpendApproved`] if successful.
473
		#[pallet::call_index(3)]
474
		#[pallet::weight(T::WeightInfo::spend_local())]
475
		pub fn spend_local(
476
			origin: OriginFor<T>,
477
			#[pallet::compact] amount: BalanceOf<T, I>,
478
			beneficiary: AccountIdLookupOf<T>,
479
144
		) -> DispatchResult {
480
144
			let max_amount = T::SpendOrigin::ensure_origin(origin)?;
481
			ensure!(amount <= max_amount, Error::<T, I>::InsufficientPermission);
482

            
483
			with_context::<SpendContext<BalanceOf<T, I>>, _>(|v| {
484
				let context = v.or_default();
485

            
486
				// We group based on `max_amount`, to distinguish between different kind of
487
				// origins. (assumes that all origins have different `max_amount`)
488
				//
489
				// Worst case is that we reject some "valid" request.
490
				let spend = context.spend_in_context.entry(max_amount).or_default();
491

            
492
				// Ensure that we don't overflow nor use more than `max_amount`
493
				if spend.checked_add(&amount).map(|s| s > max_amount).unwrap_or(true) {
494
					Err(Error::<T, I>::InsufficientPermission)
495
				} else {
496
					*spend = spend.saturating_add(amount);
497

            
498
					Ok(())
499
				}
500
			})
501
			.unwrap_or(Ok(()))?;
502

            
503
			let beneficiary = T::Lookup::lookup(beneficiary)?;
504
			let proposal_index = Self::proposal_count();
505
			Approvals::<T, I>::try_append(proposal_index)
506
				.map_err(|_| Error::<T, I>::TooManyApprovals)?;
507
			let proposal = Proposal {
508
				proposer: beneficiary.clone(),
509
				value: amount,
510
				beneficiary: beneficiary.clone(),
511
				bond: Default::default(),
512
			};
513
			Proposals::<T, I>::insert(proposal_index, proposal);
514
			ProposalCount::<T, I>::put(proposal_index + 1);
515

            
516
			Self::deposit_event(Event::SpendApproved { proposal_index, amount, beneficiary });
517
			Ok(())
518
		}
519

            
520
		/// Force a previously approved proposal to be removed from the approval queue.
521
		///
522
		/// ## Dispatch Origin
523
		///
524
		/// Must be [`Config::RejectOrigin`].
525
		///
526
		/// ## Details
527
		///
528
		/// The original deposit will no longer be returned.
529
		///
530
		/// ### Parameters
531
		/// - `proposal_id`: The index of a proposal
532
		///
533
		/// ### Complexity
534
		/// - O(A) where `A` is the number of approvals
535
		///
536
		/// ### Errors
537
		/// - [`Error::ProposalNotApproved`]: The `proposal_id` supplied was not found in the
538
		///   approval queue, i.e., the proposal has not been approved. This could also mean the
539
		///   proposal does not exist altogether, thus there is no way it would have been approved
540
		///   in the first place.
541
		#[pallet::call_index(4)]
542
		#[pallet::weight((T::WeightInfo::remove_approval(), DispatchClass::Operational))]
543
		pub fn remove_approval(
544
			origin: OriginFor<T>,
545
			#[pallet::compact] proposal_id: ProposalIndex,
546
219
		) -> DispatchResult {
547
219
			T::RejectOrigin::ensure_origin(origin)?;
548

            
549
			Approvals::<T, I>::try_mutate(|v| -> DispatchResult {
550
				if let Some(index) = v.iter().position(|x| x == &proposal_id) {
551
					v.remove(index);
552
					Ok(())
553
				} else {
554
					Err(Error::<T, I>::ProposalNotApproved.into())
555
				}
556
			})?;
557

            
558
			Ok(())
559
		}
560

            
561
		/// Propose and approve a spend of treasury funds.
562
		///
563
		/// ## Dispatch Origin
564
		///
565
		/// Must be [`Config::SpendOrigin`] with the `Success` value being at least
566
		/// `amount` of `asset_kind` in the native asset. The amount of `asset_kind` is converted
567
		/// for assertion using the [`Config::BalanceConverter`].
568
		///
569
		/// ## Details
570
		///
571
		/// Create an approved spend for transferring a specific `amount` of `asset_kind` to a
572
		/// designated beneficiary. The spend must be claimed using the `payout` dispatchable within
573
		/// the [`Config::PayoutPeriod`].
574
		///
575
		/// ### Parameters
576
		/// - `asset_kind`: An indicator of the specific asset class to be spent.
577
		/// - `amount`: The amount to be transferred from the treasury to the `beneficiary`.
578
		/// - `beneficiary`: The beneficiary of the spend.
579
		/// - `valid_from`: The block number from which the spend can be claimed. It can refer to
580
		///   the past if the resulting spend has not yet expired according to the
581
		///   [`Config::PayoutPeriod`]. If `None`, the spend can be claimed immediately after
582
		///   approval.
583
		///
584
		/// ## Events
585
		///
586
		/// Emits [`Event::AssetSpendApproved`] if successful.
587
		#[pallet::call_index(5)]
588
		#[pallet::weight(T::WeightInfo::spend())]
589
		pub fn spend(
590
			origin: OriginFor<T>,
591
			asset_kind: Box<T::AssetKind>,
592
			#[pallet::compact] amount: AssetBalanceOf<T, I>,
593
			beneficiary: Box<BeneficiaryLookupOf<T, I>>,
594
			valid_from: Option<BlockNumberFor<T>>,
595
87
		) -> DispatchResult {
596
87
			let max_amount = T::SpendOrigin::ensure_origin(origin)?;
597
			let beneficiary = T::BeneficiaryLookup::lookup(*beneficiary)?;
598

            
599
			let now = frame_system::Pallet::<T>::block_number();
600
			let valid_from = valid_from.unwrap_or(now);
601
			let expire_at = valid_from.saturating_add(T::PayoutPeriod::get());
602
			ensure!(expire_at > now, Error::<T, I>::SpendExpired);
603

            
604
			let native_amount =
605
				T::BalanceConverter::from_asset_balance(amount, *asset_kind.clone())
606
					.map_err(|_| Error::<T, I>::FailedToConvertBalance)?;
607

            
608
			ensure!(native_amount <= max_amount, Error::<T, I>::InsufficientPermission);
609

            
610
			with_context::<SpendContext<BalanceOf<T, I>>, _>(|v| {
611
				let context = v.or_default();
612
				// We group based on `max_amount`, to distinguish between different kind of
613
				// origins. (assumes that all origins have different `max_amount`)
614
				//
615
				// Worst case is that we reject some "valid" request.
616
				let spend = context.spend_in_context.entry(max_amount).or_default();
617

            
618
				// Ensure that we don't overflow nor use more than `max_amount`
619
				if spend.checked_add(&native_amount).map(|s| s > max_amount).unwrap_or(true) {
620
					Err(Error::<T, I>::InsufficientPermission)
621
				} else {
622
					*spend = spend.saturating_add(native_amount);
623
					Ok(())
624
				}
625
			})
626
			.unwrap_or(Ok(()))?;
627

            
628
			let index = SpendCount::<T, I>::get();
629
			Spends::<T, I>::insert(
630
				index,
631
				SpendStatus {
632
					asset_kind: *asset_kind.clone(),
633
					amount,
634
					beneficiary: beneficiary.clone(),
635
					valid_from,
636
					expire_at,
637
					status: PaymentState::Pending,
638
				},
639
			);
640
			SpendCount::<T, I>::put(index + 1);
641

            
642
			Self::deposit_event(Event::AssetSpendApproved {
643
				index,
644
				asset_kind: *asset_kind,
645
				amount,
646
				beneficiary,
647
				valid_from,
648
				expire_at,
649
			});
650
			Ok(())
651
		}
652

            
653
		/// Claim a spend.
654
		///
655
		/// ## Dispatch Origin
656
		///
657
		/// Must be signed
658
		///
659
		/// ## Details
660
		///
661
		/// Spends must be claimed within some temporal bounds. A spend may be claimed within one
662
		/// [`Config::PayoutPeriod`] from the `valid_from` block.
663
		/// In case of a payout failure, the spend status must be updated with the `check_status`
664
		/// dispatchable before retrying with the current function.
665
		///
666
		/// ### Parameters
667
		/// - `index`: The spend index.
668
		///
669
		/// ## Events
670
		///
671
		/// Emits [`Event::Paid`] if successful.
672
		#[pallet::call_index(6)]
673
		#[pallet::weight(T::WeightInfo::payout())]
674
30
		pub fn payout(origin: OriginFor<T>, index: SpendIndex) -> DispatchResult {
675
30
			ensure_signed(origin)?;
676
30
			let mut spend = Spends::<T, I>::get(index).ok_or(Error::<T, I>::InvalidIndex)?;
677
			let now = frame_system::Pallet::<T>::block_number();
678
			ensure!(now >= spend.valid_from, Error::<T, I>::EarlyPayout);
679
			ensure!(spend.expire_at > now, Error::<T, I>::SpendExpired);
680
			ensure!(
681
				matches!(spend.status, PaymentState::Pending | PaymentState::Failed),
682
				Error::<T, I>::AlreadyAttempted
683
			);
684

            
685
			let id = T::Paymaster::pay(&spend.beneficiary, spend.asset_kind.clone(), spend.amount)
686
				.map_err(|_| Error::<T, I>::PayoutError)?;
687

            
688
			spend.status = PaymentState::Attempted { id };
689
			Spends::<T, I>::insert(index, spend);
690

            
691
			Self::deposit_event(Event::<T, I>::Paid { index, payment_id: id });
692

            
693
			Ok(())
694
		}
695

            
696
		/// Check the status of the spend and remove it from the storage if processed.
697
		///
698
		/// ## Dispatch Origin
699
		///
700
		/// Must be signed.
701
		///
702
		/// ## Details
703
		///
704
		/// The status check is a prerequisite for retrying a failed payout.
705
		/// If a spend has either succeeded or expired, it is removed from the storage by this
706
		/// function. In such instances, transaction fees are refunded.
707
		///
708
		/// ### Parameters
709
		/// - `index`: The spend index.
710
		///
711
		/// ## Events
712
		///
713
		/// Emits [`Event::PaymentFailed`] if the spend payout has failed.
714
		/// Emits [`Event::SpendProcessed`] if the spend payout has succeed.
715
		#[pallet::call_index(7)]
716
		#[pallet::weight(T::WeightInfo::check_status())]
717
30
		pub fn check_status(origin: OriginFor<T>, index: SpendIndex) -> DispatchResultWithPostInfo {
718
			use PaymentState as State;
719
			use PaymentStatus as Status;
720

            
721
30
			ensure_signed(origin)?;
722
30
			let mut spend = Spends::<T, I>::get(index).ok_or(Error::<T, I>::InvalidIndex)?;
723
			let now = frame_system::Pallet::<T>::block_number();
724

            
725
			if now > spend.expire_at && !matches!(spend.status, State::Attempted { .. }) {
726
				// spend has expired and no further status update is expected.
727
				Spends::<T, I>::remove(index);
728
				Self::deposit_event(Event::<T, I>::SpendProcessed { index });
729
				return Ok(Pays::No.into())
730
			}
731

            
732
			let payment_id = match spend.status {
733
				State::Attempted { id } => id,
734
				_ => return Err(Error::<T, I>::NotAttempted.into()),
735
			};
736

            
737
			match T::Paymaster::check_payment(payment_id) {
738
				Status::Failure => {
739
					spend.status = PaymentState::Failed;
740
					Spends::<T, I>::insert(index, spend);
741
					Self::deposit_event(Event::<T, I>::PaymentFailed { index, payment_id });
742
				},
743
				Status::Success | Status::Unknown => {
744
					Spends::<T, I>::remove(index);
745
					Self::deposit_event(Event::<T, I>::SpendProcessed { index });
746
					return Ok(Pays::No.into())
747
				},
748
				Status::InProgress => return Err(Error::<T, I>::Inconclusive.into()),
749
			}
750
			return Ok(Pays::Yes.into())
751
		}
752

            
753
		/// Void previously approved spend.
754
		///
755
		/// ## Dispatch Origin
756
		///
757
		/// Must be [`Config::RejectOrigin`].
758
		///
759
		/// ## Details
760
		///
761
		/// A spend void is only possible if the payout has not been attempted yet.
762
		///
763
		/// ### Parameters
764
		/// - `index`: The spend index.
765
		///
766
		/// ## Events
767
		///
768
		/// Emits [`Event::AssetSpendVoided`] if successful.
769
		#[pallet::call_index(8)]
770
		#[pallet::weight(T::WeightInfo::void_spend())]
771
12
		pub fn void_spend(origin: OriginFor<T>, index: SpendIndex) -> DispatchResult {
772
12
			T::RejectOrigin::ensure_origin(origin)?;
773
			let spend = Spends::<T, I>::get(index).ok_or(Error::<T, I>::InvalidIndex)?;
774
			ensure!(
775
				matches!(spend.status, PaymentState::Pending | PaymentState::Failed),
776
				Error::<T, I>::AlreadyAttempted
777
			);
778

            
779
			Spends::<T, I>::remove(index);
780
			Self::deposit_event(Event::<T, I>::AssetSpendVoided { index });
781
			Ok(())
782
		}
783
	}
784
}
785

            
786
impl<T: Config<I>, I: 'static> Pallet<T, I> {
787
	// Add public immutables and private mutables.
788

            
789
	/// The account ID of the treasury pot.
790
	///
791
	/// This actually does computation. If you need to keep using it, then make sure you cache the
792
	/// value and only call this once.
793
195762
	pub fn account_id() -> T::AccountId {
794
195762
		T::PalletId::get().into_account_truncating()
795
195762
	}
796

            
797
	/// Spend some money! returns number of approvals before spend.
798
	pub fn spend_funds() -> Weight {
799
		let mut total_weight = Weight::zero();
800

            
801
		let mut budget_remaining = Self::pot();
802
		Self::deposit_event(Event::Spending { budget_remaining });
803
		let account_id = Self::account_id();
804

            
805
		let mut missed_any = false;
806
		let mut imbalance = <PositiveImbalanceOf<T, I>>::zero();
807
		let proposals_len = Approvals::<T, I>::mutate(|v| {
808
			let proposals_approvals_len = v.len() as u32;
809
			v.retain(|&index| {
810
				// Should always be true, but shouldn't panic if false or we're screwed.
811
				if let Some(p) = Self::proposals(index) {
812
					if p.value <= budget_remaining {
813
						budget_remaining -= p.value;
814
						<Proposals<T, I>>::remove(index);
815

            
816
						// return their deposit.
817
						let err_amount = T::Currency::unreserve(&p.proposer, p.bond);
818
						debug_assert!(err_amount.is_zero());
819

            
820
						// provide the allocation.
821
						imbalance.subsume(T::Currency::deposit_creating(&p.beneficiary, p.value));
822

            
823
						Self::deposit_event(Event::Awarded {
824
							proposal_index: index,
825
							award: p.value,
826
							account: p.beneficiary,
827
						});
828
						false
829
					} else {
830
						missed_any = true;
831
						true
832
					}
833
				} else {
834
					false
835
				}
836
			});
837
			proposals_approvals_len
838
		});
839

            
840
		total_weight += T::WeightInfo::on_initialize_proposals(proposals_len);
841

            
842
		// Call Runtime hooks to external pallet using treasury to compute spend funds.
843
		T::SpendFunds::spend_funds(
844
			&mut budget_remaining,
845
			&mut imbalance,
846
			&mut total_weight,
847
			&mut missed_any,
848
		);
849

            
850
		if !missed_any {
851
			// burn some proportion of the remaining budget if we run a surplus.
852
			let burn = (T::Burn::get() * budget_remaining).min(budget_remaining);
853
			budget_remaining -= burn;
854

            
855
			let (debit, credit) = T::Currency::pair(burn);
856
			imbalance.subsume(debit);
857
			T::BurnDestination::on_unbalanced(credit);
858
			Self::deposit_event(Event::Burnt { burnt_funds: burn })
859
		}
860

            
861
		// Must never be an error, but better to be safe.
862
		// proof: budget_remaining is account free balance minus ED;
863
		// Thus we can't spend more than account free balance minus ED;
864
		// Thus account is kept alive; qed;
865
		if let Err(problem) =
866
			T::Currency::settle(&account_id, imbalance, WithdrawReasons::TRANSFER, KeepAlive)
867
		{
868
			print("Inconsistent state - couldn't settle imbalance for funds spent by treasury");
869
			// Nothing else to do here.
870
			drop(problem);
871
		}
872

            
873
		Self::deposit_event(Event::Rollover { rollover_balance: budget_remaining });
874

            
875
		total_weight
876
	}
877

            
878
	/// Return the amount of money in the pot.
879
	// The existential deposit is not part of the pot so treasury account never gets deleted.
880
194763
	pub fn pot() -> BalanceOf<T, I> {
881
194763
		T::Currency::free_balance(&Self::account_id())
882
194763
			// Must never be less than 0 but better be safe.
883
194763
			.saturating_sub(T::Currency::minimum_balance())
884
194763
	}
885

            
886
	/// Ensure the correctness of the state of this pallet.
887
	#[cfg(any(feature = "try-runtime", test))]
888
53637
	fn do_try_state() -> Result<(), sp_runtime::TryRuntimeError> {
889
53637
		Self::try_state_proposals()?;
890
53637
		Self::try_state_spends()?;
891

            
892
53637
		Ok(())
893
53637
	}
894

            
895
	/// ### Invariants of proposal storage items
896
	///
897
	/// 1. [`ProposalCount`] >= Number of elements in [`Proposals`].
898
	/// 2. Each entry in [`Proposals`] should be saved under a key strictly less than current
899
	/// [`ProposalCount`].
900
	/// 3. Each [`ProposalIndex`] contained in [`Approvals`] should exist in [`Proposals`].
901
	/// Note, that this automatically implies [`Approvals`].count() <= [`Proposals`].count().
902
	#[cfg(any(feature = "try-runtime", test))]
903
53637
	fn try_state_proposals() -> Result<(), sp_runtime::TryRuntimeError> {
904
53637
		let current_proposal_count = ProposalCount::<T, I>::get();
905
53637
		ensure!(
906
53637
			current_proposal_count as usize >= Proposals::<T, I>::iter().count(),
907
			"Actual number of proposals exceeds `ProposalCount`."
908
		);
909

            
910
53637
		Proposals::<T, I>::iter_keys().try_for_each(|proposal_index| -> DispatchResult {
911
			ensure!(
912
				current_proposal_count as u32 > proposal_index,
913
				"`ProposalCount` should by strictly greater than any ProposalIndex used as a key for `Proposals`."
914
			);
915
			Ok(())
916
53637
		})?;
917

            
918
53637
		Approvals::<T, I>::get()
919
53637
			.iter()
920
53637
			.try_for_each(|proposal_index| -> DispatchResult {
921
				ensure!(
922
					Proposals::<T, I>::contains_key(proposal_index),
923
					"Proposal indices in `Approvals` must also be contained in `Proposals`."
924
				);
925
				Ok(())
926
53637
			})?;
927

            
928
53637
		Ok(())
929
53637
	}
930

            
931
	/// ## Invariants of spend storage items
932
	///
933
	/// 1. [`SpendCount`] >= Number of elements in [`Spends`].
934
	/// 2. Each entry in [`Spends`] should be saved under a key strictly less than current
935
	/// [`SpendCount`].
936
	/// 3. For each spend entry contained in [`Spends`] we should have spend.expire_at
937
	/// > spend.valid_from.
938
	#[cfg(any(feature = "try-runtime", test))]
939
53637
	fn try_state_spends() -> Result<(), sp_runtime::TryRuntimeError> {
940
53637
		let current_spend_count = SpendCount::<T, I>::get();
941
53637
		ensure!(
942
53637
			current_spend_count as usize >= Spends::<T, I>::iter().count(),
943
			"Actual number of spends exceeds `SpendCount`."
944
		);
945

            
946
53637
		Spends::<T, I>::iter_keys().try_for_each(|spend_index| -> DispatchResult {
947
			ensure!(
948
				current_spend_count > spend_index,
949
				"`SpendCount` should by strictly greater than any SpendIndex used as a key for `Spends`."
950
			);
951
			Ok(())
952
53637
		})?;
953

            
954
53637
		Spends::<T, I>::iter().try_for_each(|(_index, spend)| -> DispatchResult {
955
			ensure!(
956
				spend.valid_from < spend.expire_at,
957
				"Spend cannot expire before it becomes valid."
958
			);
959
			Ok(())
960
53637
		})?;
961

            
962
53637
		Ok(())
963
53637
	}
964
}
965

            
966
impl<T: Config<I>, I: 'static> OnUnbalanced<NegativeImbalanceOf<T, I>> for Pallet<T, I> {
967
	fn on_nonzero_unbalanced(amount: NegativeImbalanceOf<T, I>) {
968
		let numeric_amount = amount.peek();
969

            
970
		// Must resolve into existing but better to be safe.
971
		let _ = T::Currency::resolve_creating(&Self::account_id(), amount);
972

            
973
		Self::deposit_event(Event::Deposit { value: numeric_amount });
974
	}
975
}
976

            
977
/// TypedGet implementation to get the AccountId of the Treasury.
978
pub struct TreasuryAccountId<R>(PhantomData<R>);
979
impl<R> sp_runtime::traits::TypedGet for TreasuryAccountId<R>
980
where
981
	R: crate::Config,
982
{
983
	type Type = <R as frame_system::Config>::AccountId;
984
	fn get() -> Self::Type {
985
		<crate::Pallet<R>>::account_id()
986
	}
987
}