Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli: Responses #81

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions jstz_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ sha2 = "0.9"
rand = "0.8"
tiny-bip39 = "1.0.0"
bincode = "1.3.3"
reqwest = { version = "0.11.22", features = ["json"] }
tokio = { version = "1.33.0", features = ["full"] }

[[bin]]
name = "jstz"
Expand Down
3 changes: 3 additions & 0 deletions jstz_cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub struct Config {
pub octez_node_port: u16,
/// The port of the octez RPC node
pub octez_node_rpc_port: u16,
/// The port of the jstz node
pub jstz_node_port: u16,
/// Sandbox config (None if sandbox is not running)
pub sandbox: Option<SandboxConfig>,
/// List of accounts
Expand Down Expand Up @@ -114,6 +116,7 @@ impl Config {
octez_path: PathBuf::from_str(".").unwrap(),
octez_node_port: 18731,
octez_node_rpc_port: 18730,
jstz_node_port: 8933,
sandbox: None,
accounts: AccountConfig::default(),
}
Expand Down
12 changes: 11 additions & 1 deletion jstz_cli/src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use jstz_proto::operation::{Content, DeployContract, Operation, SignedOperation}

use crate::{
config::Config,
jstz::JstzClient,
octez::OctezClient,
utils::{from_file_or_id, piped_input},
};

pub fn exec(
pub async fn exec(
self_address: Option<String>,
contract_code: Option<String>,
balance: u64,
Expand Down Expand Up @@ -39,6 +40,8 @@ pub fn exec(
op,
);

let hash = signed_op.hash();

println!(
"Signed operation: {}",
serde_json::to_string_pretty(&serde_json::to_value(&signed_op)?)?
Expand All @@ -51,6 +54,13 @@ pub fn exec(
bincode::serialize(&signed_op)?,
)?;

let receipt = JstzClient::new(cfg)
.wait_for_operation_receipt(&hash)
.await?;

println!("Receipt: {:?}", receipt);

cfg.save()?;

Ok(())
}
60 changes: 60 additions & 0 deletions jstz_cli/src/jstz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::time::Duration;

use anyhow::Result;
use jstz_proto::{operation::OperationHash, receipt::Receipt};
use tokio::time::sleep;

use crate::config::Config;

pub struct JstzClient {
endpoint: String,
client: reqwest::Client,
}

impl JstzClient {
pub fn new(cfg: &Config) -> Self {
Self {
endpoint: format!("http://127.0.0.1:{}", cfg.jstz_node_port),
client: reqwest::Client::new(),
}
}

pub async fn get_operation_receipt(
&self,
hash: &OperationHash,
) -> Result<Option<Receipt>> {
let response = self
.get(&format!(
"{}/operations/{}/receipt",
self.endpoint,
hash.to_string()
))
.await?;

if response.status().is_success() {
let receipt = response.json::<Receipt>().await?;

Ok(Some(receipt))
} else {
Ok(None)
}
}

pub async fn wait_for_operation_receipt(
&self,
hash: &OperationHash,
) -> Result<Receipt> {
loop {
if let Some(receipt) = self.get_operation_receipt(hash).await? {
return Ok(receipt);
}

// tokio sleep
sleep(Duration::from_millis(100)).await;
}
}

async fn get(&self, url: &str) -> Result<reqwest::Response> {
Ok(self.client.get(url).send().await?)
}
}
15 changes: 9 additions & 6 deletions jstz_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use anyhow::Result;
use clap::Parser;
use tokio;

mod account;
mod bridge;
mod config;
mod deploy;
mod jstz;
mod octez;
mod repl;
mod run;
mod sandbox;
pub(crate) mod utils;
mod utils;

use config::Config;

Expand Down Expand Up @@ -60,7 +62,7 @@ enum Command {
},
}

fn exec(command: Command, cfg: &mut Config) -> Result<()> {
async fn exec(command: Command, cfg: &mut Config) -> Result<()> {
match command {
Command::Sandbox(sandbox_command) => sandbox::exec(cfg, sandbox_command),
Command::Bridge(bridge_command) => bridge::exec(bridge_command, cfg),
Expand All @@ -69,21 +71,22 @@ fn exec(command: Command, cfg: &mut Config) -> Result<()> {
self_address,
function_code,
balance,
} => deploy::exec(self_address, function_code, balance, cfg),
} => deploy::exec(self_address, function_code, balance, cfg).await,
Command::Run {
url,
referrer,
http_method,
json_data,
} => run::exec(cfg, referrer, url, http_method, json_data),
} => run::exec(cfg, referrer, url, http_method, json_data).await,
Command::Repl { self_address } => repl::exec(self_address, cfg),
}
}

fn main() -> Result<()> {
#[tokio::main]
async fn main() -> Result<()> {
let command = Command::parse();

let mut cfg = Config::load()?;

exec(command, &mut cfg)
exec(command, &mut cfg).await
}
12 changes: 11 additions & 1 deletion jstz_cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use jstz_proto::operation::{Content, Operation, RunContract, SignedOperation};

use crate::{
config::Config,
jstz::JstzClient,
octez::OctezClient,
utils::{from_file_or_id, piped_input},
};

pub fn exec(
pub async fn exec(
cfg: &mut Config,
referrer: Option<String>,
url: String,
Expand Down Expand Up @@ -51,6 +52,8 @@ pub fn exec(
op,
);

let hash = signed_op.hash();

println!(
"Signed operation: {}",
serde_json::to_string_pretty(&serde_json::to_value(&signed_op)?)?
Expand All @@ -63,6 +66,13 @@ pub fn exec(
bincode::serialize(&signed_op)?,
)?;

let receipt = JstzClient::new(cfg)
.wait_for_operation_receipt(&hash)
.await?;

println!("Receipt: {:?}", receipt);

cfg.save()?;

Ok(())
}
Loading