From 27458d9f8e6ca1a62ca8c15dc6b70a42ccc05bc4 Mon Sep 17 00:00:00 2001 From: findolor Date: Fri, 14 Feb 2025 17:58:43 +0300 Subject: [PATCH 1/7] convert set vault ids to work with optional and empty strings --- crates/js_api/src/gui/order_operations.rs | 2 +- crates/js_api/src/gui/state_management.rs | 12 ++++----- crates/settings/src/order.rs | 33 ++++++++++++++++------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/crates/js_api/src/gui/order_operations.rs b/crates/js_api/src/gui/order_operations.rs index bbe4fbcf0..22f8b7122 100644 --- a/crates/js_api/src/gui/order_operations.rs +++ b/crates/js_api/src/gui/order_operations.rs @@ -310,7 +310,7 @@ impl DotrainOrderGui { &mut self, is_input: bool, index: u8, - vault_id: String, + vault_id: Option, ) -> Result<(), GuiError> { let deployment = self.get_current_deployment()?; self.dotrain_order diff --git a/crates/js_api/src/gui/state_management.rs b/crates/js_api/src/gui/state_management.rs index e1293c5a4..4bd12feca 100644 --- a/crates/js_api/src/gui/state_management.rs +++ b/crates/js_api/src/gui/state_management.rs @@ -220,13 +220,11 @@ impl DotrainOrderGui { &state.selected_deployment, )?; for ((is_input, index), vault_id) in state.vault_ids { - if let Some(vault_id) = vault_id { - dotrain_order_gui - .dotrain_order - .dotrain_yaml() - .get_order(&order_key) - .and_then(|mut order| order.update_vault_id(is_input, index, vault_id))?; - } + dotrain_order_gui + .dotrain_order + .dotrain_yaml() + .get_order(&order_key) + .and_then(|mut order| order.update_vault_id(is_input, index, vault_id))?; } Ok(dotrain_order_gui) diff --git a/crates/settings/src/order.rs b/crates/settings/src/order.rs index 767e2344f..7e3658531 100644 --- a/crates/settings/src/order.rs +++ b/crates/settings/src/order.rs @@ -69,9 +69,15 @@ impl Order { &mut self, is_input: bool, index: u8, - vault_id: String, + vault_id: Option, ) -> Result { - let new_vault_id = Order::validate_vault_id(&vault_id)?; + let new_vault_id = vault_id.and_then(|v| { + if v.is_empty() { + None + } else { + Some(Order::validate_vault_id(&v).ok()?) + } + }); let mut document = self .document @@ -91,14 +97,23 @@ impl Order { { if let Some(item) = vec.get_mut(index as usize) { if let StrictYaml::Hash(ref mut item_hash) = item { - item_hash.insert( - StrictYaml::String("vault-id".to_string()), - StrictYaml::String(vault_id.to_string()), - ); - if is_input { - self.inputs[index as usize].vault_id = Some(new_vault_id); + if let Some(vault_id) = new_vault_id { + item_hash.insert( + StrictYaml::String("vault-id".to_string()), + StrictYaml::String(vault_id.to_string()), + ); + if is_input { + self.inputs[index as usize].vault_id = Some(vault_id); + } else { + self.outputs[index as usize].vault_id = Some(vault_id); + } } else { - self.outputs[index as usize].vault_id = Some(new_vault_id); + item_hash.remove(&StrictYaml::String("vault-id".to_string())); + if is_input { + self.inputs[index as usize].vault_id = None; + } else { + self.outputs[index as usize].vault_id = None; + } } } else { return Err(YamlError::Field { From 7cd74d1e0a706d9c2b7e4de6c498eb0fa8d96bf4 Mon Sep 17 00:00:00 2001 From: findolor Date: Fri, 14 Feb 2025 17:58:48 +0300 Subject: [PATCH 2/7] update tests --- packages/orderbook/test/js_api/gui.test.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/orderbook/test/js_api/gui.test.ts b/packages/orderbook/test/js_api/gui.test.ts index 192a24c70..59e17a70d 100644 --- a/packages/orderbook/test/js_api/gui.test.ts +++ b/packages/orderbook/test/js_api/gui.test.ts @@ -1200,6 +1200,12 @@ ${dotrainWithoutVaultIds}`; const vaultIds: IOVaultIds = gui.getVaultIds(); assert.equal(vaultIds.get('input')?.[0], '0x123'); assert.equal(vaultIds.get('output')?.[0], '0x234'); + + gui.setVaultId(true, 0, undefined); + assert.equal(gui.getVaultIds().get('input')?.[0], undefined); + + gui.setVaultId(false, 0, ''); + assert.equal(gui.getVaultIds().get('output')?.[0], undefined); }); it('should skip deposits with zero amount for deposit calldata', async () => { From aaf2ff1cba8047f7e9b06dc550c9851c557ee9e1 Mon Sep 17 00:00:00 2001 From: findolor Date: Fri, 14 Feb 2025 18:58:18 +0300 Subject: [PATCH 3/7] return error on invalid values --- crates/settings/src/order.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/settings/src/order.rs b/crates/settings/src/order.rs index 7e3658531..4cc6aeeff 100644 --- a/crates/settings/src/order.rs +++ b/crates/settings/src/order.rs @@ -71,13 +71,30 @@ impl Order { index: u8, vault_id: Option, ) -> Result { - let new_vault_id = vault_id.and_then(|v| { + let new_vault_id = if let Some(ref v) = vault_id { if v.is_empty() { None } else { - Some(Order::validate_vault_id(&v).ok()?) + match Order::validate_vault_id(&v) { + Ok(id) => Some(id), + Err(e) => { + return Err(YamlError::Field { + kind: FieldErrorKind::InvalidValue { + field: "vault-id".to_string(), + reason: e.to_string(), + }, + location: format!( + "index '{index}' of {} in order '{}'", + if is_input { "inputs" } else { "outputs" }, + self.key + ), + }); + } + } } - }); + } else { + None + }; let mut document = self .document @@ -782,7 +799,7 @@ pub enum ParseOrderConfigSourceError { expected: String, found: String, }, - #[error("Failed to parse vault {}", 0)] + #[error("Failed to parse vault id")] VaultParseError(#[from] alloy::primitives::ruint::ParseError), } From 4afe167c7c22c0c59d7fd9c306a1c1fd1e851e48 Mon Sep 17 00:00:00 2001 From: findolor Date: Fri, 14 Feb 2025 18:58:23 +0300 Subject: [PATCH 4/7] update tests --- crates/settings/src/yaml/dotrain.rs | 12 ++++++++---- packages/orderbook/test/js_api/gui.test.ts | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/settings/src/yaml/dotrain.rs b/crates/settings/src/yaml/dotrain.rs index 74f0a8dc1..0181a7dd1 100644 --- a/crates/settings/src/yaml/dotrain.rs +++ b/crates/settings/src/yaml/dotrain.rs @@ -548,9 +548,11 @@ mod tests { assert!(order.inputs[0].vault_id.is_none()); assert!(order.outputs[0].vault_id.is_none()); - let mut updated_order = order.update_vault_id(true, 0, "1".to_string()).unwrap(); + let mut updated_order = order + .update_vault_id(true, 0, Some("1".to_string())) + .unwrap(); let updated_order = updated_order - .update_vault_id(false, 0, "11".to_string()) + .update_vault_id(false, 0, Some("11".to_string())) .unwrap(); assert_eq!(updated_order.inputs[0].vault_id, Some(U256::from(1))); @@ -560,9 +562,11 @@ mod tests { assert_eq!(order.inputs[0].vault_id, Some(U256::from(1))); assert_eq!(order.outputs[0].vault_id, Some(U256::from(11))); - let mut updated_order = order.update_vault_id(true, 0, "3".to_string()).unwrap(); + let mut updated_order = order + .update_vault_id(true, 0, Some("3".to_string())) + .unwrap(); let updated_order = updated_order - .update_vault_id(false, 0, "33".to_string()) + .update_vault_id(false, 0, Some("33".to_string())) .unwrap(); assert_eq!(updated_order.inputs[0].vault_id, Some(U256::from(3))); assert_eq!(updated_order.outputs[0].vault_id, Some(U256::from(33))); diff --git a/packages/orderbook/test/js_api/gui.test.ts b/packages/orderbook/test/js_api/gui.test.ts index 59e17a70d..11ed141c1 100644 --- a/packages/orderbook/test/js_api/gui.test.ts +++ b/packages/orderbook/test/js_api/gui.test.ts @@ -1206,6 +1206,10 @@ ${dotrainWithoutVaultIds}`; gui.setVaultId(false, 0, ''); assert.equal(gui.getVaultIds().get('output')?.[0], undefined); + + expect(() => gui.setVaultId(true, 0, 'test')).toThrow( + "Invalid value for field 'vault-id': Failed to parse vault id in index '0' of inputs in order 'some-order'" + ); }); it('should skip deposits with zero amount for deposit calldata', async () => { From 86d1f043c09d499dfadfd8d5f4219c3fdfd78b26 Mon Sep 17 00:00:00 2001 From: findolor Date: Fri, 14 Feb 2025 18:58:32 +0300 Subject: [PATCH 5/7] update UI to show error --- .../src/lib/components/deployment/TokenIOInput.svelte | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/ui-components/src/lib/components/deployment/TokenIOInput.svelte b/packages/ui-components/src/lib/components/deployment/TokenIOInput.svelte index 886ccbc6e..2037f7536 100644 --- a/packages/ui-components/src/lib/components/deployment/TokenIOInput.svelte +++ b/packages/ui-components/src/lib/components/deployment/TokenIOInput.svelte @@ -42,8 +42,13 @@ const handleInput = async () => { const isInput = label === 'Input'; - gui?.setVaultId(isInput, i, inputValue); - handleUpdateGuiState(gui); + try { + gui?.setVaultId(isInput, i, inputValue); + handleUpdateGuiState(gui); + } catch (e) { + const errorMessage = (e as Error).message ? (e as Error).message : 'Error setting vault ID.'; + error = errorMessage; + } }; $: if (vault.token?.key) { From 80e3122e7865c6aacc64e9f4bb5f9906e782fe9d Mon Sep 17 00:00:00 2001 From: findolor Date: Sat, 15 Feb 2025 11:32:41 +0300 Subject: [PATCH 6/7] fix clippy --- crates/settings/src/order.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/settings/src/order.rs b/crates/settings/src/order.rs index 4cc6aeeff..aa8832a65 100644 --- a/crates/settings/src/order.rs +++ b/crates/settings/src/order.rs @@ -75,7 +75,7 @@ impl Order { if v.is_empty() { None } else { - match Order::validate_vault_id(&v) { + match Order::validate_vault_id(v) { Ok(id) => Some(id), Err(e) => { return Err(YamlError::Field { From a17c0aa85acae465350ae3367a110a4037c5d57c Mon Sep 17 00:00:00 2001 From: highonhopium Date: Sat, 15 Feb 2025 18:24:58 +0000 Subject: [PATCH 7/7] error msg and advanced options check --- .../components/deployment/DeploymentSteps.svelte | 16 ++++++++++------ .../components/deployment/TokenIOInput.svelte | 1 + 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/packages/ui-components/src/lib/components/deployment/DeploymentSteps.svelte b/packages/ui-components/src/lib/components/deployment/DeploymentSteps.svelte index 84e5124e5..c22f8ae34 100644 --- a/packages/ui-components/src/lib/components/deployment/DeploymentSteps.svelte +++ b/packages/ui-components/src/lib/components/deployment/DeploymentSteps.svelte @@ -227,15 +227,19 @@ const areAllTokensSelected = async () => { if (gui) { try { - allTokensSelected = gui?.areAllTokensSelected(); + allTokensSelected = gui.areAllTokensSelected(); if (!allTokensSelected) return; - const vaultIds = gui?.getVaultIds(); - const inputVaultIds = vaultIds?.get('input'); - const outputVaultIds = vaultIds?.get('output'); // if we have deposits or vault ids set, show advanced options - const deposits = gui?.getDeposits(); - if (deposits || inputVaultIds || outputVaultIds) { + const vaultIds = gui.getVaultIds(); + const inputVaultIds = vaultIds.get('input'); + const outputVaultIds = vaultIds.get('output'); + const deposits = gui.getDeposits(); + if ( + deposits.length > 0 || + (inputVaultIds && inputVaultIds.some((v) => v)) || + (outputVaultIds && outputVaultIds.some((v) => v)) + ) { showAdvancedOptions = true; } } catch (e) { diff --git a/packages/ui-components/src/lib/components/deployment/TokenIOInput.svelte b/packages/ui-components/src/lib/components/deployment/TokenIOInput.svelte index 2037f7536..55280ee78 100644 --- a/packages/ui-components/src/lib/components/deployment/TokenIOInput.svelte +++ b/packages/ui-components/src/lib/components/deployment/TokenIOInput.svelte @@ -42,6 +42,7 @@ const handleInput = async () => { const isInput = label === 'Input'; + error = ''; try { gui?.setVaultId(isInput, i, inputValue); handleUpdateGuiState(gui);