Skip to content

Commit

Permalink
add new method for parsing all the order token keys
Browse files Browse the repository at this point in the history
  • Loading branch information
findolor committed Feb 24, 2025
1 parent a7ea9ed commit cae18bb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
37 changes: 36 additions & 1 deletion crates/settings/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{yaml::FieldErrorKind, *};
use alloy::primitives::{private::rand, U256};
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
collections::{BTreeSet, HashMap},
str::FromStr,
sync::{Arc, RwLock},
};
Expand Down Expand Up @@ -434,6 +434,41 @@ impl OrderCfg {

Ok(vault_ids)
}

pub fn parse_io_token_keys(
documents: Vec<Arc<RwLock<StrictYaml>>>,
order_key: &str,
) -> Result<Vec<String>, YamlError> {
let mut token_keys = BTreeSet::new();

for document in documents {
let document_read = document.read().map_err(|_| YamlError::ReadLockError)?;

if let Ok(orders_hash) = require_hash(&document_read, Some("orders"), None) {
if let Some(order_yaml) =
orders_hash.get(&StrictYaml::String(order_key.to_string()))
{
let location = format!("order '{}'", order_key);

let inputs = require_vec(order_yaml, "inputs", Some(location.clone()))?;
let outputs = require_vec(order_yaml, "outputs", Some(location.clone()))?;

for input in inputs {
let token_key =
require_string(input, Some("token"), Some(location.clone()))?;
token_keys.insert(token_key);
}
for output in outputs {
let token_key =
require_string(output, Some("token"), Some(location.clone()))?;
token_keys.insert(token_key);
}
}
}
}

Ok(token_keys.into_iter().collect())
}
}

impl YamlParsableHash for OrderCfg {
Expand Down
5 changes: 5 additions & 0 deletions crates/settings/src/yaml/dotrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,11 @@ mod tests {
OrderCfg::parse_vault_ids(dotrain_yaml.documents.clone(), &order.key, false).unwrap();
assert_eq!(output_vault_ids.len(), 1);
assert_eq!(output_vault_ids[0], Some("2".to_string()));
let io_token_keys =
OrderCfg::parse_io_token_keys(dotrain_yaml.documents.clone(), &order.key).unwrap();
assert_eq!(io_token_keys.len(), 2);
assert_eq!(io_token_keys[0], "token1");
assert_eq!(io_token_keys[1], "token2");

let scenario_keys = dotrain_yaml.get_scenario_keys().unwrap();
assert_eq!(scenario_keys.len(), 3);
Expand Down

0 comments on commit cae18bb

Please sign in to comment.