Skip to content

Commit 5fb78f1

Browse files
authored
Merge branch 'cowprotocol:main' into main
2 parents 2a197d1 + 06d76b1 commit 5fb78f1

File tree

8 files changed

+118
-148
lines changed

8 files changed

+118
-148
lines changed

crates/autopilot/src/database/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,14 @@ struct Metrics {
139139
unused_app_data: prometheus::IntGauge,
140140

141141
/// Timing of db queries.
142-
#[metric(name = "autopilot_database_queries", labels("type"))]
142+
#[metric(
143+
name = "autopilot_database_queries",
144+
labels("type"),
145+
buckets(
146+
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0, 20.0, 30.0, 40.0, 50.0,
147+
60.0, 70.0, 80.0, 90.0
148+
)
149+
)]
143150
database_queries: prometheus::HistogramVec,
144151

145152
/// Number of active connections in the database pool.

crates/contracts/artifacts/SimulateCode.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/contracts/build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,6 @@ fn main() {
19441944
.add_network_str(GNOSIS, "0x8262d639c38470F38d2eff15926F7071c28057Af")
19451945
.add_network_str(SEPOLIA, "0x8262d639c38470F38d2eff15926F7071c28057Af")
19461946
});
1947-
generate_contract("SimulateCode");
19481947

19491948
// Support contract used for solver fee simulations.
19501949
generate_contract("AnyoneAuthenticator");

crates/contracts/solidity/Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ CONTRACTS := \
1212
Balances.sol \
1313
Multicall.sol \
1414
Signatures.sol \
15-
SimulateCode.sol \
1615
Solver.sol \
1716
Spardose.sol \
1817
Swapper.sol \

crates/contracts/solidity/SimulateCode.sol

Lines changed: 0 additions & 45 deletions
This file was deleted.

crates/contracts/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ mod macros;
3838

3939
#[cfg(feature = "bin")]
4040
pub mod paths;
41-
pub mod storage_accessible;
4241
pub mod vault;
4342
pub mod web3;
4443

@@ -135,7 +134,6 @@ pub mod support {
135134
Balances;
136135
Multicall;
137136
Signatures;
138-
SimulateCode;
139137
Solver;
140138
Spardose;
141139
Swapper;

crates/contracts/src/storage_accessible.rs

Lines changed: 0 additions & 58 deletions
This file was deleted.

crates/database/src/orders.rs

Lines changed: 110 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -705,23 +705,104 @@ WHERE
705705
/// - pending pre-signature
706706
/// - ethflow specific invalidation conditions
707707
#[rustfmt::skip]
708-
const OPEN_ORDERS: &str = const_format::concatcp!(
709-
"SELECT * FROM ( ",
710-
"SELECT ", SELECT,
711-
" FROM ", FROM,
712-
" LEFT OUTER JOIN ethflow_orders eth_o on eth_o.uid = o.uid ",
713-
" WHERE o.valid_to >= $1",
714-
" AND CASE WHEN eth_o.valid_to IS NULL THEN true ELSE eth_o.valid_to >= $1 END",
715-
r#") AS unfiltered
716-
WHERE
717-
CASE kind
718-
WHEN 'sell' THEN sum_sell < sell_amount
719-
WHEN 'buy' THEN sum_buy < buy_amount
720-
END AND
721-
(NOT invalidated) AND
722-
(onchain_placement_error IS NULL)
723-
"#
724-
);
708+
const OPEN_ORDERS: &str = r#"
709+
WITH live_orders AS (
710+
SELECT o.*
711+
FROM orders o
712+
LEFT JOIN ethflow_orders e ON e.uid = o.uid
713+
WHERE o.cancellation_timestamp IS NULL
714+
AND o.valid_to >= $1
715+
AND (e.valid_to IS NULL OR e.valid_to >= $1)
716+
AND NOT EXISTS (SELECT 1 FROM invalidations i WHERE i.order_uid = o.uid)
717+
AND NOT EXISTS (SELECT 1 FROM onchain_order_invalidations oi WHERE oi.uid = o.uid)
718+
AND NOT EXISTS (SELECT 1 FROM onchain_placed_orders op WHERE op.uid = o.uid
719+
AND op.placement_error IS NOT NULL)
720+
),
721+
trades_agg AS (
722+
SELECT t.order_uid,
723+
SUM(t.buy_amount) AS sum_buy,
724+
SUM(t.sell_amount) AS sum_sell,
725+
SUM(t.fee_amount) AS sum_fee
726+
FROM trades t
727+
JOIN live_orders lo ON lo.uid = t.order_uid
728+
GROUP BY t.order_uid
729+
)
730+
SELECT
731+
lo.uid,
732+
lo.owner,
733+
lo.creation_timestamp,
734+
lo.sell_token,
735+
lo.buy_token,
736+
lo.sell_amount,
737+
lo.buy_amount,
738+
lo.valid_to,
739+
lo.app_data,
740+
lo.fee_amount,
741+
lo.kind,
742+
lo.partially_fillable,
743+
lo.signature,
744+
lo.receiver,
745+
lo.signing_scheme,
746+
lo.settlement_contract,
747+
lo.sell_token_balance,
748+
lo.buy_token_balance,
749+
lo.class,
750+
751+
COALESCE(ta.sum_buy, 0) AS sum_buy,
752+
COALESCE(ta.sum_sell, 0) AS sum_sell,
753+
COALESCE(ta.sum_fee, 0) AS sum_fee,
754+
false AS invalidated,
755+
(lo.signing_scheme = 'presign' AND COALESCE(pe.unsigned, TRUE)) AS presignature_pending,
756+
ARRAY(
757+
SELECT (p.target, p.value, p.data)
758+
FROM interactions p
759+
WHERE p.order_uid = lo.uid AND p.execution = 'pre'
760+
ORDER BY p.index
761+
) AS pre_interactions,
762+
ARRAY(
763+
SELECT (p.target, p.value, p.data)
764+
FROM interactions p
765+
WHERE p.order_uid = lo.uid AND p.execution = 'post'
766+
ORDER BY p.index
767+
) AS post_interactions,
768+
ed.ethflow_data,
769+
opo.onchain_user,
770+
NULL AS onchain_placement_error,
771+
COALESCE(fee_agg.executed_fee,0) AS executed_fee,
772+
COALESCE(fee_agg.executed_fee_token, lo.sell_token) AS executed_fee_token,
773+
ad.full_app_data
774+
FROM live_orders lo
775+
LEFT JOIN LATERAL (
776+
SELECT NOT signed AS unsigned
777+
FROM presignature_events
778+
WHERE order_uid = lo.uid
779+
ORDER BY block_number DESC, log_index DESC
780+
LIMIT 1
781+
) pe ON TRUE
782+
LEFT JOIN LATERAL (
783+
SELECT sender AS onchain_user
784+
FROM onchain_placed_orders
785+
WHERE uid = lo.uid
786+
ORDER BY block_number DESC
787+
LIMIT 1
788+
) opo ON TRUE
789+
LEFT JOIN LATERAL (
790+
SELECT ROW(tx_hash, eo.valid_to) AS ethflow_data
791+
FROM ethflow_orders eo
792+
LEFT JOIN ethflow_refunds r ON r.order_uid = eo.uid
793+
WHERE eo.uid = lo.uid
794+
) ed ON TRUE
795+
LEFT JOIN LATERAL (
796+
SELECT SUM(executed_fee) AS executed_fee,
797+
(ARRAY_AGG(executed_fee_token))[1] AS executed_fee_token
798+
FROM order_execution
799+
WHERE order_uid = lo.uid
800+
) fee_agg ON TRUE
801+
LEFT JOIN app_data ad ON ad.contract_app_data = lo.app_data
802+
LEFT JOIN trades_agg ta ON ta.order_uid = lo.uid
803+
WHERE ((lo.kind = 'sell' AND COALESCE(ta.sum_sell,0) < lo.sell_amount) OR
804+
(lo.kind = 'buy' AND COALESCE(ta.sum_buy ,0) < lo.buy_amount))
805+
"#;
725806

726807
/// Uses the conditions from OPEN_ORDERS and checks the fok limit orders have
727808
/// surplus fee.
@@ -763,28 +844,6 @@ FROM settlements
763844
sqlx::query_scalar(QUERY).fetch_one(ex).await
764845
}
765846

766-
/// Counts the number of limit orders with the conditions of OPEN_ORDERS. Used
767-
/// to enforce a maximum number of limit orders per user.
768-
#[instrument(skip_all)]
769-
pub async fn count_limit_orders_by_owner(
770-
ex: &mut PgConnection,
771-
min_valid_to: i64,
772-
owner: &Address,
773-
) -> Result<i64, sqlx::Error> {
774-
const QUERY: &str = const_format::concatcp!(
775-
"SELECT COUNT (*) FROM (",
776-
OPEN_ORDERS,
777-
" AND class = 'limit'",
778-
" AND owner = $2",
779-
" ) AS subquery"
780-
);
781-
sqlx::query_scalar(QUERY)
782-
.bind(min_valid_to)
783-
.bind(owner)
784-
.fetch_one(ex)
785-
.await
786-
}
787-
788847
#[derive(Debug, sqlx::FromRow)]
789848
pub struct OrderWithQuote {
790849
pub order_buy_amount: BigDecimal,
@@ -1616,6 +1675,18 @@ mod tests {
16161675
assert!(!get_full_order(&mut db).await.unwrap().presignature_pending);
16171676
}
16181677

1678+
#[tokio::test]
1679+
#[ignore]
1680+
async fn get_orders_with_quote() {
1681+
let mut db = PgConnection::connect("postgresql://").await.unwrap();
1682+
let mut db = db.begin().await.unwrap();
1683+
assert!(
1684+
user_orders_with_quote(&mut db, 0, &Default::default())
1685+
.await
1686+
.is_ok()
1687+
)
1688+
}
1689+
16191690
#[tokio::test]
16201691
#[ignore]
16211692
async fn postgres_onchain_invalidated_orders() {

0 commit comments

Comments
 (0)