From 77788732f7ed446a21586ede7416e6a599b0c97b Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Tue, 19 Nov 2024 03:49:20 +1000 Subject: [PATCH 1/2] Do not run npm commands but output instructions instead (#1731) --- .../ts-tests/initialize.sh | 1 + .../commands/contract/bindings/typescript.rs | 24 ++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh b/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh index d85eff115..031061cca 100755 --- a/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh +++ b/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh @@ -37,6 +37,7 @@ function deploy_all() { } function bind() { exe eval "./soroban contract bindings typescript --contract-id $(cat $1) --output-dir ./node_modules/$2 --overwrite" + exe eval "sh -c \"cd ./node_modules/$2 && npm install && npm run build\"" } function bind_all() { bind contract-id-custom-types.txt test-custom-types diff --git a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs index 84e2b1762..4fcfe0955 100644 --- a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs +++ b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs @@ -5,6 +5,7 @@ use soroban_spec_tools::contract as contract_spec; use soroban_spec_typescript::{self as typescript, boilerplate::Project}; use stellar_strkey::DecodeError; +use crate::print::Print; use crate::wasm; use crate::{ commands::{contract::fetch, global, NetworkRunnable}, @@ -13,6 +14,7 @@ use crate::{ network::{self, Network}, }, get_spec::{self, get_remote_contract_spec}, + xdr::{Hash, ScAddress}, }; #[derive(Parser, Debug, Clone)] @@ -83,7 +85,9 @@ impl NetworkRunnable for Cmd { global_args: Option<&global::Args>, config: Option<&config::Args>, ) -> Result<(), Error> { + let print = Print::new(global_args.is_some_and(|a| a.quiet)); let spec = if let Some(wasm) = &self.wasm { + print.infoln("Loading contract spec from file..."); let wasm: wasm::Args = wasm.into(); wasm.parse()?.spec } else { @@ -97,6 +101,8 @@ impl NetworkRunnable for Cmd { .resolve_contract_id(&self.contract_id, &network.network_passphrase)? .0; + let contract_address = ScAddress::Contract(Hash(contract_id)); + print.globeln(format!("Downloading contract spec: {contract_address}")); get_remote_contract_spec( &contract_id, &self.locator, @@ -129,6 +135,7 @@ impl NetworkRunnable for Cmd { .expect("why did we remove the default futurenet network?") .into() }); + print.infoln(format!("Network: {network_passphrase}")); let absolute_path = self.output_dir.canonicalize()?; let file_name = absolute_path .file_name() @@ -136,6 +143,7 @@ impl NetworkRunnable for Cmd { let contract_name = &file_name .to_str() .ok_or_else(|| Error::NotUtf8(file_name.to_os_string()))?; + print.infoln(format!("Embedding contract address: {}", self.contract_id)); p.init( contract_name, &self.contract_id, @@ -143,17 +151,11 @@ impl NetworkRunnable for Cmd { &network_passphrase, &spec, )?; - std::process::Command::new("npm") - .arg("install") - .current_dir(&self.output_dir) - .spawn()? - .wait()?; - std::process::Command::new("npm") - .arg("run") - .arg("build") - .current_dir(&self.output_dir) - .spawn()? - .wait()?; + print.checkln("Generated!"); + print.infoln(format!( + "Run \"npm install && npm run build\" in {:?} to build the JavaScript NPM package.", + self.output_dir + )); Ok(()) } } From 90db678c08e51e84f05719b5f0965e8f53c3c893 Mon Sep 17 00:00:00 2001 From: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com> Date: Tue, 19 Nov 2024 04:19:46 +1000 Subject: [PATCH 2/2] Use the same network and contract resolution for all of the bindings generation logic (#1733) --- .../commands/contract/bindings/typescript.rs | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs index 4fcfe0955..679e757f4 100644 --- a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs +++ b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs @@ -9,10 +9,7 @@ use crate::print::Print; use crate::wasm; use crate::{ commands::{contract::fetch, global, NetworkRunnable}, - config::{ - self, locator, - network::{self, Network}, - }, + config::{self, locator, network}, get_spec::{self, get_remote_contract_spec}, xdr::{Hash, ScAddress}, }; @@ -86,22 +83,25 @@ impl NetworkRunnable for Cmd { config: Option<&config::Args>, ) -> Result<(), Error> { let print = Print::new(global_args.is_some_and(|a| a.quiet)); + + let network = self.network.get(&self.locator).ok().unwrap_or_else(|| { + network::DEFAULTS + .get("testnet") + .expect("no network specified and testnet network not found") + .into() + }); + + let contract_id = self + .locator + .resolve_contract_id(&self.contract_id, &network.network_passphrase)? + .0; + let contract_address = ScAddress::Contract(Hash(contract_id)); + let spec = if let Some(wasm) = &self.wasm { print.infoln("Loading contract spec from file..."); let wasm: wasm::Args = wasm.into(); wasm.parse()?.spec } else { - let network = config.map_or_else( - || self.network.get(&self.locator).map_err(Error::from), - |c| c.get_network().map_err(Error::from), - )?; - - let contract_id = self - .locator - .resolve_contract_id(&self.contract_id, &network.network_passphrase)? - .0; - - let contract_address = ScAddress::Contract(Hash(contract_id)); print.globeln(format!("Downloading contract spec: {contract_address}")); get_remote_contract_spec( &contract_id, @@ -125,17 +125,7 @@ impl NetworkRunnable for Cmd { } std::fs::create_dir_all(&self.output_dir)?; let p: Project = self.output_dir.clone().try_into()?; - let Network { - rpc_url, - network_passphrase, - .. - } = self.network.get(&self.locator).ok().unwrap_or_else(|| { - network::DEFAULTS - .get("futurenet") - .expect("why did we remove the default futurenet network?") - .into() - }); - print.infoln(format!("Network: {network_passphrase}")); + print.infoln(format!("Network: {}", network.network_passphrase)); let absolute_path = self.output_dir.canonicalize()?; let file_name = absolute_path .file_name() @@ -143,12 +133,12 @@ impl NetworkRunnable for Cmd { let contract_name = &file_name .to_str() .ok_or_else(|| Error::NotUtf8(file_name.to_os_string()))?; - print.infoln(format!("Embedding contract address: {}", self.contract_id)); + print.infoln(format!("Embedding contract address: {contract_address}")); p.init( contract_name, - &self.contract_id, - &rpc_url, - &network_passphrase, + &contract_address.to_string(), + &network.rpc_url, + &network.network_passphrase, &spec, )?; print.checkln("Generated!");