Skip to content

Commit

Permalink
Merge pull request #1016 from rainlanguage/2024-11-22-gui-lib-simple-…
Browse files Browse the repository at this point in the history
…frontend

Creating deployment UI to test out gui library
  • Loading branch information
hardyjosh authored Nov 29, 2024
2 parents 72c8521 + a6f1d28 commit f750dd1
Show file tree
Hide file tree
Showing 23 changed files with 1,399 additions and 252 deletions.
13 changes: 9 additions & 4 deletions crates/common/src/dotrain_order/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,21 @@ impl DotrainOrder {
let deployment = self.get_deployment(deployment_name)?;
let mut calldatas = Vec::new();

for output in &deployment.order.outputs {
for (i, output) in deployment.order.outputs.iter().enumerate() {
let vault_id = output
.vault_id
.ok_or(DotrainOrderCalldataError::VaultIdNotFound)?;
.ok_or(DotrainOrderCalldataError::VaultIdNotFound(i.to_string()))?;

let token_deposit = token_deposits
.get(&(vault_id, output.token.address))
.ok_or(DotrainOrderCalldataError::TokenNotFound(
output.token.address.to_string(),
))?;

if *token_deposit == U256::ZERO {
continue;
}

let calldata = DepositArgs {
token: output.token.address,
amount: token_deposit.to_owned(),
Expand Down Expand Up @@ -153,8 +158,8 @@ pub enum DotrainOrderCalldataError {
#[error("Token not found {0}")]
TokenNotFound(String),

#[error("Vault id not found")]
VaultIdNotFound,
#[error("Vault id not found for output index: {0}")]
VaultIdNotFound(String),

#[error(transparent)]
DepositError(#[from] DepositError),
Expand Down
55 changes: 52 additions & 3 deletions crates/common/src/dotrain_order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
add_order::{ORDERBOOK_ADDORDER_POST_TASK_ENTRYPOINTS, ORDERBOOK_ORDER_ENTRYPOINTS},
rainlang::compose_to_rainlang,
};
use alloy::primitives::{private::rand, Address};
use alloy::primitives::{private::rand, Address, U256};
use alloy_ethers_typecast::transaction::{ReadableClient, ReadableClientError};
use dotrain::{error::ComposeError, RainDocument};
use futures::future::join_all;
Expand Down Expand Up @@ -82,6 +82,9 @@ pub enum DotrainOrderError {

#[error("Token {0} not found")]
TokenNotFound(String),

#[error("Invalid index for vault ID")]
InvalidVaultIdIndex,
}

#[cfg(target_family = "wasm")]
Expand Down Expand Up @@ -451,7 +454,11 @@ impl DotrainOrder {
Ok(())
}

pub fn populate_vault_ids(&mut self, deployment_name: &str) -> Result<(), DotrainOrderError> {
pub fn populate_vault_ids(
&mut self,
deployment_name: &str,
custom_vault_id: Option<U256>,
) -> Result<(), DotrainOrderError> {
let deployment = self
.config_source
.deployments
Expand All @@ -467,7 +474,7 @@ impl DotrainOrder {
.ok_or(DotrainOrderError::OrderNotFound(deployment.order.clone()))?
.clone();

let vault_id = rand::random();
let vault_id = custom_vault_id.unwrap_or(rand::random());

let new_inputs = order
.inputs
Expand Down Expand Up @@ -514,6 +521,48 @@ impl DotrainOrder {
self.update_config_source(self.config_source.clone())?;
Ok(())
}

pub fn set_vault_id(
&mut self,
deployment_name: &str,
is_input: bool,
index: u8,
vault_id: U256,
) -> Result<(), DotrainOrderError> {
let deployment = self
.config_source
.deployments
.get(deployment_name)
.ok_or(DotrainOrderError::DeploymentNotFound(
deployment_name.to_string(),
))?
.clone();
let mut order = self
.config_source
.orders
.get(&deployment.order)
.ok_or(DotrainOrderError::OrderNotFound(deployment.order.clone()))?
.clone();

if is_input {
if index as usize >= order.inputs.len() {
return Err(DotrainOrderError::InvalidVaultIdIndex);
}
order.inputs[index as usize].vault_id = Some(vault_id);
} else {
if index as usize >= order.outputs.len() {
return Err(DotrainOrderError::InvalidVaultIdIndex);
}
order.outputs[index as usize].vault_id = Some(vault_id);
}

self.config_source
.orders
.insert(deployment.order.clone(), order.clone());
self.update_config_source(self.config_source.clone())?;

Ok(())
}
}

#[cfg(test)]
Expand Down
60 changes: 50 additions & 10 deletions crates/js_api/src/gui/deposits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,33 @@ impl_all_wasm_traits!(TokenDeposit);
#[wasm_bindgen]
impl DotrainOrderGui {
#[wasm_bindgen(js_name = "getDeposits")]
pub fn get_deposits(&self) -> Vec<TokenDeposit> {
self.deposits.clone()
pub fn get_deposits(&self) -> Result<Vec<TokenDeposit>, GuiError> {
self.deposits
.iter()
.map(|(token, value)| {
let gui_deposit = self
.deployment
.deposits
.iter()
.find(|dg| dg.token_name == *token)
.ok_or(GuiError::DepositTokenNotFound(token.clone()))?;
let amount: String = if value.is_preset {
gui_deposit
.presets
.iter()
.find(|preset| **preset == value.value)
.ok_or(GuiError::InvalidPreset)?
.clone()
} else {
value.value.clone()
};
Ok(TokenDeposit {
token: gui_deposit.token_name.clone(),
amount,
address: gui_deposit.token.address,
})
})
.collect::<Result<Vec<TokenDeposit>, GuiError>>()
}

#[wasm_bindgen(js_name = "saveDeposit")]
Expand All @@ -25,20 +50,35 @@ impl DotrainOrderGui {
.find(|dg| dg.token_name == token)
.ok_or(GuiError::DepositTokenNotFound(token.clone()))?;

let deposit_token = TokenDeposit {
token: gui_deposit.token_name.clone(),
amount,
address: gui_deposit.token.address,
let value = if let Some(index) = gui_deposit.presets.iter().position(|p| **p == amount) {
field_values::PairValue {
is_preset: true,
value: index.to_string(),
}
} else {
field_values::PairValue {
is_preset: false,
value: amount,
}
};

if !self.deposits.iter().any(|d| d.token == token) {
self.deposits.push(deposit_token);
}
self.deposits.insert(token, value);
Ok(())
}

#[wasm_bindgen(js_name = "removeDeposit")]
pub fn remove_deposit(&mut self, token: String) {
self.deposits.retain(|deposit| deposit.token != token);
self.deposits.remove(&token);
}

#[wasm_bindgen(js_name = "getDepositPresets")]
pub fn get_deposit_presets(&self, token: String) -> Result<Vec<String>, GuiError> {
let gui_deposit = self
.deployment
.deposits
.iter()
.find(|dg| dg.token_name == token)
.ok_or(GuiError::DepositTokenNotFound(token.clone()))?;
Ok(gui_deposit.presets.clone())
}
}
18 changes: 14 additions & 4 deletions crates/js_api/src/gui/field_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ impl DotrainOrderGui {
pub fn save_field_value(&mut self, binding: String, value: PairValue) -> Result<(), GuiError> {
let field_definition = self.get_field_definition(&binding)?;
if value.is_preset {
if !field_definition
let presets = field_definition
.presets
.ok_or(GuiError::BindingHasNoPresets(binding.clone()))?;

if !presets
.iter()
.find(|preset| preset.id == value.value)
.is_some()
Expand All @@ -50,6 +53,11 @@ impl DotrainOrderGui {
Ok(())
}

#[wasm_bindgen(js_name = "removeFieldValue")]
pub fn remove_field_value(&mut self, binding: String) {
self.field_values.remove(&binding);
}

#[wasm_bindgen(js_name = "getFieldValue")]
pub fn get_field_value(&self, binding: String) -> Result<GuiPreset, GuiError> {
let field_value = self
Expand All @@ -59,12 +67,14 @@ impl DotrainOrderGui {
let preset = match field_value.is_preset {
true => {
let field_definition = self.get_field_definition(&binding)?;
let preset = field_definition
let presets = field_definition
.presets
.ok_or(GuiError::BindingHasNoPresets(binding.clone()))?;
presets
.iter()
.find(|preset| preset.id == field_value.value)
.ok_or(GuiError::InvalidPreset)?;
preset.clone()
.ok_or(GuiError::InvalidPreset)?
.clone()
}
false => GuiPreset {
id: "".to_string(),
Expand Down
10 changes: 6 additions & 4 deletions crates/js_api/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct DotrainOrderGui {
dotrain_order: DotrainOrder,
deployment: GuiDeployment,
field_values: BTreeMap<String, field_values::PairValue>,
deposits: Vec<deposits::TokenDeposit>,
deposits: BTreeMap<String, field_values::PairValue>,
select_tokens: Option<BTreeMap<String, Address>>,
onchain_token_info: BTreeMap<Address, TokenInfo>,
}
Expand Down Expand Up @@ -106,7 +106,7 @@ impl DotrainOrderGui {
dotrain_order,
deployment: gui_deployment.clone(),
field_values: BTreeMap::new(),
deposits: vec![],
deposits: BTreeMap::new(),
select_tokens,
onchain_token_info,
})
Expand Down Expand Up @@ -164,8 +164,8 @@ pub enum GuiError {
OrderbookNotFound,
#[error("Deserialized config mismatch")]
DeserializedConfigMismatch,
#[error("Vault id not found")]
VaultIdNotFound,
#[error("Vault id not found for output index: {0}")]
VaultIdNotFound(String),
#[error("Deployer not found")]
DeployerNotFound,
#[error("Token not found {0}")]
Expand All @@ -176,6 +176,8 @@ pub enum GuiError {
SelectTokensNotSet,
#[error("Token must be selected: {0}")]
TokenMustBeSelected(String),
#[error("Binding has no presets: {0}")]
BindingHasNoPresets(String),
#[error(transparent)]
DotrainOrderError(#[from] DotrainOrderError),
#[error(transparent)]
Expand Down
Loading

0 comments on commit f750dd1

Please sign in to comment.