Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
ab8357b
initial commit
MartinquaXD Apr 22, 2026
76b12c6
fixup
MartinquaXD Apr 22, 2026
d410031
fixup
MartinquaXD Apr 22, 2026
417c9a0
introduce Solver enum
MartinquaXD Apr 22, 2026
19926d9
factory
MartinquaXD Apr 22, 2026
9731a30
Only allow list specific solver address
MartinquaXD Apr 22, 2026
0f03c74
add function for funding settlement contract
MartinquaXD Apr 22, 2026
afedc26
add executed amounts
MartinquaXD Apr 22, 2026
db932ac
Move flashloan router address into simulator thingy
MartinquaXD Apr 22, 2026
258539b
fixup
MartinquaXD Apr 22, 2026
969ffee
simulate(), handle fillable amounts
MartinquaXD Apr 24, 2026
ae91cf6
Better handling of block target and executed amount
MartinquaXD Apr 27, 2026
2ccedbb
Move encoding logic into separate file
MartinquaXD Apr 27, 2026
c1a0fe2
move code around
MartinquaXD Apr 27, 2026
36d46aa
store chain_id and convert to tenderly request
MartinquaXD Apr 27, 2026
9259b06
current block watcher in simulator
MartinquaXD Apr 27, 2026
fb58a25
more fixes
MartinquaXD Apr 28, 2026
8a0cd78
fmt
MartinquaXD Apr 28, 2026
dcdd986
more fixes
MartinquaXD Apr 28, 2026
6ecd600
correct tenderly block
MartinquaXD Apr 29, 2026
93a8dda
comment for idea
MartinquaXD Apr 29, 2026
5c1f7c3
extract wrappers from appdata
MartinquaXD Apr 29, 2026
3f33170
Populate properties from appdata
MartinquaXD Apr 29, 2026
7d0253a
.context() instead of unwrap()
MartinquaXD Apr 29, 2026
764d3e7
fix tests
MartinquaXD Apr 29, 2026
934da39
remove test log
MartinquaXD Apr 29, 2026
67d7a8a
Merge branch 'main' into new-api-simulator-crate
squadgazzz Apr 29, 2026
c1aabeb
more functionality needed for trade verification
MartinquaXD Apr 30, 2026
29c2254
remove need for native token address in trade verification
MartinquaXD Apr 30, 2026
9aec495
Nicer API for handling state overrides
MartinquaXD Apr 30, 2026
0a092dc
build final state overrides in separate function
MartinquaXD Apr 30, 2026
ba042f0
claude suggested changes
MartinquaXD Apr 30, 2026
bfc5627
use `SettlementSimulator` in trade verification
MartinquaXD Apr 30, 2026
ff58e63
Remove old code
MartinquaXD Apr 30, 2026
104b759
assemble state overrides via simulator crate
MartinquaXD Apr 30, 2026
12dead3
best effort override resolution
MartinquaXD Apr 30, 2026
aad3997
Remove customize functionality
MartinquaXD Apr 30, 2026
56b020b
Support multiple orders
MartinquaXD Apr 30, 2026
4dd3628
nicer API
MartinquaXD Apr 30, 2026
a0c3bcc
nits
MartinquaXD Apr 30, 2026
1de44d9
api improvements
MartinquaXD Apr 30, 2026
2afaf88
2 price vector entries per order
MartinquaXD Apr 30, 2026
439d719
move gas_limit into settlement simulator
MartinquaXD Apr 30, 2026
0929b38
Merge branch 'main' into new-api-simulator-crate
MartinquaXD Apr 30, 2026
29ed574
sort dependencies
MartinquaXD Apr 30, 2026
543d466
fix lints
MartinquaXD Apr 30, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions contracts/artifacts/Solver.json

Large diffs are not rendered by default.

9 changes: 2 additions & 7 deletions contracts/artifacts/Trader.json

Large diffs are not rendered by default.

41 changes: 12 additions & 29 deletions contracts/generated/contracts-generated/solver/src/lib.rs

Large diffs are not rendered by default.

41 changes: 12 additions & 29 deletions contracts/generated/contracts-generated/trader/src/lib.rs

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions contracts/solidity/Solver.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;

import { IERC20, INativeERC20 } from "./interfaces/IERC20.sol";
import { IERC20 } from "./interfaces/IERC20.sol";
import { Interaction, Trade, ISettlement } from "./interfaces/ISettlement.sol";
import { Caller } from "./libraries/Caller.sol";
import { Math } from "./libraries/Math.sol";
Expand Down Expand Up @@ -117,15 +117,13 @@ contract Solver layout at 0x14f5b2c185fc03c75c787d1f0e10ea137cc6d235a0047448eff1
ISettlement settlementContract,
address sellToken,
uint256 sellAmount,
address nativeToken,
address spardose
) external {
uint256 gasStart = gasleft();
trader.ensureTradePreconditions(
settlementContract,
sellToken,
sellAmount,
nativeToken,
spardose
);
// Account for costs of gas used outside of metered section.
Expand Down
19 changes: 0 additions & 19 deletions contracts/solidity/Trader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,15 @@ contract Trader layout at 0x02565dba7d68dcbed629110024b7b5e785bfc1a484602045eea5
/// a stable address in tests.
/// @param sellToken - token being sold by the trade
/// @param sellAmount - expected amount to be sold according to the quote
/// @param nativeToken - ERC20 version of the chain's native token
/// @param spardose - piggy bank for requesting additional funds
function ensureTradePreconditions(
ISettlement settlementContract,
address sellToken,
uint256 sellAmount,
address nativeToken,
address spardose
) external {
require(!triggerInitialization(), "prepareSwap can only be called once");

if (sellToken == nativeToken) {
uint256 availableNativeToken = IERC20(sellToken).balanceOf(address(this));
if (availableNativeToken < sellAmount) {
uint256 amountToWrap = sellAmount - availableNativeToken;
// If the user has sufficient balance, simulate the wrapping the missing
// `ETH` so the user doesn't have to spend gas on that just to get a quote.
// If they are happy with the quote and want to create an order they will
// actually have to do the wrapping, though. Note that we don't attempt to
// wrap if the user doesn't have sufficient `ETH` balance, since that would
// revert. Instead, we fall-through so that we handle insufficient sell
// token balances uniformly for all tokens.
if (address(this).balance >= amountToWrap) {
INativeERC20(nativeToken).deposit{value: amountToWrap}();
}
}
}

address vaultRelayer = settlementContract.vaultRelayer();
uint256 currentAllowance = IERC20(sellToken).allowance(address(this), vaultRelayer);
if (currentAllowance < sellAmount) {
Expand Down
57 changes: 52 additions & 5 deletions crates/e2e/tests/e2e/order_simulation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use {
alloy::{primitives::Address, providers::Provider},
alloy::{
primitives::{Address, ruint::aliases::U256},
providers::Provider,
},
configs::test_util::TestDefault,
e2e::setup::{API_HOST, OnchainComponents, Services, TIMEOUT, run_test, wait_for_condition},
ethrpc::{Web3, alloy::CallBuilderExt},
model::{
order::{OrderCreation, OrderKind},
order::{
BuyTokenDestination,
OrderCreation,
OrderCreationAppData,
OrderKind,
SellTokenSource,
},
signature::EcdsaSigningScheme,
},
number::units::EthUnit,
Expand Down Expand Up @@ -50,15 +59,53 @@ async fn custom_order_simulation(web3: Web3) {
.await;

let client = services.client();
let valid_to = model::time::now_in_epoch_seconds() + 100;

let sell_amount = 1u64.eth();
let request = orderbook::dto::OrderSimulationRequest {
let app_data = "{}";

// Sign an OrderCreation with the same fields the simulation will encode.
// simulate_custom_order hashes `app_data` with keccak256; OrderCreation::Full
// does the same, so the signature covers exactly the same OrderData hash.
let signed = OrderCreation {
sell_token: *token.address(),
buy_token: *onchain.contracts().weth.address(),
sell_amount: sell_amount.try_into().expect("Sell amount is non zero"),
sell_amount,
buy_amount: 1u64.eth(),
kind: OrderKind::Sell,
owner: trader.address(),
receiver: Some(Address::default()),
sell_token_balance: SellTokenSource::Erc20,
buy_token_balance: BuyTokenDestination::Erc20,
fee_amount: U256::ZERO,
valid_to,
app_data: OrderCreationAppData::Full {
full: app_data.to_string(),
},
partially_fillable: false,
..Default::default()
}
.sign(
EcdsaSigningScheme::Eip712,
&onchain.contracts().domain_separator,
&trader.signer,
);

let request = orderbook::dto::OrderSimulationRequest {
sell_token: signed.sell_token,
buy_token: signed.buy_token,
sell_amount: signed.sell_amount.try_into().unwrap(),
buy_amount: signed.buy_amount,
kind: signed.kind,
owner: trader.address(),
receiver: signed.receiver,
sell_token_balance: signed.sell_token_balance,
buy_token_balance: signed.buy_token_balance,
app_data: app_data.to_string(),
block_number: None,
signature: signed.signature,
fee_amount: signed.fee_amount,
valid_to: signed.valid_to,
partially_fillable: signed.partially_fillable,
};

// Trader has no sell tokens — simulation should revert.
Expand Down
29 changes: 15 additions & 14 deletions crates/e2e/tests/e2e/quote_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use {
price_estimation::BalanceOverridesConfig,
test_util::TestDefault,
},
contracts::ERC20,
contracts::{ERC20, GPv2Settlement},
e2e::setup::*,
ethrpc::{Web3, alloy::CallBuilderExt},
model::{
Expand All @@ -28,7 +28,7 @@ use {
trade_verifier::{PriceQuery, TradeVerifier, TradeVerifying},
},
serde_json::json,
simulator::swap_simulator::SwapSimulator,
simulator::simulation_builder::SettlementSimulator,
std::{collections::HashMap, sync::Arc},
};

Expand Down Expand Up @@ -189,32 +189,33 @@ async fn test_bypass_verification_for_rfq_quotes(web3: Web3) {
.unwrap();
let onchain = OnchainComponents::deployed(web3.clone()).await;
let balance_overrides = Arc::new(BalanceOverrides::default());
let gas_limit = 12_000_000;
let simulator = SwapSimulator::new(
balance_overrides.clone(),
let gas_limit = 12_000_000u64;
let settlement_contract = GPv2Settlement::GPv2Settlement::new(
*onchain.contracts().gp_settlement.address(),
web3.provider.clone(),
);
let simulator = SettlementSimulator::new(
settlement_contract,
Default::default(),
Default::default(),
*onchain.contracts().weth.address(),
block_stream.clone(),
web3.clone(),
gas_limit,
balance_overrides,
block_stream.clone(),
None,
)
.await
.unwrap();

let verifier = TradeVerifier::new(
web3.clone(),
None,
simulator,
None,
Arc::new(web3.clone()),
balance_overrides,
*onchain.contracts().gp_settlement.address(),
BigDecimal::zero(),
Default::default(),
0,
u32::MAX,
)
.await
.unwrap();
);

let verify_trade = |tx_origin| {
let verifier = verifier.clone();
Expand Down
25 changes: 20 additions & 5 deletions crates/orderbook/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2731,31 +2731,46 @@ components:
if omitted.
allOf:
- $ref: "#/components/schemas/Address"
nullable: true
sellTokenBalance:
description: Where the sell token should be drawn from.
allOf:
- $ref: "#/components/schemas/SellTokenSource"
default: erc20
buyTokenBalance:
description: Where the buy token should be transferred to.
allOf:
- $ref: "#/components/schemas/BuyTokenDestination"
default: erc20
appData:
description: >
Full app data JSON string. Defaults to `"{}"` if omitted.
Full app data JSON string.
type: string
nullable: true
blockNumber:
type: integer
nullable: true
signingScheme:
$ref: "#/components/schemas/SigningScheme"
signature:
$ref: "#/components/schemas/Signature"
feeAmount:
description: >
The fee amount in sell token atoms. Expected to be 0; only present
because it must be part of the signed order data.
allOf:
- $ref: "#/components/schemas/TokenAmount"
validTo:
description: Unix timestamp (`uint32`) until which the order is valid.
type: integer
partiallyFillable:
description: Whether the order can be partially filled or must be filled all at once.
type: boolean
required:
- sellToken
- buyToken
- sellAmount
- buyAmount
- kind
- owner
- signingScheme
- signature
OrderSimulation:
description: >
The Tenderly simulation request for an order, along with any
Expand Down
23 changes: 18 additions & 5 deletions crates/orderbook/src/dto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ pub mod order;
use {
alloy::primitives::U256,
eth_domain_types::{Address, NonZeroU256},
model::order::{BuyTokenDestination, OrderKind, SellTokenSource},
model::{
order::{BuyTokenDestination, OrderKind, SellTokenSource},
signature::Signature,
},
number::serialization::HexOrDecimalU256,
serde::{Deserialize, Serialize},
serde_with::serde_as,
Expand Down Expand Up @@ -45,12 +48,22 @@ pub struct OrderSimulationRequest {
/// token transfer or internal Balancer Vault transfer.
#[serde(default)]
pub buy_token_balance: BuyTokenDestination,
/// Full app data JSON. Defaults to `"{}"` if omitted.
#[serde(default)]
pub app_data: Option<String>,
/// Full app data JSON.
pub app_data: String,
/// The block number at which the simulation should happen
#[serde(default)]
pub block_number: Option<u64>,
/// The order signature (signing scheme + bytes). Required to pass
/// signature verification in the settlement contract during simulation.
#[serde(flatten)]
pub signature: Signature,
/// The fee amount signed by the user. This field is expected to be 0 and
/// only still there because it needs to be signed by the user.
pub fee_amount: U256,
/// UNIX timestamp when the order will expire.
pub valid_to: u32,
/// Whether the order needs to be filled all at once or allows to be filled
/// over multiple partial executions.
pub partially_fillable: bool,
}

/// The result of Order simulation, contains the error (if any)
Expand Down
1 change: 0 additions & 1 deletion crates/orderbook/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pub mod database;
pub mod dto;
mod ipfs;
mod ipfs_app_data;
pub mod order_simulator;
pub mod orderbook;
mod quoter;
pub mod run;
Expand Down
Loading
Loading