Skip to content

Commit

Permalink
snippets generation fixes to accommodate chain simulator setup
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Nov 5, 2024
1 parent f623af7 commit 98792f9
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
"#;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()

Check warning on line 172 in framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs#L172

warning: the borrowed expression implements the required traits --> framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs:172:18 | 172 | File::create(&lib_path).unwrap() | ^^^^^^^^^ help: change this to: `lib_path` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args = note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default
Raw output
framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs:172:18:w:warning: the borrowed expression implements the required traits
   --> framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs:172:18
    |
172 |     File::create(&lib_path).unwrap()
    |                  ^^^^^^^^^ help: change this to: `lib_path`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
    = note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default


__END__

Check warning on line 172 in framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs#L172

warning: the borrowed expression implements the required traits --> framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs:172:18 | 172 | File::create(&lib_path).unwrap() | ^^^^^^^^^ help: change this to: `lib_path` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args = note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default
Raw output
framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs:172:18:w:warning: the borrowed expression implements the required traits
   --> framework/meta-lib/src/contract/generate_snippets/snippet_crate_gen.rs:172:18
    |
172 |     File::create(&lib_path).unwrap()
    |                  ^^^^^^^^^ help: change this to: `lib_path`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
    = note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default


__END__
}

fn file_exists(path: &str) -> bool {
fs::metadata(path).is_ok()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ 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,
},
};

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);
}
}

Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}

0 comments on commit 98792f9

Please sign in to comment.