Skip to content

Commit

Permalink
backport fixes from #4
Browse files Browse the repository at this point in the history
  • Loading branch information
nlordell committed Dec 4, 2024
1 parent 0d48acf commit 723758d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
2 changes: 1 addition & 1 deletion crates/contracts/artifacts/Spardose.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"abi":[{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"ensureBalance","outputs":[],"stateMutability":"nonpayable","type":"function"}],"bytecode":"0x608060405234801561001057600080fd5b5061042f806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e366004610342565b610045565b005b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8481166004830152600091908416906370a0823190602401602060405180830381865afa1580156100b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d9919061037e565b90508181106100e85750505050565b60006100f48284610397565b905061011773ffffffffffffffffffffffffffffffffffffffff851686836101ae565b6101a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f5f746f6b656e0000000000000000000000000000000000000000000000000000606482015260840160405180910390fd5b5050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff848116602483015260448083018590528351808403909101815260649092019092526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb00000000000000000000000000000000000000000000000000000000179052600091829061024290871683610257565b905061024d8161026c565b9695505050505050565b606061026583600084610293565b9392505050565b600081516000148061028d57508180602001905181019061028d91906103d1565b92915050565b606060008473ffffffffffffffffffffffffffffffffffffffff1684846040516102bd91906103f3565b60006040518083038185875af1925050503d80600081146102fa576040519150601f19603f3d011682016040523d82523d6000602084013e6102ff565b606091505b50925090508061031157815160208301fd5b509392505050565b803573ffffffffffffffffffffffffffffffffffffffff8116811461033d57600080fd5b919050565b60008060006060848603121561035757600080fd5b61036084610319565b925061036e60208501610319565b9150604084013590509250925092565b60006020828403121561039057600080fd5b5051919050565b8181038181111561028d577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000602082840312156103e357600080fd5b8151801515811461026557600080fd5b6000825160005b8181101561041457602081860181015185830152016103fa565b50600092019182525091905056fea164736f6c6343000811000a","deployedBytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e366004610342565b610045565b005b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8481166004830152600091908416906370a0823190602401602060405180830381865afa1580156100b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d9919061037e565b90508181106100e85750505050565b60006100f48284610397565b905061011773ffffffffffffffffffffffffffffffffffffffff851686836101ae565b6101a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f5f746f6b656e0000000000000000000000000000000000000000000000000000606482015260840160405180910390fd5b5050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff848116602483015260448083018590528351808403909101815260649092019092526020810180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb00000000000000000000000000000000000000000000000000000000179052600091829061024290871683610257565b905061024d8161026c565b9695505050505050565b606061026583600084610293565b9392505050565b600081516000148061028d57508180602001905181019061028d91906103d1565b92915050565b606060008473ffffffffffffffffffffffffffffffffffffffff1684846040516102bd91906103f3565b60006040518083038185875af1925050503d80600081146102fa576040519150601f19603f3d011682016040523d82523d6000602084013e6102ff565b606091505b50925090508061031157815160208301fd5b509392505050565b803573ffffffffffffffffffffffffffffffffffffffff8116811461033d57600080fd5b919050565b60008060006060848603121561035757600080fd5b61036084610319565b925061036e60208501610319565b9150604084013590509250925092565b60006020828403121561039057600080fd5b5051919050565b8181038181111561028d577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000602082840312156103e357600080fd5b8151801515811461026557600080fd5b6000825160005b8181101561041457602081860181015185830152016103fa565b50600092019182525091905056fea164736f6c6343000811000a","devdoc":{"methods":{}},"userdoc":{"methods":{}}}
{"abi":[{"inputs":[{"internalType":"address","name":"trader","type":"address"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"ensureBalance","outputs":[],"stateMutability":"nonpayable","type":"function"}],"bytecode":"0x608060405234801561001057600080fd5b506103ee806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e3660046102fa565b610045565b005b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8481166004830152600091908416906370a0823190602401602060405180830381865afa1580156100b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d99190610336565b90508181106100e85750505050565b60006100f4828461034f565b905061011773ffffffffffffffffffffffffffffffffffffffff851686836101ae565b6101a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f5f746f6b656e0000000000000000000000000000000000000000000000000000606482015260840160405180910390fd5b5050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb00000000000000000000000000000000000000000000000000000000179052915160009260609190871690610247908490610389565b6000604051808303816000865af19150503d8060008114610284576040519150601f19603f3d011682016040523d82523d6000602084013e610289565b606091505b5090935090508280156102a057506102a0816102aa565b9695505050505050565b60008151600014806102cb5750818060200190518101906102cb91906103b8565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff811681146102f557600080fd5b919050565b60008060006060848603121561030f57600080fd5b610318846102d1565b9250610326602085016102d1565b9150604084013590509250925092565b60006020828403121561034857600080fd5b5051919050565b818103818111156102cb577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b818110156103aa5760208186018101518583015201610390565b506000920191825250919050565b6000602082840312156103ca57600080fd5b815180151581146103da57600080fd5b939250505056fea164736f6c6343000811000a","deployedBytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e3660046102fa565b610045565b005b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8481166004830152600091908416906370a0823190602401602060405180830381865afa1580156100b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d99190610336565b90508181106100e85750505050565b60006100f4828461034f565b905061011773ffffffffffffffffffffffffffffffffffffffff851686836101ae565b6101a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f5f746f6b656e0000000000000000000000000000000000000000000000000000606482015260840160405180910390fd5b5050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb00000000000000000000000000000000000000000000000000000000179052915160009260609190871690610247908490610389565b6000604051808303816000865af19150503d8060008114610284576040519150601f19603f3d011682016040523d82523d6000602084013e610289565b606091505b5090935090508280156102a057506102a0816102aa565b9695505050505050565b60008151600014806102cb5750818060200190518101906102cb91906103b8565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff811681146102f557600080fd5b919050565b60008060006060848603121561030f57600080fd5b610318846102d1565b9250610326602085016102d1565b9150604084013590509250925092565b60006020828403121561034857600080fd5b5051919050565b818103818111156102cb577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b818110156103aa5760208186018101518583015201610390565b506000920191825250919050565b6000602082840312156103ca57600080fd5b815180151581146103da57600080fd5b939250505056fea164736f6c6343000811000a","devdoc":{"methods":{}},"userdoc":{"methods":{}}}
5 changes: 3 additions & 2 deletions crates/contracts/solidity/libraries/SafeERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ library SafeERC20 {

function trySafeTransfer(IERC20 self, address target, uint256 amount) internal returns (bool success) {
bytes memory cdata = abi.encodeCall(self.transfer, (target, amount));
bytes memory rdata = address(self).doCall(cdata);
return check(rdata);
bytes memory rdata;
(success, rdata) = address(self).call(cdata);
return success && check(rdata);
}

function safeApprove(IERC20 self, address target, uint256 amount) internal {
Expand Down
51 changes: 48 additions & 3 deletions crates/e2e/tests/e2e/quote_verification.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use {
e2e::setup::*,
ethcontract::H160,
e2e::{setup::*, tx},
ethcontract::{H160, U256},
ethrpc::Web3,
model::{
order::{BuyTokenDestination, OrderKind, SellTokenSource},
Expand All @@ -18,6 +18,12 @@ use {
std::{str::FromStr, sync::Arc},
};

#[tokio::test]
#[ignore]
async fn local_node_standard_verified_quote() {
run_test(standard_verified_quote).await;
}

#[tokio::test]
#[ignore]
async fn forked_node_bypass_verification_for_rfq_quotes() {
Expand All @@ -42,6 +48,45 @@ async fn local_node_verified_quote_with_simulated_balance() {
run_test(verified_quote_with_simulated_balance).await;
}

/// Verified quotes work as expected.
async fn standard_verified_quote(web3: Web3) {
tracing::info!("Setting up chain state.");
let mut onchain = OnchainComponents::deploy(web3).await;

let [solver] = onchain.make_solvers(to_wei(10)).await;
let [trader] = onchain.make_accounts(to_wei(1)).await;
let [token] = onchain
.deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000))
.await;

token.mint(trader.address(), to_wei(1)).await;
tx!(
trader.account(),
token.approve(onchain.contracts().allowance, to_wei(1))
);

tracing::info!("Starting services.");
let services = Services::new(&onchain).await;
services.start_protocol(solver).await;

// quote where the trader has no balances or approval set.
let response = services
.submit_quote(&OrderQuoteRequest {
from: trader.address(),
sell_token: token.address(),
buy_token: onchain.contracts().weth.address(),
side: OrderQuoteSide::Sell {
sell_amount: SellAmount::BeforeFee {
value: to_wei(1).try_into().unwrap(),
},
},
..Default::default()
})
.await
.unwrap();
assert!(response.verified);
}

/// The block number from which we will fetch state for the forked tests.
const FORK_BLOCK_MAINNET: u64 = 19796077;

Expand Down Expand Up @@ -237,7 +282,7 @@ async fn verified_quote_with_simulated_balance(web3: Web3) {
)],
..Default::default()
},
solver.clone(),
solver,
)
.await;

Expand Down
4 changes: 3 additions & 1 deletion crates/shared/src/code_simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ mod tests {
use {
super::*,
crate::{ethrpc::create_env_test_transport, tenderly_api::TenderlyHttpApi},
ethcontract::H256,
hex_literal::hex,
maplit::hashmap,
std::time::Duration,
Expand Down Expand Up @@ -385,7 +386,8 @@ mod tests {
hashmap! {
addr!("D533a949740bb3306d119CC777fa900bA034cd52") => StateOverride {
state_diff: Some(hashmap! {
H256(balance_slot) => 1.into()
H256(balance_slot) =>
H256(hex!("0000000000000000000000000000000000000000000000000000000000000001"))
}),
..Default::default()
},
Expand Down
29 changes: 17 additions & 12 deletions crates/shared/src/price_estimation/trade_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,17 @@ impl TradeVerifier {
);
}

// Set up mocked solver.
let mut solver_override = StateOverride {
code: Some(deployed_bytecode!(Solver)),
..Default::default()
};
// Setup the funding contract override. Regardless of whether or not the
// contract has funds, it needs to exist in order to not revert
// simulations (Solidity reverts on attempts to call addresses without
// any code).
overrides.insert(
Self::SPARDOSE,
StateOverride {
code: Some(deployed_bytecode!(Spardose)),
..Default::default()
},
);

// Provide mocked balances if possible to the solver to allow it to
// give some balances to the trader in order to verify trades even for
Expand All @@ -309,15 +315,14 @@ impl TradeVerifier {
{
tracing::debug!(?solver_balance_override, "solver balance override enabled");
overrides.insert(query.sell_token, solver_balance_override);
overrides.insert(
Self::SPARDOSE,
StateOverride {
code: Some(deployed_bytecode!(Spardose)),
..Default::default()
},
);
}

// Set up mocked solver.
let mut solver_override = StateOverride {
code: Some(deployed_bytecode!(Solver)),
..Default::default()
};

// If the trade requires a special tx.origin we also need to fake the
// authenticator and tx origin balance.
if trade.tx_origin.is_some_and(|origin| origin != trade.solver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl BalanceOverriding for ConfigurationBalanceOverrides {
let strategy = self.0.get(&request.token)?;
match strategy {
Strategy::Mapping { slot } => {
let slot = address_mapping_storage_slot(&slot, &request.holder);
let slot = address_mapping_storage_slot(slot, &request.holder);
let value = {
let mut value = H256::default();
request.amount.to_big_endian(&mut value.0);
Expand Down Expand Up @@ -144,7 +144,8 @@ mod tests {
}),
Some(StateOverride {
state_diff: Some(hashmap! {
H256(hex!("fca351f4d96129454cfc8ef7930b638ac71fea35eb69ee3b8d959496beb04a33")) => 0x42_u64.into()
H256(hex!("fca351f4d96129454cfc8ef7930b638ac71fea35eb69ee3b8d959496beb04a33")) =>
H256(hex!("0000000000000000000000000000000000000000000000000000000000000042"))
}),
..Default::default()
}),
Expand Down

0 comments on commit 723758d

Please sign in to comment.