(
mut builder: SimulationBuilder,
)
| 342 | } |
| 343 | |
| 344 | pub(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 { |
no test coverage detected