multiversx_sc_meta_lib/contract/sc_config/
contract_variant.rs

1use std::path::{Path, PathBuf};
2
3use super::{contract_variant_builder::default_wasm_crate_name, ContractVariantSettings};
4use crate::cli::BuildArgs;
5use multiversx_sc::abi::ContractAbi;
6
7/// Represents a contract created by the framework when building.
8///
9/// It might have only some of the endpoints written by the developer and maybe some other function.
10pub struct ContractVariant {
11    /// Initially, there was only 1 wasm crate, called `wasm`.
12    ///
13    /// Then, we got multi-contracts, but the wasm crate became the "main" one.
14    ///
15    /// Then we removed that mechanism completely, and it remained purely cosmetic,
16    /// being named just `wasm`, instead of `wasm-{contract-name}`.
17    ///
18    /// We are still keeping this around for contracts with only one output contract, for the cosmetic bit.
19    pub wasm_dir_short_name: bool,
20
21    /// The contract id is defined in `multicontract.toml`. It has no effect on the produced assets.
22    ///
23    /// It can be the same as the contract name, but it is not necessary.
24    pub contract_id: String,
25
26    /// The name, as seen in the generated contract names.
27    ///
28    /// It is either defined in the multicontract.toml, or is inferred from the main crate name.
29    pub contract_name: String,
30
31    /// The name of the wasm crate, as it appear in Cargo.toml. It is normally the `contract_name` field, followed by the `-wasm` suffix.
32    ///
33    /// However, the main contract Cargo.toml is given explicitly, so this name might differ.
34    pub wasm_crate_name: String,
35
36    /// Collection of flags, specified in the multicontract config.
37    pub settings: ContractVariantSettings,
38
39    /// Filtered and processed ABI of the output contract.
40    pub abi: ContractAbi,
41}
42
43impl ContractVariant {
44    pub fn default_from_abi(abi: &ContractAbi) -> Self {
45        let default_contract_config_name = abi.build_info.contract_crate.name.to_string();
46        let wasm_crate_name = default_wasm_crate_name(&default_contract_config_name);
47        ContractVariant {
48            wasm_dir_short_name: true,
49            settings: ContractVariantSettings::default(),
50            contract_id: default_contract_config_name.clone(),
51            contract_name: default_contract_config_name,
52            wasm_crate_name,
53            abi: abi.clone(),
54        }
55    }
56
57    pub fn public_name_snake_case(&self) -> String {
58        self.contract_name.replace('-', "_")
59    }
60
61    /// The name of the directory of the wasm crate.
62    ///
63    /// Note this does not necessarily have to match the wasm crate name defined in Cargo.toml.
64    pub fn wasm_crate_dir_name(&self) -> String {
65        if self.wasm_dir_short_name {
66            "wasm".to_string()
67        } else {
68            format!("wasm-{}", &self.contract_name)
69        }
70    }
71
72    pub fn wasm_crate_path(&self) -> PathBuf {
73        Path::new("..").join(self.wasm_crate_dir_name())
74    }
75
76    pub fn cargo_toml_path(&self) -> PathBuf {
77        Path::new(&self.wasm_crate_path()).join("Cargo.toml")
78    }
79
80    pub fn some_other_test_path(&self) -> PathBuf {
81        Path::new(&self.wasm_crate_path()).join("test-Cargo.toml")
82    }
83
84    pub fn wasm_crate_name_snake_case(&self) -> String {
85        self.wasm_crate_name.replace('-', "_")
86    }
87
88    pub fn resolve_wasm_target_dir(&self, explicit_target_dir: &Option<String>) -> PathBuf {
89        let wasm_crate_path = self.wasm_crate_path();
90        if let Some(explicit_target_dir) = explicit_target_dir {
91            // usually the explicit_target_dir is absolute,
92            // but if it isn't, we need to take the path of the wasm crate into account
93            Path::new(&wasm_crate_path).join(explicit_target_dir)
94        } else {
95            Path::new(&wasm_crate_path).join("target")
96        }
97    }
98
99    /// This is where Rust will initially compile the WASM binary.
100    pub fn wasm_compilation_output_path(&self, explicit_target_dir: &Option<String>) -> PathBuf {
101        let target_dir = self.resolve_wasm_target_dir(explicit_target_dir);
102        let wasm_file_name = format!("{}.wasm", &self.wasm_crate_name_snake_case());
103
104        Path::new(&target_dir)
105            .join(&self.settings.rustc_target)
106            .join("release")
107            .join(wasm_file_name)
108    }
109
110    pub fn abi_output_name(&self) -> String {
111        format!("{}.abi.json", &self.contract_name)
112    }
113
114    fn output_name_base(&self, build_args: &BuildArgs) -> String {
115        if let Some(wasm_name_override) = &build_args.wasm_name_override {
116            wasm_name_override.clone()
117        } else if let Some(suffix) = &build_args.wasm_name_suffix {
118            format!("{}-{suffix}", &self.contract_name)
119        } else {
120            self.contract_name.clone()
121        }
122    }
123
124    pub fn wasm_output_name(&self, build_args: &BuildArgs) -> String {
125        format!("{}.wasm", self.output_name_base(build_args))
126    }
127
128    pub fn wat_output_name(&self, build_args: &BuildArgs) -> String {
129        format!("{}.wat", self.output_name_base(build_args))
130    }
131
132    pub fn mxsc_file_output_name(&self, build_args: &BuildArgs) -> String {
133        format!("{}.mxsc.json", self.output_name_base(build_args))
134    }
135
136    pub fn imports_json_output_name(&self, build_args: &BuildArgs) -> String {
137        format!("{}.imports.json", self.output_name_base(build_args))
138    }
139
140    pub fn twiggy_top_name(&self, build_args: &BuildArgs) -> String {
141        format!("twiggy-top-{}.txt", self.output_name_base(build_args))
142    }
143
144    pub fn twiggy_paths_name(&self, build_args: &BuildArgs) -> String {
145        format!("twiggy-paths-{}.txt", self.output_name_base(build_args))
146    }
147
148    pub fn twiggy_monos_name(&self, build_args: &BuildArgs) -> String {
149        format!("twiggy-monos-{}.txt", self.output_name_base(build_args))
150    }
151
152    pub fn twiggy_dominators_name(&self, build_args: &BuildArgs) -> String {
153        format!(
154            "twiggy-dominators-{}.txt",
155            self.output_name_base(build_args)
156        )
157    }
158
159    pub fn endpoint_names(&self) -> Vec<String> {
160        self.abi
161            .endpoints
162            .iter()
163            .map(|endpoint| endpoint.name.to_string())
164            .collect()
165    }
166
167    /// Yields "init" + all endpoint names + "callBack" (if it exists).
168    ///
169    /// Should correspond to all wasm exported functions.
170    pub fn all_exported_function_names(&self) -> Vec<String> {
171        let mut result = vec!["init".to_string()];
172        if !self.abi.upgrade_constructors.is_empty() {
173            result.push("upgrade".to_string())
174        }
175        result.append(&mut self.endpoint_names());
176        if self.abi.has_callback {
177            result.push("callBack".to_string());
178        }
179        result
180    }
181}
182
183impl std::fmt::Debug for ContractVariant {
184    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
185        f.debug_struct("ContractVariant")
186            .field("wasm_dir_short_name", &self.wasm_dir_short_name)
187            .field("config_name", &self.contract_id)
188            .field("public_name", &self.contract_name)
189            .field("num-constructors", &self.abi.constructors.len())
190            .field(
191                "num-upgrade-constructors",
192                &self.abi.upgrade_constructors.len(),
193            )
194            .field("num-endpoints", &self.abi.endpoints.len())
195            .field("settings", &self.settings)
196            .finish()
197    }
198}