Skip to content

Commit

Permalink
Merge branch 'main' into 2024-08-29-quote-debug
Browse files Browse the repository at this point in the history
  • Loading branch information
hardyjosh committed Sep 3, 2024
2 parents 2d21eab + cd6dabf commit 21d16ed
Show file tree
Hide file tree
Showing 11 changed files with 1,026 additions and 36 deletions.
4 changes: 2 additions & 2 deletions crates/cli/src/commands/order/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ impl Execute for AddOrderCalldata {
None => None,
};
let order = DotrainOrder::new(dotrain, settings).await?;
let dotrain_string = order.dotrain.clone();
let dotrain_string = order.dotrain().to_string();

let config_deployment = order
.config
.config()
.deployments
.get(&self.deployment)
.ok_or(anyhow!("specified deployment is undefined!"))?;
Expand Down
179 changes: 179 additions & 0 deletions crates/cli/src/commands/order/filter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
use crate::execute::Execute;
use anyhow::{anyhow, Result};
use clap::{ArgAction, Parser};
use rain_orderbook_common::dotrain_order::DotrainOrder;
use std::fs::read_to_string;
use std::path::PathBuf;

/// Generate a new .rain with unused frontmatter cleaned, ie frontmatter will only include the
/// specified deployments (and their related fields) from a given .rain and an optional setting.yml
#[derive(Parser, Clone)]
pub struct Filter {
/// Path to the .rain file
#[arg(short = 'f', long, value_name = "PATH")]
dotrain_file: PathBuf,

/// Path to the settings yaml file
#[arg(short = 'c', long, value_name = "PATH")]
settings_file: Option<PathBuf>,

/// List of deployment keys to include in the output .rain frontmatter
#[arg(short = 'e', long, value_name = "DEPLOYMENT", num_args = 1..)]
deployments: Vec<String>,

/// Optional output file path to write the result into
#[arg(short = 'o', long, value_name = "PATH")]
pub output: Option<PathBuf>,

/// Print the result on console (send result to std out)
#[arg(long, action = ArgAction::SetTrue)]
pub stdout: bool,
}

impl Execute for Filter {
async fn execute(&self) -> Result<()> {
// read inpput files
let dotrain = read_to_string(self.dotrain_file.clone()).map_err(|e| anyhow!(e))?;
let settings = match &self.settings_file {
Some(settings_file) => {
Some(read_to_string(settings_file.clone()).map_err(|e| anyhow!(e))?)
}
None => None,
};

// generate new dotrain order instance with cleaned up frontmatter
let order = DotrainOrder::new_with_frontmatter_filtered_by_deployment(
dotrain,
settings,
&self
.deployments
.iter()
.map(String::as_str)
.collect::<Vec<&str>>(),
)
.await?;

// handle output
if let Some(output) = &self.output {
std::fs::write(output, order.dotrain())?;
}
if self.stdout {
println!("{}", order.dotrain());
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;

#[test]
fn verify_cli() {
Filter::command().debug_assert();
}

#[tokio::test]
async fn test_execute_happy() {
let setting = r#"
networks:
some-network:
rpc: https://abcd.com
chain-id: 123
network-id: 123
currency: ETH
subgraphs:
some-sg: https://www.some-sg.com
deployers:
some-deployer:
network: some-network
address: 0xF14E09601A47552De6aBd3A0B165607FaFd2B5Ba
orderbooks:
some-orderbook:
address: 0xc95A5f8eFe14d7a20BD2E5BAFEC4E71f8Ce0B9A6
network: some-network
subgraph: some-sg
"#;
let dotrain = r#"
tokens:
token1:
network: some-network
address: 0xc2132d05d31c914a87c6611c10748aeb04b58e8f
decimals: 6
label: T1
symbol: T1
token2:
network: some-network
address: 0x8f3cf7ad23cd3cadbd9735aff958023239c6a063
decimals: 18
label: T2
symbol: T2
scenarios:
some-scenario:
network: some-network
deployer: some-deployer
scenarios:
child-scenario:
bindings:
key1: value1
orders:
some-order:
inputs:
- token: token1
vault-id: 1
outputs:
- token: token1
vault-id: 1
deployer: some-deployer
orderbook: some-orderbook
deployments:
some-deployment:
scenario: some-scenario.child-scenario
order: some-order
---
#calculate-io
_ _: 0 0;
#handle-io
:;
#handle-add-order
:;"#;

let dotrain_path = "./test_dotrain_filter.rain";
let settings_path = "./test_settings_filter.yml";
std::fs::write(dotrain_path, dotrain).unwrap();
std::fs::write(settings_path, setting).unwrap();

let filter = Filter {
dotrain_file: dotrain_path.into(),
settings_file: Some(settings_path.into()),
deployments: vec!["some-deployment".to_string()],
output: None,
stdout: true,
};

assert!(filter.execute().await.is_ok());

std::fs::remove_file(dotrain_path).unwrap();
std::fs::remove_file(settings_path).unwrap();
}

#[tokio::test]
async fn test_execute_unhappy() {
let filter = Filter {
dotrain_file: "./bad-path/test.rain".into(),
settings_file: None,
deployments: vec!["some-deployment".to_string()],
output: None,
stdout: true,
};

assert!(filter.execute().await.is_err());
}
}
6 changes: 6 additions & 0 deletions crates/cli/src/commands/order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod add;
mod calldata;
mod compose;
mod detail;
mod filter;
mod list;
mod orderbook_address;
mod remove;
Expand All @@ -13,6 +14,7 @@ use anyhow::Result;
use calldata::AddOrderCalldata;
use clap::Parser;
use compose::Compose;
use filter::Filter;

use detail::CliOrderDetailArgs;
use list::CliOrderListArgs;
Expand Down Expand Up @@ -46,6 +48,9 @@ pub enum Order {
alias = "ob-addr"
)]
OrderbookAddress(OrderbookAddress),

#[command()]
Filter(Filter),
}

impl Execute for Order {
Expand All @@ -58,6 +63,7 @@ impl Execute for Order {
Order::Compose(compose) => compose.execute().await,
Order::Calldata(calldata) => calldata.execute().await,
Order::OrderbookAddress(orderbook_address) => orderbook_address.execute().await,
Order::Filter(filter) => filter.execute().await,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/cli/src/commands/order/orderbook_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::execute::Execute;
use crate::output::{output, SupportedOutputEncoding};
use anyhow::{anyhow, Result};
use clap::Parser;
use rain_orderbook_app_settings::Config;
use rain_orderbook_common::dotrain_order::DotrainOrder;
use std::fs::read_to_string;
use std::path::PathBuf;
Expand Down Expand Up @@ -36,7 +35,7 @@ impl Execute for OrderbookAddress {
None => None,
};
let order = DotrainOrder::new(dotrain, settings).await?;
let order_config: Config = order.clone().config;
let order_config = order.config().clone();
let deployment_ref = order_config
.deployments
.get(&self.deployment)
Expand Down
Loading

0 comments on commit 21d16ed

Please sign in to comment.