Skip to main content

foundry_config/
revive.rs

1use foundry_compilers::{
2    ProjectPathsConfig, error::SolcError, multi::MultiCompilerLanguage, resolc::ResolcSettings,
3    solc::SolcSettings,
4};
5use serde::{Deserialize, Serialize};
6use std::{fmt::Display, str::FromStr};
7
8use crate::{Config, SolcReq};
9
10/// Polkadot execution mode
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
12#[serde(rename_all = "lowercase")]
13pub enum PolkadotMode {
14    Evm,
15    Pvm,
16}
17
18impl FromStr for PolkadotMode {
19    type Err = String;
20
21    fn from_str(s: &str) -> Result<Self, Self::Err> {
22        match s.to_lowercase().as_str() {
23            "evm" => Ok(Self::Evm),
24            "pvm" => Ok(Self::Pvm),
25            "" => Ok(Self::Evm), // Default when --polkadot with no value
26            _ => Err(format!("Invalid polkadot mode: {s}. Use 'evm' or 'pvm'")),
27        }
28    }
29}
30
31impl Display for PolkadotMode {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        match self {
34            Self::Evm => write!(f, "evm"),
35            Self::Pvm => write!(f, "pvm"),
36        }
37    }
38}
39
40/// Filename for resolc cache
41pub const RESOLC_SOLIDITY_FILES_CACHE_FILENAME: &str = "resolc-solidity-files-cache.json";
42
43/// Name of the subdirectory for solc artifacts in dual compilation mode
44pub const SOLC_ARTIFACTS_SUBDIR: &str = "solc";
45
46pub const CONTRACT_SIZE_LIMIT: usize = 250_000;
47
48#[derive(Clone, Debug, PartialEq, Eq, Serialize, Default, Deserialize)]
49/// Resolc Config
50pub struct PolkadotConfig {
51    /// Enable compilation using resolc
52    pub resolc_compile: bool,
53
54    /// Use pallet-revive runtime backend
55    pub polkadot: Option<PolkadotMode>,
56
57    /// The resolc compiler
58    pub resolc: Option<SolcReq>,
59
60    /// The optimization mode string for resolc
61    pub optimizer_mode: Option<char>,
62
63    /// The emulated EVM linear heap memory static buffer size in bytes
64    pub heap_size: Option<u32>,
65
66    /// The contracts total stack size in bytes
67    pub stack_size: Option<u32>,
68
69    /// Generate source based debug information in the output code file
70    pub debug_information: Option<bool>,
71}
72
73impl PolkadotConfig {
74    /// Returns the `ProjectPathsConfig` sub set of the config.
75    pub fn project_paths(config: &Config) -> ProjectPathsConfig<MultiCompilerLanguage> {
76        let builder = ProjectPathsConfig::builder()
77            .cache(config.cache_path.join(RESOLC_SOLIDITY_FILES_CACHE_FILENAME))
78            .sources(&config.src)
79            .tests(&config.test)
80            .scripts(&config.script)
81            .libs(config.libs.iter())
82            .remappings(config.get_all_remappings())
83            .allowed_path(&config.root)
84            .allowed_paths(&config.libs)
85            .allowed_paths(&config.allow_paths)
86            .include_paths(&config.include_paths)
87            .artifacts(&config.out);
88
89        builder.build_with_root(&config.root)
90    }
91
92    pub fn resolc_settings(config: &Config) -> Result<SolcSettings, SolcError> {
93        config.solc_settings().map(|mut s| {
94            s.extra_settings = ResolcSettings::new(
95                config.polkadot.optimizer_mode,
96                config.polkadot.heap_size,
97                config.polkadot.stack_size,
98                config.polkadot.debug_information,
99            );
100            s
101        })
102    }
103}