Skip to content

Commit 68f627d

Browse files
committed
Feature flag for sell=buy
1 parent 685ea18 commit 68f627d

File tree

5 files changed

+178
-36
lines changed

5 files changed

+178
-36
lines changed

crates/e2e/tests/e2e/place_order_with_quote.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ async fn local_node_place_order_with_quote_same_token_pair() {
2929
run_test(place_order_with_quote_same_token_pair).await;
3030
}
3131

32+
#[tokio::test]
33+
#[ignore]
34+
async fn local_node_place_order_with_quote_same_token_pair_error() {
35+
run_test(place_order_with_quote_same_token_pair_error).await;
36+
}
37+
3238
async fn place_order_with_quote(web3: Web3) {
3339
let mut onchain = OnchainComponents::deploy(web3.clone()).await;
3440

@@ -125,7 +131,7 @@ async fn place_order_with_quote(web3: Web3) {
125131
assert_eq!(quote_metadata.unwrap().0, order_quote.metadata);
126132
}
127133

128-
async fn place_order_with_quote_same_token_pair(web3: Web3) {
134+
async fn place_order_with_quote_same_token_pair_error(web3: Web3) {
129135
let mut onchain = OnchainComponents::deploy(web3.clone()).await;
130136

131137
let [solver] = onchain.make_solvers(to_wei(10)).await;
@@ -147,6 +153,58 @@ async fn place_order_with_quote_same_token_pair(web3: Web3) {
147153
let services = Services::new(&onchain).await;
148154
services.start_protocol(solver.clone()).await;
149155

156+
// Disable auto-mine so we don't accidentally mine a settlement
157+
web3.api::<TestNodeApi<_>>()
158+
.set_automine_enabled(false)
159+
.await
160+
.expect("Must be able to disable automine");
161+
162+
tracing::info!("Quoting");
163+
let quote_sell_amount = to_wei(1);
164+
let quote_request = OrderQuoteRequest {
165+
from: trader.address(),
166+
sell_token: token.address().into_legacy(),
167+
buy_token: token.address().into_legacy(),
168+
side: OrderQuoteSide::Sell {
169+
sell_amount: SellAmount::BeforeFee {
170+
value: NonZeroU256::try_from(quote_sell_amount).unwrap(),
171+
},
172+
},
173+
..Default::default()
174+
};
175+
assert!(services.submit_quote(&quote_request).await.is_err());
176+
}
177+
178+
async fn place_order_with_quote_same_token_pair(web3: Web3) {
179+
let mut onchain = OnchainComponents::deploy(web3.clone()).await;
180+
181+
let [solver] = onchain.make_solvers(to_wei(10)).await;
182+
let [trader] = onchain.make_accounts(to_wei(10)).await;
183+
let [token] = onchain
184+
.deploy_tokens_with_weth_uni_v2_pools(to_wei(1_000), to_wei(1_000))
185+
.await;
186+
187+
token.mint(trader.address(), to_wei(10)).await;
188+
189+
token
190+
.approve(onchain.contracts().allowance.into_alloy(), eth(10))
191+
.from(trader.address().into_alloy())
192+
.send_and_watch()
193+
.await
194+
.unwrap();
195+
196+
tracing::info!("Starting services.");
197+
let services = Services::new(&onchain).await;
198+
services
199+
.start_protocol_with_args(
200+
ExtraServiceArgs {
201+
api: vec!["--allow-same-sell-and-buy-token=true".to_string()],
202+
..Default::default()
203+
},
204+
solver.clone(),
205+
)
206+
.await;
207+
150208
// Disable auto-mine so we don't accidentally mine a settlement
151209
web3.api::<TestNodeApi<_>>()
152210
.set_automine_enabled(false)

crates/orderbook/src/api/post_order.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ impl IntoWarpReply for PartialValidationErrorWrapper {
7272
),
7373
StatusCode::BAD_REQUEST,
7474
),
75+
PartialValidationError::SameBuyAndSellToken => with_status(
76+
error(
77+
"SameBuyAndSellToken",
78+
"Buy token is the same as the sell token.",
79+
),
80+
StatusCode::BAD_REQUEST,
81+
),
7582
PartialValidationError::UnsupportedToken { token, reason } => with_status(
7683
error(
7784
"UnsupportedToken",

crates/orderbook/src/arguments.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ pub struct Arguments {
141141
/// whether an order is actively being bid on.
142142
#[clap(long, env, default_value = "5")]
143143
pub active_order_competition_threshold: u32,
144+
145+
/// Allow same sell and buy token
146+
#[clap(long, env, action = clap::ArgAction::Set, default_value = "false")]
147+
pub allow_same_sell_and_buy_token: bool,
144148
}
145149

146150
impl std::fmt::Display for Arguments {
@@ -172,6 +176,7 @@ impl std::fmt::Display for Arguments {
172176
db_read_url,
173177
max_gas_per_order,
174178
active_order_competition_threshold,
179+
allow_same_sell_and_buy_token,
175180
} = self;
176181

177182
write!(f, "{shared}")?;
@@ -225,6 +230,10 @@ impl std::fmt::Display for Arguments {
225230
f,
226231
"active_order_competition_threshold: {active_order_competition_threshold}"
227232
)?;
233+
writeln!(
234+
f,
235+
"allow_same_sell_and_buy_token: {allow_same_sell_and_buy_token}"
236+
)?;
228237

229238
Ok(())
230239
}

crates/orderbook/src/run.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -409,25 +409,30 @@ pub async fn run(args: Arguments) {
409409
let chainalysis_oracle = ChainalysisOracle::Instance::deployed(&web3.alloy)
410410
.await
411411
.ok();
412-
let order_validator = Arc::new(OrderValidator::new(
413-
Arc::new(order_validation::banned::Users::new(
414-
chainalysis_oracle,
415-
args.banned_users,
416-
args.banned_users_max_cache_size.get().to_u64().unwrap(),
417-
)),
418-
validity_configuration,
419-
args.eip1271_skip_creation_validation,
420-
bad_token_detector.clone(),
421-
hooks_contract,
422-
optimal_quoter.clone(),
423-
balance_fetcher,
424-
signature_validator,
425-
Arc::new(postgres_write.clone()),
426-
args.max_limit_orders_per_user,
427-
code_fetcher,
428-
app_data_validator.clone(),
429-
args.max_gas_per_order,
430-
));
412+
let order_validator = Arc::new(
413+
OrderValidator::new(
414+
Arc::new(order_validation::banned::Users::new(
415+
chainalysis_oracle,
416+
args.banned_users,
417+
args.banned_users_max_cache_size.get().to_u64().unwrap(),
418+
)),
419+
validity_configuration,
420+
args.eip1271_skip_creation_validation,
421+
bad_token_detector.clone(),
422+
hooks_contract,
423+
optimal_quoter.clone(),
424+
balance_fetcher,
425+
signature_validator,
426+
Arc::new(postgres_write.clone()),
427+
args.max_limit_orders_per_user,
428+
code_fetcher,
429+
app_data_validator.clone(),
430+
args.max_gas_per_order,
431+
)
432+
.with_sell_and_buy_validation(
433+
(!args.allow_same_sell_and_buy_token).then(|| native_token.clone()),
434+
),
435+
);
431436
let ipfs = args
432437
.ipfs_gateway
433438
.map(|url| {

0 commit comments

Comments
 (0)