From 069dc8374e7bda1cc7f6de2b21e600f4d04b9c78 Mon Sep 17 00:00:00 2001 From: Ajinkya Kulkarni Date: Tue, 25 Feb 2025 15:09:28 +0100 Subject: [PATCH] cleanup --- crates/utils/cw-client/src/cli.rs | 36 ++++++------ crates/utils/cw-client/src/grpc.rs | 4 +- crates/utils/cw-client/src/lib.rs | 2 +- crates/utils/tcbinfo-updater/src/main.rs | 73 +++++++++++++----------- 4 files changed, 61 insertions(+), 54 deletions(-) diff --git a/crates/utils/cw-client/src/cli.rs b/crates/utils/cw-client/src/cli.rs index c7c5b79..5338c90 100644 --- a/crates/utils/cw-client/src/cli.rs +++ b/crates/utils/cw-client/src/cli.rs @@ -1,11 +1,11 @@ -use std::process::Command; -use serde_json::Value; +use crate::CwClient; use color_eyre::{eyre::eyre, Help, Report, Result}; use cosmrs::{tendermint::chain::Id, AccountId}; +use quoted_string::strip_dquotes; use reqwest::Url; use serde::de::DeserializeOwned; -use quoted_string::strip_dquotes; -use crate::CwClient; +use serde_json::Value; +use std::process::Command; #[derive(Clone, Debug)] pub enum CliClientType { @@ -47,7 +47,7 @@ impl CliClient { } } - fn new_command(&self) -> Result { + fn new_command(&self) -> Result { let bin = self.kind.bin(); if !self.is_bin_available(&bin) { return Err(eyre!("Binary '{}' not found in PATH", bin)).suggestion(format!( @@ -57,29 +57,31 @@ impl CliClient { } Ok(Command::new(self.kind.bin())) - } - - pub fn neutrond( url: Url) -> Self { - let mut command = Command::new("neutrond"); + } + + pub fn neutrond(url: Url) -> Self { + let mut command = Command::new("neutrond"); let command = command .arg("q") .arg("feemarket") .arg("gas-price") .arg("untrn") - .arg ("-o") + .arg("-o") .arg("json"); let output = command.output().expect("command output failed").stdout; - let output_str: String = std::str::from_utf8(&output).expect("unable to parse command output").to_string(); + let output_str: String = std::str::from_utf8(&output) + .expect("unable to parse command output") + .to_string(); let json: Value = serde_json::from_str(&output_str).expect("could not convert to JSON"); let amount = &json["price"]["amount"].to_string(); - + let price = strip_dquotes(amount) .expect("strip dquotes failed") .parse::() .expect("couldn't parse gas price"); - + let gas_price = format!("{}untrn", price); - + Self { kind: CliClientType::Neutrond, url, @@ -181,10 +183,9 @@ impl CwClient for CliClient { .args(["tx", "wasm"]) .args(["execute", contract.as_ref(), &msg.to_string()]) .args(["--gas", "auto"]) - .args(["--gas-prices", &self.gas_price]) + .args(["--gas-prices", &self.gas_price]) .args(["--from", sender]) - .args(["--gas-adjustment", "1.3"]) - + .args(["--gas-adjustment", "1.3"]) .args(["--output", "json"]) .arg("-y"); let output = command.output()?; @@ -289,4 +290,3 @@ impl CwClient for CliClient { Ok((trusted_height, trusted_hash)) } } - diff --git a/crates/utils/cw-client/src/grpc.rs b/crates/utils/cw-client/src/grpc.rs index e8d8bf0..59fbb5d 100644 --- a/crates/utils/cw-client/src/grpc.rs +++ b/crates/utils/cw-client/src/grpc.rs @@ -87,7 +87,7 @@ impl CwClient for GrpcClient { chain_id: &TmChainId, gas: u64, _sender: &str, - msg: M + msg: M, ) -> Result { let tm_pubkey = self.sk.public_key(); let sender = tm_pubkey @@ -256,7 +256,7 @@ mod tests { 2000000, "/* unused since we're getting the account from the sk */", json!([]), - "11000untrn", + ) .await?; println!("{}", tx_hash); diff --git a/crates/utils/cw-client/src/lib.rs b/crates/utils/cw-client/src/lib.rs index 3141457..c6979c7 100644 --- a/crates/utils/cw-client/src/lib.rs +++ b/crates/utils/cw-client/src/lib.rs @@ -36,7 +36,7 @@ pub trait CwClient { chain_id: &Id, gas: u64, sender: &str, - msg: M + msg: M, ) -> Result; fn deploy( diff --git a/crates/utils/tcbinfo-updater/src/main.rs b/crates/utils/tcbinfo-updater/src/main.rs index 36e53c7..af3345c 100644 --- a/crates/utils/tcbinfo-updater/src/main.rs +++ b/crates/utils/tcbinfo-updater/src/main.rs @@ -1,12 +1,12 @@ use cw_client::{CliClient, CwClient}; -use quartz_tcbinfo_msgs::{QueryMsg, ExecuteMsg}; +use quartz_tcbinfo_msgs::{ExecuteMsg, QueryMsg}; use quoted_string::strip_dquotes; - use quoted_string::test_utils::TestSpec; +use quoted_string::test_utils::TestSpec; use quoted_string::to_content; use reqwest::Url; use serde_json::{json, Value}; use std::env; -use tendermint::chain::id::Id; +use tendermint::chain::id::Id; type Fmspc = String; type Update = String; @@ -41,35 +41,44 @@ async fn get_fmspc_list() -> Vec { fmspc_list } -async fn upsert_tcbinfo(url: &str, contract_addr: &str, chain_id : Id, sender: &str, gas: u64) -> Result<(), &'static str> { - +async fn upsert_tcbinfo( + url: &str, + contract_addr: &str, + chain_id: Id, + sender: &str, + gas: u64, +) -> Result<(), &'static str> { let network = Url::parse(url).expect("couldn't parse network URL"); let client = CliClient::neutrond(network); let fmspc_list = get_fmspc_list().await; for fmspc in fmspc_list { - let tcbinfo_from_api = get_tcbinfo(fmspc.clone(), "standard".to_string()).await; - + // assert!(verify_signature(tcbinfo.clone(), key)); - let contract_address = contract_addr + let contract_address = contract_addr .parse() .expect("failed to parse contract address"); - let query_msg = QueryMsg::GetTcbInfo { fmspc: fmspc.clone() }; - - let tcbinfo_on_chain = { if let Ok(res) = client.query_smart::(&contract_address, json!(query_msg)).await { - to_content::(&res["data"]["tcb_info"].to_string()).unwrap().to_string() - } else { - "".to_string() - } - - + let query_msg = QueryMsg::GetTcbInfo { + fmspc: fmspc.clone(), }; - + + let tcbinfo_on_chain = { + if let Ok(res) = client + .query_smart::(&contract_address, json!(query_msg)) + .await + { + to_content::(&res["data"]["tcb_info"].to_string()) + .unwrap() + .to_string() + } else { + "".to_string() + } + }; + if tcbinfo_on_chain != tcbinfo_from_api { println!("updating on-chain TCBInfo for FMSPC: {fmspc}"); - let execute_msg = ExecuteMsg { tcb_info: tcbinfo_from_api.to_string(), certificate: TCB_SIGNER.to_string(), @@ -79,9 +88,9 @@ async fn upsert_tcbinfo(url: &str, contract_addr: &str, chain_id : Id, sender: & .tx_execute( &contract_address, &chain_id, - gas, + gas, sender, - json!(execute_msg) + json!(execute_msg), ) .await; println!("{res:?}"); @@ -98,18 +107,16 @@ pub async fn main() { let args: Vec = env::args().collect(); let url = &args[1]; let chain_id = &args[2]; - let contract_address : &str = &args[3]; + let contract_address: &str = &args[3]; let sender: &str = &args[4]; let gas: &u64 = &args[5].parse().expect("gas must be a u64 value"); - upsert_tcbinfo(url, contract_address, Id::try_from(chain_id.clone()).expect("invalid chain id"), sender, *gas).await.expect("TCBInfo update failed"); + upsert_tcbinfo( + url, + contract_address, + Id::try_from(chain_id.clone()).expect("invalid chain id"), + sender, + *gas, + ) + .await + .expect("TCBInfo update failed"); } - -/*let url =; - let chain_id = tendermint::chain::id::Id::try_from("pion-1").expect("invalid chain id"); -contract_address = ; -let sender = ; -gas = 1000000, -fees = , - */ - -