From be7b80e184863272ab9c44bfb01d5b0fd7506ed5 Mon Sep 17 00:00:00 2001 From: MartinquaXD Date: Fri, 13 Dec 2024 12:34:36 +0000 Subject: [PATCH] Use `.expect()` and comment on try-catch blocks --- crates/contracts/solidity/Trader.sol | 1 + .../src/price_estimation/trade_verifier.rs | 42 +++++++++++++------ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/crates/contracts/solidity/Trader.sol b/crates/contracts/solidity/Trader.sol index 37f34f75c3..ff486515ab 100644 --- a/crates/contracts/solidity/Trader.sol +++ b/crates/contracts/solidity/Trader.sol @@ -105,6 +105,7 @@ contract Trader { // We first reset the allowance to 0 since some ERC20 tokens (e.g. USDT) // require that due to this attack: // https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 + // We catch reverts because we'll later assert the correct approval got set anyway. try IERC20(sellToken).approve(address(settlementContract.vaultRelayer()), 0) {} catch {} try IERC20(sellToken).approve(address(settlementContract.vaultRelayer()), type(uint256).max) {} diff --git a/crates/shared/src/price_estimation/trade_verifier.rs b/crates/shared/src/price_estimation/trade_verifier.rs index 12bad16c16..dd99562cb2 100644 --- a/crates/shared/src/price_estimation/trade_verifier.rs +++ b/crates/shared/src/price_estimation/trade_verifier.rs @@ -470,7 +470,13 @@ fn encode_settlement( OrderKind::Buy => query.in_amount.get(), }; let weth = dummy_contract!(WETH9, native_token); - let calldata = weth.methods().withdraw(buy_amount).tx.data.unwrap().0; + let calldata = weth + .methods() + .withdraw(buy_amount) + .tx + .data + .expect("data gets populated by function call above") + .0; trade_interactions.push((native_token, 0.into(), Bytes(calldata))); tracing::trace!("adding unwrap interaction for paying out ETH"); } @@ -496,18 +502,23 @@ fn encode_settlement( OrderKind::Buy => *out_amount, }; let solver = dummy_contract!(Solver, trade.solver()); - let setup_step = solver.ensure_trade_preconditions( - verification.from, - settlement, - query.sell_token, - sell_amount, - native_token, - TradeVerifier::SPARDOSE, - ); + let setup_call = solver + .ensure_trade_preconditions( + verification.from, + settlement, + query.sell_token, + sell_amount, + native_token, + TradeVerifier::SPARDOSE, + ) + .tx + .data + .expect("data gets populated by function call above") + .0; Interaction { target: solver.address(), value: 0.into(), - data: setup_step.tx.data.unwrap().0, + data: setup_call, } }; @@ -681,9 +692,14 @@ fn add_balance_queries( // track how much `sell_token` the `from` address actually spent OrderKind::Buy => (query.sell_token, verification.from), }; - let query_balance = solver.methods().store_balance(token, owner, true); - let query_balance = Bytes(query_balance.tx.data.unwrap().0); - let interaction = (solver.address(), 0.into(), query_balance); + let query_balance_call = solver + .methods() + .store_balance(token, owner, true) + .tx + .data + .expect("data gets populated by function call above") + .0; + let interaction = (solver.address(), 0.into(), Bytes(query_balance_call)); // query balance query at the end of pre-interactions settlement.interactions[0].push(interaction.clone()); // query balance right after we payed out all `buy_token`