MCPcopy Create free account
hub / github.com/cowprotocol/services / prepare_simulation

Method prepare_simulation

crates/shared/src/order_creation_simulation.rs:107–152  ·  view source on GitHub ↗
(
        &self,
        order: &Order,
        full_app_data: &str,
        full_balance_check: bool,
    )

Source from the content-addressed store, hash-verified

105
106impl OrderCreationSimulator {
107 async fn prepare_simulation(
108 &self,
109 order: &Order,
110 full_app_data: &str,
111 full_balance_check: bool,
112 ) -> Result<EthCallInputs, OrderSimulationError> {
113 let amount_to_fill = match !order.data.partially_fillable || full_balance_check {
114 true => ExecutionAmount::Full,
115 false => {
116 let sell_token = ERC20::new(order.data.sell_token, self.inner.provider());
117 let sell_token_balance = sell_token
118 .balanceOf(order.metadata.owner)
119 .call()
120 .await
121 .unwrap_or_default();
122 let clamped_sell_amount =
123 sell_token_balance.clamp(U256::ONE, order.data.sell_amount);
124 let final_amount = match order.data.kind {
125 OrderKind::Sell => clamped_sell_amount,
126 // convert maxium sell amount to buy tokens
127 OrderKind::Buy => clamped_sell_amount
128 .checked_mul(order.data.buy_amount)
129 .and_then(|val| val.checked_div(order.data.sell_amount))
130 .unwrap_or(U256::ONE)
131 .max(U256::ONE),
132 };
133 ExecutionAmount::Explicit(final_amount)
134 }
135 };
136 let sim_order = simulation_builder::Order::new(order.data)
137 .with_signature(order.metadata.owner, order.signature.clone())
138 .fill_at(amount_to_fill, PriceEncoding::LimitPrice);
139
140 self.inner
141 .new_simulation_builder()
142 .with_orders([sim_order])
143 .parameters_from_app_data(full_app_data)
144 .map_err(|err| OrderSimulationError::Infra(anyhow!(err).context("parse app data")))?
145 .from_solver(Solver::Fake(None))
146 .provide_sufficient_buy_tokens()
147 .presign_orders()
148 .at_block(Block::Latest)
149 .build()
150 .await
151 .map_err(|err| OrderSimulationError::Infra(anyhow!(err).context("build")))
152 }
153}
154
155/// Runs the simulation, handles errors caused by fundamental limitations of

Callers 1

simulateMethod · 0.80

Calls 15

checked_mulMethod · 0.80
checked_divMethod · 0.80
fill_atMethod · 0.80
with_signatureMethod · 0.80
at_blockMethod · 0.80
presign_ordersMethod · 0.80
from_solverMethod · 0.80
with_ordersMethod · 0.80
contextMethod · 0.80

Tested by

no test coverage detected