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
// This file is part of Gear.

// Copyright (C) 2023-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

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

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

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Module contains macros that help to implement Config type
//! for various pallets of Substrate.
//! All used types should be in scope.

use frame_support::{pallet_prelude::*, sp_runtime::Perbill, weights::RuntimeDbWeight};
use frame_system::limits::BlockWeights;

#[macro_export]
macro_rules! impl_pallet_balances {
    ($runtime:ty) => {
        impl pallet_balances::Config for $runtime {
            type MaxLocks = ();
            type MaxHolds = ConstU32<1>;
            type MaxFreezes = ConstU32<1>;
            type MaxReserves = ();
            type RuntimeFreezeReason = RuntimeFreezeReason;
            type FreezeIdentifier = RuntimeFreezeReason;
            type RuntimeHoldReason = RuntimeHoldReason;
            type ReserveIdentifier = [u8; 8];
            type Balance = Balance;
            type DustRemoval = ();
            type RuntimeEvent = RuntimeEvent;
            type ExistentialDeposit = ExistentialDeposit;
            type AccountStore = System;
            type WeightInfo = ();
        }
    };
}

pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75);
pub const MAX_BLOCK: u64 = 250_000_000_000;

frame_support::parameter_types! {
    pub RuntimeBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults(
        Weight::from_parts(MAX_BLOCK, u64::MAX),
        NORMAL_DISPATCH_RATIO,
    );
    pub const SS58Prefix: u8 = 42;
    pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 1_110, write: 2_300 };
    pub const MinimumPeriod: u64 = 500;
}

#[macro_export]
macro_rules! impl_pallet_system {
    ($( $tokens:tt )*) => {
        #[allow(dead_code)]
        type SystemConfigDbWeight = $crate::pallet_tests::DbWeight;
        #[allow(dead_code)]
        type SystemConfigBlockWeights = $crate::pallet_tests::RuntimeBlockWeights;

        mod pallet_tests_system_config_impl {
            use super::*;

            $crate::impl_pallet_system_inner!($( $tokens )*);
        }
    };
}

#[macro_export]
macro_rules! impl_pallet_system_inner {
    ($runtime:ty$(,)?) => {
        impl frame_system::Config for $runtime {
            type BaseCallFilter = frame_support::traits::Everything;
            type BlockWeights = SystemConfigBlockWeights;
            type BlockLength = ();
            type DbWeight = SystemConfigDbWeight;
            type RuntimeOrigin = RuntimeOrigin;
            type RuntimeCall = RuntimeCall;
            type Nonce = u64;
            type Hash = H256;
            type Hashing = BlakeTwo256;
            type AccountId = AccountId;
            type Lookup = IdentityLookup<Self::AccountId>;
            type Block = Block;
            type RuntimeEvent = RuntimeEvent;
            type BlockHashCount = BlockHashCount;
            type Version = ();
            type PalletInfo = PalletInfo;
            type AccountData = pallet_balances::AccountData<Balance>;
            type OnNewAccount = ();
            type OnKilledAccount = ();
            type SystemWeightInfo = ();
            type SS58Prefix = $crate::pallet_tests::SS58Prefix;
            type OnSetCode = ();
            type MaxConsumers = frame_support::traits::ConstU32<16>;
        }
    };

    ($runtime:ty, DbWeight = $db_weight:ty $(, $( $rest:tt )*)?) => {
        type SystemConfigDbWeight = $db_weight;

        $crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?);
    };

    ($runtime:ty, BlockWeights = $block_weights:ty $(, $( $rest:tt )*)?) => {
        type SystemConfigBlockWeights = $block_weights;

        $crate::impl_pallet_system_inner!($runtime, $($( $rest )*)?);
    };
}

#[macro_export]
macro_rules! impl_pallet_timestamp {
    ($runtime:ty) => {
        impl pallet_timestamp::Config for Test {
            type Moment = u64;
            type OnTimestampSet = ();
            type MinimumPeriod = $crate::pallet_tests::MinimumPeriod;
            type WeightInfo = ();
        }
    };
}

#[macro_export]
macro_rules! impl_pallet_authorship {
    ($( $tokens:tt )*) => {
        #[allow(dead_code)]
        pub struct FixedBlockAuthor;

        impl FindAuthor<AccountId> for FixedBlockAuthor {
            fn find_author<'a, I: 'a>(_: I) -> Option<AccountId> {
                Some(BLOCK_AUTHOR)
            }
        }

        #[allow(dead_code)]
        type AuthorshipFindAuthor = FixedBlockAuthor;
        #[allow(dead_code)]
        type AuthorshipEventHandler = ();

        mod pallet_tests_authorship_config_impl {
            use super::*;

            $crate::impl_pallet_authorship_inner!($( $tokens )*);
        }
    };
}

#[macro_export]
macro_rules! impl_pallet_authorship_inner {
    ($runtime:ty$(,)?) => {
        impl pallet_authorship::Config for $runtime {
            type FindAuthor = AuthorshipFindAuthor;
            type EventHandler = AuthorshipEventHandler;
        }
    };

    ($runtime:ty, FindAuthor = $find_author:ty $(, $( $rest:tt )*)?) => {
        type AuthorshipFindAuthor = $find_author;

        $crate::impl_pallet_authorship_inner!($runtime, $($( $rest )*)?);
    };

    ($runtime:ty, EventHandler = $event_handler:ty $(, $( $rest:tt )*)?) => {
        type AuthorshipEventHandler = $event_handler;

        $crate::impl_pallet_authorship_inner!($runtime, $($( $rest )*)?);
    };
}