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

Revert "Use quote id just for debugging (#3054)" #3096

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
11 changes: 11 additions & 0 deletions crates/autopilot/src/database/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ impl QuoteStoring for Postgres {
Ok(id)
}

async fn get(&self, id: QuoteId) -> Result<Option<QuoteData>> {
let _timer = super::Metrics::get()
.database_queries
.with_label_values(&["get_quote"])
.start_timer();

let mut ex = self.pool.acquire().await?;
let quote = database::quotes::get(&mut ex, id).await?;
quote.map(TryFrom::try_from).transpose()
}

async fn find(
&self,
params: QuoteSearchParameters,
Expand Down
2 changes: 1 addition & 1 deletion crates/autopilot/src/database/onchain_order_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,7 @@ mod test {
let cloned_quote = quote.clone();
order_quoter
.expect_find_quote()
.returning(move |_| Ok(cloned_quote.clone()));
.returning(move |_, _| Ok(cloned_quote.clone()));
let mut custom_onchain_order_parser = MockOnchainOrderParsing::<u8, u8>::new();
custom_onchain_order_parser
.expect_parse_custom_event_data()
Expand Down
41 changes: 41 additions & 0 deletions crates/database/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ RETURNING id
Ok(id)
}

pub async fn get(ex: &mut PgConnection, id: QuoteId) -> Result<Option<Quote>, sqlx::Error> {
const QUERY: &str = r#"
SELECT *
FROM quotes
WHERE id = $1
"#;
sqlx::query_as(QUERY).bind(id).fetch_optional(ex).await
}

/// Fields for searching stored quotes.
#[derive(Clone)]
pub struct QuoteSearchParameters {
Expand Down Expand Up @@ -151,6 +160,38 @@ mod tests {
Utc.timestamp_opt(Utc::now().timestamp(), 0).unwrap()
}

#[tokio::test]
#[ignore]
async fn postgres_save_and_get_quote_by_id() {
let mut db = PgConnection::connect("postgresql://").await.unwrap();
let mut db = db.begin().await.unwrap();
crate::clear_DANGER_(&mut db).await.unwrap();

let now = low_precision_now();
let mut quote = Quote {
id: Default::default(),
sell_token: ByteArray([1; 20]),
buy_token: ByteArray([2; 20]),
sell_amount: 3.into(),
buy_amount: 4.into(),
gas_amount: 5.,
gas_price: 6.,
sell_token_price: 7.,
order_kind: OrderKind::Sell,
expiration_timestamp: now,
quote_kind: QuoteKind::Standard,
solver: ByteArray([1; 20]),
};
let id = save(&mut db, &quote).await.unwrap();
quote.id = id;
assert_eq!(get(&mut db, id).await.unwrap().unwrap(), quote);

remove_expired_quotes(&mut db, now + Duration::seconds(30))
.await
.unwrap();
assert_eq!(get(&mut db, id).await.unwrap(), None);
}

#[tokio::test]
#[ignore]
async fn postgres_save_and_find_quote() {
Expand Down
23 changes: 6 additions & 17 deletions crates/e2e/src/setup/onchain_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,28 +251,17 @@ impl OnchainComponents {
let solvers = self.make_accounts::<N>(with_wei).await;

for solver in &solvers {
self.allow_solving(solver, true).await;
self.contracts
.gp_authenticator
.add_solver(solver.address())
.send()
.await
.expect("failed to add solver");
}

solvers
}

/// Adds or removes the passed `account` from the list of allowed solver
/// addresses. This can be useful to temporarily disable active solvers
/// until all the necessary pre-conditions for a test have been set up.
pub async fn allow_solving(&self, account: &TestAccount, is_allowed: bool) {
let tx = if is_allowed {
self.contracts
.gp_authenticator
.add_solver(account.address())
} else {
self.contracts
.gp_authenticator
.remove_solver(account.address())
};
tx.send().await.expect("failed to update authenticator");
}

/// Generate next `N` accounts with the given initial balance and
/// authenticate them as solvers on a forked network.
pub async fn make_solvers_forked<const N: usize>(
Expand Down
42 changes: 19 additions & 23 deletions crates/e2e/tests/e2e/protocol_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ async fn combined_protocol_fees(web3: Web3) {
autopilot: autopilot_config,
..Default::default()
},
solver.clone(),
solver,
)
.await;

Expand Down Expand Up @@ -157,11 +157,7 @@ async fn combined_protocol_fees(web3: Web3) {
.await
.unwrap()
.try_into()
.expect("Expected exactly three elements");

// Disable solver until orders have been placed and onchain liquidity
// got updated.
onchain.allow_solving(&solver, false).await;
.expect("Expected exactly four elements");

let market_price_improvement_order = OrderCreation {
sell_amount,
Expand Down Expand Up @@ -198,20 +194,6 @@ async fn combined_protocol_fees(web3: Web3) {
SecretKeyRef::from(&SecretKey::from_slice(trader.private_key()).unwrap()),
);

let [market_price_improvement_uid, limit_surplus_order_uid, partner_fee_order_uid] =
futures::future::try_join_all(
[
&market_price_improvement_order,
&limit_surplus_order,
&partner_fee_order,
]
.map(|order| services.create_order(order)),
)
.await
.unwrap()
.try_into()
.expect("Expected exactly three elements");

tracing::info!("Rebalancing AMM pools for market & limit order.");
onchain
.mint_token_to_weth_uni_v2_pool(&market_order_token, to_wei(1000))
Expand Down Expand Up @@ -266,9 +248,23 @@ async fn combined_protocol_fees(web3: Web3) {
.await
.unwrap()
.try_into()
.expect("Expected exactly three elements");
.expect("Expected exactly two elements");

let [market_price_improvement_uid, limit_surplus_order_uid, partner_fee_order_uid] =
futures::future::try_join_all(
[
&market_price_improvement_order,
&limit_surplus_order,
&partner_fee_order,
]
.map(|order| services.create_order(order)),
)
.await
.unwrap()
.try_into()
.expect("Expected exactly four elements");

onchain.allow_solving(&solver, true).await;
onchain.mint_block().await;

tracing::info!("Waiting for orders to trade.");
let metadata_updated = || async {
Expand Down Expand Up @@ -349,7 +345,7 @@ async fn combined_protocol_fees(web3: Web3) {
.await
.unwrap()
.try_into()
.expect("Expected exactly three elements");
.expect("Expected exactly four elements");
assert_approximately_eq!(
market_executed_surplus_fee_in_buy_token,
market_order_token_balance
Expand Down
10 changes: 3 additions & 7 deletions crates/e2e/tests/e2e/solver_competition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ async fn fairness_check(web3: Web3) {
.await,
colocation::start_baseline_solver(
"solver2".into(),
solver.clone(),
solver,
onchain.contracts().weth.address(),
vec![base_b.address()],
1,
Expand All @@ -208,9 +208,6 @@ async fn fairness_check(web3: Web3) {
])
.await;

// Disable solving until all orders have been placed.
onchain.allow_solving(&solver, false).await;

// Place Orders
let order_a = OrderCreation {
sell_token: token_a.address(),
Expand All @@ -228,6 +225,8 @@ async fn fairness_check(web3: Web3) {
);
let uid_a = services.create_order(&order_a).await.unwrap();

onchain.mint_block().await;

let order_b = OrderCreation {
sell_token: token_b.address(),
sell_amount: to_wei(10),
Expand All @@ -244,9 +243,6 @@ async fn fairness_check(web3: Web3) {
);
services.create_order(&order_b).await.unwrap();

// Enable solving again
onchain.allow_solving(&solver, true).await;

// Wait for trade
let indexed_trades = || async {
onchain.mint_block().await;
Expand Down
11 changes: 11 additions & 0 deletions crates/orderbook/src/database/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ impl QuoteStoring for Postgres {
Ok(id)
}

async fn get(&self, id: QuoteId) -> Result<Option<QuoteData>> {
let _timer = super::Metrics::get()
.database_queries
.with_label_values(&["get_quote"])
.start_timer();

let mut ex = self.pool.acquire().await?;
let quote = database::quotes::get(&mut ex, id).await?;
quote.map(TryFrom::try_from).transpose()
}

async fn find(
&self,
params: QuoteSearchParameters,
Expand Down
Loading
Loading