Skip to content

Commit

Permalink
make changes
Browse files Browse the repository at this point in the history
  • Loading branch information
findolor committed Nov 20, 2024
1 parent ddcee4f commit b3b1d68
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 68 deletions.
97 changes: 35 additions & 62 deletions crates/common/src/dotrain_order/calldata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
};
use alloy::{
hex::FromHexError,
primitives::private::rand,
primitives::{Bytes, U256},
};
use rain_orderbook_app_settings::{deployment::Deployment, orderbook::Orderbook};
Expand All @@ -28,14 +27,19 @@ use wasm_bindgen::describe::{inform, WasmDescribe, WasmDescribeVector, VECTOR};
#[cfg(target_family = "wasm")]
use wasm_bindgen::{JsValue, UnwrapThrowExt};

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[cfg_attr(
target_family = "wasm",
derive(Tsify),
tsify(into_wasm_abi, from_wasm_abi)
)]
pub struct ApprovalCalldata {
#[tsify(type = "string")]
#[cfg_attr(target_family = "wasm", tsify(type = "string"))]
token: Address,
#[tsify(type = "string")]
#[cfg_attr(target_family = "wasm", tsify(type = "string"))]
calldata: Bytes,
}
#[cfg(target_family = "wasm")]
impl_wasm_traits!(ApprovalCalldata);

impl DotrainOrder {
Expand Down Expand Up @@ -103,66 +107,32 @@ impl DotrainOrder {
Ok(calldatas)
}

fn check_and_save_vault_ids(
&mut self,
deployment_name: &str,
) -> Result<(), DotrainOrderCalldataError> {
let mut deployment = self.get_deployment(deployment_name)?.as_ref().clone();
let mut order = deployment.order.as_ref().clone();
let vault_id = rand::random();

let new_inputs = deployment
.order
.inputs
.iter()
.map(|input| {
let mut input = input.clone();
input.vault_id = Some(input.vault_id.unwrap_or(vault_id));
input
})
.collect();
let new_outputs = deployment
.order
.outputs
.iter()
.map(|output| {
let mut output = output.clone();
output.vault_id = Some(output.vault_id.unwrap_or(vault_id));
output
})
.collect();

order.inputs = new_inputs;
order.outputs = new_outputs;
deployment.order = Arc::new(order);
self.config
.deployments
.insert(deployment_name.to_string(), Arc::new(deployment));

Ok(())
}

pub async fn generate_deposit_calldatas(
&mut self,
deployment_name: &str,
token_deposits: &HashMap<Address, U256>,
token_deposits: &HashMap<(U256, Address), U256>,
) -> Result<Vec<Bytes>, DotrainOrderCalldataError> {
let deployment = self.get_deployment(deployment_name)?;
let mut calldatas = Vec::new();

self.check_and_save_vault_ids(deployment_name)?;

for output in &deployment.order.outputs {
let token_deposit = token_deposits.get(&output.token.address).ok_or(
DotrainOrderCalldataError::TokenNotFound(output.token.address.to_string()),
)?;
let vault_id = output
.vault_id
.ok_or(DotrainOrderCalldataError::VaultIdNotFound)?;

let token_deposit = token_deposits
.get(&(vault_id, output.token.address))
.ok_or(DotrainOrderCalldataError::TokenNotFound(
output.token.address.to_string(),
))?;
let calldata = DepositArgs {
token: output.token.address,
amount: token_deposit.to_owned(),
vault_id: output.vault_id.unwrap(),
vault_id,
}
.get_deposit_calldata()
.await?;

calldatas.push(Bytes::copy_from_slice(&calldata));
}

Expand All @@ -176,17 +146,17 @@ impl DotrainOrder {
let deployment = self.get_deployment(deployment_name)?;
let orderbook = self.get_orderbook(deployment_name)?;

self.check_and_save_vault_ids(deployment_name)?;

let calldata =
AddOrderArgs::new_from_deployment(self.dotrain(), deployment.as_ref().to_owned())
.await?
.get_add_order_calldata(TransactionArgs {
orderbook_address: orderbook.address,
rpc_url: orderbook.network.rpc.to_string(),
..Default::default()
})
.await?;
let calldata = AddOrderArgs::new_from_deployment(
self.dotrain().to_string(),
deployment.as_ref().to_owned(),
)
.await?
.get_add_order_calldata(TransactionArgs {
orderbook_address: orderbook.address,
rpc_url: orderbook.network.rpc.to_string(),
..Default::default()
})
.await?;

Ok(Bytes::copy_from_slice(&calldata))
}
Expand All @@ -203,6 +173,9 @@ pub enum DotrainOrderCalldataError {
#[error("Token not found {0}")]
TokenNotFound(String),

#[error("Vault id not found")]
VaultIdNotFound,

#[error(transparent)]
DepositError(#[from] DepositError),

Expand Down
48 changes: 47 additions & 1 deletion crates/common/src/dotrain_order/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
add_order::{ORDERBOOK_ADDORDER_POST_TASK_ENTRYPOINTS, ORDERBOOK_ORDER_ENTRYPOINTS},
rainlang::compose_to_rainlang,
};
use alloy::primitives::Address;
use alloy::primitives::{private::rand, Address};
use alloy_ethers_typecast::transaction::{ReadableClient, ReadableClientError};
use dotrain::{error::ComposeError, RainDocument};
use futures::future::join_all;
Expand All @@ -15,6 +15,7 @@ use rain_orderbook_app_settings::{
Config, ParseConfigSourceError,
};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use thiserror::Error;
use typeshare::typeshare;
#[cfg(target_family = "wasm")]
Expand Down Expand Up @@ -388,6 +389,51 @@ impl DotrainOrder {

Ok(())
}

pub fn populate_vault_ids(&mut self, deployment_name: &str) -> Result<(), DotrainOrderError> {
let mut deployment = self
.config
.deployments
.get(deployment_name)
.cloned()
.ok_or(DotrainOrderError::DeploymentNotFound(
deployment_name.to_string(),
))?
.as_ref()
.clone();
let mut order = deployment.order.as_ref().clone();
let vault_id = rand::random();

let new_inputs = deployment
.order
.inputs
.iter()
.map(|input| {
let mut input = input.clone();
input.vault_id = Some(input.vault_id.unwrap_or(vault_id));
input
})
.collect();
let new_outputs = deployment
.order
.outputs
.iter()
.map(|output| {
let mut output = output.clone();
output.vault_id = Some(output.vault_id.unwrap_or(vault_id));
output
})
.collect();

order.inputs = new_inputs;
order.outputs = new_outputs;
deployment.order = Arc::new(order);
self.config
.deployments
.insert(deployment_name.to_string(), Arc::new(deployment));

Ok(())
}
}

#[cfg(test)]
Expand Down
33 changes: 28 additions & 5 deletions crates/js_api/src/gui/order_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,19 @@ impl DotrainOrderGui {
/// Returns a vector of bytes
#[wasm_bindgen(js_name = "generateDepositCalldatas")]
pub async fn generate_deposit_calldatas(&mut self) -> Result<JsValue, GuiError> {
let token_deposits = self
.get_vaults_and_deposits()?
.iter()
.map(|(order_io, amount)| {
(
(order_io.vault_id.unwrap(), order_io.token.address),
*amount,
)
})
.collect::<HashMap<_, _>>();
let calldatas = self
.dotrain_order
.generate_deposit_calldatas(
&self.deployment.deployment_name,
&self.get_deposits_as_map()?,
)
.generate_deposit_calldatas(&self.deployment.deployment_name, &token_deposits)
.await?;
Ok(serde_wasm_bindgen::to_value(&calldatas)?)
}
Expand All @@ -157,7 +164,16 @@ impl DotrainOrderGui {
#[wasm_bindgen(js_name = "generateDepositAndAddOrderCalldatas")]
pub async fn generate_deposit_and_add_order_calldatas(&mut self) -> Result<JsValue, GuiError> {
let orderbook = self.get_orderbook()?;
let token_deposits = self.get_deposits_as_map()?;
let token_deposits = self
.get_vaults_and_deposits()?
.iter()
.map(|(order_io, amount)| {
(
(order_io.vault_id.unwrap(), order_io.token.address),
*amount,
)
})
.collect::<HashMap<_, _>>();

let mut calls = Vec::new();
let deposit_calldatas = self
Expand Down Expand Up @@ -186,4 +202,11 @@ impl DotrainOrderGui {
&aggregate3Call { calls }.abi_encode(),
))?)
}

#[wasm_bindgen(js_name = "populateVaultIds")]
pub fn populate_vault_ids(&mut self) -> Result<(), GuiError> {
self.dotrain_order
.populate_vault_ids(&self.deployment.deployment_name)?;
Ok(())
}
}

0 comments on commit b3b1d68

Please sign in to comment.