From 98792f997414e017ea99a66a8a145bb2be137ede Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Tue, 5 Nov 2024 19:28:30 +0100 Subject: [PATCH] snippets generation fixes to accommodate chain simulator setup --- .../generate_snippets/snippet_crate_gen.rs | 30 +++++- .../generate_snippets/snippet_gen_main.rs | 46 ++++++--- .../snippet_sc_functions_gen.rs | 5 +- .../generate_snippets/snippet_template_gen.rs | 99 +++++++++++++++++-- 4 files changed, 155 insertions(+), 25 deletions(-) diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs index 0b9d9485e1..cd38cfa86b 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs @@ -8,6 +8,8 @@ use crate::version_history; static SNIPPETS_SOURCE_FILE_NAME: &str = "interactor_main.rs"; static SC_CONFIG_PATH: &str = "../sc-config.toml"; +static CONFIG_TOML_PATH: &str = "config.toml"; +static CONFIG_SOURCE_FILE_NAME: &str = "config.rs"; static FULL_PROXY_ENTRY: &str = r#"[[proxy]] path = "interactor/src/proxy.rs" "#; @@ -89,7 +91,9 @@ clap = {{ version = "4.4.7", features = ["derive"] }} serde = {{ version = "1.0", features = ["derive"] }} toml = "0.8.6" -# [workspace] +# uncomment when using chain simulator +# [features] +# chain-simulator-tests = [] "# ) .unwrap(); @@ -144,6 +148,30 @@ pub(crate) fn create_sc_config_file(overwrite: bool) { writeln!(&mut file, "\n{FULL_PROXY_ENTRY}").unwrap(); } +pub(crate) fn create_config_toml_file(snippets_folder_path: &str) { + let config_path = format!("{snippets_folder_path}/src/{CONFIG_TOML_PATH}"); + + let mut file = File::create(config_path).unwrap(); + + writeln!( + &mut file, + r#" +# chain_type = 'simulator' +# gateway_uri = 'http://localhost:8085' + +chain_type = 'real' +gateway_uri = 'https://devnet-gateway.multiversx.com' +"# + ) + .unwrap(); +} + +pub(crate) fn create_config_rust_file(snippets_folder_path: &str) -> File { + let lib_path = format!("{snippets_folder_path}/src/{CONFIG_SOURCE_FILE_NAME}"); + + File::create(&lib_path).unwrap() +} + fn file_exists(path: &str) -> bool { fs::metadata(path).is_ok() } diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs index 354ef09b02..c566dd49b6 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_gen_main.rs @@ -7,13 +7,16 @@ use crate::cli::GenerateSnippetsArgs; use super::{ super::meta_config::MetaConfig, snippet_crate_gen::{ - create_and_get_lib_file, create_sc_config_file, create_snippets_cargo_toml, - create_snippets_folder, create_snippets_gitignore, create_src_folder, + create_and_get_lib_file, create_config_rust_file, create_config_toml_file, + create_sc_config_file, create_snippets_cargo_toml, create_snippets_folder, + create_snippets_gitignore, create_src_folder, }, snippet_sc_functions_gen::write_interact_struct_impl, snippet_template_gen::{ - write_interact_struct_declaration, write_snippet_constants, write_snippet_imports, - write_snippet_main_function, write_snippet_state_impl, write_state_struct_declaration, + write_config_constants, write_config_imports, write_config_struct_declaration, + write_config_struct_impl, write_interact_struct_declaration, write_snippet_constants, + write_snippet_imports, write_snippet_main_function, write_snippet_state_impl, + write_state_struct_declaration, }, }; @@ -21,9 +24,11 @@ impl MetaConfig { pub fn generate_rust_snippets(&self, args: &GenerateSnippetsArgs) { let main_contract = self.sc_config.main_contract(); let crate_name = &main_contract.contract_name; - let file = + let mut file = create_snippets_crate_and_get_lib_file(&self.snippets_dir, crate_name, args.overwrite); - write_snippets_to_file(file, &self.original_contract_abi, crate_name); + write_snippets_to_file(&mut file, &self.original_contract_abi, crate_name); + let mut config_file = create_config_and_get_file(&self.snippets_dir); + write_config_to_file(&mut config_file); } } @@ -41,12 +46,25 @@ fn create_snippets_crate_and_get_lib_file( create_and_get_lib_file(snippets_folder_path, overwrite) } -fn write_snippets_to_file(mut file: File, abi: &ContractAbi, crate_name: &str) { - write_snippet_imports(&mut file); - write_snippet_constants(&mut file); - write_snippet_main_function(&mut file, abi); - write_state_struct_declaration(&mut file); - write_snippet_state_impl(&mut file); - write_interact_struct_declaration(&mut file); - write_interact_struct_impl(&mut file, abi, crate_name); +#[must_use] +fn create_config_and_get_file(snippets_folder_path: &str) -> File { + create_config_toml_file(snippets_folder_path); + create_config_rust_file(snippets_folder_path) +} + +fn write_snippets_to_file(file: &mut File, abi: &ContractAbi, crate_name: &str) { + write_snippet_imports(file); + write_snippet_constants(file); + write_snippet_main_function(file, abi); + write_state_struct_declaration(file); + write_snippet_state_impl(file); + write_interact_struct_declaration(file); + write_interact_struct_impl(file, abi, crate_name); +} + +fn write_config_to_file(file: &mut File) { + write_config_imports(file); + write_config_constants(file); + write_config_struct_declaration(file); + write_config_struct_impl(file); } diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs index 46621e5074..b5789c9645 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_sc_functions_gen.rs @@ -13,10 +13,11 @@ pub(crate) fn write_interact_struct_impl(file: &mut File, abi: &ContractAbi, cra file, r#"impl ContractInteract {{ async fn new() -> Self {{ - let mut interactor = Interactor::new(GATEWAY).await; + let config = Config::new(); + let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()).await; interactor.set_current_dir_from_workspace("{}"); - let wallet_address = interactor.register_wallet(test_wallets::alice()); + let wallet_address = interactor.register_wallet(test_wallets::alice()).await; let contract_code = BytesValue::interpret_from( {}, diff --git a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs index a7e7a41f1d..95ecbd64df 100644 --- a/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs +++ b/framework/meta-lib/src/contract/generate_snippets/snippet_template_gen.rs @@ -10,9 +10,10 @@ pub(crate) fn write_snippet_imports(file: &mut File) { "#![allow(non_snake_case)] mod proxy; +mod config; +use config::Config; use multiversx_sc_snippets::imports::*; -use multiversx_sc_snippets::sdk; use serde::{{Deserialize, Serialize}}; use std::{{ io::{{Read, Write}}, @@ -26,13 +27,7 @@ use std::{{ } pub(crate) fn write_snippet_constants(file: &mut File) { - writeln!( - file, - "const GATEWAY: &str = sdk::gateway::DEVNET_GATEWAY; -const STATE_FILE: &str = \"state.toml\"; -" - ) - .unwrap(); + writeln!(file, "const STATE_FILE: &str = \"state.toml\";").unwrap(); write_newline(file); } @@ -156,3 +151,91 @@ pub(crate) fn write_snippet_state_impl(file: &mut File) { write_newline(file); } + +pub(crate) fn write_config_imports(file: &mut File) { + writeln!( + file, + "#![allow(unused)] + +use serde::Deserialize; +use std::io::Read; +" + ) + .unwrap(); + + write_newline(file); +} + +pub(crate) fn write_config_constants(file: &mut File) { + writeln!( + file, + "/// Config file +const CONFIG_FILE: &str = \"config.toml\"; +" + ) + .unwrap(); + + write_newline(file); +} + +pub(crate) fn write_config_struct_declaration(file: &mut File) { + writeln!( + file, + r#"#[derive(Debug, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ChainType {{ + Real, + Simulator, + }} + +/// Contract Interact configuration +#[derive(Debug, Deserialize)] +pub struct Config {{ + pub gateway_uri: String, + pub chain_type: ChainType, + }} +"# + ) + .unwrap(); + + write_newline(file); +} + +pub(crate) fn write_config_struct_impl(file: &mut File) { + writeln!( + file, + r#"impl Config {{ + // Deserializes config from file + pub fn new() -> Self {{ + let mut file = std::fs::File::open(CONFIG_FILE).unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + toml::from_str(&content).unwrap() + }} + + pub fn chain_simulator_config() -> Self {{ + Config {{ + gateway_uri: "http://localhost:8085".to_owned(), + chain_type: ChainType::Simulator, + }} + }} + + // Returns the gateway URI + pub fn gateway_uri(&self) -> &str {{ + &self.gateway_uri + }} + + // Returns if chain type is chain simulator + pub fn use_chain_simulator(&self) -> bool {{ + match self.chain_type {{ + ChainType::Real => false, + ChainType::Simulator => true, + }} + }} + }} +"# + ) + .unwrap(); + + write_newline(file); +}