Skip to content

Commit

Permalink
Merge pull request #17 from tonlabs/getconfig-cmd
Browse files Browse the repository at this point in the history
Add `getconfig` cmd
  • Loading branch information
AlexeyVavilin authored May 19, 2020
2 parents 34eddea + 6a0d82d commit a9c0f92
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 1 deletion.
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.3"
version = "0.1.4"

[dependencies]
base64 = "0.10.1"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ By default, tonos-cli connects to `https://net.ton.dev` network.

tonos-cli getkeypair <keyfile.json> "<seed_phrase>"

### Query commands:

### 1) Get global config

tonos-cli getconfig <index>

### Smart contract commands:

### 1) Generate Contract Address
Expand Down
240 changes: 240 additions & 0 deletions src/getconfig.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
/*
* Copyright 2018-2020 TON DEV SOLUTIONS LTD.
*
* Licensed under the SOFTWARE EVALUATION License (the "License"); you may not use
* this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific TON DEV software governing permissions and
* limitations under the License.
*/
use crate::config::Config;
use serde_json::json;
use ton_client_rs::{TonClient, OrderBy, SortDirection};

const QUERY_FIELDS: &str = r#"
master {
config {
p0
p1
p2
p3
p4
p6 {
mint_new_price
mint_add_price
}
p7 {
currency
value
}
p8 {
version
capabilities
}
p9
p10
p12 {
workchain_id
enabled_since
actual_min_split
min_split
max_split
active
accept_msgs flags
zerostate_root_hash
zerostate_file_hash
version
basic
vm_version
vm_mode
min_addr_len
max_addr_len
addr_len_step
workchain_type_id
}
p14 {
masterchain_block_fee
basechain_block_fee
}
p15 {
validators_elected_for
elections_start_before
elections_end_before
stake_held_for
}
p16 {
max_validators
max_main_validators
min_validators
}
p17 {
min_stake(format:DEC)
max_stake(format:DEC)
min_total_stake(format:DEC)
max_stake_factor
}
p20 {
gas_price
gas_limit
special_gas_limit
gas_credit
block_gas_limit
freeze_due_limit
delete_due_limit
flat_gas_limit
flat_gas_price
}
p21 {
gas_price
gas_limit
special_gas_limit
gas_credit
block_gas_limit
freeze_due_limit
delete_due_limit
flat_gas_limit
flat_gas_price
}
p24 {
lump_price bit_price cell_price ihr_price_factor first_frac next_frac
}
p25 {
lump_price bit_price cell_price ihr_price_factor first_frac next_frac
}
p28 {
shuffle_mc_validators
mc_catchain_lifetime
shard_catchain_lifetime
shard_validators_lifetime
shard_validators_num
}
p29 {
new_catchain_ids
round_candidates
next_candidate_delay_ms
consensus_timeout_ms
fast_attempts
attempt_duration
catchain_max_deps
max_block_bytes
max_collated_bytes
}
p31
p32 {
utime_since
utime_until
total
total_weight(format:DEC)
list {
public_key
adnl_addr
weight(format:DEC)
}
}
p33 {
utime_since
utime_until
total
total_weight(format:DEC)
list {
public_key
adnl_addr
weight(format:DEC)
}
}
p34 {
utime_since
utime_until
total
total_weight(format:DEC)
list {
public_key
adnl_addr
weight(format:DEC)
}
}
p35 {
utime_since
utime_until
total
total_weight(format:DEC)
list {
public_key
adnl_addr
weight(format:DEC)
}
}
p36 {
utime_since
utime_until
total
total_weight(format:DEC)
list {
public_key
adnl_addr
weight(format:DEC)
}
}
p37 {
utime_since
utime_until
total
total_weight(format:DEC)
list {
public_key
adnl_addr
weight(format:DEC)
}
}
p39 {
adnl_addr
temp_public_key
seqno
valid_until
signature_r
signature_s
}
}
}
"#;

pub fn query_global_config(conf: Config, index: &str) -> Result<(), String> {
let ton = TonClient::new_with_base_url(&conf.url)
.map_err(|e| format!("failed to create tonclient: {}", e.to_string()))?;

let _i = i32::from_str_radix(index, 10)
.map_err(|e| format!(r#"failed to parse "index": {}"#, e))?;

let config_name = format!("p{}", index);

println!("Quering...");
let last_key_block_query = ton.queries.blocks.query(
"{}",
"id prev_key_block_seqno",
Some(OrderBy{ path: "seq_no".to_owned(), direction: SortDirection::Descending }),
Some(1),
).map_err(|e| format!("failed to query last key block: {}", e.to_string()))?;

let config_query = ton.queries.blocks.query(
&json!({
"seq_no": {
"eq": last_key_block_query[0]["prev_key_block_seqno"].as_u64().unwrap()
},
"workchain_id": {
"eq": -1
}
}).to_string(),
QUERY_FIELDS,
None,
None,
).map_err(|e| format!("failed to query master block config: {}", e.to_string()))?;

let config = &config_query[0]["master"]["config"][&config_name];
let config_str = serde_json::to_string_pretty(&config)
.map_err(|e| format!("failed to parse config body from sdk: {}", e))?;
println!("Config {}: {}", config_name, config_str);
Ok(())
}
15 changes: 15 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod deploy;
mod genaddr;
mod helpers;
mod voting;
mod getconfig;

use account::get_account;
use call::{call_contract, call_contract_with_msg, generate_message, parse_params};
Expand All @@ -35,6 +36,7 @@ use config::{Config, set_config};
use crypto::{generate_mnemonic, extract_pubkey, generate_keypair};
use deploy::deploy_contract;
use genaddr::generate_address;
use getconfig::query_global_config;
use voting::{create_proposal, decode_proposal, vote};

const VERBOSE_MODE: bool = true;
Expand Down Expand Up @@ -233,6 +235,10 @@ fn main_internal() -> Result <(), String> {
(@arg ID: +required +takes_value "Proposal transaction id.")
)
)
(@subcommand getconfig =>
(about: "Reads global configuration parameter with defined index.")
(@arg INDEX: +required +takes_value "Parameter index.")
)
(@setting SubcommandRequired)
).get_matches();

Expand Down Expand Up @@ -290,6 +296,9 @@ fn main_internal() -> Result <(), String> {
return proposal_decode_command(m, conf);
}
}
if let Some(m) = matches.subcommand_matches("getconfig") {
return getconfig_command(m, conf);
}
if let Some(_) = matches.subcommand_matches("version") {
println!(
"tonlabs-cli {}\nCOMMIT_ID: {}\nBUILD_DATE: {}\nCOMMIT_DATE: {}\nGIT_BRANCH: {}",
Expand Down Expand Up @@ -528,4 +537,10 @@ fn proposal_decode_command(matches: &ArgMatches, config: Config) -> Result<(), S
let id = matches.value_of("ID");
print_args!(matches, address, id);
decode_proposal(config, address.unwrap(), id.unwrap())
}

fn getconfig_command(matches: &ArgMatches, config: Config) -> Result<(), String> {
let index = matches.value_of("INDEX");
print_args!(matches, index);
query_global_config(config, index.unwrap())
}

0 comments on commit a9c0f92

Please sign in to comment.