From bb45e4224399be7a842b772d766eefcf1fa2d0e5 Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Wed, 21 Aug 2024 17:59:29 +0530 Subject: [PATCH 01/12] order calldata cli option --- crates/cli/src/commands/order/calldata.rs | 68 +++++++++++++++++++++++ crates/cli/src/commands/order/mod.rs | 7 +++ 2 files changed, 75 insertions(+) create mode 100644 crates/cli/src/commands/order/calldata.rs diff --git a/crates/cli/src/commands/order/calldata.rs b/crates/cli/src/commands/order/calldata.rs new file mode 100644 index 000000000..c7b31a606 --- /dev/null +++ b/crates/cli/src/commands/order/calldata.rs @@ -0,0 +1,68 @@ +use crate::execute::Execute; +use crate::output::{output, SupportedOutputEncoding}; +use anyhow::{anyhow, Result}; +use clap::Args; +use rain_orderbook_common::dotrain_order::DotrainOrder; +use std::fs::read_to_string; +use std::path::PathBuf; +use rain_orderbook_app_settings::{ + Config +}; +use rain_orderbook_common::add_order::AddOrderArgs; +use std::ops::Deref; +use alloy::sol_types::SolCall; + +#[derive(Args, Clone)] +pub struct Calldata { + #[arg( + short = 'f', + long, + help = "Path to the .rain file specifying the order" + )] + dotrain_file: PathBuf, + + #[arg(short = 'c', long, help = "Path to the settings yaml file")] + settings_file: Option, + + #[arg(short = 'e', long, help = "Deployment key to select from frontmatter")] + deployment: String, + + #[arg(short = 'o', long, help = "Output encoding", default_value = "binary")] + encoding: SupportedOutputEncoding, + + #[arg(short, long, help = "RPC URL")] + pub rpc_url: String, + +} + +impl Execute for Calldata { + async fn execute(&self) -> Result<()> { + 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, + }; + let order = DotrainOrder::new(dotrain, settings).await?; + let order_config: Config = order.clone().config; + let dotrain_string: String = order.clone().dotrain; + + let config_deployment = order_config + .deployments + .get(&self.deployment) + .ok_or(anyhow!("specified deployment is undefined!"))?; + + let add_order_args = AddOrderArgs::new_from_deployment(dotrain_string, config_deployment.deref().clone()) + .await; + + let add_order_calldata = add_order_args? + .try_into_call(self.rpc_url.clone()) + .await? + .abi_encode(); + + output(&None, self.encoding.clone(), &add_order_calldata)?; + + Ok(()) + } +} diff --git a/crates/cli/src/commands/order/mod.rs b/crates/cli/src/commands/order/mod.rs index 3bf13fb83..776034087 100644 --- a/crates/cli/src/commands/order/mod.rs +++ b/crates/cli/src/commands/order/mod.rs @@ -1,5 +1,6 @@ mod add; mod compose; +mod calldata; mod detail; mod list; mod remove; @@ -9,6 +10,8 @@ use add::CliOrderAddArgs; use anyhow::Result; use clap::Parser; use compose::Compose; +use calldata::Calldata; + use detail::CliOrderDetailArgs; use list::CliOrderListArgs; use remove::CliOrderRemoveArgs; @@ -29,6 +32,9 @@ pub enum Order { #[command(about = "Compose a .rain order file to Rainlang", alias = "comp")] Compose(Compose), + + #[command(about = "Generate calldata for addOrder from a composition", alias = "call")] + Calldata(Calldata), } impl Execute for Order { @@ -39,6 +45,7 @@ impl Execute for Order { Order::Create(create) => create.execute().await, Order::Remove(remove) => remove.execute().await, Order::Compose(compose) => compose.execute().await, + Order::Calldata(calldata) => calldata.execute().await, } } } From 6eb0147132e9daa505e12a66587929991a1fc32e Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Wed, 21 Aug 2024 18:57:28 +0530 Subject: [PATCH 02/12] update calldata cli --- crates/cli/src/commands/order/calldata.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/cli/src/commands/order/calldata.rs b/crates/cli/src/commands/order/calldata.rs index c7b31a606..1d8ec59e8 100644 --- a/crates/cli/src/commands/order/calldata.rs +++ b/crates/cli/src/commands/order/calldata.rs @@ -29,10 +29,6 @@ pub struct Calldata { #[arg(short = 'o', long, help = "Output encoding", default_value = "binary")] encoding: SupportedOutputEncoding, - - #[arg(short, long, help = "RPC URL")] - pub rpc_url: String, - } impl Execute for Calldata { @@ -57,7 +53,9 @@ impl Execute for Calldata { .await; let add_order_calldata = add_order_args? - .try_into_call(self.rpc_url.clone()) + .try_into_call( + config_deployment.scenario.deployer.network.rpc.to_string() + ) .await? .abi_encode(); From 3a18a6afbeee070c84c95086e690a48105e969dc Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Wed, 21 Aug 2024 19:00:24 +0530 Subject: [PATCH 03/12] fmt --- crates/cli/src/commands/order/calldata.rs | 19 ++++++++----------- crates/cli/src/commands/order/mod.rs | 9 ++++++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/cli/src/commands/order/calldata.rs b/crates/cli/src/commands/order/calldata.rs index 1d8ec59e8..657580ae9 100644 --- a/crates/cli/src/commands/order/calldata.rs +++ b/crates/cli/src/commands/order/calldata.rs @@ -1,16 +1,14 @@ use crate::execute::Execute; use crate::output::{output, SupportedOutputEncoding}; +use alloy::sol_types::SolCall; use anyhow::{anyhow, Result}; use clap::Args; +use rain_orderbook_app_settings::Config; +use rain_orderbook_common::add_order::AddOrderArgs; use rain_orderbook_common::dotrain_order::DotrainOrder; use std::fs::read_to_string; -use std::path::PathBuf; -use rain_orderbook_app_settings::{ - Config -}; -use rain_orderbook_common::add_order::AddOrderArgs; use std::ops::Deref; -use alloy::sol_types::SolCall; +use std::path::PathBuf; #[derive(Args, Clone)] pub struct Calldata { @@ -49,16 +47,15 @@ impl Execute for Calldata { .get(&self.deployment) .ok_or(anyhow!("specified deployment is undefined!"))?; - let add_order_args = AddOrderArgs::new_from_deployment(dotrain_string, config_deployment.deref().clone()) + let add_order_args = + AddOrderArgs::new_from_deployment(dotrain_string, config_deployment.deref().clone()) .await; let add_order_calldata = add_order_args? - .try_into_call( - config_deployment.scenario.deployer.network.rpc.to_string() - ) + .try_into_call(config_deployment.scenario.deployer.network.rpc.to_string()) .await? .abi_encode(); - + output(&None, self.encoding.clone(), &add_order_calldata)?; Ok(()) diff --git a/crates/cli/src/commands/order/mod.rs b/crates/cli/src/commands/order/mod.rs index 776034087..86edb8a77 100644 --- a/crates/cli/src/commands/order/mod.rs +++ b/crates/cli/src/commands/order/mod.rs @@ -1,6 +1,6 @@ mod add; -mod compose; mod calldata; +mod compose; mod detail; mod list; mod remove; @@ -8,9 +8,9 @@ mod remove; use crate::execute::Execute; use add::CliOrderAddArgs; use anyhow::Result; +use calldata::Calldata; use clap::Parser; use compose::Compose; -use calldata::Calldata; use detail::CliOrderDetailArgs; use list::CliOrderListArgs; @@ -33,7 +33,10 @@ pub enum Order { #[command(about = "Compose a .rain order file to Rainlang", alias = "comp")] Compose(Compose), - #[command(about = "Generate calldata for addOrder from a composition", alias = "call")] + #[command( + about = "Generate calldata for addOrder from a composition", + alias = "call" + )] Calldata(Calldata), } From 608d6000445a25665d3f169b842d168504e497d8 Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Thu, 22 Aug 2024 12:56:32 +0530 Subject: [PATCH 04/12] update var name --- crates/cli/src/commands/order/calldata.rs | 4 ++-- crates/cli/src/commands/order/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/cli/src/commands/order/calldata.rs b/crates/cli/src/commands/order/calldata.rs index 657580ae9..1e12325da 100644 --- a/crates/cli/src/commands/order/calldata.rs +++ b/crates/cli/src/commands/order/calldata.rs @@ -11,7 +11,7 @@ use std::ops::Deref; use std::path::PathBuf; #[derive(Args, Clone)] -pub struct Calldata { +pub struct AddOrderCalldata { #[arg( short = 'f', long, @@ -29,7 +29,7 @@ pub struct Calldata { encoding: SupportedOutputEncoding, } -impl Execute for Calldata { +impl Execute for AddOrderCalldata { async fn execute(&self) -> Result<()> { let dotrain = read_to_string(self.dotrain_file.clone()).map_err(|e| anyhow!(e))?; let settings = match &self.settings_file { diff --git a/crates/cli/src/commands/order/mod.rs b/crates/cli/src/commands/order/mod.rs index 86edb8a77..0dca2de6b 100644 --- a/crates/cli/src/commands/order/mod.rs +++ b/crates/cli/src/commands/order/mod.rs @@ -8,7 +8,7 @@ mod remove; use crate::execute::Execute; use add::CliOrderAddArgs; use anyhow::Result; -use calldata::Calldata; +use calldata::AddOrderCalldata; use clap::Parser; use compose::Compose; @@ -37,7 +37,7 @@ pub enum Order { about = "Generate calldata for addOrder from a composition", alias = "call" )] - Calldata(Calldata), + Calldata(AddOrderCalldata), } impl Execute for Order { From ea5336abb4d0a316bb3e53be3f94a704fa38d968 Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Thu, 22 Aug 2024 13:43:21 +0530 Subject: [PATCH 05/12] add orderbook address command --- crates/cli/src/commands/order/mod.rs | 10 ++++ crates/cli/src/commands/order/orderbook.rs | 58 ++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 crates/cli/src/commands/order/orderbook.rs diff --git a/crates/cli/src/commands/order/mod.rs b/crates/cli/src/commands/order/mod.rs index 0dca2de6b..9bde8f874 100644 --- a/crates/cli/src/commands/order/mod.rs +++ b/crates/cli/src/commands/order/mod.rs @@ -4,11 +4,13 @@ mod compose; mod detail; mod list; mod remove; +mod orderbook; use crate::execute::Execute; use add::CliOrderAddArgs; use anyhow::Result; use calldata::AddOrderCalldata; +use crate::commands::order::orderbook::Orderbook; use clap::Parser; use compose::Compose; @@ -38,6 +40,12 @@ pub enum Order { alias = "call" )] Calldata(AddOrderCalldata), + + #[command( + about = "Get the orderbook address for a given order", + alias = "ob" + )] + OrderbookAddress(Orderbook), } impl Execute for Order { @@ -49,6 +57,8 @@ impl Execute for Order { Order::Remove(remove) => remove.execute().await, Order::Compose(compose) => compose.execute().await, Order::Calldata(calldata) => calldata.execute().await, + Order::OrderbookAddress(orderbook_address) => orderbook_address.execute().await, + } } } diff --git a/crates/cli/src/commands/order/orderbook.rs b/crates/cli/src/commands/order/orderbook.rs new file mode 100644 index 000000000..d61a96f9e --- /dev/null +++ b/crates/cli/src/commands/order/orderbook.rs @@ -0,0 +1,58 @@ +use crate::execute::Execute; +use crate::output::{output, SupportedOutputEncoding}; +use anyhow::{anyhow, Result}; +use clap::Args; +use rain_orderbook_app_settings::Config; +use rain_orderbook_common::dotrain_order::DotrainOrder; +use std::fs::read_to_string; +use std::path::PathBuf; + +#[derive(Args, Clone)] +pub struct Orderbook { + #[arg( + short = 'f', + long, + help = "Path to the .rain file specifying the order" + )] + dotrain_file: PathBuf, + + #[arg(short = 'c', long, help = "Path to the settings yaml file")] + settings_file: Option, + + #[arg(short = 'e', long, help = "Deployment key to select from frontmatter")] + deployment: String, + + #[arg(short = 'o', long, help = "Output encoding", default_value = "binary")] + encoding: SupportedOutputEncoding, +} + +impl Execute for Orderbook { + async fn execute(&self) -> Result<()> { + 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, + }; + let order = DotrainOrder::new(dotrain, settings).await?; + let order_config: Config = order.clone().config; + + let config_deployment = order_config + .deployments + .get(&self.deployment) + .ok_or(anyhow!("specified deployment is undefined!"))?; + + let network_name = config_deployment.scenario.deployer.network.name.clone(); + + let orderbook = order_config + .orderbooks + .get(&network_name) + .ok_or(anyhow!("specified orderbook is undefined!"))?; + + let orderbook_address = orderbook.address.to_vec(); + output(&None, self.encoding.clone(), &orderbook_address)?; + + Ok(()) + } +} \ No newline at end of file From be02225be02c6264d7088de6ddf61b9d9ada1499 Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Thu, 22 Aug 2024 13:44:04 +0530 Subject: [PATCH 06/12] fmt --- crates/cli/src/commands/order/mod.rs | 10 +++------- crates/cli/src/commands/order/orderbook.rs | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/crates/cli/src/commands/order/mod.rs b/crates/cli/src/commands/order/mod.rs index 9bde8f874..35f64326a 100644 --- a/crates/cli/src/commands/order/mod.rs +++ b/crates/cli/src/commands/order/mod.rs @@ -3,14 +3,14 @@ mod calldata; mod compose; mod detail; mod list; -mod remove; mod orderbook; +mod remove; +use crate::commands::order::orderbook::Orderbook; use crate::execute::Execute; use add::CliOrderAddArgs; use anyhow::Result; use calldata::AddOrderCalldata; -use crate::commands::order::orderbook::Orderbook; use clap::Parser; use compose::Compose; @@ -41,10 +41,7 @@ pub enum Order { )] Calldata(AddOrderCalldata), - #[command( - about = "Get the orderbook address for a given order", - alias = "ob" - )] + #[command(about = "Get the orderbook address for a given order", alias = "ob")] OrderbookAddress(Orderbook), } @@ -58,7 +55,6 @@ impl Execute for Order { Order::Compose(compose) => compose.execute().await, Order::Calldata(calldata) => calldata.execute().await, Order::OrderbookAddress(orderbook_address) => orderbook_address.execute().await, - } } } diff --git a/crates/cli/src/commands/order/orderbook.rs b/crates/cli/src/commands/order/orderbook.rs index d61a96f9e..fa3a826dc 100644 --- a/crates/cli/src/commands/order/orderbook.rs +++ b/crates/cli/src/commands/order/orderbook.rs @@ -55,4 +55,4 @@ impl Execute for Orderbook { Ok(()) } -} \ No newline at end of file +} From fcafd60089e8bb3038c3d0765fcbfad37cbaf6db Mon Sep 17 00:00:00 2001 From: Siddharth2207 Date: Thu, 22 Aug 2024 21:32:02 +0530 Subject: [PATCH 07/12] rename command and crate --- crates/cli/src/commands/order/mod.rs | 8 ++++---- .../commands/order/{orderbook.rs => orderbook_address.rs} | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) rename crates/cli/src/commands/order/{orderbook.rs => orderbook_address.rs} (96%) diff --git a/crates/cli/src/commands/order/mod.rs b/crates/cli/src/commands/order/mod.rs index 35f64326a..3692eac21 100644 --- a/crates/cli/src/commands/order/mod.rs +++ b/crates/cli/src/commands/order/mod.rs @@ -3,10 +3,10 @@ mod calldata; mod compose; mod detail; mod list; -mod orderbook; +mod orderbook_address; mod remove; -use crate::commands::order::orderbook::Orderbook; +use crate::commands::order::orderbook_address::OrderbookAddress; use crate::execute::Execute; use add::CliOrderAddArgs; use anyhow::Result; @@ -41,8 +41,8 @@ pub enum Order { )] Calldata(AddOrderCalldata), - #[command(about = "Get the orderbook address for a given order", alias = "ob")] - OrderbookAddress(Orderbook), + #[command(about = "Get the orderbook address for a given order", alias = "ob-addr")] + OrderbookAddress(OrderbookAddress), } impl Execute for Order { diff --git a/crates/cli/src/commands/order/orderbook.rs b/crates/cli/src/commands/order/orderbook_address.rs similarity index 96% rename from crates/cli/src/commands/order/orderbook.rs rename to crates/cli/src/commands/order/orderbook_address.rs index fa3a826dc..87d1ca4cc 100644 --- a/crates/cli/src/commands/order/orderbook.rs +++ b/crates/cli/src/commands/order/orderbook_address.rs @@ -8,7 +8,7 @@ use std::fs::read_to_string; use std::path::PathBuf; #[derive(Args, Clone)] -pub struct Orderbook { +pub struct OrderbookAddress { #[arg( short = 'f', long, @@ -26,7 +26,7 @@ pub struct Orderbook { encoding: SupportedOutputEncoding, } -impl Execute for Orderbook { +impl Execute for OrderbookAddress { async fn execute(&self) -> Result<()> { let dotrain = read_to_string(self.dotrain_file.clone()).map_err(|e| anyhow!(e))?; let settings = match &self.settings_file { From ffca4549b5acc92df2b858115809aedb6d59a3fe Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Thu, 22 Aug 2024 18:01:03 +0000 Subject: [PATCH 08/12] add tests --- Cargo.lock | 2 + crates/cli/Cargo.toml | 7 +- crates/cli/src/commands/order/calldata.rs | 205 +++++++++++++++- crates/cli/src/commands/order/mod.rs | 5 +- .../src/commands/order/orderbook_address.rs | 226 +++++++++++++++++- crates/cli/src/main.rs | 11 + crates/cli/src/output.rs | 2 +- 7 files changed, 448 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd27464da..092bde54f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6306,6 +6306,7 @@ dependencies = [ "chrono", "clap", "comfy-table", + "httpmock", "rain_orderbook_app_settings", "rain_orderbook_bindings", "rain_orderbook_common", @@ -6315,6 +6316,7 @@ dependencies = [ "rust-bigint", "serde", "serde_bytes", + "serde_json", "tokio", "tracing", "tracing-subscriber", diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index b3c5efb89..4dce6b5a3 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -22,7 +22,6 @@ reqwest = { workspace = true } rust-bigint = { workspace = true } serde = { workspace = true } serde_bytes = { workspace = true } -# tokio = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ['env-filter'] } comfy-table = { workspace = true } @@ -32,4 +31,8 @@ chrono = { workspace = true } tokio = { workspace = true, features = ["full"] } [target.'cfg(target_family = "wasm")'.dependencies] -tokio = { workspace = true, features = ["sync", "macros", "io-util", "rt", "time"] } \ No newline at end of file +tokio = { workspace = true, features = ["sync", "macros", "io-util", "rt", "time"] } + +[dev-dependencies] +httpmock = "0.7.0" +serde_json = { workspace = true } \ No newline at end of file diff --git a/crates/cli/src/commands/order/calldata.rs b/crates/cli/src/commands/order/calldata.rs index 1e12325da..a6190af1b 100644 --- a/crates/cli/src/commands/order/calldata.rs +++ b/crates/cli/src/commands/order/calldata.rs @@ -2,7 +2,7 @@ use crate::execute::Execute; use crate::output::{output, SupportedOutputEncoding}; use alloy::sol_types::SolCall; use anyhow::{anyhow, Result}; -use clap::Args; +use clap::Parser; use rain_orderbook_app_settings::Config; use rain_orderbook_common::add_order::AddOrderArgs; use rain_orderbook_common::dotrain_order::DotrainOrder; @@ -10,7 +10,7 @@ use std::fs::read_to_string; use std::ops::Deref; use std::path::PathBuf; -#[derive(Args, Clone)] +#[derive(Parser, Clone)] pub struct AddOrderCalldata { #[arg( short = 'f', @@ -61,3 +61,204 @@ impl Execute for AddOrderCalldata { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use alloy::primitives::B256; + use alloy::primitives::{hex::encode_prefixed, Address}; + use alloy_ethers_typecast::rpc::Response; + use clap::CommandFactory; + use httpmock::{Method::POST, MockServer}; + use serde_json::{from_str, Value}; + use std::str::FromStr; + + #[test] + fn verify_cli() { + AddOrderCalldata::command().debug_assert(); + } + + #[test] + fn test_cli_args() { + let dotrain_file = PathBuf::from_str("./some/dotrain_file.dotrain").unwrap(); + let settings_file = PathBuf::from_str("./some/settings_file.dotrain").unwrap(); + let deployment_str = "some-deployment"; + let output_str = "hex"; + + let cmd = AddOrderCalldata::command(); + let result = cmd + .try_get_matches_from(vec![ + "cmd", + "-f", + dotrain_file.to_str().unwrap(), + "-c", + settings_file.to_str().unwrap(), + "-e", + deployment_str, + "-o", + output_str, + ]) + .unwrap(); + assert_eq!( + result.get_one::("dotrain_file"), + Some(&dotrain_file) + ); + assert_eq!( + result.get_one::("settings_file"), + Some(&settings_file) + ); + assert_eq!( + result.get_one::("deployment"), + Some(&deployment_str.to_string()) + ); + assert_eq!( + result.get_one::("encoding"), + Some(&SupportedOutputEncoding::Hex) + ); + } + + #[tokio::test] + async fn test_execute() { + let rpc_server = MockServer::start_async().await; + let dotrain = format!( + " +networks: + some-network: + rpc: {} + 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 + +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 + +orders: + some-order: + inputs: + - token: token1 + vault-id: 1 + outputs: + - token: token2 + vault-id: 1 + deployer: some-deployer + orderbook: some-orderbook + +deployments: + some-deployment: + scenario: some-scenario + order: some-order +--- +#calculate-io +_ _: 0 0; +#handle-io +:; +#post-add-order +:;", + rpc_server.url("/rpc").as_str() + ); + + let dotrain_path = "./test_dotrain.dotrain"; + let _ = std::fs::write(dotrain_path, dotrain); + + // mock rpc response data + // mock iInterpreter() call + rpc_server.mock(|when, then| { + when.method(POST).path("/rpc").body_contains("0xf0cfdd37"); + then.json_body_obj( + &from_str::( + &Response::new_success( + 1, + encode_prefixed(B256::left_padding_from(&Address::random().0 .0)).as_str(), + ) + .to_json_string() + .unwrap(), + ) + .unwrap(), + ); + }); + // mock iStore() call + rpc_server.mock(|when, then| { + when.method(POST).path("/rpc").body_contains("0xc19423bc"); + then.json_body_obj( + &from_str::( + &Response::new_success( + 2, + encode_prefixed(B256::left_padding_from(&Address::random().0 .0)).as_str(), + ) + .to_json_string() + .unwrap(), + ) + .unwrap(), + ); + }); + // mock iParser() call + rpc_server.mock(|when, then| { + when.method(POST).path("/rpc").body_contains("0x24376855"); + then.json_body_obj( + &from_str::( + &Response::new_success( + 3, + encode_prefixed(B256::left_padding_from(&Address::random().0 .0)).as_str(), + ) + .to_json_string() + .unwrap(), + ) + .unwrap(), + ); + }); + // mock parse2() call + rpc_server.mock(|when, then| { + when.method(POST).path("/rpc").body_contains("0xa3869e14"); + then.json_body_obj( + &from_str::( + &Response::new_success(4, "0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000") + .to_json_string() + .unwrap(), + ) + .unwrap(), + ); + }); + + let add_order_calldata = AddOrderCalldata { + dotrain_file: dotrain_path.into(), + settings_file: None, + deployment: "some-deployment".to_string(), + encoding: SupportedOutputEncoding::Hex, + }; + // should succeed without err + add_order_calldata.execute().await.unwrap(); + + // remove test file + std::fs::remove_file(dotrain_path).unwrap(); + } +} diff --git a/crates/cli/src/commands/order/mod.rs b/crates/cli/src/commands/order/mod.rs index 3692eac21..d644a2eed 100644 --- a/crates/cli/src/commands/order/mod.rs +++ b/crates/cli/src/commands/order/mod.rs @@ -41,7 +41,10 @@ pub enum Order { )] Calldata(AddOrderCalldata), - #[command(about = "Get the orderbook address for a given order", alias = "ob-addr")] + #[command( + about = "Get the orderbook address for a given order", + alias = "ob-addr" + )] OrderbookAddress(OrderbookAddress), } diff --git a/crates/cli/src/commands/order/orderbook_address.rs b/crates/cli/src/commands/order/orderbook_address.rs index 87d1ca4cc..7cb98ee31 100644 --- a/crates/cli/src/commands/order/orderbook_address.rs +++ b/crates/cli/src/commands/order/orderbook_address.rs @@ -1,13 +1,13 @@ use crate::execute::Execute; use crate::output::{output, SupportedOutputEncoding}; use anyhow::{anyhow, Result}; -use clap::Args; +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; -#[derive(Args, Clone)] +#[derive(Parser, Clone)] pub struct OrderbookAddress { #[arg( short = 'f', @@ -44,11 +44,14 @@ impl Execute for OrderbookAddress { .ok_or(anyhow!("specified deployment is undefined!"))?; let network_name = config_deployment.scenario.deployer.network.name.clone(); + println!("{}", network_name); let orderbook = order_config .orderbooks - .get(&network_name) - .ok_or(anyhow!("specified orderbook is undefined!"))?; + .iter() + .find(|(k, v)| **k == network_name || v.network.name == network_name) + .ok_or(anyhow!("specified orderbook is undefined!"))? + .1; let orderbook_address = orderbook.address.to_vec(); output(&None, self.encoding.clone(), &orderbook_address)?; @@ -56,3 +59,218 @@ impl Execute for OrderbookAddress { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::*; + use clap::CommandFactory; + use std::str::FromStr; + + #[test] + fn verify_cli() { + OrderbookAddress::command().debug_assert(); + } + + #[test] + fn test_cli_args() { + let dotrain_file = PathBuf::from_str("./some/dotrain_file.dotrain").unwrap(); + let settings_file = PathBuf::from_str("./some/settings_file.dotrain").unwrap(); + let deployment_str = "some-deployment"; + let output_str = "hex"; + + let cmd = OrderbookAddress::command(); + let result = cmd + .try_get_matches_from(vec![ + "cmd", + "-f", + dotrain_file.to_str().unwrap(), + "-c", + settings_file.to_str().unwrap(), + "-e", + deployment_str, + "-o", + output_str, + ]) + .unwrap(); + assert_eq!( + result.get_one::("dotrain_file"), + Some(&dotrain_file) + ); + assert_eq!( + result.get_one::("settings_file"), + Some(&settings_file) + ); + assert_eq!( + result.get_one::("deployment"), + Some(&deployment_str.to_string()) + ); + assert_eq!( + result.get_one::("encoding"), + Some(&SupportedOutputEncoding::Hex) + ); + } + + #[tokio::test] + async fn test_execute_same_name() { + let dotrain = " +networks: + some-network: + rpc: https://some-url.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 + +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 + +orders: + some-order: + inputs: + - token: token1 + vault-id: 1 + outputs: + - token: token2 + vault-id: 1 + deployer: some-deployer + orderbook: some-orderbook + +deployments: + some-deployment: + scenario: some-scenario + order: some-order +--- +#calculate-io +_ _: 0 0; +#handle-io +:; +#post-add-order +:;"; + + let dotrain_path = "./test_dotrain.dotrain"; + let _ = std::fs::write(dotrain_path, dotrain); + + let orderbook_adress = OrderbookAddress { + dotrain_file: dotrain_path.into(), + settings_file: None, + deployment: "some-deployment".to_string(), + encoding: SupportedOutputEncoding::Hex, + }; + // should succeed without err + orderbook_adress.execute().await.unwrap(); + + // remove test file + std::fs::remove_file(dotrain_path).unwrap(); + } + + #[tokio::test] + async fn test_execute_diff_name() { + let dotrain = " +networks: + some-network: + rpc: https://some-url.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-network: + address: 0xc95A5f8eFe14d7a20BD2E5BAFEC4E71f8Ce0B9A6 + network: some-network + subgraph: some-sg + +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 + +orders: + some-order: + inputs: + - token: token1 + vault-id: 1 + outputs: + - token: token2 + vault-id: 1 + deployer: some-deployer + orderbook: some-network + +deployments: + some-deployment: + scenario: some-scenario + order: some-order +--- +#calculate-io +_ _: 0 0; +#handle-io +:; +#post-add-order +:;"; + + let dotrain_path = "./test_dotrain.dotrain"; + let _ = std::fs::write(dotrain_path, dotrain); + + let orderbook_adress = OrderbookAddress { + dotrain_file: dotrain_path.into(), + settings_file: None, + deployment: "some-deployment".to_string(), + encoding: SupportedOutputEncoding::Hex, + }; + // should succeed without err + orderbook_adress.execute().await.unwrap(); + + // remove test file + std::fs::remove_file(dotrain_path).unwrap(); + } +} diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 36941b1f7..182a66354 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -31,3 +31,14 @@ async fn main() -> Result<()> { let cli = Cli::parse(); cli.orderbook.execute().await } + +#[cfg(test)] +mod tests { + use super::*; + use clap::CommandFactory; + + #[test] + fn verify_cli() { + Cli::command().debug_assert(); + } +} diff --git a/crates/cli/src/output.rs b/crates/cli/src/output.rs index 137fe54a9..adcefa1da 100644 --- a/crates/cli/src/output.rs +++ b/crates/cli/src/output.rs @@ -1,7 +1,7 @@ use std::io::Write; use std::path::PathBuf; -#[derive(clap::ValueEnum, Clone)] +#[derive(Debug, clap::ValueEnum, Clone, PartialEq)] pub enum SupportedOutputEncoding { Binary, Hex, From 2d87c428825bb66f4630e0a66e738113e31e703f Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Thu, 22 Aug 2024 18:04:32 +0000 Subject: [PATCH 09/12] update --- crates/cli/src/commands/order/calldata.rs | 2 +- crates/cli/src/commands/order/orderbook_address.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/cli/src/commands/order/calldata.rs b/crates/cli/src/commands/order/calldata.rs index a6190af1b..59bc95ec9 100644 --- a/crates/cli/src/commands/order/calldata.rs +++ b/crates/cli/src/commands/order/calldata.rs @@ -187,7 +187,7 @@ _ _: 0 0; rpc_server.url("/rpc").as_str() ); - let dotrain_path = "./test_dotrain.dotrain"; + let dotrain_path = "./test_dotrain1.rain"; let _ = std::fs::write(dotrain_path, dotrain); // mock rpc response data diff --git a/crates/cli/src/commands/order/orderbook_address.rs b/crates/cli/src/commands/order/orderbook_address.rs index 7cb98ee31..91f9b340a 100644 --- a/crates/cli/src/commands/order/orderbook_address.rs +++ b/crates/cli/src/commands/order/orderbook_address.rs @@ -176,7 +176,7 @@ _ _: 0 0; #post-add-order :;"; - let dotrain_path = "./test_dotrain.dotrain"; + let dotrain_path = "./test_dotrain1.rain"; let _ = std::fs::write(dotrain_path, dotrain); let orderbook_adress = OrderbookAddress { @@ -258,7 +258,7 @@ _ _: 0 0; #post-add-order :;"; - let dotrain_path = "./test_dotrain.dotrain"; + let dotrain_path = "./test_dotrain2.rain"; let _ = std::fs::write(dotrain_path, dotrain); let orderbook_adress = OrderbookAddress { From b8e4ad081322a020b43db739c51e8b0f37b1f28b Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Thu, 22 Aug 2024 18:28:37 +0000 Subject: [PATCH 10/12] update --- crates/cli/src/commands/order/orderbook_address.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/cli/src/commands/order/orderbook_address.rs b/crates/cli/src/commands/order/orderbook_address.rs index 91f9b340a..ff7481fd6 100644 --- a/crates/cli/src/commands/order/orderbook_address.rs +++ b/crates/cli/src/commands/order/orderbook_address.rs @@ -44,7 +44,6 @@ impl Execute for OrderbookAddress { .ok_or(anyhow!("specified deployment is undefined!"))?; let network_name = config_deployment.scenario.deployer.network.name.clone(); - println!("{}", network_name); let orderbook = order_config .orderbooks From bb289e98554818c4bff16eae7632622d0971a93c Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Thu, 22 Aug 2024 20:46:29 +0000 Subject: [PATCH 11/12] update --- crates/cli/src/commands/order/calldata.rs | 15 +-- .../src/commands/order/orderbook_address.rs | 94 ++++--------------- 2 files changed, 28 insertions(+), 81 deletions(-) diff --git a/crates/cli/src/commands/order/calldata.rs b/crates/cli/src/commands/order/calldata.rs index 59bc95ec9..5888f58dc 100644 --- a/crates/cli/src/commands/order/calldata.rs +++ b/crates/cli/src/commands/order/calldata.rs @@ -80,8 +80,8 @@ mod tests { #[test] fn test_cli_args() { - let dotrain_file = PathBuf::from_str("./some/dotrain_file.dotrain").unwrap(); - let settings_file = PathBuf::from_str("./some/settings_file.dotrain").unwrap(); + let dotrain_file = PathBuf::from_str("./some/dotrain_file.rain").unwrap(); + let settings_file = PathBuf::from_str("./some/settings_file.rain").unwrap(); let deployment_str = "some-deployment"; let output_str = "hex"; @@ -188,7 +188,7 @@ _ _: 0 0; ); let dotrain_path = "./test_dotrain1.rain"; - let _ = std::fs::write(dotrain_path, dotrain); + std::fs::write(dotrain_path, dotrain).unwrap(); // mock rpc response data // mock iInterpreter() call @@ -198,7 +198,8 @@ _ _: 0 0; &from_str::( &Response::new_success( 1, - encode_prefixed(B256::left_padding_from(&Address::random().0 .0)).as_str(), + encode_prefixed(B256::left_padding_from(Address::random().as_slice())) + .as_str(), ) .to_json_string() .unwrap(), @@ -213,7 +214,8 @@ _ _: 0 0; &from_str::( &Response::new_success( 2, - encode_prefixed(B256::left_padding_from(&Address::random().0 .0)).as_str(), + encode_prefixed(B256::left_padding_from(Address::random().as_slice())) + .as_str(), ) .to_json_string() .unwrap(), @@ -228,7 +230,8 @@ _ _: 0 0; &from_str::( &Response::new_success( 3, - encode_prefixed(B256::left_padding_from(&Address::random().0 .0)).as_str(), + encode_prefixed(B256::left_padding_from(Address::random().as_slice())) + .as_str(), ) .to_json_string() .unwrap(), diff --git a/crates/cli/src/commands/order/orderbook_address.rs b/crates/cli/src/commands/order/orderbook_address.rs index ff7481fd6..bee01c9b0 100644 --- a/crates/cli/src/commands/order/orderbook_address.rs +++ b/crates/cli/src/commands/order/orderbook_address.rs @@ -72,8 +72,8 @@ mod tests { #[test] fn test_cli_args() { - let dotrain_file = PathBuf::from_str("./some/dotrain_file.dotrain").unwrap(); - let settings_file = PathBuf::from_str("./some/settings_file.dotrain").unwrap(); + let dotrain_file = PathBuf::from_str("./some/dotrain_file.rain").unwrap(); + let settings_file = PathBuf::from_str("./some/settings_file.rain").unwrap(); let deployment_str = "some-deployment"; let output_str = "hex"; @@ -109,9 +109,9 @@ mod tests { ); } - #[tokio::test] - async fn test_execute_same_name() { - let dotrain = " + fn get_test_dotrain(orderbook: &str) -> String { + format!( + " networks: some-network: rpc: https://some-url.com @@ -128,7 +128,7 @@ deployers: address: 0xF14E09601A47552De6aBd3A0B165607FaFd2B5Ba orderbooks: - some-orderbook: + {}: address: 0xc95A5f8eFe14d7a20BD2E5BAFEC4E71f8Ce0B9A6 network: some-network subgraph: some-sg @@ -161,7 +161,6 @@ orders: - token: token2 vault-id: 1 deployer: some-deployer - orderbook: some-orderbook deployments: some-deployment: @@ -173,10 +172,17 @@ _ _: 0 0; #handle-io :; #post-add-order -:;"; +:;", + orderbook + ) + } - let dotrain_path = "./test_dotrain1.rain"; - let _ = std::fs::write(dotrain_path, dotrain); + #[tokio::test] + async fn test_execute_same_name() { + let dotrain = get_test_dotrain("some-orderbook"); + + let dotrain_path = "./test_dotrain2.rain"; + std::fs::write(dotrain_path, dotrain).unwrap(); let orderbook_adress = OrderbookAddress { dotrain_file: dotrain_path.into(), @@ -193,72 +199,10 @@ _ _: 0 0; #[tokio::test] async fn test_execute_diff_name() { - let dotrain = " -networks: - some-network: - rpc: https://some-url.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-network: - address: 0xc95A5f8eFe14d7a20BD2E5BAFEC4E71f8Ce0B9A6 - network: some-network - subgraph: some-sg - -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 - -orders: - some-order: - inputs: - - token: token1 - vault-id: 1 - outputs: - - token: token2 - vault-id: 1 - deployer: some-deployer - orderbook: some-network + let dotrain = get_test_dotrain("some-network"); -deployments: - some-deployment: - scenario: some-scenario - order: some-order ---- -#calculate-io -_ _: 0 0; -#handle-io -:; -#post-add-order -:;"; - - let dotrain_path = "./test_dotrain2.rain"; - let _ = std::fs::write(dotrain_path, dotrain); + let dotrain_path = "./test_dotrain3.rain"; + std::fs::write(dotrain_path, dotrain).unwrap(); let orderbook_adress = OrderbookAddress { dotrain_file: dotrain_path.into(), From 08123c3af2c77bdbbc03589551b3634cb666b71e Mon Sep 17 00:00:00 2001 From: rouzwelt Date: Thu, 22 Aug 2024 23:11:44 +0000 Subject: [PATCH 12/12] update --- Cargo.lock | 1 - crates/cli/Cargo.toml | 1 - crates/cli/src/commands/order/calldata.rs | 75 +++++++------------ .../src/commands/order/orderbook_address.rs | 59 +++++++-------- 4 files changed, 58 insertions(+), 78 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 092bde54f..ce9ea6cf3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6316,7 +6316,6 @@ dependencies = [ "rust-bigint", "serde", "serde_bytes", - "serde_json", "tokio", "tracing", "tracing-subscriber", diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 4dce6b5a3..77ee8e665 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -35,4 +35,3 @@ tokio = { workspace = true, features = ["sync", "macros", "io-util", "rt", "time [dev-dependencies] httpmock = "0.7.0" -serde_json = { workspace = true } \ No newline at end of file diff --git a/crates/cli/src/commands/order/calldata.rs b/crates/cli/src/commands/order/calldata.rs index 5888f58dc..e7fcd1e79 100644 --- a/crates/cli/src/commands/order/calldata.rs +++ b/crates/cli/src/commands/order/calldata.rs @@ -3,7 +3,6 @@ use crate::output::{output, SupportedOutputEncoding}; use alloy::sol_types::SolCall; use anyhow::{anyhow, Result}; use clap::Parser; -use rain_orderbook_app_settings::Config; use rain_orderbook_common::add_order::AddOrderArgs; use rain_orderbook_common::dotrain_order::DotrainOrder; use std::fs::read_to_string; @@ -39,10 +38,10 @@ impl Execute for AddOrderCalldata { None => None, }; let order = DotrainOrder::new(dotrain, settings).await?; - let order_config: Config = order.clone().config; - let dotrain_string: String = order.clone().dotrain; + let dotrain_string = order.dotrain.clone(); - let config_deployment = order_config + let config_deployment = order + .config .deployments .get(&self.deployment) .ok_or(anyhow!("specified deployment is undefined!"))?; @@ -65,12 +64,11 @@ impl Execute for AddOrderCalldata { #[cfg(test)] mod tests { use super::*; - use alloy::primitives::B256; - use alloy::primitives::{hex::encode_prefixed, Address}; + use alloy::primitives::{hex::encode_prefixed, Address, Bytes, B256}; + use alloy::sol_types::SolValue; use alloy_ethers_typecast::rpc::Response; use clap::CommandFactory; - use httpmock::{Method::POST, MockServer}; - use serde_json::{from_str, Value}; + use httpmock::MockServer; use std::str::FromStr; #[test] @@ -193,62 +191,47 @@ _ _: 0 0; // mock rpc response data // mock iInterpreter() call rpc_server.mock(|when, then| { - when.method(POST).path("/rpc").body_contains("0xf0cfdd37"); - then.json_body_obj( - &from_str::( - &Response::new_success( - 1, - encode_prefixed(B256::left_padding_from(Address::random().as_slice())) - .as_str(), - ) - .to_json_string() - .unwrap(), + when.path("/rpc").body_contains("0xf0cfdd37"); + then.body( + Response::new_success( + 1, + &B256::left_padding_from(Address::random().as_slice()).to_string(), ) + .to_json_string() .unwrap(), ); }); // mock iStore() call rpc_server.mock(|when, then| { - when.method(POST).path("/rpc").body_contains("0xc19423bc"); - then.json_body_obj( - &from_str::( - &Response::new_success( - 2, - encode_prefixed(B256::left_padding_from(Address::random().as_slice())) - .as_str(), - ) - .to_json_string() - .unwrap(), + when.path("/rpc").body_contains("0xc19423bc"); + then.body( + Response::new_success( + 2, + &B256::left_padding_from(Address::random().as_slice()).to_string(), ) + .to_json_string() .unwrap(), ); }); // mock iParser() call rpc_server.mock(|when, then| { - when.method(POST).path("/rpc").body_contains("0x24376855"); - then.json_body_obj( - &from_str::( - &Response::new_success( - 3, - encode_prefixed(B256::left_padding_from(Address::random().as_slice())) - .as_str(), - ) - .to_json_string() - .unwrap(), + when.path("/rpc").body_contains("0x24376855"); + then.body( + Response::new_success( + 3, + &B256::left_padding_from(Address::random().as_slice()).to_string(), ) + .to_json_string() .unwrap(), ); }); // mock parse2() call rpc_server.mock(|when, then| { - when.method(POST).path("/rpc").body_contains("0xa3869e14"); - then.json_body_obj( - &from_str::( - &Response::new_success(4, "0x000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000021234000000000000000000000000000000000000000000000000000000000000") - .to_json_string() - .unwrap(), - ) - .unwrap(), + when.path("/rpc").body_contains("0xa3869e14"); + then.body( + Response::new_success(4, &encode_prefixed(Bytes::from(vec![1, 2]).abi_encode())) + .to_json_string() + .unwrap(), ); }); diff --git a/crates/cli/src/commands/order/orderbook_address.rs b/crates/cli/src/commands/order/orderbook_address.rs index bee01c9b0..d1299e428 100644 --- a/crates/cli/src/commands/order/orderbook_address.rs +++ b/crates/cli/src/commands/order/orderbook_address.rs @@ -37,23 +37,24 @@ impl Execute for OrderbookAddress { }; let order = DotrainOrder::new(dotrain, settings).await?; let order_config: Config = order.clone().config; - - let config_deployment = order_config + let deployment_ref = order_config .deployments .get(&self.deployment) .ok_or(anyhow!("specified deployment is undefined!"))?; - let network_name = config_deployment.scenario.deployer.network.name.clone(); - - let orderbook = order_config - .orderbooks - .iter() - .find(|(k, v)| **k == network_name || v.network.name == network_name) - .ok_or(anyhow!("specified orderbook is undefined!"))? - .1; - - let orderbook_address = orderbook.address.to_vec(); - output(&None, self.encoding.clone(), &orderbook_address)?; + let orderbook_address = if let Some(v) = &deployment_ref.order.orderbook { + v.address + } else { + let network_name = &deployment_ref.scenario.deployer.network.name; + order_config + .orderbooks + .iter() + .find(|(k, v)| *k == network_name || v.network.name == *network_name) + .ok_or(anyhow!("specified orderbook is undefined!"))? + .1 + .address + }; + output(&None, self.encoding.clone(), orderbook_address.as_slice())?; Ok(()) } @@ -78,19 +79,17 @@ mod tests { let output_str = "hex"; let cmd = OrderbookAddress::command(); - let result = cmd - .try_get_matches_from(vec![ - "cmd", - "-f", - dotrain_file.to_str().unwrap(), - "-c", - settings_file.to_str().unwrap(), - "-e", - deployment_str, - "-o", - output_str, - ]) - .unwrap(); + let result = cmd.get_matches_from(vec![ + "cmd", + "-f", + dotrain_file.to_str().unwrap(), + "-c", + settings_file.to_str().unwrap(), + "-e", + deployment_str, + "-o", + output_str, + ]); assert_eq!( result.get_one::("dotrain_file"), Some(&dotrain_file) @@ -109,7 +108,7 @@ mod tests { ); } - fn get_test_dotrain(orderbook: &str) -> String { + fn get_test_dotrain(orderbook_key: &str) -> String { format!( " networks: @@ -173,12 +172,12 @@ _ _: 0 0; :; #post-add-order :;", - orderbook + orderbook_key ) } #[tokio::test] - async fn test_execute_same_name() { + async fn test_execute_diff_name() { let dotrain = get_test_dotrain("some-orderbook"); let dotrain_path = "./test_dotrain2.rain"; @@ -198,7 +197,7 @@ _ _: 0 0; } #[tokio::test] - async fn test_execute_diff_name() { + async fn test_execute_same_name() { let dotrain = get_test_dotrain("some-network"); let dotrain_path = "./test_dotrain3.rain";