Skip to content

Commit

Permalink
Merge pull request #127 from leeyr338/support-estimate_gas
Browse files Browse the repository at this point in the history
Supports JSON-RPC interface: estimateQuota.
  • Loading branch information
leeyr338 authored Oct 25, 2019
2 parents 60fc9fa + ececce3 commit a38fb40
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
39 changes: 39 additions & 0 deletions cita-cli/src/cli/rpc_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,39 @@ pub fn rpc_command() -> App<'static, 'static> {
.subcommand(
SubCommand::with_name("getVersion").about("Get release version info of all modules"),
)
.subcommand(
SubCommand::with_name("estimateQuota")
.about("Estimate a transaction's quota used.")
.arg(
Arg::with_name("from")
.long("from")
.validator(|address| parse_address(address.as_str()))
.takes_value(true)
.help("From address"),
)
.arg(
Arg::with_name("to")
.long("to")
.validator(|address| parse_address(address.as_str()))
.takes_value(true)
.required(true)
.help("To address"),
)
.arg(
Arg::with_name("data")
.long("data")
.takes_value(true)
.help("The data"),
)
.arg(
Arg::with_name("height")
.long("height")
.takes_value(true)
.validator(|s| parse_height(s.as_str()))
.default_value("latest")
.help("The block number"),
),
)
}

/// RPC processor
Expand Down Expand Up @@ -605,6 +638,12 @@ pub fn rpc_processor(
("getVersion", _) => {
<Client as ClientExt<JsonRpcResponse, ToolError>>::get_version(&client)
}
("estimateQuota", Some(m)) => client.estimate_quota(
m.value_of("from"),
m.value_of("to").unwrap(),
m.value_of("data"),
m.value_of("height").unwrap(),
),
_ => {
return Err(sub_matches.usage().to_owned());
}
Expand Down
53 changes: 49 additions & 4 deletions cita-tool/src/client/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ const GET_STORAGE_AT: &str = "getStorageAt";

const GET_VERSION: &str = "getVersion";

const ESTIMATE_QUOTA: &str = "estimateQuota";

/// Store action target address
pub const STORE_ADDRESS: &str = "0xffffffffffffffffffffffffffffffffff010000";
/// StoreAbi action target address
Expand Down Expand Up @@ -520,6 +522,7 @@ impl Default for Client {
/// * getStateProof
/// * getStorageAt
/// * getVersion
/// * estimateQuota
pub trait ClientExt<T, E>
where
T: serde::Serialize + serde::Deserialize<'static> + ::std::fmt::Display,
Expand Down Expand Up @@ -587,6 +590,14 @@ where
fn get_storage_at(&self, address: &str, key: &str, height: &str) -> Result<T, E>;
/// getVersion: Get release version info of all modules
fn get_version(&self) -> Result<T, E>;
/// estimateQuota: Estimate a transaction's quota used
fn estimate_quota(
&self,
from: Option<&str>,
to: &str,
data: Option<&str>,
height: &str,
) -> Result<T, E>;
}

impl ClientExt<JsonRpcResponse, ToolError> for Client {
Expand Down Expand Up @@ -718,16 +729,16 @@ impl ClientExt<JsonRpcResponse, ToolError> for Client {
let mut object = HashMap::new();

object.insert(String::from("to"), ParamsValue::String(String::from(to)));
if from.is_some() {
if let Some(from) = from {
object.insert(
String::from("from"),
ParamsValue::String(String::from(from.unwrap())),
ParamsValue::String(String::from(from)),
);
}
if data.is_some() {
if let Some(data) = data {
object.insert(
String::from("data"),
ParamsValue::String(String::from(data.unwrap())),
ParamsValue::String(String::from(data)),
);
}

Expand Down Expand Up @@ -972,6 +983,40 @@ impl ClientExt<JsonRpcResponse, ToolError> for Client {
JsonRpcParams::new().insert("method", ParamsValue::String(String::from(GET_VERSION)));
Ok(self.send_request(vec![params].into_iter())?.pop().unwrap())
}

fn estimate_quota(
&self,
from: Option<&str>,
to: &str,
data: Option<&str>,
height: &str,
) -> Result<JsonRpcResponse, ToolError> {
let mut object = HashMap::new();

object.insert(String::from("to"), ParamsValue::String(String::from(to)));
if let Some(from) = from {
object.insert(
String::from("from"),
ParamsValue::String(String::from(from)),
);
}
if let Some(data) = data {
object.insert(
String::from("data"),
ParamsValue::String(String::from(data)),
);
}

let param = ParamsValue::List(vec![
ParamsValue::Map(object),
ParamsValue::String(String::from(height)),
]);
let params = JsonRpcParams::new()
.insert("method", ParamsValue::String(String::from(ESTIMATE_QUOTA)))
.insert("params", param);

Ok(self.send_request(vec![params].into_iter())?.pop().unwrap())
}
}

/// Store data or contract ABI to chain
Expand Down

0 comments on commit a38fb40

Please sign in to comment.