Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New config in sc-config.toml: proxy-paths #1484

Closed
wants to merge 12 commits into from
4 changes: 4 additions & 0 deletions contracts/examples/adder/sc-config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[settings]
proxy-paths = ["src/adder_proxy.rs"]

[contracts.adder]
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion contracts/examples/adder/src/adder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use multiversx_sc::imports::*;

pub mod temp_proxy;
pub mod adder_proxy;

/// One of the simplest smart contracts possible,
/// it holds a single variable in storage, which anyone can increment.
Expand Down
6 changes: 3 additions & 3 deletions contracts/examples/adder/tests/adder_blackbox_chained_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn adder_blackbox_chained() {
)
.chain_deploy(|tx| {
tx.from(AddressExpr("owner"))
.typed(temp_proxy::AdderProxy)
.typed(adder_proxy::AdderProxy)
.init(5u32)
.code(MxscExpr("output/adder.mxsc.json"))
.with_result(WithResultNewAddress::new(|new_address| {
Expand All @@ -36,7 +36,7 @@ fn adder_blackbox_chained() {
})
.chain_query(|tx| {
tx.to(ScExpr("adder"))
.typed(temp_proxy::AdderProxy)
.typed(adder_proxy::AdderProxy)
.sum()
.with_result(WithResultSimilar::new(|value: BigUint| {
assert_eq!(value, BigUint::from(5u32));
Expand All @@ -45,7 +45,7 @@ fn adder_blackbox_chained() {
.chain_call(|tx| {
tx.from(AddressExpr("owner"))
.to(ScExpr("adder"))
.typed(temp_proxy::AdderProxy)
.typed(adder_proxy::AdderProxy)
.add(3u32)
.with_result(WithRawTxResponse(|response| {
assert!(response.tx_error.is_success());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn adder_blackbox_legacy_proxy() {
world
.tx()
.from(OWNER)
.typed(temp_proxy::AdderProxy)
.typed(adder_proxy::AdderProxy)
.init(5u32)
.code(CODE_EXPR)
.with_result(WithResultNewAddress::new(|new_address| {
Expand Down
6 changes: 3 additions & 3 deletions contracts/examples/adder/tests/adder_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn adder_blackbox() {
world
.tx()
.from(OWNER)
.typed(temp_proxy::AdderProxy)
.typed(adder_proxy::AdderProxy)
.init(5u32)
.code(CODE_EXPR)
.with_result(WithResultNewAddress::new(|new_address| {
Expand All @@ -44,7 +44,7 @@ fn adder_blackbox() {
let value = world
.query()
.to(SC_ADDER)
.typed(temp_proxy::AdderProxy)
.typed(adder_proxy::AdderProxy)
.sum()
.returns(ReturnsSimilar::<BigUint>::new())
.run();
Expand All @@ -54,7 +54,7 @@ fn adder_blackbox() {
.tx()
.from(OWNER)
.to(SC_ADDER)
.typed(temp_proxy::AdderProxy)
.typed(adder_proxy::AdderProxy)
.add(1u32)
.with_result(WithRawTxResponse(|response| {
assert!(response.tx_error.is_success());
Expand Down
21 changes: 2 additions & 19 deletions framework/meta/src/cli_args/cli_args_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub enum ContractCliAction {
name = "proxy",
about = "Generates a proxy, based on the contract ABI."
)]
GenerateProxies(GenerateProxyArgs),
GenerateProxies,
}

impl CliArgsToRaw for ContractCliAction {
Expand Down Expand Up @@ -103,9 +103,8 @@ impl CliArgsToRaw for ContractCliAction {
raw.push("snippets".to_string());
raw.append(&mut args.to_raw());
},
ContractCliAction::GenerateProxies(args) => {
ContractCliAction::GenerateProxies => {
raw.push("proxy".to_string());
raw.append(&mut args.to_raw());
},
}
raw
Expand All @@ -128,19 +127,3 @@ impl CliArgsToRaw for GenerateSnippetsArgs {
raw
}
}
#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct GenerateProxyArgs {
/// Override TxProxy project if it already exists.
#[arg(long, verbatim_doc_comment)]
pub overwrite: bool,
}

impl CliArgsToRaw for GenerateProxyArgs {
fn to_raw(&self) -> Vec<String> {
let mut raw = Vec::new();
if self.overwrite {
raw.push("--overwrite".to_string());
}
raw
}
}
4 changes: 1 addition & 3 deletions framework/meta/src/cmd/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ pub fn cli_main<AbiObj: ContractAbiProvider>() {
ContractCliAction::GenerateSnippets(gs_arg) => {
meta_config_opt.generate_rust_snippets(&gs_arg)
},
ContractCliAction::GenerateProxies(arg) => {
meta_config_opt.generate_rust_proxies_struct(&arg)
},
ContractCliAction::GenerateProxies => meta_config_opt.generate_proxy(),
}
}

Expand Down
13 changes: 3 additions & 10 deletions framework/meta/src/cmd/contract/generate_proxy/proxy_crate_gen.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
use std::fs::File;

#[must_use]
pub(crate) fn create_file(proxies_file_name: &str, overwrite: bool) -> File {
let file = format!("../{proxies_file_name}");
pub(crate) fn create_file(proxy_file_name: &str) -> File {
let file = format!("../{proxy_file_name}");

if overwrite {
File::create(&file).expect("could not write proxy file")
} else {
match File::options().create_new(true).write(true).open(&file) {
Ok(f) => f,
Err(_) => panic!("{file} file already exists, --overwrite option was not provided"),
}
}
File::create(file).expect("could not write proxy file")
}
22 changes: 15 additions & 7 deletions framework/meta/src/cmd/contract/generate_proxy/proxy_gen_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use std::fs::File;

use multiversx_sc::abi::ContractAbi;

use crate::cli_args::GenerateProxyArgs;

use super::{
super::meta_config::MetaConfig,
proxy_crate_gen::create_file,
Expand All @@ -16,16 +14,26 @@ use super::{
const PROXIES_SOURCE_FILE_NAME: &str = "/output/proxy.rs";

impl MetaConfig {
pub fn generate_rust_proxies_struct(&self, args: &GenerateProxyArgs) {
let file = create_file(PROXIES_SOURCE_FILE_NAME, args.overwrite);
write_proxies_to_file(file, self.original_contract_abi.clone());
pub fn generate_proxy(&self) {
if self.sc_config.proxy_paths.is_empty() {
write_proxy_with_explicit_path(PROXIES_SOURCE_FILE_NAME, &self.original_contract_abi);
} else {
for path in &self.sc_config.proxy_paths {
write_proxy_with_explicit_path(path, &self.original_contract_abi);
}
}
}
}

fn write_proxies_to_file(mut file: File, abi: ContractAbi) {
fn write_proxy_with_explicit_path(path: &str, abi: &ContractAbi) {
let file = create_file(path);
write_proxy_to_file(file, abi);
}

fn write_proxy_to_file(mut file: File, abi: &ContractAbi) {
write_header(&mut file);
write_struct_template(&mut file, &abi.name);
write_impl_for_tx_proxy(&mut file, &abi.name);
write_struct_tx_proxy_methods(&mut file, &abi.name);
write_content(&mut file, abi);
write_content(&mut file, abi.clone());
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ impl ScConfig {
ScConfig {
default_contract_config_name: config.settings.main.clone().unwrap_or_default(),
contracts,
proxy_paths: config.settings.proxy_paths.clone().unwrap_or_default(),
}
}

Expand All @@ -295,6 +296,7 @@ impl ScConfig {
wasm_crate_name,
abi: original_abi.clone(),
}],
proxy_paths: Vec::new(),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub const SC_CONFIG_FILE_NAMES: &[&str] = &["sc-config.toml", "multicontract.tom
pub struct ScConfig {
pub default_contract_config_name: String,
pub contracts: Vec<ContractVariant>,
pub proxy_paths: Vec<String>
}

impl ScConfig {
Expand Down
3 changes: 3 additions & 0 deletions framework/meta/src/cmd/contract/sc_config/sc_config_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub struct ContractVariantSerde {
#[serde(deny_unknown_fields)]
pub struct MultiContractGeneralSettingsSerde {
pub main: Option<String>,

#[serde(rename = "proxy-paths")]
andrei-marinica marked this conversation as resolved.
Show resolved Hide resolved
pub proxy_paths: Option<Vec<String>>
}

#[derive(Deserialize, Default, Debug, Clone, PartialEq, Eq)]
Expand Down
Loading