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