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

Function finish_simulation_builder

crates/simulator/src/encoding.rs:344–530  ·  view source on GitHub ↗
(
    mut builder: SimulationBuilder,
)

Source from the content-addressed store, hash-verified

342}
343
344pub(crate) async fn finish_simulation_builder(
345 mut builder: SimulationBuilder,
346) -> Result<EthCallInputs, BuildError> {
347 if builder.orders.is_empty() {
348 return Err(BuildError::NoOrder);
349 }
350
351 let block = match builder.block {
352 Block::Latest => builder.simulator.0.current_block.borrow().number,
353 Block::Number(n) => n,
354 };
355
356 let executed_amounts = futures::future::try_join_all(
357 builder
358 .orders
359 .iter()
360 .map(|o| executed_amount(&builder, o, block)),
361 )
362 .await?;
363
364 // Each order occupies exactly 2 consecutive slots in the token/price
365 // vectors: [2*i] = sell_token, [2*i+1] = buy_token.
366 // This lets every order be encoded independently without requiring a shared
367 // global token list.
368 let n = builder.orders.len();
369 let mut tokens = Vec::with_capacity(n * 2);
370 let mut clearing_prices = Vec::with_capacity(n * 2);
371 for order in &builder.orders {
372 let (sell_price, buy_price) = match &order.price_encoding {
373 PriceEncoding::LimitPrice => (order.data.buy_amount, order.data.sell_amount),
374 PriceEncoding::Custom {
375 sell_price,
376 buy_price,
377 } => (*sell_price, *buy_price),
378 };
379 tokens.push(order.data.sell_token);
380 tokens.push(order.data.buy_token);
381 clearing_prices.push(sell_price);
382 clearing_prices.push(buy_price);
383 }
384
385 if builder.provide_buy_tokens {
386 let settlement = *builder.simulator.0.settlement.address();
387 for (i, (order, &exec)) in builder.orders.iter().zip(&executed_amounts).enumerate() {
388 let sell_price = clearing_prices[2 * i];
389 let buy_price = clearing_prices[2 * i + 1];
390 let amount = match order.data.kind {
391 OrderKind::Sell => sell_price
392 .saturating_mul(exec)
393 .checked_div(buy_price)
394 .unwrap_or(U256::MAX),
395 OrderKind::Buy => exec,
396 }
397 // give 1 wei extra to avoid issues with rounding divisions
398 .saturating_add(U256::ONE);
399 builder
400 .account_override_requests
401 .push(AccountOverrideRequest::Balance {

Callers 1

buildMethod · 0.85

Calls 15

executed_amountFunction · 0.85
encode_tradeFunction · 0.85
encode_interactionsFunction · 0.85
lenMethod · 0.80
checked_divMethod · 0.80
extendMethod · 0.80
into_settle_callMethod · 0.80
to_be_bytesMethod · 0.80
collectMethod · 0.80
currentMethod · 0.80

Tested by

no test coverage detected