Skip to content

Commit

Permalink
Use .expect() and comment on try-catch blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinquaXD committed Dec 13, 2024
1 parent bd3a519 commit be7b80e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions crates/contracts/solidity/Trader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
42 changes: 29 additions & 13 deletions crates/shared/src/price_estimation/trade_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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,
}
};

Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit be7b80e

Please sign in to comment.