Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JIT orders in the trade verifier #3085

Merged
merged 44 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a423e57
Quote with JIT orders API
squadgazzz Oct 24, 2024
0bf5393
Updated description
squadgazzz Oct 24, 2024
9cc9abe
Redundant defaults
squadgazzz Oct 25, 2024
898e5e7
Redundant amount
squadgazzz Oct 25, 2024
5e1d001
Support jit orders quote
squadgazzz Oct 25, 2024
0937d4e
Address rc
squadgazzz Oct 28, 2024
e759f42
Restore the custom quote deserializer
squadgazzz Oct 28, 2024
e7b756a
Address rc
squadgazzz Oct 28, 2024
f40fafb
Serde untagged
squadgazzz Oct 28, 2024
1dddcf5
Merge branch '3082/jit-orders-quote-api' into 3082/trade-verifier-jit…
squadgazzz Oct 28, 2024
18686fe
Fixes after merge
squadgazzz Oct 28, 2024
67af897
Address rc
squadgazzz Oct 28, 2024
8bcb18a
Merge branch 'main' into 3082/trade-verifier-jit-orders-support
squadgazzz Oct 28, 2024
1ee64ba
Compute expected amounts
squadgazzz Oct 28, 2024
f676a96
Compute jit orders net changes
squadgazzz Oct 30, 2024
941647c
Combine tests
squadgazzz Oct 30, 2024
9d522d2
Avoid redundant clone
squadgazzz Oct 30, 2024
81836ec
Use checked_ceil_div
squadgazzz Oct 30, 2024
bb83789
Merge branch 'main' into 3082/trade-verifier-jit-orders-support
squadgazzz Oct 30, 2024
a37b3ac
Redundant modulus
squadgazzz Oct 30, 2024
265e598
Comment
squadgazzz Oct 30, 2024
ac2dbd4
Avoid tokens and prices duplicates
squadgazzz Oct 30, 2024
8c7503a
Tests comment
squadgazzz Oct 30, 2024
e68e0df
Encode jit orders function
squadgazzz Oct 31, 2024
0afb473
Encode fake trade function
squadgazzz Oct 31, 2024
09e6330
BigRational::neg()
squadgazzz Oct 31, 2024
2e71436
Use BigDecimal in the config
squadgazzz Oct 31, 2024
a308910
Avoid calculation with empty jit orders
squadgazzz Nov 1, 2024
3067cd9
Address comments
squadgazzz Nov 5, 2024
4a0767c
Naming
squadgazzz Nov 5, 2024
e051528
Fetch the balances in the solver contract
squadgazzz Nov 7, 2024
2bcb3ac
Refactor the unit test
squadgazzz Nov 7, 2024
bea5183
Revert balance fetching changes
squadgazzz Nov 7, 2024
7b29d96
Avoid using IR optimizer
squadgazzz Nov 7, 2024
0be2814
Revert "Revert balance fetching changes"
squadgazzz Nov 18, 2024
277b4e3
Adjust balance fetching
squadgazzz Nov 18, 2024
f1e1806
Pr comments
squadgazzz Nov 28, 2024
8718d83
Ceil div
squadgazzz Nov 28, 2024
4ec514e
Merge branch 'main' into 3082/trade-verifier-jit-orders-support
squadgazzz Nov 28, 2024
8324c32
Redundant ceil
squadgazzz Nov 28, 2024
47d2642
Merge branch 'main' into 3082/trade-verifier-jit-orders-support
squadgazzz Dec 2, 2024
9b0e906
Redundant changes
squadgazzz Dec 2, 2024
fc0ad49
Comment
squadgazzz Dec 2, 2024
ca9109d
Merge branch 'main' into 3082/trade-verifier-jit-orders-support
squadgazzz Dec 2, 2024
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
1 change: 1 addition & 0 deletions crates/autopilot/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ pub async fn run(args: Arguments) {
code_fetcher: code_fetcher.clone(),
},
)
.await
.expect("failed to initialize price estimator factory");

let native_price_estimator = price_estimator_factory
Expand Down
111 changes: 91 additions & 20 deletions crates/driver/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -294,29 +294,63 @@ components:
example: "0x30cff40d9f60caa68a37f0ee73253ad6ad72b45580c945fe3ab67596476937197854163b1b0d24e77dca702b97b5cc33e0f83dcb626122a6"
QuoteResponse:
oneOf:
- description: |
Successful Quote
- $ref: "#/components/schemas/LegacyQuote"
- $ref: "#/components/schemas/QuoteWithJitOrders"
- $ref: "#/components/schemas/Error"
LegacyQuote:
description: |
Successful Quote

The Solver knows how to fill the request with these parameters.
The Solver knows how to fill the request with these parameters.

If the request was of type `buy` then the response's buy amount has the same value as the
request's amount and the sell amount was filled in by the server. Vice versa for type
`sell`.
type: object
properties:
amount:
$ref: "#/components/schemas/TokenAmount"
interactions:
type: array
items:
$ref: "#/components/schemas/Interaction"
solver:
description: The address of the solver that quoted this order.
$ref: "#/components/schemas/Address"
gas:
type: integer
description: How many units of gas this trade is estimated to cost.
QuoteWithJitOrders:
description: |
Successful Quote with JIT orders support.

If the request was of type `buy` then the response's buy amount has the same value as the
request's amount and the sell amount was filled in by the server. Vice versa for type
`sell`.
The Solver knows how to fill the request with these parameters.
type: object
properties:
clearingPrices:
description: |
Mapping of hex token address to the uniform clearing price.
type: object
properties:
amount:
$ref: "#/components/schemas/TokenAmount"
interactions:
type: array
items:
$ref: "#/components/schemas/Interaction"
solver:
description: The address of the solver that quoted this order.
$ref: "#/components/schemas/Address"
gas:
type: integer
description: How many units of gas this trade is estimated to cost.
- $ref: "#/components/schemas/Error"
additionalProperties:
$ref: "#/components/schemas/BigUint"
preInteractions:
type: array
items:
$ref: "#/components/schemas/Interaction"
interactions:
type: array
items:
$ref: "#/components/schemas/Interaction"
solver:
description: The address of the solver that quoted this order.
$ref: "#/components/schemas/Address"
gas:
type: integer
description: How many units of gas this trade is estimated to cost.
jitOrders:
type: array
items:
$ref: "#/components/schemas/JitOrder"
DateTime:
description: An ISO 8601 UTC date time string.
type: string
Expand Down Expand Up @@ -514,6 +548,43 @@ components:
$ref: "#/components/schemas/TokenAmount"
solver:
$ref: "#/components/schemas/Address"
JitOrder:
type: object
properties:
sellToken:
$ref: "#/components/schemas/Address"
buyToken:
$ref: "#/components/schemas/Address"
sellAmount:
$ref: "#/components/schemas/TokenAmount"
buyAmount:
$ref: "#/components/schemas/TokenAmount"
executedAmount:
$ref: "#/components/schemas/TokenAmount"
receiver:
$ref: "#/components/schemas/Address"
validTo:
type: integer
side:
type: string
enum: ["buy", "sell"]
sellTokenSource:
type: string
enum: ["erc20", "internal", "external"]
buyTokenSource:
type: string
enum: ["erc20", "internal"]
appData:
type: string
signature:
description: |
Hex encoded bytes with `0x` prefix. The content depends on the `signingScheme`.
For `presign`, this should contain the address of the owner.
For `eip1271`, the signature should consist of `<owner_address><signature_bytes>`.
type: string
signingScheme:
type: string
enum: ["eip712", "ethsign", "presign", "eip1271"]
Error:
description: Response on API errors.
type: object
Expand Down
10 changes: 6 additions & 4 deletions crates/e2e/tests/e2e/quote_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use {
Estimate,
Verification,
},
trade_finding::{Interaction, Trade},
trade_finding::{Interaction, LegacyTrade, Trade},
},
std::{str::FromStr, sync::Arc},
};
Expand Down Expand Up @@ -62,7 +62,9 @@ async fn test_bypass_verification_for_rfq_quotes(web3: Web3) {
onchain.contracts().gp_settlement.address(),
onchain.contracts().weth.address(),
0.0,
);
)
.await
.unwrap();

let verify_trade = |tx_origin| {
let verifier = verifier.clone();
Expand All @@ -86,7 +88,7 @@ async fn test_bypass_verification_for_rfq_quotes(web3: Web3) {
sell_token_source: SellTokenSource::Erc20,
buy_token_destination: BuyTokenDestination::Erc20,
},
Trade {
Trade::Legacy(LegacyTrade {
out_amount: 16380122291179526144u128.into(),
gas_estimate: Some(225000),
interactions: vec![Interaction {
Expand All @@ -98,7 +100,7 @@ async fn test_bypass_verification_for_rfq_quotes(web3: Web3) {
solver: H160::from_str("0xe3067c7c27c1038de4e8ad95a83b927d23dfbd99")
.unwrap(),
tx_origin,
},
}),
)
.await
}
Expand Down
1 change: 1 addition & 0 deletions crates/orderbook/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ pub async fn run(args: Arguments) {
code_fetcher: code_fetcher.clone(),
},
)
.await
.expect("failed to initialize price estimator factory");

let native_price_estimator = price_estimator_factory
Expand Down
31 changes: 18 additions & 13 deletions crates/shared/src/price_estimation/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,28 +74,30 @@ pub struct Components {
}

impl<'a> PriceEstimatorFactory<'a> {
pub fn new(
pub async fn new(
args: &'a Arguments,
shared_args: &'a arguments::Arguments,
network: Network,
components: Components,
) -> Result<Self> {
Ok(Self {
trade_verifier: Self::trade_verifier(args, shared_args, &network, &components),
trade_verifier: Self::trade_verifier(args, shared_args, &network, &components).await?,
args,
network,
components,
estimators: HashMap::new(),
})
}

fn trade_verifier(
async fn trade_verifier(
args: &'a Arguments,
shared_args: &arguments::Arguments,
network: &Network,
components: &Components,
) -> Option<Arc<dyn TradeVerifying>> {
let web3 = network.simulation_web3.clone()?;
) -> Result<Option<Arc<dyn TradeVerifying>>> {
let Some(web3) = network.simulation_web3.clone() else {
squadgazzz marked this conversation as resolved.
Show resolved Hide resolved
return Ok(None);
};
let web3 = ethrpc::instrumented::instrument_with_label(&web3, "simulator".into());

let tenderly = shared_args
Expand All @@ -112,14 +114,17 @@ impl<'a> PriceEstimatorFactory<'a> {
None => Arc::new(web3.clone()),
};

Some(Arc::new(TradeVerifier::new(
web3,
simulator,
components.code_fetcher.clone(),
network.block_stream.clone(),
network.settlement,
network.native_token,
args.quote_inaccuracy_limit,
Ok(Some(Arc::new(
TradeVerifier::new(
web3,
simulator,
components.code_fetcher.clone(),
network.block_stream.clone(),
network.settlement,
network.native_token,
args.quote_inaccuracy_limit,
)
.await?,
)))
}

Expand Down
Loading
Loading