Skip to content

Commit

Permalink
Merge pull request #1132 from rainlanguage/2025-01-08-yaml-struct-cha…
Browse files Browse the repository at this point in the history
…nges

Implementing DotrainYaml and OrderbookYaml changes in DotrainOrderGui
  • Loading branch information
hardyjosh authored Jan 9, 2025
2 parents 1856277 + 94787ed commit ccba893
Show file tree
Hide file tree
Showing 24 changed files with 664 additions and 197 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 10 additions & 23 deletions crates/common/src/dotrain_order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ use typeshare::typeshare;

pub mod calldata;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
pub struct DotrainOrder {
dotrain: String,
dotrain_yaml: DotrainYaml,
orderbook_yaml: OrderbookYaml,
}

impl PartialEq for DotrainOrder {
Expand Down Expand Up @@ -140,13 +139,9 @@ impl DotrainOrder {
sources.extend(settings);
}

let dotrain_yaml = DotrainYaml::new(sources.clone(), false)?;
let orderbook_yaml = OrderbookYaml::new(sources, false)?;

Ok(Self {
dotrain,
dotrain_yaml,
orderbook_yaml,
dotrain_yaml: DotrainYaml::new(sources.clone(), false)?,
})
}

Expand Down Expand Up @@ -216,20 +211,12 @@ impl DotrainOrder {
&self.dotrain
}

pub fn dotrain_yaml(&self) -> &DotrainYaml {
&self.dotrain_yaml
}

pub fn dotrain_yaml_mut(&mut self) -> &mut DotrainYaml {
&mut self.dotrain_yaml
}

pub fn orderbook_yaml(&self) -> &OrderbookYaml {
&self.orderbook_yaml
pub fn dotrain_yaml(&self) -> DotrainYaml {
self.dotrain_yaml.clone()
}

pub fn orderbook_yaml_mut(&mut self) -> &mut OrderbookYaml {
&mut self.orderbook_yaml
pub fn orderbook_yaml(&self) -> OrderbookYaml {
OrderbookYaml::from_documents(self.dotrain_yaml.documents.clone())
}

pub async fn get_pragmas_for_scenario(
Expand All @@ -255,7 +242,7 @@ impl DotrainOrder {
let network = &self.dotrain_yaml.get_scenario(scenario)?.deployer.network;

let rpc = &network.rpc;
let metaboard = self.orderbook_yaml.get_metaboard(&network.key)?.url;
let metaboard = self.orderbook_yaml().get_metaboard(&network.key)?.url;
Ok(
AuthoringMetaV2::fetch_for_contract(address, rpc.to_string(), metaboard.to_string())
.await?,
Expand Down Expand Up @@ -345,7 +332,7 @@ impl DotrainOrder {
pub async fn validate_raindex_version(&self) -> Result<(), DotrainOrderError> {
let app_sha = GH_COMMIT_SHA.to_string();

if let Some(raindex_version) = &self.orderbook_yaml.get_raindex_version()? {
if let Some(raindex_version) = &self.orderbook_yaml().get_raindex_version()? {
if app_sha != *raindex_version {
return Err(DotrainOrderError::RaindexVersionMismatch(
app_sha,
Expand Down Expand Up @@ -411,7 +398,7 @@ _ _: 0 0;

assert_eq!(
dotrain_order
.orderbook_yaml
.orderbook_yaml()
.get_network("polygon")
.unwrap()
.rpc
Expand Down Expand Up @@ -548,7 +535,7 @@ networks:

assert_eq!(
merged_dotrain_order
.orderbook_yaml
.orderbook_yaml()
.get_network("mainnet")
.unwrap()
.rpc
Expand Down
1 change: 0 additions & 1 deletion crates/js_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ alloy = { workspace = true, features = [ "dyn-abi" ] }
flate2 = "1.0.34"
base64 = "0.22.1"
bincode = "1.3.3"
sha2 = "0.10.8"
19 changes: 11 additions & 8 deletions crates/js_api/src/gui/deposits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ impl_all_wasm_traits!(TokenDeposit);
impl DotrainOrderGui {
#[wasm_bindgen(js_name = "getDeposits")]
pub fn get_deposits(&self) -> Result<Vec<TokenDeposit>, GuiError> {
let deployment = self.get_current_deployment()?;
self.deposits
.iter()
.map(|(token, value)| {
let gui_deposit = self
.deployment
let gui_deposit = deployment
.deposits
.iter()
.find(|dg| dg.token.key == *token)
.ok_or(GuiError::DepositTokenNotFound(token.clone()))?;
let amount: String = if value.is_preset {
let index = value
.value
.parse::<usize>()
.map_err(|_| GuiError::InvalidPreset)?;
gui_deposit
.presets
.iter()
.find(|preset| **preset == value.value)
.get(index)
.ok_or(GuiError::InvalidPreset)?
.clone()
} else {
Expand All @@ -43,8 +46,8 @@ impl DotrainOrderGui {

#[wasm_bindgen(js_name = "saveDeposit")]
pub fn save_deposit(&mut self, token: String, amount: String) -> Result<(), GuiError> {
let gui_deposit = self
.deployment
let deployment = self.get_current_deployment()?;
let gui_deposit = deployment
.deposits
.iter()
.find(|dg| dg.token.key == token)
Expand Down Expand Up @@ -73,8 +76,8 @@ impl DotrainOrderGui {

#[wasm_bindgen(js_name = "getDepositPresets")]
pub fn get_deposit_presets(&self, token: String) -> Result<Vec<String>, GuiError> {
let gui_deposit = self
.deployment
let deployment = self.get_current_deployment()?;
let gui_deposit = deployment
.deposits
.iter()
.find(|dg| dg.token.key == token)
Expand Down
9 changes: 5 additions & 4 deletions crates/js_api/src/gui/field_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ impl DotrainOrderGui {

#[wasm_bindgen(js_name = "getFieldDefinition")]
pub fn get_field_definition(&self, binding: &str) -> Result<GuiFieldDefinition, GuiError> {
let field_definition = self
.deployment
let deployment = self.get_current_deployment()?;
let field_definition = deployment
.fields
.iter()
.find(|field| field.binding == binding)
Expand All @@ -109,7 +109,8 @@ impl DotrainOrderGui {
}

#[wasm_bindgen(js_name = "getAllFieldDefinitions")]
pub fn get_all_field_definitions(&self) -> Vec<GuiFieldDefinition> {
self.deployment.fields.clone()
pub fn get_all_field_definitions(&self) -> Result<Vec<GuiFieldDefinition>, GuiError> {
let deployment = self.get_current_deployment()?;
Ok(deployment.fields.clone())
}
}
33 changes: 13 additions & 20 deletions crates/js_api/src/gui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl_all_wasm_traits!(TokenInfos);
#[wasm_bindgen]
pub struct DotrainOrderGui {
dotrain_order: DotrainOrder,
deployment: GuiDeployment,
selected_deployment: String,
field_values: BTreeMap<String, field_values::PairValue>,
deposits: BTreeMap<String, field_values::PairValue>,
select_tokens: Option<BTreeMap<String, Address>>,
Expand Down Expand Up @@ -73,7 +73,7 @@ impl DotrainOrderGui {
.deployments
.into_iter()
.find(|(name, _)| name == &deployment_name)
.ok_or(GuiError::DeploymentNotFound(deployment_name))?;
.ok_or(GuiError::DeploymentNotFound(deployment_name.clone()))?;

let select_tokens = gui_deployment.select_tokens.clone().map(|tokens| {
tokens
Expand Down Expand Up @@ -110,29 +110,14 @@ impl DotrainOrderGui {

Ok(Self {
dotrain_order,
deployment: gui_deployment.clone(),
selected_deployment: deployment_name.clone(),
field_values: BTreeMap::new(),
deposits: BTreeMap::new(),
select_tokens,
onchain_token_info,
})
}

fn refresh_gui_deployment(&mut self) -> Result<(), GuiError> {
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)
.ok_or(GuiError::DeploymentNotFound(self.deployment.key.clone()))?;
self.deployment = gui_deployment.clone();
Ok(())
}

#[wasm_bindgen(js_name = "getGuiConfig")]
pub fn get_gui_config(&self) -> Result<Gui, GuiError> {
let gui = self
Expand All @@ -144,8 +129,16 @@ impl DotrainOrderGui {
}

#[wasm_bindgen(js_name = "getCurrentDeployment")]
pub fn get_current_deployment(&self) -> GuiDeployment {
self.deployment.clone()
pub fn get_current_deployment(&self) -> Result<GuiDeployment, GuiError> {
let gui = self.get_gui_config()?;
let (_, gui_deployment) = gui
.deployments
.into_iter()
.find(|(name, _)| name == &self.selected_deployment)
.ok_or(GuiError::DeploymentNotFound(
self.selected_deployment.clone(),
))?;
Ok(gui_deployment.clone())
}

/// Get all token infos in input and output vaults
Expand Down
Loading

0 comments on commit ccba893

Please sign in to comment.