Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
[v1.6] dove call: create and publish transactions (#209)
Browse files Browse the repository at this point in the history
* $ dove call

* README.md + hints

* rebase [master]

* updating "dove call" tests
  • Loading branch information
vladimirovmm authored Feb 16, 2022
1 parent 97ed9b6 commit 03b38de
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 43 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)'
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions dove/src/call/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 27 additions & 10 deletions dove/src/cmd/call.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
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;
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<String>,
#[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);
})
}
}

Expand All @@ -46,7 +61,7 @@ fn store_transaction(
name: &str,
rf: &BytecodeRef,
tx: Transaction,
) -> Result<(), Error> {
) -> Result<PathBuf, Error> {
let tx_dir = ctx.tx_output_path(get_package_from_path(&rf.0));
if !tx_dir.exists() {
fs::create_dir_all(&tx_dir)?;
Expand All @@ -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<A: AsRef<Path>>(path: A) -> Option<String> {
Expand Down
4 changes: 2 additions & 2 deletions dove/src/cmd/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
37 changes: 11 additions & 26 deletions dove/tests/test_cmd_dove_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}

0 comments on commit 03b38de

Please sign in to comment.