From 1e98b26f3176b7a49f99a1113405dad4667144c3 Mon Sep 17 00:00:00 2001 From: findolor Date: Fri, 10 Jan 2025 21:11:11 +0100 Subject: [PATCH] return empty map on missing select tokens and add method to clear select token --- crates/js_api/src/gui/select_tokens.rs | 35 +++++++++++++++------- packages/orderbook/test/js_api/gui.test.ts | 32 +++++++++++++++++++- 2 files changed, 55 insertions(+), 12 deletions(-) diff --git a/crates/js_api/src/gui/select_tokens.rs b/crates/js_api/src/gui/select_tokens.rs index 005662b5d..51f501f9b 100644 --- a/crates/js_api/src/gui/select_tokens.rs +++ b/crates/js_api/src/gui/select_tokens.rs @@ -21,20 +21,22 @@ impl DotrainOrderGui { /// Get all selected tokens and their addresses /// - /// Returns a map of token name to address + /// Returns a map of token key to address #[wasm_bindgen(js_name = "getSelectTokens")] pub fn get_select_tokens(&self) -> Result { - let select_tokens = self - .select_tokens - .clone() - .ok_or(GuiError::SelectTokensNotSet)?; - Ok(SelectTokens(select_tokens)) + let gui = self.get_current_deployment()?; + + if gui.select_tokens.is_none() || self.select_tokens.is_none() { + return Ok(SelectTokens(BTreeMap::new())); + } + + Ok(SelectTokens(self.select_tokens.clone().unwrap())) } #[wasm_bindgen(js_name = "saveSelectTokenAddress")] pub async fn save_select_token_address( &mut self, - token_name: String, + key: String, address: String, ) -> Result<(), GuiError> { let deployment = self.get_current_deployment()?; @@ -42,12 +44,12 @@ impl DotrainOrderGui { .select_tokens .clone() .ok_or(GuiError::SelectTokensNotSet)?; - if !select_tokens.contains_key(&token_name) { - return Err(GuiError::TokenNotFound(token_name.clone())); + if !select_tokens.contains_key(&key) { + return Err(GuiError::TokenNotFound(key.clone())); } let address = Address::from_str(&address)?; - select_tokens.insert(token_name.clone(), address); + select_tokens.insert(key.clone(), address); self.select_tokens = Some(select_tokens); let rpc_url = deployment @@ -65,8 +67,19 @@ impl DotrainOrderGui { self.dotrain_order .orderbook_yaml() - .get_token(&token_name)? + .get_token(&key)? .update_address(&address.to_string())?; Ok(()) } + + #[wasm_bindgen(js_name = "clearSelectTokenAddress")] + pub fn clear_select_token_address(&mut self, key: String) -> Result<(), GuiError> { + let mut select_tokens = self + .select_tokens + .clone() + .ok_or(GuiError::SelectTokensNotSet)?; + select_tokens.insert(key.clone(), Address::ZERO); + self.select_tokens = Some(select_tokens); + Ok(()) + } } diff --git a/packages/orderbook/test/js_api/gui.test.ts b/packages/orderbook/test/js_api/gui.test.ts index 880f138c8..1eb5ac5c6 100644 --- a/packages/orderbook/test/js_api/gui.test.ts +++ b/packages/orderbook/test/js_api/gui.test.ts @@ -983,7 +983,7 @@ describe('Rain Orderbook JS API Package Bindgen Tests - Gui', async function () ); let testGui = await DotrainOrderGui.chooseDeployment(dotrainWithGui, 'some-deployment'); - expect(() => testGui.getSelectTokens()).toThrow('Select tokens not set'); + assert.equal(testGui.getSelectTokens().size, 0); await expect( async () => await testGui.saveSelectTokenAddress('token1', '0x1') ).rejects.toThrow('Select tokens not set'); @@ -1049,5 +1049,35 @@ describe('Rain Orderbook JS API Package Bindgen Tests - Gui', async function () '0x8888888888888888888888888888888888888888' ); }); + + it('should clear select token address to fallback value', async () => { + mockServer + .forPost('/rpc-url') + .once() + .withBodyIncluding('0x82ad56cb') + .thenSendJsonRpcResult( + '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000007546f6b656e203100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025431000000000000000000000000000000000000000000000000000000000000' + ); + // token2 info + mockServer + .forPost('/rpc-url') + .once() + .withBodyIncluding('0x82ad56cb') + .thenSendJsonRpcResult( + '0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000754656b656e203200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025432000000000000000000000000000000000000000000000000000000000000' + ); + + await gui.saveSelectTokenAddress('token1', '0x6666666666666666666666666666666666666666'); + assert.equal( + gui.getSelectTokens().get('token1'), + '0x6666666666666666666666666666666666666666' + ); + + gui.clearSelectTokenAddress('token1'); + assert.equal( + gui.getSelectTokens().get('token1'), + '0x0000000000000000000000000000000000000000' + ); + }); }); });