1
// Copyright 2019-2025 PureStake Inc.
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam 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
// Moonbeam 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 Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
use clap::Parser;
18
use moonbase_runtime::{
19
	MoonbasePrecompiles, PrecompileName as MoonbaseNames, Runtime as MoonbaseRuntime,
20
};
21
use moonbeam_runtime::{
22
	MoonbeamPrecompiles, PrecompileName as MoonbeamNames, Runtime as MoonbeamRuntime,
23
};
24
use moonriver_runtime::{
25
	MoonriverPrecompiles, PrecompileName as MoonriverNames, Runtime as MoonriverRuntime,
26
};
27
use precompile_utils::precompile_set::PrecompileKind;
28

            
29
#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum, Default)]
30
enum Format {
31
	#[default]
32
	Debug,
33
	Json,
34
	JsonPretty,
35
}
36

            
37
#[derive(Copy, Clone, Debug, PartialEq, Eq, clap::ValueEnum)]
38
enum Network {
39
	Moonbeam,
40
	Moonriver,
41
	Moonbase,
42
}
43

            
44
#[derive(Parser, Debug)]
45
#[command(author, version, about)]
46
struct Args {
47
	/// In which format the checks list should be exported.
48
	#[arg(short, long, value_enum)]
49
	format: Option<Format>,
50
	/// From which network we want to extract precompile data.
51
	#[arg(short, long, value_enum)]
52
	network: Network,
53
}
54

            
55
fn main() {
56
	let args = Args::parse();
57

            
58
	let mut summary = match args.network {
59
		Network::Moonbeam => MoonbeamPrecompiles::<MoonbeamRuntime>::new().summarize_checks(),
60
		Network::Moonbase => MoonbasePrecompiles::<MoonbaseRuntime>::new().summarize_checks(),
61
		Network::Moonriver => MoonriverPrecompiles::<MoonriverRuntime>::new().summarize_checks(),
62
	};
63

            
64
	for item in summary.iter_mut() {
65
		let name = match (&args.network, &item.precompile_kind) {
66
			(Network::Moonbeam, PrecompileKind::Single(address)) => {
67
				MoonbeamNames::from_address(*address).map(|v| format!("{v:?}"))
68
			}
69
			(Network::Moonbase, PrecompileKind::Single(address)) => {
70
				MoonbaseNames::from_address(*address).map(|v| format!("{v:?}"))
71
			}
72
			(Network::Moonriver, PrecompileKind::Single(address)) => {
73
				MoonriverNames::from_address(*address).map(|v| format!("{v:?}"))
74
			}
75
			(_, PrecompileKind::Prefixed(prefix)) if prefix == &[0xff, 0xff, 0xff, 0xff] => {
76
				Some("ForeignAssets".into())
77
			}
78
			(_, PrecompileKind::Prefixed(prefix)) if prefix == &[0xff, 0xff, 0xff, 0xfe] => {
79
				Some("LocalAssets".into())
80
			}
81
			_ => None,
82
		};
83

            
84
		item.name = name;
85
	}
86

            
87
	let output = match args.format.unwrap_or_default() {
88
		Format::Debug => format!("{summary:#?}"),
89
		Format::Json => serde_json::to_string(&summary).expect("to serialize correctly"),
90
		Format::JsonPretty => {
91
			serde_json::to_string_pretty(&summary).expect("to serialize correctly")
92
		}
93
	};
94

            
95
	println!("{output}");
96
}