Skip to content

Commit

Permalink
Merge pull request #1081 from rainlanguage/2024-12-14-ob-yaml-metaboards
Browse files Browse the repository at this point in the history
Implementing metaboards in OrderbookYaml
  • Loading branch information
hardyjosh authored Dec 18, 2024
2 parents 3698581 + 096ffa1 commit 9d3ca21
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 21 deletions.
18 changes: 14 additions & 4 deletions crates/settings/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -95,7 +96,16 @@ impl TryFrom<ConfigSource> 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::<Result<HashMap<String, Arc<Subgraph>>, ParseConfigSourceError>>()?;

let metaboards = item
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions crates/settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
130 changes: 130 additions & 0 deletions crates/settings/src/metaboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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};

#[typeshare]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
#[serde(default)]
pub struct Metaboard {
#[serde(skip)]
pub document: Arc<RwLock<StrictYaml>>,
pub key: String,
#[typeshare(typescript(type = "string"))]
pub url: Url,
}

impl Metaboard {
pub fn validate_url(value: &str) -> Result<Url, ParseError> {
Url::parse(value)
}
}

impl YamlParsableHash for Metaboard {
fn parse_all_from_yaml(
document: Arc<RwLock<StrictYaml>>,
) -> Result<HashMap<String, Metaboard>, 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 = Metaboard::validate_url(&require_string(
metaboard_yaml,
None,
Some(format!(
"metaboard value must be a string for key: {metaboard_key}"
)),
)?)?;

let metaboard = Metaboard {
document: document.clone(),
key: metaboard_key.clone(),
url,
};

Ok((metaboard_key, metaboard))
})
.collect()
}
}

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 PartialEq for Metaboard {
fn eq(&self, other: &Self) -> bool {
self.key == other.key && self.url == other.url
}
}

#[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 = Metaboard::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 = Metaboard::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 = Metaboard::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: invalid-url
"#;
let res = Metaboard::parse_all_from_yaml(get_document(yaml));
assert!(res.is_err());
}
}
8 changes: 5 additions & 3 deletions crates/settings/src/orderbook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::collections::HashMap;
use std::str::FromStr;
use std::sync::{Arc, RwLock};
use strict_yaml_rust::StrictYaml;
use subgraph::Subgraph;
use thiserror::Error;
use typeshare::typeshare;
use yaml::{optional_string, require_hash, require_string, YamlError, YamlParsableHash};
Expand Down Expand Up @@ -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 =
Arc::new(Subgraph::parse_from_yaml(document.clone(), &subgraph_name)?);

let label = optional_string(orderbook_yaml, "label");

Expand All @@ -76,7 +78,7 @@ impl YamlParsableHash for Orderbook {
key: orderbook_key.clone(),
address,
network: Arc::new(network),
subgraph: Arc::new(subgraph),
subgraph,
label,
};

Expand All @@ -93,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,
}
}
Expand Down
59 changes: 54 additions & 5 deletions crates/settings/src/subgraph.rs
Original file line number Diff line number Diff line change
@@ -1,16 +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 url::Url;
use typeshare::typeshare;
use url::{ParseError, Url};

#[typeshare]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
#[serde(default)]
pub struct Subgraph {
#[serde(skip)]
pub document: Arc<RwLock<StrictYaml>>,
pub key: String,
#[typeshare(typescript(type = "string"))]
pub url: Url,
}

impl Subgraph {
pub fn validate_url(value: &str) -> Result<Url, ParseError> {
Url::parse(value)
}
}

impl YamlParsableHash for Subgraph {
fn parse_all_from_yaml(
document: Arc<RwLock<StrictYaml>>,
) -> Result<HashMap<String, Url>, YamlError> {
) -> Result<HashMap<String, Subgraph>, YamlError> {
let document_read = document.read().map_err(|_| YamlError::ReadLockError)?;
let subgraphs_hash = require_hash(
&document_read,
Expand All @@ -23,20 +42,42 @@ 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 = Subgraph::validate_url(&require_string(
subgraph_yaml,
None,
Some(format!(
"subgraph value must be a string for key: {subgraph_key}"
)),
)?)?;

Ok((subgraph_key, url))
let subgraph = Subgraph {
document: document.clone(),
key: subgraph_key.clone(),
url,
};

Ok((subgraph_key, subgraph))
})
.collect()
}
}

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 PartialEq for Subgraph {
fn eq(&self, other: &Self) -> bool {
self.key == other.key && self.url == other.url
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -78,5 +119,13 @@ subgraphs:
"subgraph value must be a string for key: TestSubgraph".to_string()
)
);

let yaml = r#"
subgraphs:
TestSubgraph: https://subgraph.com
"#;
let result = Subgraph::parse_all_from_yaml(get_document(yaml)).unwrap();
assert_eq!(result.len(), 1);
assert!(result.contains_key("TestSubgraph"));
}
}
13 changes: 11 additions & 2 deletions crates/settings/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Network> {
Expand Down Expand Up @@ -32,7 +33,11 @@ pub fn mock_orderbook() -> Arc<Orderbook> {
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(),
})
}
Expand Down Expand Up @@ -77,5 +82,9 @@ pub fn mock_plot(name: &str) -> (String, Plot) {
}

pub fn mock_subgraph() -> Arc<Subgraph> {
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(),
})
}
Loading

0 comments on commit 9d3ca21

Please sign in to comment.