Skip to content

Commit

Permalink
return empty map on missing select tokens and add method to clear sel…
Browse files Browse the repository at this point in the history
…ect token
  • Loading branch information
findolor committed Jan 10, 2025
1 parent b86f340 commit 1e98b26
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 12 deletions.
35 changes: 24 additions & 11 deletions crates/js_api/src/gui/select_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,35 @@ 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<SelectTokens, GuiError> {
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()?;
let mut select_tokens = self
.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
Expand All @@ -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(())
}
}
32 changes: 31 additions & 1 deletion packages/orderbook/test/js_api/gui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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'
);
});
});
});

0 comments on commit 1e98b26

Please sign in to comment.