Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nlordell committed Dec 5, 2024
1 parent 8f09fc2 commit 604beb4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/contracts/artifacts/Solver.json

Large diffs are not rendered by default.

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":"0x608060405234801561001057600080fd5b506103ee806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e3660046102fa565b610045565b005b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8481166004830152600091908416906370a0823190602401602060405180830381865afa1580156100b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d99190610336565b90508181106100e85750505050565b60006100f4828461034f565b905061011773ffffffffffffffffffffffffffffffffffffffff851686836101ae565b6101a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f5f746f6b656e0000000000000000000000000000000000000000000000000000606482015260840160405180910390fd5b5050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb00000000000000000000000000000000000000000000000000000000179052915160009260609190871690610247908490610389565b6000604051808303816000865af19150503d8060008114610284576040519150601f19603f3d011682016040523d82523d6000602084013e610289565b606091505b5090935090508280156102a057506102a0816102aa565b9695505050505050565b60008151600014806102cb5750818060200190518101906102cb91906103b8565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff811681146102f557600080fd5b919050565b60008060006060848603121561030f57600080fd5b610318846102d1565b9250610326602085016102d1565b9150604084013590509250925092565b60006020828403121561034857600080fd5b5051919050565b818103818111156102cb577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b818110156103aa5760208186018101518583015201610390565b506000920191825250919050565b6000602082840312156103ca57600080fd5b815180151581146103da57600080fd5b939250505056fea164736f6c6343000811000a","deployedBytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e3660046102fa565b610045565b005b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff8481166004830152600091908416906370a0823190602401602060405180830381865afa1580156100b5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d99190610336565b90508181106100e85750505050565b60006100f4828461034f565b905061011773ffffffffffffffffffffffffffffffffffffffff851686836101ae565b6101a7576040517f08c379a000000000000000000000000000000000000000000000000000000000815260206004820152602660248201527f74726164657220646f6573206e6f74206861766520656e6f7567682073656c6c60448201527f5f746f6b656e0000000000000000000000000000000000000000000000000000606482015260840160405180910390fd5b5050505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb00000000000000000000000000000000000000000000000000000000179052915160009260609190871690610247908490610389565b6000604051808303816000865af19150503d8060008114610284576040519150601f19603f3d011682016040523d82523d6000602084013e610289565b606091505b5090935090508280156102a057506102a0816102aa565b9695505050505050565b60008151600014806102cb5750818060200190518101906102cb91906103b8565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff811681146102f557600080fd5b919050565b60008060006060848603121561030f57600080fd5b610318846102d1565b9250610326602085016102d1565b9150604084013590509250925092565b60006020828403121561034857600080fd5b5051919050565b818103818111156102cb577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b818110156103aa5760208186018101518583015201610390565b506000920191825250919050565b6000602082840312156103ca57600080fd5b815180151581146103da57600080fd5b939250505056fea164736f6c6343000811000a","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":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"}],"bytecode":"0x608060405234801561001057600080fd5b50610364806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e366004610277565b610057565b604051901515815260200160405180910390f35b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff848116600483015260009182918516906370a0823190602401602060405180830381865afa1580156100c8573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100ec91906102b3565b90508281106100ff576001915050610133565b600061010b82856102cc565b905061012e73ffffffffffffffffffffffffffffffffffffffff8616878361013a565b925050505b9392505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb000000000000000000000000000000000000000000000000000000001790529151600092606091908716906101d3908490610306565b6000604051808303816000865af19150503d8060008114610210576040519150601f19603f3d011682016040523d82523d6000602084013e610215565b606091505b50909350905082801561012e575061012e8160008151600014806102485750818060200190518101906102489190610335565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff8116811461027257600080fd5b919050565b60008060006060848603121561028c57600080fd5b6102958461024e565b92506102a36020850161024e565b9150604084013590509250925092565b6000602082840312156102c557600080fd5b5051919050565b81810381811115610248577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b81811015610327576020818601810151858301520161030d565b506000920191825250919050565b60006020828403121561034757600080fd5b8151801515811461013357600080fdfea164736f6c6343000811000a","deployedBytecode":"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c56cca8314610030575b600080fd5b61004361003e366004610277565b610057565b604051901515815260200160405180910390f35b6040517f70a0823100000000000000000000000000000000000000000000000000000000815273ffffffffffffffffffffffffffffffffffffffff848116600483015260009182918516906370a0823190602401602060405180830381865afa1580156100c8573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100ec91906102b3565b90508281106100ff576001915050610133565b600061010b82856102cc565b905061012e73ffffffffffffffffffffffffffffffffffffffff8616878361013a565b925050505b9392505050565b6040805173ffffffffffffffffffffffffffffffffffffffff8481166024830152604480830185905283518084039091018152606490920183526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fa9059cbb000000000000000000000000000000000000000000000000000000001790529151600092606091908716906101d3908490610306565b6000604051808303816000865af19150503d8060008114610210576040519150601f19603f3d011682016040523d82523d6000602084013e610215565b606091505b50909350905082801561012e575061012e8160008151600014806102485750818060200190518101906102489190610335565b92915050565b803573ffffffffffffffffffffffffffffffffffffffff8116811461027257600080fd5b919050565b60008060006060848603121561028c57600080fd5b6102958461024e565b92506102a36020850161024e565b9150604084013590509250925092565b6000602082840312156102c557600080fd5b5051919050565b81810381811115610248577f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000825160005b81811015610327576020818601810151858301520161030d565b506000920191825250919050565b60006020828403121561034757600080fd5b8151801515811461013357600080fdfea164736f6c6343000811000a","devdoc":{"methods":{}},"userdoc":{"methods":{}}}
10 changes: 7 additions & 3 deletions crates/contracts/solidity/Solver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,13 @@ contract Solver {
nativeToken
);

// Ensure that the user has sufficient sell token balance for the
// swap using balance overrides.
Spardose(mock.spardose).ensureBalance(trader, sellToken, sellAmount);
// Ensure that the user has sufficient sell token balance. In case
// balance overrides are enabled, the Spardose will fund the trader
// with simulated balances.
require(
Spardose(mock.spardose).ensureBalance(trader, sellToken, sellAmount),
"trader does not have enough sell token"
);
}

// Warm the storage for sending ETH to smart contract addresses.
Expand Down
16 changes: 7 additions & 9 deletions crates/contracts/solidity/Spardose.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@ contract Spardose {
using SafeERC20 for *;

/// @dev Ensures that the trader has at least `amount` tokens. If not, it
/// will transfer the difference to the trader.
/// will try and transfer the difference to the trader.
///
/// @param trader - the address of the trader
/// @param token - the token to ensure a balance for
/// @param amount - the amount of `token` that the `trader` must hold.
function ensureBalance(address trader, address token, uint256 amount) external {
/// @param amount - the amount of `token` that the `trader` must hold
///
/// @return success - the `trader`'s `token` balance is more than `amount`
function ensureBalance(address trader, address token, uint256 amount) external returns (bool success) {
uint256 traderBalance = IERC20(token).balanceOf(trader);
if (traderBalance >= amount) {
// Nothing to do.
return;
return true;
}

uint256 difference = amount - traderBalance;
require(
IERC20(token).trySafeTransfer(trader, difference),
"trader does not have enough sell_token"
);
return IERC20(token).trySafeTransfer(trader, difference);
}
}
7 changes: 3 additions & 4 deletions crates/shared/src/price_estimation/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,10 @@ impl<'a> PriceEstimatorFactory<'a> {
network.native_token,
args.quote_inaccuracy_limit.clone(),
)
.await?;
.await?
.with_balance_overrides(balance_overrides);

Ok(Some(Arc::new(
verifier.with_balance_overrides(balance_overrides),
)))
Ok(Some(Arc::new(verifier)))
}

fn native_token_price_estimation_amount(&self) -> Result<NonZeroU256> {
Expand Down
9 changes: 8 additions & 1 deletion crates/shared/src/price_estimation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,14 @@ pub struct Arguments {
)]
pub quote_timeout: Duration,

/// Token configuration for simulated balances on verified quotes.
/// Token configuration for simulated balances on verified quotes. This
/// allows the quote verification system to produce verified quotes for
/// traders without sufficient balance for the configured token pairs.
///
/// The expected format is a comma separated list of `${ADDR}@${SLOT}`,
/// where `ADDR` is the token address and `SLOT` is the Solidity storage
/// slot for the balances mapping. For example for WETH:
/// `0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2@3`.
#[clap(long, env, default_value_t)]
pub quote_token_balance_overrides: ConfigurationBalanceOverrides,
}
Expand Down
9 changes: 5 additions & 4 deletions crates/shared/src/price_estimation/trade_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,13 @@ impl TradeVerifier {
},
);

// Provide mocked balances if possible to the solver to allow it to
// Provide mocked balances if possible to the spardose to allow it to
// give some balances to the trader in order to verify trades even for
// owners without balances. Note that we explicitly override the
// **solver's** balance and not the trader's directly. This allows the
// owners without balances. Note that we use a separate account for
// funding to not interfere with the settlement process. This allows the
// simulation to conditionally transfer the balance only when it is
// safe to mock the trade pre-conditions on behalf of the user. We use
// safe to mock the trade pre-conditions on behalf of the user and to
// not alter solver balances which may be used during settlement. We use
// a similar strategy for determining whether or not to set approvals on
// behalf of the trader.
if let Some(solver_balance_override) =
Expand Down

0 comments on commit 604beb4

Please sign in to comment.