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

Function finish_simulation_builder

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

Callers 1

buildMethod · 0.85

Calls 15

executed_amountFunction · 0.85
encode_tradeFunction · 0.85
encode_interactionsFunction · 0.85
checked_divMethod · 0.80
extendMethod · 0.80
into_settle_callMethod · 0.80
to_be_bytesMethod · 0.80
collectMethod · 0.80
overrides_forMethod · 0.80
PreSignatureClass · 0.50

Tested by

no test coverage detected