1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16
#![cfg_attr(not(feature = "std"), no_std)]
17

            
18
use frame_support::traits::{fungible::Credit, tokens::imbalance::ResolveTo, OnUnbalanced};
19
use pallet_balances::NegativeImbalance;
20
#[cfg(feature = "runtime-benchmarks")]
21
pub mod benchmarking;
22
pub mod migrations;
23

            
24
pub struct DealWithFees<R>(sp_std::marker::PhantomData<R>);
25
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for DealWithFees<R>
26
where
27
    R: pallet_balances::Config + pallet_treasury::Config + frame_system::Config,
28
    pallet_treasury::NegativeImbalanceOf<R>: From<NegativeImbalance<R>>,
29
{
30
    // this seems to be called for substrate-based transactions
31
2532
    fn on_unbalanceds(
32
2532
        mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
33
2532
    ) {
34
2532
        if let Some(fees) = fees_then_tips.next() {
35
            // 100% of fees & tips goes to the treasury.
36
2532
            ResolveTo::<pallet_treasury::TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(fees);
37

            
38
2532
            if let Some(tip) = fees_then_tips.next() {
39
2532
                ResolveTo::<pallet_treasury::TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(tip);
40
2532
            }
41
        }
42
2532
    }
43

            
44
    fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
45
        // 100% goes to the treasury
46
        ResolveTo::<pallet_treasury::TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
47
    }
48
}