Skip to content

Commit

Permalink
Add offline option for proposal cmds
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita.m committed May 20, 2020
1 parent 61b538f commit c07ebfb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ readme = "README.md"
license-file = "LICENSE.md"
keywords = ["TON", "SDK", "smart contract", "tonlabs"]
edition = "2018"
version = "0.1.5"
version = "0.1.6"

[dependencies]
base64 = "0.10.1"
Expand Down
31 changes: 27 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,16 @@ fn main_internal() -> Result <(), String> {
(@arg DEST: +required +takes_value "Address of proposal contract.")
(@arg COMMENT: +required +takes_value "Proposal description (max symbols 382).")
(@arg KEYS: +required +takes_value "Seed phrase or path to keypair file.")
(@arg OFFLINE: -f --offline "Prints signed message to terminal instead of sending it.")
(@arg LIFETIME: -l --lifetime +takes_value "Period of time in seconds while message is valid.")
)
(@subcommand vote =>
(about: "Confirms proposal transaction in multisignature wallet.")
(@arg ADDRESS: +required +takes_value "Address of multisignature wallet.")
(@arg ID: +required +takes_value "Proposal transaction id.")
(@arg KEYS: +required +takes_value "Seed phrase or path to keypair file.")
(@arg OFFLINE: -f --offline "Prints signed message to terminal instead of sending it.")
(@arg LIFETIME: -l --lifetime +takes_value "Period of time in seconds while message is valid.")
)
(@subcommand decode =>
(about: "Prints comment string from proposal transaction.")
Expand Down Expand Up @@ -547,17 +551,36 @@ fn proposal_create_command(matches: &ArgMatches, config: Config) -> Result<(), S
let dest = matches.value_of("DEST");
let keys = matches.value_of("KEYS");
let comment = matches.value_of("COMMENT");
print_args!(matches, address, comment, keys);
create_proposal(config, address.unwrap(), keys, dest.unwrap(), comment.unwrap())
let lifetime = matches.value_of("LIFETIME");
let offline = matches.is_present("OFFLINE");
print_args!(matches, address, comment, keys, lifetime);

let lifetime = lifetime.map(|val| {
u32::from_str_radix(val, 10)
.map_err(|e| format!("failed to parse lifetime: {}", e))
})
.transpose()?
.unwrap_or(config.timeout);

create_proposal(config, address.unwrap(), keys, dest.unwrap(), comment.unwrap(), lifetime, offline)
}

fn proposal_vote_command(matches: &ArgMatches, config: Config) -> Result<(), String> {
let address = matches.value_of("ADDRESS");
let keys = matches.value_of("KEYS");
let id = matches.value_of("ID");
print_args!(matches, address, id, keys);
vote(config, address.unwrap(), keys, id.unwrap())
let lifetime = matches.value_of("LIFETIME");
let offline = matches.is_present("OFFLINE");
print_args!(matches, address, id, keys, lifetime);

let lifetime = lifetime.map(|val| {
u32::from_str_radix(val, 10)
.map_err(|e| format!("failed to parse lifetime: {}", e))
})
.transpose()?
.unwrap_or(config.timeout);

vote(config, address.unwrap(), keys, id.unwrap(), lifetime, offline)
}

fn proposal_decode_command(matches: &ArgMatches, config: Config) -> Result<(), String> {
Expand Down
74 changes: 53 additions & 21 deletions src/voting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ pub fn create_proposal(
keys: Option<&str>,
dest: &str,
text: &str,
lifetime: u32,
offline: bool,
) -> Result<(), String> {

let msg_body: serde_json::Value = serde_json::from_str(
Expand All @@ -184,39 +186,69 @@ pub fn create_proposal(
"bounce": false,
"allBalance": false,
"payload": body_base64,
});
}).to_string();

call::call_contract(
conf,
addr,
MSIG_ABI.to_string(),
"submitTransaction",
&serde_json::to_string(&params).unwrap(),
keys.map(|s| s.to_owned()),
false
)
let keys = keys.map(|s| s.to_owned());

if offline {
call::generate_message(
conf,
addr,
MSIG_ABI.to_string(),
"submitTransaction",
&params,
keys,
lifetime)
} else {

call::call_contract(
conf,
addr,
MSIG_ABI.to_string(),
"submitTransaction",
&params,
keys,
false
)
}
}

pub fn vote(
conf: Config,
addr: &str,
keys: Option<&str>,
trid: &str,
lifetime: u32,
offline: bool,
) -> Result<(), String> {

let params = json!({
"transactionId": trid,
});
}).to_string();

call::call_contract(
conf,
addr,
MSIG_ABI.to_string(),
"confirmTransaction",
&serde_json::to_string(&params).unwrap(),
keys.map(|s| s.to_owned()),
false
)
let keys = keys.map(|s| s.to_owned());

if offline {
call::generate_message(
conf,
addr,
MSIG_ABI.to_string(),
"confirmTransaction",
&params,
keys,
lifetime
)
} else {
call::call_contract(
conf,
addr,
MSIG_ABI.to_string(),
"confirmTransaction",
&params,
keys,
false
)
}
}

pub fn decode_proposal(
Expand All @@ -235,7 +267,7 @@ pub fn decode_proposal(
true
)?;

let txns = result["output"]["transactions"].as_array()
let txns = result["transactions"].as_array()
.ok_or(format!(r#"failed to decode result: "transactions" array not found"#))?;

for txn in txns {
Expand Down

0 comments on commit c07ebfb

Please sign in to comment.