Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/common/src/raindex_client/vaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl RaindexVault {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
#[wasm_bindgen]
pub struct AccountBalance {
Expand Down
77 changes: 29 additions & 48 deletions crates/js_api/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,9 @@ impl DotrainOrderGui {
param_description = "Key of the deployment to activate (must exist in YAML)"
)]
selected_deployment: String,
#[wasm_export(
param_description = "Optional JavaScript function called on state changes. \
#[wasm_export(param_description = "Optional function called on state changes. \
After a state change (deposit, field value, vault id, select token, etc.), the callback is called with the new state. \
This is useful for auto-saving the state of the GUI across sessions."
)]
This is useful for auto-saving the state of the GUI across sessions.")]
state_update_callback: Option<js_sys::Function>,
) -> Result<DotrainOrderGui, GuiError> {
let dotrain_order = DotrainOrder::create(dotrain.clone(), None).await?;
Expand Down Expand Up @@ -407,7 +405,7 @@ impl DotrainOrderGui {
unchecked_return_type = "NameAndDescriptionCfg",
return_description = "Order name, description, and optional short description"
)]
pub async fn get_order_details(
pub fn get_order_details(
#[wasm_export(param_description = "Complete dotrain YAML content")] dotrain: String,
) -> Result<NameAndDescriptionCfg, GuiError> {
let documents = DotrainOrderGui::get_yaml_documents(&dotrain)?;
Expand Down Expand Up @@ -446,7 +444,7 @@ impl DotrainOrderGui {
unchecked_return_type = "Map<string, NameAndDescriptionCfg>",
return_description = "Map of deployment key to metadata"
)]
pub async fn get_deployment_details(
pub fn get_deployment_details(
#[wasm_export(param_description = "Complete dotrain YAML content")] dotrain: String,
) -> Result<BTreeMap<String, NameAndDescriptionCfg>, GuiError> {
let documents = DotrainOrderGui::get_yaml_documents(&dotrain)?;
Expand Down Expand Up @@ -474,11 +472,11 @@ impl DotrainOrderGui {
unchecked_return_type = "NameAndDescriptionCfg",
return_description = "Deployment name and description"
)]
pub async fn get_deployment_detail(
pub fn get_deployment_detail(
#[wasm_export(param_description = "Complete dotrain YAML content")] dotrain: String,
#[wasm_export(param_description = "Deployment identifier to look up")] key: String,
) -> Result<NameAndDescriptionCfg, GuiError> {
let deployment_details = DotrainOrderGui::get_deployment_details(dotrain).await?;
let deployment_details = DotrainOrderGui::get_deployment_details(dotrain)?;
let deployment_detail = deployment_details
.get(&key)
.ok_or(GuiError::DeploymentNotFound(key))?;
Expand Down Expand Up @@ -1261,10 +1259,8 @@ _ _: 0 0;
}

#[wasm_bindgen_test]
async fn test_get_order_details() {
let order_details = DotrainOrderGui::get_order_details(get_yaml())
.await
.unwrap();
fn test_get_order_details() {
let order_details = DotrainOrderGui::get_order_details(get_yaml()).unwrap();
assert_eq!(order_details.name, "Fixed limit");
assert_eq!(order_details.description, "Fixed limit order");
assert_eq!(
Expand All @@ -1287,9 +1283,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let err = DotrainOrderGui::get_order_details(yaml.to_string())
.await
.unwrap_err();
let err = DotrainOrderGui::get_order_details(yaml.to_string()).unwrap_err();
assert_eq!(
err.to_string(),
YamlError::Field {
Expand Down Expand Up @@ -1318,9 +1312,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let err = DotrainOrderGui::get_order_details(yaml.to_string())
.await
.unwrap_err();
let err = DotrainOrderGui::get_order_details(yaml.to_string()).unwrap_err();
assert_eq!(
err.to_string(),
YamlError::Field {
Expand Down Expand Up @@ -1350,9 +1342,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let err = DotrainOrderGui::get_order_details(yaml.to_string())
.await
.unwrap_err();
let err = DotrainOrderGui::get_order_details(yaml.to_string()).unwrap_err();
assert_eq!(
err.to_string(),
YamlError::Field {
Expand All @@ -1368,10 +1358,8 @@ _ _: 0 0;
}

#[wasm_bindgen_test]
async fn test_get_deployment_details() {
let deployment_details = DotrainOrderGui::get_deployment_details(get_yaml())
.await
.unwrap();
fn test_get_deployment_details() {
let deployment_details = DotrainOrderGui::get_deployment_details(get_yaml()).unwrap();
assert_eq!(deployment_details.len(), 3);
let deployment_detail = deployment_details.get("some-deployment").unwrap();
assert_eq!(deployment_detail.name, "Buy WETH with USDC on Base.");
Expand Down Expand Up @@ -1409,9 +1397,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let details = DotrainOrderGui::get_deployment_details(yaml.to_string())
.await
.unwrap();
let details = DotrainOrderGui::get_deployment_details(yaml.to_string()).unwrap();
assert_eq!(details.len(), 0);

let yaml = format!(
Expand All @@ -1429,9 +1415,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let err = DotrainOrderGui::get_deployment_details(yaml.to_string())
.await
.unwrap_err();
let err = DotrainOrderGui::get_deployment_details(yaml.to_string()).unwrap_err();
assert_eq!(
err.to_string(),
YamlError::Field {
Expand Down Expand Up @@ -1460,9 +1444,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let err = DotrainOrderGui::get_deployment_details(yaml.to_string())
.await
.unwrap_err();
let err = DotrainOrderGui::get_deployment_details(yaml.to_string()).unwrap_err();
assert_eq!(
err.to_string(),
YamlError::Field {
Expand Down Expand Up @@ -1495,9 +1477,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let err = DotrainOrderGui::get_deployment_details(yaml.to_string())
.await
.unwrap_err();
let err = DotrainOrderGui::get_deployment_details(yaml.to_string()).unwrap_err();
assert_eq!(
err.to_string(),
YamlError::Field {
Expand Down Expand Up @@ -1530,9 +1510,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let err = DotrainOrderGui::get_deployment_details(yaml.to_string())
.await
.unwrap_err();
let err = DotrainOrderGui::get_deployment_details(yaml.to_string()).unwrap_err();
assert_eq!(
err.to_string(),
YamlError::Field {
Expand Down Expand Up @@ -1563,9 +1541,7 @@ _ _: 0 0;
"#,
spec_version = SpecVersion::current()
);
let err = DotrainOrderGui::get_deployment_details(yaml.to_string())
.await
.unwrap_err();
let err = DotrainOrderGui::get_deployment_details(yaml.to_string()).unwrap_err();
assert_eq!(
err.to_string(),
YamlError::Field {
Expand All @@ -1581,10 +1557,9 @@ _ _: 0 0;
}

#[wasm_bindgen_test]
async fn test_get_deployment_detail() {
fn test_get_deployment_detail() {
let deployment_detail =
DotrainOrderGui::get_deployment_detail(get_yaml(), "some-deployment".to_string())
.await
.unwrap();
assert_eq!(deployment_detail.name, "Buy WETH with USDC on Base.");
assert_eq!(
Expand Down Expand Up @@ -1650,8 +1625,8 @@ _ _: 0 0;
#[cfg(not(target_family = "wasm"))]
mod select_token_tests {
use super::*;
use alloy_ethers_typecast::rpc::Response;
use httpmock::MockServer;
use serde_json::json;

pub const SELECT_TOKEN_YAML: &str = r#"
gui:
Expand Down Expand Up @@ -1736,6 +1711,7 @@ _ _: 0 0;
let server = MockServer::start_async().await;
let yaml = format!(
r#"
version: {spec_version}
networks:
some-network:
rpcs:
Expand All @@ -1745,16 +1721,21 @@ networks:
currency: ETH
{yaml}
"#,
spec_version = SpecVersion::current(),
yaml = SELECT_TOKEN_YAML,
rpc_url = server.url("/rpc")
);

server.mock(|when, then| {
when.method("POST").path("/rpc").body_contains("0x82ad56cb");
then.body(Response::new_success(1, "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000007546f6b656e203100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025431000000000000000000000000000000000000000000000000000000000000").to_json_string().unwrap());
then.json_body(json!({
"jsonrpc": "2.0",
"id": 1,
"result": "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000007546f6b656e203100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025431000000000000000000000000000000000000000000000000000000000000",
}));
});

let gui = DotrainOrderGui::new_with_deployment(
let mut gui = DotrainOrderGui::new_with_deployment(
Comment on lines -1757 to +1738
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need to be mut now? Did a type signature of some method used below change?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clippy flagged it for some reason when i was working on it i am not sure entirely

yaml.to_string(),
"some-deployment".to_string(),
None,
Expand Down
59 changes: 31 additions & 28 deletions crates/js_api/src/gui/select_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,15 @@ mod tests {
#[cfg(not(target_family = "wasm"))]
mod non_wasm_tests {
use crate::gui::{DotrainOrderGui, GuiError};
use alloy::primitives::Address;
use alloy_ethers_typecast::rpc::Response;
use alloy::primitives::{Address, U256};
use httpmock::MockServer;
use rain_orderbook_app_settings::spec_version::SpecVersion;
use rain_orderbook_common::raindex_client::vaults::AccountBalance;
use serde_json::json;
use std::str::FromStr;

const TEST_YAML_TEMPLATE: &str = r#"
version: {spec_version}
gui:
name: Fixed limit
description: Fixed limit order
Expand Down Expand Up @@ -685,7 +688,8 @@ gui:
default: 10
networks:
some-network:
rpc: {rpc_url}
rpcs:
- {rpc_url}
chain-id: 123
network-id: 123
currency: ETH
Expand Down Expand Up @@ -739,14 +743,20 @@ _ _: 0 0;
#[tokio::test]
async fn test_set_select_token() {
let server = MockServer::start_async().await;
let yaml = TEST_YAML_TEMPLATE.replace("{rpc_url}", &server.url("/rpc"));
let yaml = TEST_YAML_TEMPLATE
.replace("{rpc_url}", &server.url("/rpc"))
.replace("{spec_version}", &SpecVersion::current().to_string());

server.mock(|when, then| {
when.method("POST").path("/rpc").body_contains("0x82ad56cb");
then.body(Response::new_success(1, "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000007546f6b656e203100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025431000000000000000000000000000000000000000000000000000000000000").to_json_string().unwrap());
when.method("POST").path("/rpc").body_contains("0x82ad56cb");
then.json_body(json!({
"jsonrpc": "2.0",
"id": 1,
"result": "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000007546f6b656e203100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000025431000000000000000000000000000000000000000000000000000000000000",
}));
});

let gui = DotrainOrderGui::new_with_deployment(
let mut gui = DotrainOrderGui::new_with_deployment(
yaml.to_string(),
"some-deployment".to_string(),
None,
Expand Down Expand Up @@ -820,29 +830,25 @@ _ _: 0 0;
#[tokio::test]
async fn test_get_account_balance() {
let server = MockServer::start_async().await;
let yaml = TEST_YAML_TEMPLATE.replace("{rpc_url}", &server.url("/rpc"));
let yaml = TEST_YAML_TEMPLATE
.replace("{rpc_url}", &server.url("/rpc"))
.replace("{spec_version}", &SpecVersion::current().to_string());

server.mock(|when, then| {
when.method("POST").path("/rpc").body_contains("0x313ce567");
then.body(
Response::new_success(
1,
"0x0000000000000000000000000000000000000000000000000000000000000012",
)
.to_json_string()
.unwrap(),
);
then.json_body(json!({
"jsonrpc": "2.0",
"id": 1,
"result": "0x0000000000000000000000000000000000000000000000000000000000000012",
}));
});
server.mock(|when, then| {
when.method("POST").path("/rpc").body_contains("0x70a08231");
then.body(
Response::new_success(
1,
"0x00000000000000000000000000000000000000000000000000000000000003e8",
)
.to_json_string()
.unwrap(),
);
then.json_body(json!({
"jsonrpc": "2.0",
"id": 1,
"result": "0x00000000000000000000000000000000000000000000000000000000000003e8",
}));
});

let gui = DotrainOrderGui::new_with_deployment(
Expand All @@ -863,10 +869,7 @@ _ _: 0 0;

assert_eq!(
balance,
AccountBalance {
balance: U256::from(1000),
formatted_balance: "0.000000000000001".to_string(),
}
AccountBalance::new(U256::from(1000), "0.000000000000001".to_string(),)
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions crates/js_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod bindings;
#[cfg(target_family = "wasm")]
pub mod gui;
#[cfg(target_family = "wasm")]
pub mod registry;
#[cfg(target_family = "wasm")]
pub mod yaml;

// re-export other crates to include their wasm bindings as single export point
Expand Down
Loading