Browse Source

drk: made contract deploy serialized instruction optional

skoupidi 9 months ago
parent
commit
42a634ab59
3 changed files with 27 additions and 19 deletions
  1. 2 1
      bin/drk/src/cli_util.rs
  2. 19 15
      bin/drk/src/interactive.rs
  3. 6 3
      bin/drk/src/main.rs

+ 2 - 1
bin/drk/src/cli_util.rs

@@ -494,7 +494,8 @@ pub fn generate_completions(shell: &str) -> Result<String> {
 
     let wasm_path = Arg::with_name("wasm-path").help("Path to contract wasm bincode");
 
-    let deploy_ix = Arg::with_name("deploy-ix").help("Path to serialized deploy instruction");
+    let deploy_ix =
+        Arg::with_name("deploy-ix").help("Optional path to serialized deploy instruction");
 
     let deploy = SubCommand::with_name("deploy").about("Deploy a smart contract").args(&vec![
         deploy_auth.clone(),

+ 19 - 15
bin/drk/src/interactive.rs

@@ -365,7 +365,7 @@ fn hints(buffer: &str) -> Option<(String, i32, bool)> {
         "token mint " => Some(("<token> <amount> <recipient> [spend-hook] [user-data]".to_string(), color, bold)),
         "token freeze " => Some(("<token>".to_string(), color, bold)),
         "contract " => Some(("(generate-deploy|list|deploy|lock)".to_string(), color, bold)),
-        "contract deploy " => Some(("<deploy-auth> <wasm-path> <deploy-ix>".to_string(), color, bold)),
+        "contract deploy " => Some(("<deploy-auth> <wasm-path> [deploy-ix]".to_string(), color, bold)),
         "contract lock " => Some(("<deploy-auth>".to_string(), color, bold)),
         _ => None,
     }
@@ -3144,9 +3144,9 @@ async fn handle_contract_list(drk: &DrkPtr, parts: &[&str], output: &mut Vec<Str
 /// Auxiliary function to define the contract deploy subcommand handling.
 async fn handle_contract_deploy(drk: &DrkPtr, parts: &[&str], output: &mut Vec<String>) {
     // Check correct subcommand structure
-    if parts.len() != 5 {
+    if parts.len() != 4 && parts.len() != 5 {
         output.push(String::from("Malformed `contract deploy` subcommand"));
-        output.push(String::from("Usage: contract deploy <deploy-auth> <wasm-path> <deploy-ix>"));
+        output.push(String::from("Usage: contract deploy <deploy-auth> <wasm-path> [deploy-ix]"));
         return
     }
 
@@ -3174,19 +3174,23 @@ async fn handle_contract_deploy(drk: &DrkPtr, parts: &[&str], output: &mut Vec<S
         }
     };
 
-    let file_path = match expand_path(parts[4]) {
-        Ok(p) => p,
-        Err(e) => {
-            output.push(format!("Error while expanding deploy instruction file path: {e}"));
-            return
-        }
-    };
-    let deploy_ix = match smol::fs::read(file_path).await {
-        Ok(d) => d,
-        Err(e) => {
-            output.push(format!("Error while reading deploy instruction file: {e}"));
-            return
+    let deploy_ix = if parts.len() == 5 {
+        let file_path = match expand_path(parts[4]) {
+            Ok(p) => p,
+            Err(e) => {
+                output.push(format!("Error while expanding deploy instruction file path: {e}"));
+                return
+            }
+        };
+        match smol::fs::read(file_path).await {
+            Ok(d) => d,
+            Err(e) => {
+                output.push(format!("Error while reading deploy instruction file: {e}"));
+                return
+            }
         }
+    } else {
+        vec![]
     };
 
     let lock = drk.read().await;

+ 6 - 3
bin/drk/src/main.rs

@@ -530,8 +530,8 @@ enum ContractSubcmd {
         /// Path to contract wasm bincode
         wasm_path: String,
 
-        /// Path to serialized deploy instruction
-        deploy_ix: String,
+        /// Optional path to serialized deploy instruction
+        deploy_ix: Option<String>,
     },
 
     /// Lock a smart contract
@@ -2607,7 +2607,10 @@ async fn realmain(args: Args, ex: ExecutorPtr) -> Result<()> {
             ContractSubcmd::Deploy { deploy_auth, wasm_path, deploy_ix } => {
                 // Read the wasm bincode and deploy instruction
                 let wasm_bin = smol::fs::read(expand_path(&wasm_path)?).await?;
-                let deploy_ix = smol::fs::read(expand_path(&deploy_ix)?).await?;
+                let deploy_ix = match deploy_ix {
+                    Some(p) => smol::fs::read(expand_path(&p)?).await?,
+                    None => vec![],
+                };
 
                 let drk = new_wallet(
                     blockchain_config.cache_path,