Skip to content

Commit

Permalink
Merge pull request #824 from rainlanguage/2024-09-03-watchlist-implem…
Browse files Browse the repository at this point in the history
…entation
  • Loading branch information
hardyjosh authored Sep 6, 2024
2 parents 3b6f3a1 + 5c6a3d6 commit fc74c9a
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
22 changes: 22 additions & 0 deletions crates/settings/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub struct Config {
pub deployments: HashMap<String, Arc<Deployment>>,
pub sentry: Option<bool>,
pub raindex_version: Option<String>,
#[typeshare(typescript(type = "Record<string, string>"))]
pub watchlist: Option<HashMap<String, Arc<String>>>,
}

pub type Subgraph = Url;
Expand Down Expand Up @@ -169,6 +171,12 @@ impl TryFrom<ConfigSource> for Config {
})
.collect::<Result<HashMap<String, Arc<Chart>>, ParseConfigSourceError>>()?;

let watchlist = item.watchlist.map(|wl| {
wl.into_iter()
.map(|(name, address)| (name, Arc::new(address)))
.collect::<HashMap<String, Arc<String>>>()
});

let config = Config {
raindex_version: item.raindex_version,
networks,
Expand All @@ -182,6 +190,7 @@ impl TryFrom<ConfigSource> for Config {
charts,
deployments,
sentry: item.sentry,
watchlist,
};

Ok(config)
Expand Down Expand Up @@ -273,6 +282,10 @@ mod tests {
let charts = HashMap::new();
let deployments = HashMap::new();
let sentry = Some(true);
let watchlist = Some(HashMap::from([(
"name-one".to_string(),
"address-one".to_string(),
)]));

let config_string = ConfigSource {
raindex_version: Some("0x123".to_string()),
Expand All @@ -288,6 +301,7 @@ mod tests {
charts,
deployments,
sentry,
watchlist,
};

let config_result = Config::try_from(config_string);
Expand Down Expand Up @@ -346,5 +360,13 @@ mod tests {

// Verify raindex_version
assert_eq!(config.raindex_version, Some("0x123".to_string()));

// Verify watchlist
assert!(config.watchlist.is_some());
let watchlist = config.watchlist.as_ref().unwrap();
assert_eq!(watchlist.len(), 1);
let (name, address) = watchlist.iter().next().unwrap();
assert_eq!(name, "name-one");
assert_eq!(address.as_str(), "address-one");
}
}
12 changes: 11 additions & 1 deletion crates/settings/src/config_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub struct ConfigSource {
pub sentry: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub raindex_version: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub watchlist: Option<HashMap<String, String>>,
}

#[typeshare]
Expand Down Expand Up @@ -371,7 +373,11 @@ deployments:
scenario: mainScenario
order: buyETH
sentry: true"#,
sentry: true
watchlist:
name-one: address-one
name-two: address-two"#,
mocked_chain_id_server.url("/json")
);

Expand Down Expand Up @@ -476,6 +482,10 @@ sentry: true"#,
assert_eq!(order.orderbook, expected_order.orderbook);

assert_eq!(config.raindex_version, Some("123".to_string()));

let watchlist = config.watchlist.unwrap();
assert_eq!(watchlist.get("name-one").unwrap(), "address-one");
assert_eq!(watchlist.get("name-two").unwrap(), "address-two");
}

#[tokio::test]
Expand Down
41 changes: 41 additions & 0 deletions crates/settings/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub enum MergeError {

#[error("There is already a remote networks definition called {0}")]
RemoteNetworksCollision(String),

#[error("There is already a watchlist called {0}")]
WatchlistCollision(String),
}

impl ConfigSource {
Expand Down Expand Up @@ -147,6 +150,22 @@ impl ConfigSource {
(Some(_), Some(_)) => Err(MergeError::DeploymentCollision("sentry".into())),
}?;

// Watchlist
match (&mut self.watchlist, other.watchlist) {
(Some(watchlist), Some(other_watchlist)) => {
for (key, value) in other_watchlist {
if watchlist.contains_key(&key) {
return Err(MergeError::WatchlistCollision(key));
}
watchlist.insert(key, value);
}
}
(None, Some(other_watchlist)) => {
self.watchlist = Some(other_watchlist);
}
_ => {}
}

Ok(())
}
}
Expand Down Expand Up @@ -251,6 +270,22 @@ impl Config {
(Some(_), Some(_)) => Err(MergeError::DeploymentCollision("sentry".into())),
}?;

// Watchlist
match (&mut self.watchlist, other.watchlist) {
(Some(watchlist), Some(other_watchlist)) => {
for (key, value) in other_watchlist {
if watchlist.contains_key(&key) {
return Err(MergeError::WatchlistCollision(key));
}
watchlist.insert(key, value);
}
}
(None, Some(other_watchlist)) => {
self.watchlist = Some(other_watchlist);
}
_ => {}
}

Ok(())
}
}
Expand All @@ -277,6 +312,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: None,
};

let other = ConfigSource {
Expand All @@ -293,6 +329,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: None,
};

assert_eq!(config.merge(other), Ok(()));
Expand All @@ -314,6 +351,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: None,
};

let mut other = ConfigSource {
Expand All @@ -330,6 +368,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: None,
};

// Add a collision to cause an unsuccessful merge
Expand Down Expand Up @@ -365,6 +404,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: None,
};

let mut other = ConfigSource {
Expand All @@ -381,6 +421,7 @@ mod tests {
networks: HashMap::new(),
deployments: HashMap::new(),
sentry: None,
watchlist: None,
};

other.metaboards.insert(
Expand Down
1 change: 1 addition & 0 deletions crates/settings/src/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ mod tests {
charts: HashMap::new(), // Assuming no charts for simplification
deployments: HashMap::new(),
sentry: None,
watchlist: None, // Assuming no watchlist for simplification
};

// Perform the conversion
Expand Down
4 changes: 4 additions & 0 deletions tauri-app/src/lib/mocks/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const mockConfigSource: ConfigSource = {
metaboards: {
metaboard1: 'https://example.com/metaboard1',
},
watchlist: {
name_one: 'address_one',
name_two: 'address_two',
},
};

export const mockSettingsStore = writable<ConfigSource>(mockConfigSource);
3 changes: 3 additions & 0 deletions tauri-app/src/lib/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ export const hasRequiredSettings = derived(
$activeNetworkRef !== undefined && $activeOrderbookRef !== undefined,
);

// watchlist
export const watchlist = derived(settings, ($settings) => $settings?.watchlist ?? []);

// When networks / orderbooks settings updated, reset active network / orderbook
settings.subscribe(async () => {
const $settings = await settings.load();
Expand Down
8 changes: 8 additions & 0 deletions tauri-app/src/tests/pickConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ export const config: Config = {
},
},
},
watchlist: {
name_one: 'address_one',
name_two: 'address_two',
},
};

export const configSource: ConfigSource = {
Expand Down Expand Up @@ -214,6 +218,10 @@ export const configSource: ConfigSource = {
order: 'sell',
},
},
watchlist: {
name_one: 'address_one',
name_two: 'address_two',
},
};

test('pick deployments', () => {
Expand Down

0 comments on commit fc74c9a

Please sign in to comment.