1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
use cfg_primitives::{
	constants::{CENTI_CFG, TREASURY_FEE_RATIO},
	types::Balance,
	AccountId,
};
use cfg_traits::fees::{Fee, Fees, PayFee};
use cfg_types::fee_keys::FeeKey;
use frame_support::{
	dispatch::DispatchResult,
	traits::{Currency, Get, Imbalance, OnUnbalanced},
	weights::{
		constants::ExtrinsicBaseWeight, WeightToFeeCoefficient, WeightToFeeCoefficients,
		WeightToFeePolynomial,
	},
};
use smallvec::smallvec;
use sp_arithmetic::Perbill;

pub type NegativeImbalance<R> = <pallet_balances::Pallet<R> as Currency<
	<R as frame_system::Config>::AccountId,
>>::NegativeImbalance;

struct ToAuthor<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<NegativeImbalance<R>> for ToAuthor<R>
where
	R: pallet_balances::Config + pallet_authorship::Config,
{
	fn on_nonzero_unbalanced(amount: NegativeImbalance<R>) {
		if let Some(author) = <pallet_authorship::Pallet<R>>::author() {
			<pallet_balances::Pallet<R>>::resolve_creating(&author, amount);
		}
	}
}

pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
impl<R> OnUnbalanced<NegativeImbalance<R>> for DealWithFees<R>
where
	R: pallet_balances::Config + pallet_treasury::Config + pallet_authorship::Config,
	pallet_treasury::Pallet<R>: OnUnbalanced<NegativeImbalance<R>>,
{
	fn on_unbalanceds<B>(mut fees_then_tips: impl Iterator<Item = NegativeImbalance<R>>) {
		if let Some(fees) = fees_then_tips.next() {
			// for fees, split the destination
			let (treasury_amount, mut author_amount) = fees.ration(
				TREASURY_FEE_RATIO.deconstruct(),
				(Perbill::one() - TREASURY_FEE_RATIO).deconstruct(),
			);
			if let Some(tips) = fees_then_tips.next() {
				// for tips, if any, 100% to author
				tips.merge_into(&mut author_amount);
			}

			use pallet_treasury::Pallet as Treasury;
			<Treasury<R> as OnUnbalanced<_>>::on_unbalanced(treasury_amount);
			<ToAuthor<R> as OnUnbalanced<_>>::on_unbalanced(author_amount);
		}
	}
}

/// Handles converting a weight scalar to a fee value, based on the scale
/// and granularity of the node's balance type.
///
/// This should typically create a mapping between the following ranges:
///   - [0, frame_system::MaximumBlockWeight]
///   - [Balance::min, Balance::max]
///
/// Yet, it can be used for any other sort of change to weight-fee. Some
/// examples being:
///   - Setting it to `0` will essentially disable the weight fee.
///   - Setting it to `1` will cause the literal `#[weight = x]` values to be
///     charged.
pub struct WeightToFee;
impl WeightToFeePolynomial for WeightToFee {
	type Balance = Balance;

	fn polynomial() -> WeightToFeeCoefficients<Self::Balance> {
		let p = CENTI_CFG;
		let q = 10 * Balance::from(ExtrinsicBaseWeight::get().ref_time());

		smallvec!(WeightToFeeCoefficient {
			degree: 1,
			negative: false,
			coeff_frac: Perbill::from_rational(p % q, q),
			coeff_integer: p / q,
		})
	}
}

/// See doc from PayFee
pub struct FeeToTreasury<F, V>(sp_std::marker::PhantomData<(F, V)>);
impl<
		F: Fees<AccountId = AccountId, Balance = Balance, FeeKey = FeeKey>,
		V: Get<Fee<Balance, FeeKey>>,
	> PayFee<AccountId> for FeeToTreasury<F, V>
{
	fn pay(payer: &AccountId) -> DispatchResult {
		F::fee_to_treasury(payer, V::get())
	}

	#[cfg(feature = "runtime-benchmarks")]
	fn add_pay_requirements(payer: &AccountId) {
		F::add_fee_requirements(payer, V::get());
	}
}

/// See doc from PayFee
pub struct FeeToAuthor<F, V>(sp_std::marker::PhantomData<(F, V)>);
impl<
		F: Fees<AccountId = AccountId, Balance = Balance, FeeKey = FeeKey>,
		V: Get<Fee<Balance, FeeKey>>,
	> PayFee<AccountId> for FeeToAuthor<F, V>
{
	fn pay(payer: &AccountId) -> DispatchResult {
		F::fee_to_author(payer, V::get())
	}

	#[cfg(feature = "runtime-benchmarks")]
	fn add_pay_requirements(payer: &AccountId) {
		F::add_fee_requirements(payer, V::get());
	}
}

/// See doc from PayFee
pub struct FeeToBurn<F, V>(sp_std::marker::PhantomData<(F, V)>);
impl<
		F: Fees<AccountId = AccountId, Balance = Balance, FeeKey = FeeKey>,
		V: Get<Fee<Balance, FeeKey>>,
	> PayFee<AccountId> for FeeToBurn<F, V>
{
	fn pay(payer: &AccountId) -> DispatchResult {
		F::fee_to_burn(payer, V::get())
	}

	#[cfg(feature = "runtime-benchmarks")]
	fn add_pay_requirements(payer: &AccountId) {
		F::add_fee_requirements(payer, V::get());
	}
}

#[cfg(test)]
mod test {
	use cfg_primitives::{AccountId, TREASURY_FEE_RATIO};
	use cfg_types::ids::TREASURY_PALLET_ID;
	use frame_support::{
		derive_impl, parameter_types,
		traits::{
			tokens::{PayFromAccount, UnityAssetBalanceConversion},
			Currency, FindAuthor,
		},
		PalletId,
	};
	use sp_core::ConstU64;
	use sp_runtime::{traits::IdentityLookup, Perbill};
	use sp_std::convert::{TryFrom, TryInto};

	use super::*;

	const TEST_ACCOUNT: AccountId = AccountId::new([1; 32]);

	frame_support::construct_runtime!(
		pub enum Runtime {
			System: frame_system,
			Authorship: pallet_authorship,
			Balances: pallet_balances,
			Treasury: pallet_treasury,
		}
	);

	parameter_types! {
		pub const BlockHashCount: u64 = 250;
	}

	#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
	impl frame_system::Config for Runtime {
		type AccountData = pallet_balances::AccountData<u64>;
		type AccountId = AccountId;
		type Block = frame_system::mocking::MockBlock<Runtime>;
		type Lookup = IdentityLookup<Self::AccountId>;
	}

	#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)]
	impl pallet_balances::Config for Runtime {
		type AccountStore = System;
		type DustRemoval = ();
		type ExistentialDeposit = ConstU64<1>;
		type RuntimeHoldReason = ();
	}

	parameter_types! {
		pub const TreasuryPalletId: PalletId = TREASURY_PALLET_ID;
		pub TreasuryAccount: AccountId = Treasury::account_id();
		pub const MaxApprovals: u32 = 100;
	}

	impl pallet_treasury::Config for Runtime {
		type ApproveOrigin = frame_system::EnsureRoot<AccountId>;
		type AssetKind = ();
		type BalanceConverter = UnityAssetBalanceConversion;
		#[cfg(feature = "runtime-benchmarks")]
		type BenchmarkHelper = ();
		type Beneficiary = Self::AccountId;
		type BeneficiaryLookup = IdentityLookup<Self::Beneficiary>;
		type Burn = ();
		type BurnDestination = ();
		type Currency = pallet_balances::Pallet<Runtime>;
		type MaxApprovals = MaxApprovals;
		type OnSlash = ();
		type PalletId = TreasuryPalletId;
		type Paymaster = PayFromAccount<Balances, TreasuryAccount>;
		type PayoutPeriod = ConstU64<10>;
		type ProposalBond = ();
		type ProposalBondMaximum = ();
		type ProposalBondMinimum = ();
		type RejectOrigin = frame_system::EnsureRoot<AccountId>;
		type RuntimeEvent = RuntimeEvent;
		type SpendFunds = ();
		type SpendOrigin = frame_support::traits::NeverEnsureOrigin<u64>;
		type SpendPeriod = ();
		type WeightInfo = ();
	}

	pub struct OneAuthor;
	impl FindAuthor<AccountId> for OneAuthor {
		fn find_author<'a, I>(_: I) -> Option<AccountId>
		where
			I: 'a,
		{
			Some(TEST_ACCOUNT)
		}
	}
	impl pallet_authorship::Config for Runtime {
		type EventHandler = ();
		type FindAuthor = OneAuthor;
	}

	#[test]
	fn test_fees_and_tip_split() {
		System::externalities().execute_with(|| {
			const FEE: u64 = 10;
			const TIP: u64 = 20;

			let fee = Balances::issue(FEE);
			let tip = Balances::issue(TIP);

			assert_eq!(Balances::free_balance(Treasury::account_id()), 0);
			assert_eq!(Balances::free_balance(TEST_ACCOUNT), 0);

			DealWithFees::on_unbalanceds(vec![fee, tip].into_iter());

			assert_eq!(
				Balances::free_balance(Treasury::account_id()),
				TREASURY_FEE_RATIO * FEE
			);
			assert_eq!(
				Balances::free_balance(TEST_ACCOUNT),
				TIP + (Perbill::one() - TREASURY_FEE_RATIO) * FEE
			);
		});
	}
}