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
pub mod universal_aliases;
27

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

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

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

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

            
59
pub struct SessionTimer<Runtime, Delay>(PhantomData<(Runtime, Delay)>);
60

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

            
68
167
    fn now() -> Self::Instant {
69
167
        pallet_session::Pallet::<Runtime>::current_index()
70
167
    }
71

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

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

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