From e1fefedff51d0262289a8abe12bed8bfbaa5ffd1 Mon Sep 17 00:00:00 2001 From: findolor Date: Sat, 14 Dec 2024 15:29:23 +0300 Subject: [PATCH 1/3] add metaboard --- crates/settings/src/lib.rs | 1 + crates/settings/src/metaboard.rs | 63 +++++++++++++++++++++++++++ crates/settings/src/yaml/orderbook.rs | 29 +++++++++--- 3 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 crates/settings/src/metaboard.rs diff --git a/crates/settings/src/lib.rs b/crates/settings/src/lib.rs index c82ca0edb..61d78260a 100644 --- a/crates/settings/src/lib.rs +++ b/crates/settings/src/lib.rs @@ -6,6 +6,7 @@ pub mod deployer; pub mod deployment; pub mod gui; pub mod merge; +pub mod metaboard; pub mod network; pub mod order; pub mod orderbook; diff --git a/crates/settings/src/metaboard.rs b/crates/settings/src/metaboard.rs new file mode 100644 index 000000000..2ab65b0c3 --- /dev/null +++ b/crates/settings/src/metaboard.rs @@ -0,0 +1,63 @@ +use crate::config::Metaboard; +use crate::yaml::{require_hash, require_string, YamlError, YamlParsableHash}; +use std::{ + collections::HashMap, + sync::{Arc, RwLock}, +}; +use strict_yaml_rust::StrictYaml; +use url::{ParseError, Url}; + +// Wrapper type just for YAML parsing +#[derive(Clone)] +pub struct YamlMetaboard(Metaboard); + +impl YamlMetaboard { + pub fn metaboard(&self) -> &Metaboard { + &self.0 + } + + pub fn validate_url(value: &str) -> Result { + Url::parse(value) + } +} + +impl YamlParsableHash for YamlMetaboard { + fn parse_all_from_yaml( + document: Arc>, + ) -> Result, YamlError> { + let document_read = document.read().map_err(|_| YamlError::ReadLockError)?; + let metaboards_hash = require_hash( + &document_read, + Some("metaboards"), + Some("missing field: metaboards".to_string()), + )?; + + metaboards_hash + .iter() + .map(|(key_yaml, metaboard_yaml)| { + let metaboard_key = key_yaml.as_str().unwrap_or_default().to_string(); + + let url = YamlMetaboard::validate_url(&require_string( + metaboard_yaml, + None, + Some(format!( + "metaboard value must be a string for key: {metaboard_key}" + )), + )?)?; + + Ok((metaboard_key, YamlMetaboard(url))) + }) + .collect() + } +} + +impl From for YamlMetaboard { + fn from(value: Metaboard) -> Self { + YamlMetaboard(value) + } +} +impl From for Metaboard { + fn from(value: YamlMetaboard) -> Self { + value.0 + } +} diff --git a/crates/settings/src/yaml/orderbook.rs b/crates/settings/src/yaml/orderbook.rs index 477f7e77c..a1349f7f9 100644 --- a/crates/settings/src/yaml/orderbook.rs +++ b/crates/settings/src/yaml/orderbook.rs @@ -1,5 +1,5 @@ use super::*; -use crate::{Network, Orderbook, Subgraph, Token}; +use crate::{metaboard::YamlMetaboard, Network, Orderbook, Subgraph, Token}; use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYamlEmitter; @@ -62,17 +62,24 @@ impl OrderbookYaml { pub fn get_orderbook(&self, key: &str) -> Result { Orderbook::parse_from_yaml(self.document.clone(), key) } + + pub fn get_metaboard_keys(&self) -> Result, YamlError> { + let metaboards = YamlMetaboard::parse_all_from_yaml(self.document.clone())?; + Ok(metaboards.keys().cloned().collect()) + } + pub fn get_metaboard(&self, key: &str) -> Result { + YamlMetaboard::parse_from_yaml(self.document.clone(), key) + } } #[cfg(test)] mod tests { - use std::str::FromStr; - + use super::*; + use crate::Metaboard; use alloy::primitives::Address; + use std::str::FromStr; use url::Url; - use super::*; - const FULL_YAML: &str = r#" networks: mainnet: @@ -173,6 +180,18 @@ mod tests { assert_eq!(orderbook.network, network.into()); assert_eq!(orderbook.subgraph, subgraph.into()); assert_eq!(orderbook.label, Some("Primary Orderbook".to_string())); + + assert_eq!(ob_yaml.get_metaboard_keys().unwrap().len(), 2); + let metaboard: Metaboard = ob_yaml.get_metaboard("board1").unwrap().into(); + assert_eq!( + metaboard, + Url::parse("https://meta.example.com/board1").unwrap() + ); + let metaboard: Metaboard = ob_yaml.get_metaboard("board2").unwrap().into(); + assert_eq!( + metaboard, + Url::parse("https://meta.example.com/board2").unwrap() + ); } #[test] From 181ad4924ba6941878fbfdcdbe512d57b900c8f1 Mon Sep 17 00:00:00 2001 From: findolor Date: Sat, 14 Dec 2024 15:42:05 +0300 Subject: [PATCH 2/3] update subgraph logic with new wrapper --- crates/settings/src/metaboard.rs | 60 ++++++++++++++++++++++++--- crates/settings/src/orderbook.rs | 6 ++- crates/settings/src/subgraph.rs | 45 ++++++++++++++++---- crates/settings/src/yaml/orderbook.rs | 20 +++++---- 4 files changed, 107 insertions(+), 24 deletions(-) diff --git a/crates/settings/src/metaboard.rs b/crates/settings/src/metaboard.rs index 2ab65b0c3..4999d9cff 100644 --- a/crates/settings/src/metaboard.rs +++ b/crates/settings/src/metaboard.rs @@ -7,15 +7,10 @@ use std::{ use strict_yaml_rust::StrictYaml; use url::{ParseError, Url}; -// Wrapper type just for YAML parsing -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct YamlMetaboard(Metaboard); impl YamlMetaboard { - pub fn metaboard(&self) -> &Metaboard { - &self.0 - } - pub fn validate_url(value: &str) -> Result { Url::parse(value) } @@ -56,8 +51,61 @@ impl From for YamlMetaboard { YamlMetaboard(value) } } + impl From for Metaboard { fn from(value: YamlMetaboard) -> Self { value.0 } } + +#[cfg(test)] +mod test { + use super::*; + use crate::yaml::tests::get_document; + + #[test] + fn test_parse_metaboards_from_yaml() { + let yaml = r#" +test: test +"#; + let error = YamlMetaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + assert_eq!( + error, + YamlError::ParseError("missing field: metaboards".to_string()) + ); + + let yaml = r#" +metaboards: + TestMetaboard: + test: https://metaboard.com +"#; + let error = YamlMetaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + assert_eq!( + error, + YamlError::ParseError( + "metaboard value must be a string for key: TestMetaboard".to_string() + ) + ); + + let yaml = r#" +metaboards: + TestMetaboard: + - https://metaboard.com +"#; + let error = YamlMetaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + assert_eq!( + error, + YamlError::ParseError( + "metaboard value must be a string for key: TestMetaboard".to_string() + ) + ); + + let yaml = r#" +metaboards: + TestMetaboard: https://metaboard.com +"#; + let result = YamlMetaboard::parse_all_from_yaml(get_document(yaml)).unwrap(); + assert_eq!(result.len(), 1); + assert!(result.contains_key("TestMetaboard")); + } +} diff --git a/crates/settings/src/orderbook.rs b/crates/settings/src/orderbook.rs index b63b95329..1155bd60a 100644 --- a/crates/settings/src/orderbook.rs +++ b/crates/settings/src/orderbook.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use std::str::FromStr; use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYaml; +use subgraph::YamlSubgraph; use thiserror::Error; use typeshare::typeshare; use yaml::{optional_string, require_hash, require_string, YamlError, YamlParsableHash}; @@ -67,7 +68,8 @@ impl YamlParsableHash for Orderbook { Some(subgraph_name) => subgraph_name, None => orderbook_key.clone(), }; - let subgraph = Subgraph::parse_from_yaml(document.clone(), &subgraph_name)?; + let subgraph = YamlSubgraph::parse_from_yaml(document.clone(), &subgraph_name)?; + let subgraph = Arc::new(subgraph.into()); let label = optional_string(orderbook_yaml, "label"); @@ -76,7 +78,7 @@ impl YamlParsableHash for Orderbook { key: orderbook_key.clone(), address, network: Arc::new(network), - subgraph: Arc::new(subgraph), + subgraph, label, }; diff --git a/crates/settings/src/subgraph.rs b/crates/settings/src/subgraph.rs index c7f82f1e0..713b7212a 100644 --- a/crates/settings/src/subgraph.rs +++ b/crates/settings/src/subgraph.rs @@ -5,12 +5,21 @@ use std::{ sync::{Arc, RwLock}, }; use strict_yaml_rust::StrictYaml; -use url::Url; +use url::{ParseError, Url}; -impl YamlParsableHash for Subgraph { +#[derive(Clone, Debug)] +pub struct YamlSubgraph(Subgraph); + +impl YamlSubgraph { + pub fn validate_url(value: &str) -> Result { + Url::parse(value) + } +} + +impl YamlParsableHash for YamlSubgraph { fn parse_all_from_yaml( document: Arc>, - ) -> Result, YamlError> { + ) -> Result, YamlError> { let document_read = document.read().map_err(|_| YamlError::ReadLockError)?; let subgraphs_hash = require_hash( &document_read, @@ -23,7 +32,7 @@ impl YamlParsableHash for Subgraph { .map(|(key_yaml, subgraph_yaml)| { let subgraph_key = key_yaml.as_str().unwrap_or_default().to_string(); - let url = Url::parse(&require_string( + let url = YamlSubgraph::validate_url(&require_string( subgraph_yaml, None, Some(format!( @@ -31,12 +40,24 @@ impl YamlParsableHash for Subgraph { )), )?)?; - Ok((subgraph_key, url)) + Ok((subgraph_key, YamlSubgraph(url))) }) .collect() } } +impl From for YamlSubgraph { + fn from(value: Subgraph) -> Self { + YamlSubgraph(value) + } +} + +impl From for Subgraph { + fn from(value: YamlSubgraph) -> Self { + value.0 + } +} + #[cfg(test)] mod test { use super::*; @@ -47,7 +68,7 @@ mod test { let yaml = r#" test: test "#; - let error = Subgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = YamlSubgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError("missing field: subgraphs".to_string()) @@ -58,7 +79,7 @@ subgraphs: TestSubgraph: test: https://subgraph.com "#; - let error = Subgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = YamlSubgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError( @@ -71,12 +92,20 @@ subgraphs: TestSubgraph: - https://subgraph.com "#; - let error = Subgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = YamlSubgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError( "subgraph value must be a string for key: TestSubgraph".to_string() ) ); + + let yaml = r#" +subgraphs: + TestSubgraph: https://subgraph.com +"#; + let result = YamlSubgraph::parse_all_from_yaml(get_document(yaml)).unwrap(); + assert_eq!(result.len(), 1); + assert!(result.contains_key("TestSubgraph")); } } diff --git a/crates/settings/src/yaml/orderbook.rs b/crates/settings/src/yaml/orderbook.rs index a1349f7f9..1e44a2747 100644 --- a/crates/settings/src/yaml/orderbook.rs +++ b/crates/settings/src/yaml/orderbook.rs @@ -1,5 +1,8 @@ use super::*; -use crate::{metaboard::YamlMetaboard, Network, Orderbook, Subgraph, Token}; +use crate::{ + metaboard::YamlMetaboard, subgraph::YamlSubgraph, Metaboard, Network, Orderbook, Subgraph, + Token, +}; use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYamlEmitter; @@ -48,11 +51,12 @@ impl OrderbookYaml { } pub fn get_subgraph_keys(&self) -> Result, YamlError> { - let subgraphs = Subgraph::parse_all_from_yaml(self.document.clone())?; + let subgraphs = YamlSubgraph::parse_all_from_yaml(self.document.clone())?; Ok(subgraphs.keys().cloned().collect()) } pub fn get_subgraph(&self, key: &str) -> Result { - Subgraph::parse_from_yaml(self.document.clone(), key) + let yaml_subgraph = YamlSubgraph::parse_from_yaml(self.document.clone(), key)?; + Ok(yaml_subgraph.into()) } pub fn get_orderbook_keys(&self) -> Result, YamlError> { @@ -67,15 +71,15 @@ impl OrderbookYaml { let metaboards = YamlMetaboard::parse_all_from_yaml(self.document.clone())?; Ok(metaboards.keys().cloned().collect()) } - pub fn get_metaboard(&self, key: &str) -> Result { - YamlMetaboard::parse_from_yaml(self.document.clone(), key) + pub fn get_metaboard(&self, key: &str) -> Result { + let yaml_metaboard = YamlMetaboard::parse_from_yaml(self.document.clone(), key)?; + Ok(yaml_metaboard.into()) } } #[cfg(test)] mod tests { use super::*; - use crate::Metaboard; use alloy::primitives::Address; use std::str::FromStr; use url::Url; @@ -182,12 +186,12 @@ mod tests { assert_eq!(orderbook.label, Some("Primary Orderbook".to_string())); assert_eq!(ob_yaml.get_metaboard_keys().unwrap().len(), 2); - let metaboard: Metaboard = ob_yaml.get_metaboard("board1").unwrap().into(); + let metaboard = ob_yaml.get_metaboard("board1").unwrap(); assert_eq!( metaboard, Url::parse("https://meta.example.com/board1").unwrap() ); - let metaboard: Metaboard = ob_yaml.get_metaboard("board2").unwrap().into(); + let metaboard = ob_yaml.get_metaboard("board2").unwrap(); assert_eq!( metaboard, Url::parse("https://meta.example.com/board2").unwrap() From 174c9db6fe63c17f9719ad53d0a423d28c4754e5 Mon Sep 17 00:00:00 2001 From: findolor Date: Tue, 17 Dec 2024 13:59:24 +0300 Subject: [PATCH 3/3] update metaboard and subgraph to be structs and refactor code --- crates/settings/src/config.rs | 18 +++++-- crates/settings/src/metaboard.rs | 61 ++++++++++++++-------- crates/settings/src/orderbook.rs | 8 +-- crates/settings/src/subgraph.rs | 56 +++++++++++++------- crates/settings/src/test.rs | 13 ++++- crates/settings/src/yaml/orderbook.rs | 23 +++----- packages/orderbook/test/js_api/gui.test.ts | 2 +- 7 files changed, 116 insertions(+), 65 deletions(-) diff --git a/crates/settings/src/config.rs b/crates/settings/src/config.rs index b8f25a010..a5340e6fc 100644 --- a/crates/settings/src/config.rs +++ b/crates/settings/src/config.rs @@ -2,8 +2,10 @@ use super::config_source::ConfigSourceError; use crate::*; use alloy::primitives::U256; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; use std::sync::Arc; +use std::{collections::HashMap, sync::RwLock}; +use strict_yaml_rust::StrictYaml; +use subgraph::Subgraph; use thiserror::Error; use typeshare::typeshare; use url::Url; @@ -45,7 +47,6 @@ pub struct Config { #[cfg(target_family = "wasm")] impl_all_wasm_traits!(Config); -pub type Subgraph = Url; pub type Metaboard = Url; pub type Vault = U256; @@ -95,7 +96,16 @@ impl TryFrom for Config { let subgraphs = item .subgraphs .into_iter() - .map(|(name, subgraph)| Ok((name, Arc::new(subgraph)))) + .map(|(name, subgraph)| { + Ok(( + name.clone(), + Arc::new(Subgraph { + document: Arc::new(RwLock::new(StrictYaml::String("".to_string()))), + key: name.clone(), + url: subgraph.clone(), + }), + )) + }) .collect::>, ParseConfigSourceError>>()?; let metaboards = item @@ -343,7 +353,7 @@ mod tests { // Verify subgraphs assert_eq!(config.subgraphs.len(), 1); let mainnet_subgraph = config.subgraphs.get("mainnet").unwrap(); - assert_eq!(mainnet_subgraph.as_str(), "https://mainnet.subgraph/"); + assert_eq!(mainnet_subgraph.url.as_str(), "https://mainnet.subgraph/"); // Verify orderbooks assert_eq!(config.orderbooks.len(), 1); diff --git a/crates/settings/src/metaboard.rs b/crates/settings/src/metaboard.rs index 4999d9cff..80c9c1f25 100644 --- a/crates/settings/src/metaboard.rs +++ b/crates/settings/src/metaboard.rs @@ -1,25 +1,35 @@ -use crate::config::Metaboard; use crate::yaml::{require_hash, require_string, YamlError, YamlParsableHash}; +use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, sync::{Arc, RwLock}, }; use strict_yaml_rust::StrictYaml; +use typeshare::typeshare; use url::{ParseError, Url}; -#[derive(Clone, Debug)] -pub struct YamlMetaboard(Metaboard); +#[typeshare] +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "kebab-case")] +#[serde(default)] +pub struct Metaboard { + #[serde(skip)] + pub document: Arc>, + pub key: String, + #[typeshare(typescript(type = "string"))] + pub url: Url, +} -impl YamlMetaboard { +impl Metaboard { pub fn validate_url(value: &str) -> Result { Url::parse(value) } } -impl YamlParsableHash for YamlMetaboard { +impl YamlParsableHash for Metaboard { fn parse_all_from_yaml( document: Arc>, - ) -> Result, YamlError> { + ) -> Result, YamlError> { let document_read = document.read().map_err(|_| YamlError::ReadLockError)?; let metaboards_hash = require_hash( &document_read, @@ -32,7 +42,7 @@ impl YamlParsableHash for YamlMetaboard { .map(|(key_yaml, metaboard_yaml)| { let metaboard_key = key_yaml.as_str().unwrap_or_default().to_string(); - let url = YamlMetaboard::validate_url(&require_string( + let url = Metaboard::validate_url(&require_string( metaboard_yaml, None, Some(format!( @@ -40,21 +50,31 @@ impl YamlParsableHash for YamlMetaboard { )), )?)?; - Ok((metaboard_key, YamlMetaboard(url))) + let metaboard = Metaboard { + document: document.clone(), + key: metaboard_key.clone(), + url, + }; + + Ok((metaboard_key, metaboard)) }) .collect() } } -impl From for YamlMetaboard { - fn from(value: Metaboard) -> Self { - YamlMetaboard(value) +impl Default for Metaboard { + fn default() -> Self { + Self { + document: Arc::new(RwLock::new(StrictYaml::String("".to_string()))), + key: "".to_string(), + url: Url::parse("https://metaboard.com").unwrap(), + } } } -impl From for Metaboard { - fn from(value: YamlMetaboard) -> Self { - value.0 +impl PartialEq for Metaboard { + fn eq(&self, other: &Self) -> bool { + self.key == other.key && self.url == other.url } } @@ -68,7 +88,7 @@ mod test { let yaml = r#" test: test "#; - let error = YamlMetaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = Metaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError("missing field: metaboards".to_string()) @@ -79,7 +99,7 @@ metaboards: TestMetaboard: test: https://metaboard.com "#; - let error = YamlMetaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = Metaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError( @@ -92,7 +112,7 @@ metaboards: TestMetaboard: - https://metaboard.com "#; - let error = YamlMetaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = Metaboard::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError( @@ -102,10 +122,9 @@ metaboards: let yaml = r#" metaboards: - TestMetaboard: https://metaboard.com + TestMetaboard: invalid-url "#; - let result = YamlMetaboard::parse_all_from_yaml(get_document(yaml)).unwrap(); - assert_eq!(result.len(), 1); - assert!(result.contains_key("TestMetaboard")); + let res = Metaboard::parse_all_from_yaml(get_document(yaml)); + assert!(res.is_err()); } } diff --git a/crates/settings/src/orderbook.rs b/crates/settings/src/orderbook.rs index 1155bd60a..ec6a838c9 100644 --- a/crates/settings/src/orderbook.rs +++ b/crates/settings/src/orderbook.rs @@ -6,7 +6,7 @@ use std::collections::HashMap; use std::str::FromStr; use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYaml; -use subgraph::YamlSubgraph; +use subgraph::Subgraph; use thiserror::Error; use typeshare::typeshare; use yaml::{optional_string, require_hash, require_string, YamlError, YamlParsableHash}; @@ -68,8 +68,8 @@ impl YamlParsableHash for Orderbook { Some(subgraph_name) => subgraph_name, None => orderbook_key.clone(), }; - let subgraph = YamlSubgraph::parse_from_yaml(document.clone(), &subgraph_name)?; - let subgraph = Arc::new(subgraph.into()); + let subgraph = + Arc::new(Subgraph::parse_from_yaml(document.clone(), &subgraph_name)?); let label = optional_string(orderbook_yaml, "label"); @@ -95,7 +95,7 @@ impl Default for Orderbook { key: "".to_string(), address: Address::ZERO, network: Arc::new(Network::default()), - subgraph: Arc::new(Subgraph::parse("https://subgraph.com").unwrap()), + subgraph: Arc::new(Subgraph::default()), label: None, } } diff --git a/crates/settings/src/subgraph.rs b/crates/settings/src/subgraph.rs index 713b7212a..a3f4b9822 100644 --- a/crates/settings/src/subgraph.rs +++ b/crates/settings/src/subgraph.rs @@ -1,25 +1,35 @@ -use crate::config::Subgraph; use crate::yaml::{require_hash, require_string, YamlError, YamlParsableHash}; +use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, sync::{Arc, RwLock}, }; use strict_yaml_rust::StrictYaml; +use typeshare::typeshare; use url::{ParseError, Url}; -#[derive(Clone, Debug)] -pub struct YamlSubgraph(Subgraph); +#[typeshare] +#[derive(Debug, Serialize, Deserialize, Clone)] +#[serde(rename_all = "kebab-case")] +#[serde(default)] +pub struct Subgraph { + #[serde(skip)] + pub document: Arc>, + pub key: String, + #[typeshare(typescript(type = "string"))] + pub url: Url, +} -impl YamlSubgraph { +impl Subgraph { pub fn validate_url(value: &str) -> Result { Url::parse(value) } } -impl YamlParsableHash for YamlSubgraph { +impl YamlParsableHash for Subgraph { fn parse_all_from_yaml( document: Arc>, - ) -> Result, YamlError> { + ) -> Result, YamlError> { let document_read = document.read().map_err(|_| YamlError::ReadLockError)?; let subgraphs_hash = require_hash( &document_read, @@ -32,7 +42,7 @@ impl YamlParsableHash for YamlSubgraph { .map(|(key_yaml, subgraph_yaml)| { let subgraph_key = key_yaml.as_str().unwrap_or_default().to_string(); - let url = YamlSubgraph::validate_url(&require_string( + let url = Subgraph::validate_url(&require_string( subgraph_yaml, None, Some(format!( @@ -40,21 +50,31 @@ impl YamlParsableHash for YamlSubgraph { )), )?)?; - Ok((subgraph_key, YamlSubgraph(url))) + let subgraph = Subgraph { + document: document.clone(), + key: subgraph_key.clone(), + url, + }; + + Ok((subgraph_key, subgraph)) }) .collect() } } -impl From for YamlSubgraph { - fn from(value: Subgraph) -> Self { - YamlSubgraph(value) +impl Default for Subgraph { + fn default() -> Self { + Self { + document: Arc::new(RwLock::new(StrictYaml::String("".to_string()))), + key: "".to_string(), + url: Url::parse("https://subgraph.com").unwrap(), + } } } -impl From for Subgraph { - fn from(value: YamlSubgraph) -> Self { - value.0 +impl PartialEq for Subgraph { + fn eq(&self, other: &Self) -> bool { + self.key == other.key && self.url == other.url } } @@ -68,7 +88,7 @@ mod test { let yaml = r#" test: test "#; - let error = YamlSubgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = Subgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError("missing field: subgraphs".to_string()) @@ -79,7 +99,7 @@ subgraphs: TestSubgraph: test: https://subgraph.com "#; - let error = YamlSubgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = Subgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError( @@ -92,7 +112,7 @@ subgraphs: TestSubgraph: - https://subgraph.com "#; - let error = YamlSubgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); + let error = Subgraph::parse_all_from_yaml(get_document(yaml)).unwrap_err(); assert_eq!( error, YamlError::ParseError( @@ -104,7 +124,7 @@ subgraphs: subgraphs: TestSubgraph: https://subgraph.com "#; - let result = YamlSubgraph::parse_all_from_yaml(get_document(yaml)).unwrap(); + let result = Subgraph::parse_all_from_yaml(get_document(yaml)).unwrap(); assert_eq!(result.len(), 1); assert!(result.contains_key("TestSubgraph")); } diff --git a/crates/settings/src/test.rs b/crates/settings/src/test.rs index 232c95c8f..6795d9bbb 100644 --- a/crates/settings/src/test.rs +++ b/crates/settings/src/test.rs @@ -2,6 +2,7 @@ use crate::*; use alloy::primitives::Address; use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYaml; +use subgraph::Subgraph; // Helper function to create a mock network pub fn mock_network() -> Arc { @@ -32,7 +33,11 @@ pub fn mock_orderbook() -> Arc { key: "".to_string(), label: Some("Orderbook1".into()), address: Address::repeat_byte(0x04), - subgraph: Arc::new("https://subgraph.com".parse().unwrap()), + subgraph: Arc::new(Subgraph { + document: Arc::new(RwLock::new(StrictYaml::String("".to_string()))), + key: "".to_string(), + url: "https://subgraph.com".parse().unwrap(), + }), network: mock_network(), }) } @@ -77,5 +82,9 @@ pub fn mock_plot(name: &str) -> (String, Plot) { } pub fn mock_subgraph() -> Arc { - Arc::new("http://subgraph.com".parse().unwrap()) + Arc::new(Subgraph { + document: Arc::new(RwLock::new(StrictYaml::String("".to_string()))), + key: "".to_string(), + url: "https://subgraph.com".parse().unwrap(), + }) } diff --git a/crates/settings/src/yaml/orderbook.rs b/crates/settings/src/yaml/orderbook.rs index 1e44a2747..34c83ada7 100644 --- a/crates/settings/src/yaml/orderbook.rs +++ b/crates/settings/src/yaml/orderbook.rs @@ -1,8 +1,5 @@ use super::*; -use crate::{ - metaboard::YamlMetaboard, subgraph::YamlSubgraph, Metaboard, Network, Orderbook, Subgraph, - Token, -}; +use crate::{metaboard::Metaboard, subgraph::Subgraph, Network, Orderbook, Token}; use std::sync::{Arc, RwLock}; use strict_yaml_rust::StrictYamlEmitter; @@ -51,12 +48,11 @@ impl OrderbookYaml { } pub fn get_subgraph_keys(&self) -> Result, YamlError> { - let subgraphs = YamlSubgraph::parse_all_from_yaml(self.document.clone())?; + let subgraphs = Subgraph::parse_all_from_yaml(self.document.clone())?; Ok(subgraphs.keys().cloned().collect()) } pub fn get_subgraph(&self, key: &str) -> Result { - let yaml_subgraph = YamlSubgraph::parse_from_yaml(self.document.clone(), key)?; - Ok(yaml_subgraph.into()) + Subgraph::parse_from_yaml(self.document.clone(), key) } pub fn get_orderbook_keys(&self) -> Result, YamlError> { @@ -68,12 +64,11 @@ impl OrderbookYaml { } pub fn get_metaboard_keys(&self) -> Result, YamlError> { - let metaboards = YamlMetaboard::parse_all_from_yaml(self.document.clone())?; + let metaboards = Metaboard::parse_all_from_yaml(self.document.clone())?; Ok(metaboards.keys().cloned().collect()) } pub fn get_metaboard(&self, key: &str) -> Result { - let yaml_metaboard = YamlMetaboard::parse_from_yaml(self.document.clone(), key)?; - Ok(yaml_metaboard.into()) + Metaboard::parse_from_yaml(self.document.clone(), key) } } @@ -171,7 +166,7 @@ mod tests { assert_eq!(ob_yaml.get_subgraph_keys().unwrap().len(), 2); let subgraph = ob_yaml.get_subgraph("mainnet").unwrap(); assert_eq!( - subgraph, + subgraph.url, Url::parse("https://api.thegraph.com/subgraphs/name/xyz").unwrap() ); @@ -186,14 +181,12 @@ mod tests { assert_eq!(orderbook.label, Some("Primary Orderbook".to_string())); assert_eq!(ob_yaml.get_metaboard_keys().unwrap().len(), 2); - let metaboard = ob_yaml.get_metaboard("board1").unwrap(); assert_eq!( - metaboard, + ob_yaml.get_metaboard("board1").unwrap().url, Url::parse("https://meta.example.com/board1").unwrap() ); - let metaboard = ob_yaml.get_metaboard("board2").unwrap(); assert_eq!( - metaboard, + ob_yaml.get_metaboard("board2").unwrap().url, Url::parse("https://meta.example.com/board2").unwrap() ); } diff --git a/packages/orderbook/test/js_api/gui.test.ts b/packages/orderbook/test/js_api/gui.test.ts index 52a08ef3f..4ad4a8efc 100644 --- a/packages/orderbook/test/js_api/gui.test.ts +++ b/packages/orderbook/test/js_api/gui.test.ts @@ -592,7 +592,7 @@ describe('Rain Orderbook JS API Package Bindgen Tests - Gui', async function () describe('state management tests', async () => { let serializedState = - 'H4sIAAAAAAAA_3WNSQoCUQxEu1VEb-FaUPLHJDuP4BX-kC-N0IL2wuMrmHYhWJuXoag6dR9x4ALZsmuEJgZbqFJsXohLEQOVs22QRJBd8uJtxrdJCiasGMjRQnO2yjyMdRgvB9PrAfqNTue7PGTamf38eRrrfIhIDCmXKu3f_htuu1lLpQGYC9fK6XaV0XydK2WAY3wBw3-Y7v0AAAA='; + 'H4sIAAAAAAAA_3WNTQrCMBCFWxXRW7gWlJlMkk52HsErmGRGilBBu_D4Ik5dCH2bb37fOzVfaVHJWn1i7lg4FMKioB5B2JGmqEgZOPnQxQwukwQPSgE-b4nqwny2xtwPtR-uB2xtAO3GqvNDnjLucD9tXujIh9hxgksuVXSu_zd3zaSlEQGmwLVxvN9kwN_lyhjgGN-eYsVu_QAAAA=='; let gui: DotrainOrderGui; beforeAll(async () => { mockServer