Skip to content

Commit

Permalink
update dotrain order gui with dotrain order changes
Browse files Browse the repository at this point in the history
  • Loading branch information
findolor committed Jan 8, 2025
1 parent fabac43 commit a4b1df0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 53 deletions.
6 changes: 6 additions & 0 deletions crates/common/src/dotrain_order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ pub struct DotrainOrder {
orderbook_yaml: OrderbookYaml,
}

impl PartialEq for DotrainOrder {
fn eq(&self, other: &Self) -> bool {
self.dotrain == other.dotrain
}
}

#[derive(Error, Debug)]
pub enum DotrainOrderError {
#[error(transparent)]
Expand Down
43 changes: 26 additions & 17 deletions crates/js_api/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use base64::{engine::general_purpose::URL_SAFE, Engine};
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use rain_orderbook_app_settings::{
gui::{Gui, GuiDeployment, GuiFieldDefinition, GuiPreset, ParseGuiConfigSourceError},
Config,
yaml::YamlError,
};
use rain_orderbook_bindings::{impl_all_wasm_traits, wasm_traits::prelude::*};
use rain_orderbook_common::{
Expand Down Expand Up @@ -47,10 +47,12 @@ impl DotrainOrderGui {
dotrain: String,
) -> Result<AvailableDeployments, GuiError> {
let dotrain_order = DotrainOrder::new(dotrain, None).await?;
let config = dotrain_order.config();
let gui_config = config.gui.clone().ok_or(GuiError::GuiConfigNotFound)?;
let gui = dotrain_order
.dotrain_yaml()
.get_gui()?
.ok_or(GuiError::GuiConfigNotFound)?;
Ok(AvailableDeployments(
gui_config.deployments.values().cloned().collect(),
gui.deployments.values().cloned().collect(),
))
}

Expand All @@ -62,10 +64,12 @@ impl DotrainOrderGui {
) -> Result<DotrainOrderGui, GuiError> {
let dotrain_order = DotrainOrder::new(dotrain, None).await?;

let config = dotrain_order.config();
let gui_config = config.gui.clone().ok_or(GuiError::GuiConfigNotFound)?;
let gui = dotrain_order
.dotrain_yaml()
.get_gui()?
.ok_or(GuiError::GuiConfigNotFound)?;

let (_, gui_deployment) = gui_config
let (_, gui_deployment) = gui
.deployments
.into_iter()
.find(|(name, _)| name == &deployment_name)
Expand Down Expand Up @@ -115,9 +119,12 @@ impl DotrainOrderGui {
}

fn refresh_gui_deployment(&mut self) -> Result<(), GuiError> {
let config = self.dotrain_order.config();
let gui_config = config.gui.clone().ok_or(GuiError::GuiConfigNotFound)?;
let (_, gui_deployment) = gui_config
let gui = self
.dotrain_order
.dotrain_yaml()
.get_gui()?
.ok_or(GuiError::GuiConfigNotFound)?;
let (_, gui_deployment) = gui
.deployments
.into_iter()
.find(|(name, _)| name == &self.deployment.key)
Expand All @@ -126,14 +133,14 @@ impl DotrainOrderGui {
Ok(())
}

#[wasm_bindgen(js_name = "getDotrainConfig")]
pub fn get_dotrain_config(&self) -> Config {
self.dotrain_order.config().clone()
}

#[wasm_bindgen(js_name = "getGuiConfig")]
pub fn get_gui_config(&self) -> Gui {
self.dotrain_order.config().gui.clone().unwrap()
pub fn get_gui_config(&self) -> Result<Gui, GuiError> {
let gui = self
.dotrain_order
.dotrain_yaml()
.get_gui()?
.ok_or(GuiError::GuiConfigNotFound)?;
Ok(gui)
}

#[wasm_bindgen(js_name = "getCurrentDeployment")]
Expand Down Expand Up @@ -216,6 +223,8 @@ pub enum GuiError {
SerdeWasmBindgenError(#[from] serde_wasm_bindgen::Error),
#[error(transparent)]
DotrainOrderCalldataError(#[from] DotrainOrderCalldataError),
#[error(transparent)]
YamlError(#[from] YamlError),
}
impl From<GuiError> for JsValue {
fn from(value: GuiError) -> Self {
Expand Down
37 changes: 19 additions & 18 deletions crates/js_api/src/gui/order_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,21 @@ impl DotrainOrderGui {
}

fn populate_vault_ids(&mut self) -> Result<(), GuiError> {
self.dotrain_order
.populate_vault_ids(&self.deployment.key, None)?;
self.refresh_gui_deployment()?;
// self.dotrain_order
// .populate_vault_ids(&self.deployment.key, None)?;
// self.refresh_gui_deployment()?;
Ok(())
}

fn update_config_source_bindings(&mut self) -> Result<(), GuiError> {
self.dotrain_order.update_config_source_bindings(
&self.deployment.deployment.scenario.key,
self.field_values
.iter()
.map(|(k, _)| Ok((k.clone(), self.get_field_value(k.clone())?.value.clone())))
.collect::<Result<HashMap<String, String>, GuiError>>()?,
)?;
self.refresh_gui_deployment()?;
// self.dotrain_order.update_config_source_bindings(
// &self.deployment.deployment.scenario.key,
// self.field_values
// .iter()
// .map(|(k, _)| Ok((k.clone(), self.get_field_value(k.clone())?.value.clone())))
// .collect::<Result<HashMap<String, String>, GuiError>>()?,
// )?;
// self.refresh_gui_deployment()?;
Ok(())
}

Expand Down Expand Up @@ -259,12 +259,13 @@ impl DotrainOrderGui {
index: u8,
vault_id: String,
) -> Result<(), GuiError> {
self.dotrain_order.set_vault_id(
&self.deployment.key,
is_input,
index,
U256::from_str(&vault_id)?,
)?;
self.refresh_gui_deployment()
// self.dotrain_order.set_vault_id(
// &self.deployment.key,
// is_input,
// index,
// U256::from_str(&vault_id)?,
// )?;
// self.refresh_gui_deployment()
Ok(())
}
}
6 changes: 3 additions & 3 deletions crates/js_api/src/gui/select_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ impl DotrainOrderGui {
let token_info = erc20.token_info(None).await?;
self.onchain_token_info.insert(address, token_info);

self.dotrain_order
.update_token_address(token_name, address)?;
self.refresh_gui_deployment()?;
// self.dotrain_order
// .update_token_address(token_name, address)?;
// self.refresh_gui_deployment()?;
Ok(())
}
}
15 changes: 0 additions & 15 deletions crates/js_api/src/gui/state_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,14 @@ use sha2::{Digest, Sha256};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
struct SerializedGuiState {
config_hash: String,
field_values: BTreeMap<String, GuiPreset>,
deposits: BTreeMap<String, GuiPreset>,
}

#[wasm_bindgen]
impl DotrainOrderGui {
fn compute_config_hash(&self) -> String {
let config = self.get_gui_config();
let bytes = bincode::serialize(&config).expect("Failed to serialize config");
let hash = Sha256::digest(&bytes);
format!("{:x}", hash)
}

#[wasm_bindgen(js_name = "serializeState")]
pub fn serialize(&self) -> Result<String, GuiError> {
let config_hash = self.compute_config_hash();

let mut field_values = BTreeMap::new();
for (k, v) in self.field_values.iter() {
let preset = if v.is_preset {
Expand Down Expand Up @@ -74,7 +64,6 @@ impl DotrainOrderGui {
}

let state = SerializedGuiState {
config_hash,
field_values: field_values.clone(),
deposits: deposits.clone(),
};
Expand Down Expand Up @@ -137,10 +126,6 @@ impl DotrainOrderGui {
self.field_values = field_values;
self.deposits = deposits;

if state.config_hash != self.compute_config_hash() {
return Err(GuiError::DeserializedConfigMismatch);
}

Ok(())
}

Expand Down

0 comments on commit a4b1df0

Please sign in to comment.