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
#[cfg(feature = "runtime-benchmarks")]
19
pub mod benchmarking;
20

            
21
pub mod migrations;
22

            
23
#[cfg(feature = "relay")]
24
pub mod relay;
25

            
26
use core::marker::PhantomData;
27
use frame_support::traits::{fungible::Credit, tokens::imbalance::ResolveTo, OnUnbalanced};
28
use pallet_balances::NegativeImbalance;
29
use sp_core::Get;
30

            
31
pub struct DealWithFees<R>(core::marker::PhantomData<R>);
32
impl<R> OnUnbalanced<Credit<R::AccountId, pallet_balances::Pallet<R>>> for DealWithFees<R>
33
where
34
    R: pallet_balances::Config + pallet_treasury::Config + frame_system::Config,
35
    pallet_treasury::NegativeImbalanceOf<R>: From<NegativeImbalance<R>>,
36
{
37
    // this seems to be called for substrate-based transactions
38
2574
    fn on_unbalanceds(
39
2574
        mut fees_then_tips: impl Iterator<Item = Credit<R::AccountId, pallet_balances::Pallet<R>>>,
40
2574
    ) {
41
2574
        if let Some(fees) = fees_then_tips.next() {
42
            // 100% of fees & tips goes to the treasury.
43
2574
            ResolveTo::<pallet_treasury::TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(fees);
44

            
45
2574
            if let Some(tip) = fees_then_tips.next() {
46
2574
                ResolveTo::<pallet_treasury::TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(tip);
47
2574
            }
48
        }
49
2574
    }
50

            
51
    fn on_nonzero_unbalanced(amount: Credit<R::AccountId, pallet_balances::Pallet<R>>) {
52
        // 100% goes to the treasury
53
        ResolveTo::<pallet_treasury::TreasuryAccountId<R>, pallet_balances::Pallet<R>>::on_unbalanced(amount);
54
    }
55
}
56

            
57
pub struct SessionTimer<Runtime, Delay>(PhantomData<(Runtime, Delay)>);
58

            
59
impl<Runtime, Delay> pallet_pooled_staking::traits::Timer for SessionTimer<Runtime, Delay>
60
where
61
    Delay: Get<u32>,
62
    Runtime: pallet_session::Config,
63
{
64
    type Instant = u32;
65

            
66
353
    fn now() -> Self::Instant {
67
353
        pallet_session::Pallet::<Runtime>::current_index()
68
353
    }
69

            
70
117
    fn is_elapsed(instant: &Self::Instant) -> bool {
71
117
        let delay = Delay::get();
72
117
        let Some(end) = instant.checked_add(delay) else {
73
            return false;
74
        };
75
117
        end <= Self::now()
76
117
    }
77

            
78
    #[cfg(feature = "runtime-benchmarks")]
79
    fn elapsed_instant() -> Self::Instant {
80
        let delay = Delay::get();
81
        Self::now()
82
            .checked_add(delay)
83
            .expect("overflow when computing valid elapsed instant")
84
    }
85

            
86
    #[cfg(feature = "runtime-benchmarks")]
87
    fn skip_to_elapsed() {
88
        let session_to_reach = Self::elapsed_instant();
89
        while Self::now() < session_to_reach {
90
            pallet_session::Pallet::<Runtime>::rotate_session();
91
        }
92
    }
93
}