From 03b38deb751b3dbec0b0a3ac97aa8d9f1fb95706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=92=D0=BB=D0=B0?= =?UTF-8?q?=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=D0=BE=D0=B2?= <32911557+vladimirovmm@users.noreply.github.com> Date: Wed, 16 Feb 2022 16:47:18 +0500 Subject: [PATCH] [v1.6] dove call: create and publish transactions (#209) * $ dove call * README.md + hints * rebase [master] * updating "dove call" tests --- README.md | 19 +++++++++++++--- dove/src/call/cmd.rs | 4 ++-- dove/src/cmd/call.rs | 37 +++++++++++++++++++++++--------- dove/src/cmd/run.rs | 4 ++-- dove/tests/test_cmd_dove_call.rs | 37 ++++++++++---------------------- 5 files changed, 58 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 8f56bc33..990cc778 100644 --- a/README.md +++ b/README.md @@ -62,10 +62,23 @@ The contents of the directories will be deleted: ## Pallet Transactions -Command `call` allows you to create transactions for Polkadot chain with [Move Pallete](https://github.com/pontem-network/sp-move) on board. +Command `call` allows you to create and publish transactions for Polkadot chain with [Move Pallete](https://github.com/pontem-network/sp-move) on board. `call` takes script identifier, type parameters, and arguments and creates a transaction file as an artifact of work. +``` +dove call [CALL] [OPTIONS] +``` + +### Input parameters +- `[CALL]` - Call declaration +- `-a` / `--args` Script arguments, e.g. 10 20 30 +- `-t`, `--type` Script type parameters, e.g. 0x1::Dfinance::USD +- `-g` / `--gas` Limitation of gas consumption per operation. A positive integer is expected +- `-u` / `--url` The url of the substrate node to query [default: ws://localhost:9944]. HTTP, HTTPS, WS protocols are supported. It is recommended to use WS. When using HTTP or HTTPS, you cannot get the publication status. +- `--account` Account from whom to publish. Address or test account name or name wallet key. Example: //Alice, alice, bob, NAME_WALLET_KEY... or 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY. When used in combination with `--secret` is ignored. +- `-s` / `--secret` Secret phrase. If a secret phrase is specified, you do not need to specify. + Example: ```shell script dove call 'store_u64(60)' @@ -176,8 +189,8 @@ dove key delete --all $ dove deploy [FILE_NAME|PATH_TO_FILE] [OPTIONS] ``` ### Input parameters -- [FILE_NAME] - Name of module or package to be published. -- [PATH_TO_FILE] - Path to the file to be published. Expected file extension: +- `[FILE_NAME]` - Name of module or package to be published. +- `[PATH_TO_FILE]` - Path to the file to be published. Expected file extension: - `pac` bundle - `mv` module - `mvt` transaction diff --git a/dove/src/call/cmd.rs b/dove/src/call/cmd.rs index 4b0d63bd..a3db5077 100644 --- a/dove/src/call/cmd.rs +++ b/dove/src/call/cmd.rs @@ -15,11 +15,11 @@ Examples: 'script_name()' 'Module::function()' 'ALIAS_ADDRESSES::Module::function()' - '0x1::Module::function' --parameters [10,10] true ALIAS_ADDRESSES 100 0x1 --type 0x01::Dfinance::USD + '0x1::Module::function' --args [10,10] true ALIAS_ADDRESSES 100 0x1 --type 0x01::Dfinance::USD "#)] call: String, #[structopt( - help = r#"Script type parametrs, e.g. 0x1::Dfinance::USD"#, + help = r#"Script type parameters, e.g. 0x1::Dfinance::USD"#, name = "Script type parameters.", long = "type", short = "t" diff --git a/dove/src/cmd/call.rs b/dove/src/cmd/call.rs index d20a9d90..f5001e98 100644 --- a/dove/src/cmd/call.rs +++ b/dove/src/cmd/call.rs @@ -1,8 +1,10 @@ -use structopt::StructOpt; use std::fmt::Debug; use std::fs; -use std::path::Path; +use std::path::{Path, PathBuf}; + +use structopt::StructOpt; use anyhow::{Error, Result}; + use lang::bytecode::accessor::BytecodeRef; use crate::cmd::deploy::run_dove_package_build; use crate::context::Context; @@ -10,34 +12,47 @@ use crate::call::cmd::CallDeclarationCmd; use crate::call::fn_call::Config; use crate::call::make_transaction; use crate::call::model::{EnrichedTransaction, Transaction}; +use crate::publish::{NodeAccessParams, Publish}; #[derive(StructOpt, Debug)] #[structopt(setting(structopt::clap::AppSettings::ColoredHelp))] #[structopt(usage = "dove call [call] [OPTIONS]\n Examples: $ dove call 'script_name<0x01::Dfinance::USD>([10,10], true, ADDRESS_ALIAS, SS58_ADDRESS, 100, 0x1)' - $ dove call 'script_name()' --parameters [10,10] true ADDRESS_ALIAS SS58_ADDRESS 100 0x1 --type 0x01::Dfinance::USD + $ dove call 'script_name()' --args [10,10] true ADDRESS_ALIAS SS58_ADDRESS 100 0x1 --type 0x01::Dfinance::USD $ dove call '0x1::Module::script_name<0x01::Dfinance::USD>()' + $ dove call 'script_name()' --account WALLET_KEY --gas 300 + $ dove call 'script_name()' --secret --url https://127.0.0.1:9933 --gas 400 + $ dove call 'script_name()' --account //Alice --gas 300 ")] pub struct ExecuteTransaction { #[structopt(flatten)] call: CallDeclarationCmd, - #[structopt(help = "Output file name.", long = "output", short = "o")] - output: Option, + #[structopt(flatten)] + request: NodeAccessParams, } impl ExecuteTransaction { pub fn apply(&mut self, ctx: &mut Context) -> Result<()> { run_dove_package_build(ctx)?; let tx = make_transaction(ctx, self.call.take(), Config::for_tx())?; - let output_filename = self.output.as_ref().take(); - match tx { + let path_transaction = match tx { EnrichedTransaction::Local { .. } => unreachable!(), EnrichedTransaction::Global { bi, tx, name } => { - store_transaction(ctx, output_filename.unwrap_or(&name), bi.bytecode_ref(), tx) + store_transaction(ctx, &name, bi.bytecode_ref(), tx)? } + }; + + if !self.request.need_to_publish() { + return Ok(()); } + + Publish::try_from((&self.request, path_transaction))? + .apply() + .map(|address| { + println!("Address: {}", address); + }) } } @@ -46,7 +61,7 @@ fn store_transaction( name: &str, rf: &BytecodeRef, tx: Transaction, -) -> Result<(), Error> { +) -> Result { let tx_dir = ctx.tx_output_path(get_package_from_path(&rf.0)); if !tx_dir.exists() { fs::create_dir_all(&tx_dir)?; @@ -61,7 +76,9 @@ fn store_transaction( fs::remove_file(&tx_file)?; } println!("Store transaction: {:?}", tx_file); - Ok(fs::write(&tx_file, bcs::to_bytes(&tx)?)?) + fs::write(&tx_file, bcs::to_bytes(&tx)?)?; + + Ok(tx_file) } fn get_package_from_path>(path: A) -> Option { diff --git a/dove/src/cmd/run.rs b/dove/src/cmd/run.rs index 218a0d50..f93fc1ce 100644 --- a/dove/src/cmd/run.rs +++ b/dove/src/cmd/run.rs @@ -25,11 +25,11 @@ use crate::call::model::EnrichedTransaction; #[structopt(usage = "dove run [call] [OPTIONS]\n Examples: $ dove run 'script_name([10,10], true, 100, 0x1, ADDRESS_ALIAS, SS58_ADDRESS)' - $ dove run script_name --parameters [10,10] true 100 0x1 ADDRESS_ALIAS SS58_ADDRESS + $ dove run script_name --args [10,10] true 100 0x1 ADDRESS_ALIAS SS58_ADDRESS $ dove run 'script_name()' $ dove run 'Module::function()' $ dove run '0x1::Module::function()' - $ dove run '0x1::Module::function' --parameters [10,10] true ALIAS_ADDRESSES SS58_ADDRESS 100 0x1 --type '0x01::Dfinance::USD' + $ dove run '0x1::Module::function' --args [10,10] true ALIAS_ADDRESSES SS58_ADDRESS 100 0x1 --type '0x01::Dfinance::USD' ")] pub struct Run { #[structopt(flatten)] diff --git a/dove/tests/test_cmd_dove_call.rs b/dove/tests/test_cmd_dove_call.rs index d9526858..1a252ec4 100644 --- a/dove/tests/test_cmd_dove_call.rs +++ b/dove/tests/test_cmd_dove_call.rs @@ -10,8 +10,18 @@ fn test_cmd_dove_call() { let project_name = "project_call"; let project_folder = new_demo_project(project_name).unwrap(); - for call in ["main()", "one_param(true)", "two_params(1,1)"] { + for (name, call) in [ + ("main", "main()"), + ("one_param", "one_param(true)"), + ("two_params", "two_params(1,1)"), + ] { dove(&["call", call], &project_folder).unwrap(); + let tx_path = project_folder + .join("build") + .join("for_tests") + .join("transaction") + .join(format!("{}.mvt", name)); + assert!(tx_path.exists()); } delete_project(&project_folder).unwrap(); } @@ -52,28 +62,3 @@ fn test_cmd_dove_call_with_type() { delete_project(&project_folder).unwrap(); } - -/// Output path -/// $ dove call 'main()' -o tmpname -#[test] -fn test_cmd_dove_call_output() { - let project_name = "project_call_output"; - let project_folder = new_demo_project(project_name).unwrap(); - - for (name, args) in [ - ("main", vec!["call", "main()"]), - ("tmpname", vec!["call", "main()", "-o", "tmpname"]), - ] { - dove(&args, &project_folder).unwrap(); - let tx_path = project_folder - .join("build") - .join("for_tests") - .join("transaction") - .join(format!("{}.mvt", name)); - - println!("{}", &tx_path.display()); - assert!(tx_path.exists()); - } - - delete_project(&project_folder).unwrap(); -}