diff --git a/crates/settings/src/config.rs b/crates/settings/src/config.rs index a587e1ccf..4d44414f6 100644 --- a/crates/settings/src/config.rs +++ b/crates/settings/src/config.rs @@ -108,7 +108,7 @@ impl TryFrom for Config { .map(|(name, order)| { Ok(( name, - Arc::new(order.try_into_order(&networks, &deployers, &orderbooks, &tokens)?), + Arc::new(order.try_into_order(&deployers, &orderbooks, &tokens)?), )) }) .collect::>, ParseConfigSourceError>>()?; diff --git a/crates/settings/src/config_source.rs b/crates/settings/src/config_source.rs index 1ca266ef1..30dd3e41e 100644 --- a/crates/settings/src/config_source.rs +++ b/crates/settings/src/config_source.rs @@ -115,7 +115,6 @@ pub struct IOString { pub struct OrderConfigSource { pub inputs: Vec, pub outputs: Vec, - pub network: NetworkRef, pub deployer: Option, pub orderbook: Option, } @@ -234,7 +233,6 @@ orders: outputs: - token: dai vault-id: 3 - network: mainnet deployer: mainDeployer orderbook: mainnetOrderbook diff --git a/crates/settings/src/order.rs b/crates/settings/src/order.rs index 1d0874df4..cbbd4cf5f 100644 --- a/crates/settings/src/order.rs +++ b/crates/settings/src/order.rs @@ -50,17 +50,11 @@ pub enum ParseOrderConfigSourceError { impl OrderConfigSource { pub fn try_into_order( self, - networks: &HashMap>, deployers: &HashMap>, orderbooks: &HashMap>, tokens: &HashMap>, ) -> Result { - let network = networks - .get(&self.network) - .ok_or(ParseOrderConfigSourceError::NetworkNotFoundError( - self.network.clone(), - )) - .map(Arc::clone)?; + let mut network = None; let deployer = self .deployer @@ -71,10 +65,15 @@ impl OrderConfigSource { ParseDeployerConfigSourceError::NetworkNotFoundError(deployer_name.clone()), )) .map(|v| { - if v.network == network { - Ok(v.clone()) + if let Some(n) = &network { + if v.network == *n { + Ok(v.clone()) + } else { + Err(ParseOrderConfigSourceError::NetworkNotMatch) + } } else { - Err(ParseOrderConfigSourceError::NetworkNotMatch) + network = Some(v.network.clone()); + Ok(v.clone()) } })? }) @@ -91,10 +90,15 @@ impl OrderConfigSource { ), )) .map(|v| { - if v.network == network { - Ok(v.clone()) + if let Some(n) = &network { + if v.network == *n { + Ok(v.clone()) + } else { + Err(ParseOrderConfigSourceError::NetworkNotMatch) + } } else { - Err(ParseOrderConfigSourceError::NetworkNotMatch) + network = Some(v.network.clone()); + Ok(v.clone()) } })? }) @@ -110,13 +114,21 @@ impl OrderConfigSource { ParseTokenConfigSourceError::NetworkNotFoundError(input.token.clone()), )) .map(|v| { - if v.network == network { + if let Some(n) = &network { + if v.network == *n { + Ok(OrderIO { + token: v.clone(), + vault_id: input.vault_id, + }) + } else { + Err(ParseOrderConfigSourceError::NetworkNotMatch) + } + } else { + network = Some(v.network.clone()); Ok(OrderIO { token: v.clone(), vault_id: input.vault_id, }) - } else { - Err(ParseOrderConfigSourceError::NetworkNotMatch) } })? }) @@ -132,13 +144,21 @@ impl OrderConfigSource { ParseTokenConfigSourceError::NetworkNotFoundError(output.token.clone()), )) .map(|v| { - if v.network == network { + if let Some(n) = &network { + if v.network == *n { + Ok(OrderIO { + token: v.clone(), + vault_id: output.vault_id, + }) + } else { + Err(ParseOrderConfigSourceError::NetworkNotMatch) + } + } else { + network = Some(v.network.clone()); Ok(OrderIO { token: v.clone(), vault_id: output.vault_id, }) - } else { - Err(ParseOrderConfigSourceError::NetworkNotMatch) } })? }) @@ -147,7 +167,9 @@ impl OrderConfigSource { Ok(Order { inputs, outputs, - network, + network: network.ok_or(ParseOrderConfigSourceError::NetworkNotFoundError( + String::new(), + ))?, deployer, orderbook, }) @@ -180,7 +202,6 @@ mod tests { tokens.insert("Token2".to_string(), token_output.clone()); let order_string = OrderConfigSource { - network: "Local Testnet".to_string(), deployer: Some("Deployer1".to_string()), orderbook: Some("Orderbook1".to_string()), inputs: vec![IOString { @@ -193,7 +214,7 @@ mod tests { }], }; - let result = order_string.try_into_order(&networks, &deployers, &orderbooks, &tokens); + let result = order_string.try_into_order(&deployers, &orderbooks, &tokens); assert!(result.is_ok()); let order = result.unwrap(); @@ -220,22 +241,14 @@ mod tests { #[test] fn test_try_into_order_network_not_found_error() { - let networks = HashMap::new(); // Empty network map - let order_string = OrderConfigSource { - network: "Nonexistent Network".to_string(), deployer: None, orderbook: None, inputs: vec![], outputs: vec![], }; - let result = order_string.try_into_order( - &networks, - &HashMap::new(), - &HashMap::new(), - &HashMap::new(), - ); + let result = order_string.try_into_order(&HashMap::new(), &HashMap::new(), &HashMap::new()); assert!(matches!( result, Err(ParseOrderConfigSourceError::NetworkNotFoundError(_)) @@ -244,19 +257,16 @@ mod tests { #[test] fn test_try_into_order_deployer_not_found_error() { - let networks = HashMap::from([("Local Testnet".to_string(), mock_network())]); let deployers = HashMap::new(); // Empty deployer map let order_string = OrderConfigSource { - network: "Local Testnet".to_string(), deployer: Some("Nonexistent Deployer".to_string()), orderbook: None, inputs: vec![], outputs: vec![], }; - let result = - order_string.try_into_order(&networks, &deployers, &HashMap::new(), &HashMap::new()); + let result = order_string.try_into_order(&deployers, &HashMap::new(), &HashMap::new()); assert!(matches!( result, Err(ParseOrderConfigSourceError::DeployerParseError(_)) @@ -265,19 +275,16 @@ mod tests { #[test] fn test_try_into_order_orderbook_not_found_error() { - let networks = HashMap::from([("Local Testnet".to_string(), mock_network())]); let orderbooks = HashMap::new(); // Empty orderbook map let order_string = OrderConfigSource { - network: "Local Testnet".to_string(), deployer: None, orderbook: Some("Nonexistent Orderbook".to_string()), inputs: vec![], outputs: vec![], }; - let result = - order_string.try_into_order(&networks, &HashMap::new(), &orderbooks, &HashMap::new()); + let result = order_string.try_into_order(&HashMap::new(), &orderbooks, &HashMap::new()); assert!(matches!( result, Err(ParseOrderConfigSourceError::OrderbookParseError(_)) @@ -286,11 +293,9 @@ mod tests { #[test] fn test_try_into_order_token_not_found_error() { - let networks = HashMap::from([("Local Testnet".to_string(), mock_network())]); let tokens = HashMap::new(); // Empty token map let order_string = OrderConfigSource { - network: "Local Testnet".to_string(), deployer: None, orderbook: None, inputs: vec![IOString { @@ -300,8 +305,7 @@ mod tests { outputs: vec![], }; - let result = - order_string.try_into_order(&networks, &HashMap::new(), &HashMap::new(), &tokens); + let result = order_string.try_into_order(&HashMap::new(), &HashMap::new(), &tokens); assert!(matches!( result, Err(ParseOrderConfigSourceError::TokenParseError(_)) diff --git a/tauri-app/src/lib/components/DropdownRadio.svelte b/tauri-app/src/lib/components/DropdownRadio.svelte index 2e0808159..145a7c24f 100644 --- a/tauri-app/src/lib/components/DropdownRadio.svelte +++ b/tauri-app/src/lib/components/DropdownRadio.svelte @@ -1,6 +1,7 @@ - {#each Object.entries(options) as [ref, option]} + {#each optionsSorted as [ref, option]}
diff --git a/tauri-app/src/lib/stores/settings.ts b/tauri-app/src/lib/stores/settings.ts index 2577e1376..10925c649 100644 --- a/tauri-app/src/lib/stores/settings.ts +++ b/tauri-app/src/lib/stores/settings.ts @@ -9,36 +9,23 @@ import { toasts } from './toasts'; import { pickBy } from 'lodash'; import { parseConfigSource } from '$lib/services/config'; -const emptyConfig = { - deployments: {}, - networks: {}, - orderbooks: {}, - orders: {}, - subgraphs: {}, - tokens: {}, - deployers: {}, - scenarios: {}, - charts: {} -} as ConfigSource; - // general export const settingsText = cachedWritableStore('settings', "", (s) => s, (s) => s); export const settingsFile = textFileStore('Orderbook Settings Yaml', ['yml', 'yaml'], get(settingsText)); -export const settings = asyncDerived(settingsText, async ($settingsText): Promise => { +export const settings = asyncDerived(settingsText, async ($settingsText): Promise => { try { const config: ConfigSource = await parseConfigSource($settingsText); return config; } catch(e) { toasts.error(e as string); - return emptyConfig; } -}, { initial: emptyConfig }); +}); // networks export const activeNetworkRef = cachedWritableStringOptional("settings.activeNetworkRef"); export const activeNetwork = asyncDerived([settings, activeNetworkRef], async ([$settings, $activeNetworkRef]) => { await settings.load(); - return ($activeNetworkRef !== undefined && $settings.networks !== undefined) ? $settings.networks[$activeNetworkRef] : undefined; + return ($activeNetworkRef !== undefined && $settings?.networks !== undefined) ? $settings.networks[$activeNetworkRef] : undefined; }); export const rpcUrl = derived(activeNetwork, ($activeNetwork) => $activeNetwork?.rpc); export const chainId = derived(activeNetwork, ($activeNetwork) => $activeNetwork?.['chain-id']); @@ -58,16 +45,25 @@ export const orderbookAddress = derived(activeOrderbook, ($activeOrderbook) => $ export const hasRequiredSettings = derived([activeNetworkRef, activeOrderbookRef], ([$activeNetworkRef, $activeOrderbookRef]) => $activeNetworkRef !== undefined && $activeOrderbookRef !== undefined); // When networks / orderbooks settings updated, reset active network / orderbook -settings.subscribe(async ($settings) => { +settings.subscribe(async () => { await settings.load(); const $activeNetworkRef = get(activeNetworkRef); const $activeOrderbookRef = get(activeOrderbookRef); + const $settings = get(settings); - if(!$settings.networks || $activeNetworkRef === undefined || !Object.keys($settings.networks).includes($activeNetworkRef)) { + if( + $settings?.networks === undefined + || $activeNetworkRef === undefined + || ($settings?.networks !== undefined && $activeNetworkRef !== undefined && !Object.keys($settings.networks).includes($activeNetworkRef)) + ) { resetActiveNetworkRef(); } - if(!$settings.orderbooks || $activeOrderbookRef === undefined || !Object.keys($settings.orderbooks).includes($activeOrderbookRef)) { + if( + !$settings?.orderbooks === undefined + || $activeOrderbookRef === undefined + || ($settings?.orderbooks !== undefined && $activeOrderbookRef !== undefined && !Object.keys($settings.orderbooks).includes($activeOrderbookRef)) + ) { resetActiveOrderbookRef(); } }); @@ -102,8 +98,9 @@ function resetActiveOrderbookRef() { // reset active orderbook to first available, otherwise undefined -function resetActiveNetworkRef() { - const $networks = get(settings).networks; +async function resetActiveNetworkRef() { + await settings.load(); + const $networks = get(settings)?.networks; if($networks !== undefined && Object.keys($networks).length > 0) { activeNetworkRef.set(Object.keys($networks)[0]);