Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions crates/orderbook/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ warp = { workspace = true }
[dev-dependencies]
mockall = { workspace = true }
tokio = { workspace = true, features = ["test-util"] }
shared = { workspace = true, features = ["test-util"] }

[build-dependencies]
anyhow = { workspace = true }
Expand Down
8 changes: 4 additions & 4 deletions crates/orderbook/src/api/get_token_metadata.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use {
crate::database::Postgres,
alloy::primitives::Address,
hyper::StatusCode,
primitive_types::H160,
std::convert::Infallible,
warp::{Filter, Rejection, reply},
};

fn get_native_prices_request() -> impl Filter<Extract = (H160,), Error = Rejection> + Clone {
warp::path!("v1" / "token" / H160 / "metadata").and(warp::get())
fn get_native_prices_request() -> impl Filter<Extract = (Address,), Error = Rejection> + Clone {
warp::path!("v1" / "token" / Address / "metadata").and(warp::get())
}

pub fn get_token_metadata(
db: Postgres,
) -> impl Filter<Extract = (super::ApiReply,), Error = Rejection> + Clone {
get_native_prices_request().and_then(move |token: H160| {
get_native_prices_request().and_then(move |token: Address| {
let db = db.clone();
async move {
let result = db.token_metadata(&token).await;
Expand Down
4 changes: 2 additions & 2 deletions crates/orderbook/src/api/get_total_surplus.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use {
crate::database::Postgres,
primitive_types::H160,
alloy::primitives::Address,
serde_json::json,
std::convert::Infallible,
warp::{Filter, Rejection, http::StatusCode, reply::with_status},
};

pub fn get(db: Postgres) -> impl Filter<Extract = (super::ApiReply,), Error = Rejection> + Clone {
warp::path!("v1" / "users" / H160 / "total_surplus")
warp::path!("v1" / "users" / Address / "total_surplus")
.and(warp::get())
.and_then(move |user| {
let db = db.clone();
Expand Down
10 changes: 4 additions & 6 deletions crates/orderbook/src/api/get_trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use {
trades::{TradeFilter, TradeRetrieving},
},
},
alloy::primitives::Address,
anyhow::{Context, Result},
model::order::OrderUid,
primitive_types::H160,
serde::Deserialize,
std::convert::Infallible,
warp::{Filter, Rejection, hyper::StatusCode, reply::with_status},
Expand All @@ -18,7 +18,7 @@ use {
#[serde(rename_all = "camelCase")]
struct Query {
pub order_uid: Option<OrderUid>,
pub owner: Option<H160>,
pub owner: Option<Address>,
}

#[derive(Debug, Eq, PartialEq)]
Expand Down Expand Up @@ -80,8 +80,6 @@ pub fn get_trades(db: Postgres) -> impl Filter<Extract = (ApiReply,), Error = Re
mod tests {
use {
super::*,
hex_literal::hex,
primitive_types::H160,
warp::test::{RequestBuilder, request},
};

Expand All @@ -92,7 +90,7 @@ mod tests {
request.method("GET").filter(&filter).await
};

let owner = H160::from_slice(&hex!("0000000000000000000000000000000000000001"));
let owner = Address::with_last_byte(1);
let owner_path = format!("/v1/trades?owner=0x{owner:x}");
let result = trade_filter(request().path(owner_path.as_str()))
.await
Expand All @@ -118,7 +116,7 @@ mod tests {
request.method("GET").filter(&filter).await
};

let owner = H160::from_slice(&hex!("0000000000000000000000000000000000000001"));
let owner = Address::with_last_byte(1);
let uid = OrderUid([1u8; 56]);
let path = format!("/v1/trades?owner=0x{owner:x}&orderUid={uid}");

Expand Down
17 changes: 9 additions & 8 deletions crates/orderbook/src/database/orders.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
super::Postgres,
crate::dto::TokenMetadata,
alloy::primitives::B256,
alloy::primitives::{Address, B256},
anyhow::{Context as _, Result},
app_data::AppDataHash,
async_trait::async_trait,
Expand Down Expand Up @@ -399,11 +399,11 @@ impl Postgres {
.collect::<Result<Vec<_>>>()
}

pub async fn token_metadata(&self, token: &H160) -> Result<TokenMetadata> {
pub async fn token_metadata(&self, token: &Address) -> Result<TokenMetadata> {
let (first_trade_block, native_price): (Option<u32>, Option<U256>) = tokio::try_join!(
self.execute_instrumented("token_first_trade_block", async {
let mut ex = self.pool.acquire().await?;
database::trades::token_first_trade_block(&mut ex, ByteArray(token.0))
database::trades::token_first_trade_block(&mut ex, ByteArray(token.0.0))
.await
.map_err(anyhow::Error::from)?
.map(u32::try_from)
Expand All @@ -412,12 +412,13 @@ impl Postgres {
}),
self.execute_instrumented("fetch_latest_token_price", async {
let mut ex = self.pool.acquire().await?;
Ok(
database::auction_prices::fetch_latest_token_price(&mut ex, ByteArray(token.0))
.await
.map_err(anyhow::Error::from)?
.and_then(|price| big_decimal_to_u256(&price)),
Ok(database::auction_prices::fetch_latest_token_price(
&mut ex,
ByteArray(token.0.0),
)
.await
.map_err(anyhow::Error::from)?
.and_then(|price| big_decimal_to_u256(&price)))
})
)?;

Expand Down
12 changes: 6 additions & 6 deletions crates/orderbook/src/database/total_surplus.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {
alloy::primitives::U256,
anyhow::Result,
database::{Address, byte_array::ByteArray},
primitive_types::{H160, U256},
sqlx::PgConnection,
};

Expand Down Expand Up @@ -61,8 +61,8 @@ trade_components AS (
JOIN trades t ON j.uid = t.order_uid
JOIN order_execution oe ON j.uid = oe.order_uid
WHERE j.owner = $1 AND NOT EXISTS (
SELECT 1
FROM orders o
SELECT 1
FROM orders o
WHERE o.uid = j.uid
)
),
Expand All @@ -88,14 +88,14 @@ FROM trade_surplus
}

impl super::Postgres {
pub async fn total_surplus(&self, user: &H160) -> Result<U256> {
pub async fn total_surplus(&self, user: &alloy::primitives::Address) -> Result<U256> {
let _timer = super::Metrics::get()
.database_queries
.with_label_values(&["get_total_surplus"])
.start_timer();

let mut ex = self.pool.acquire().await?;
let surplus = fetch_total_surplus(&mut ex, &ByteArray(user.0)).await?;
Ok(U256::from_f64_lossy(surplus))
let surplus = fetch_total_surplus(&mut ex, &ByteArray(user.0.0)).await?;
Ok(U256::from(surplus))
}
}
5 changes: 2 additions & 3 deletions crates/orderbook/src/database/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use {
alloy::primitives::{Address, B256},
anyhow::{Context, Result},
database::{byte_array::ByteArray, trades::TradesQueryRow},
ethcontract::H160,
futures::stream::TryStreamExt,
model::{fee_policy::ExecutedProtocolFee, order::OrderUid, trade::Trade},
number::conversions::big_decimal_to_big_uint,
Expand All @@ -18,7 +17,7 @@ pub trait TradeRetrieving: Send + Sync {
/// Any default value means that this field is unfiltered.
#[derive(Debug, Default, Eq, PartialEq)]
pub struct TradeFilter {
pub owner: Option<H160>,
pub owner: Option<Address>,
pub order_uid: Option<OrderUid>,
}

Expand All @@ -33,7 +32,7 @@ impl TradeRetrieving for Postgres {
let mut ex = self.pool.acquire().await?;
let trades = database::trades::trades(
&mut ex,
filter.owner.map(|owner| ByteArray(owner.0)).as_ref(),
filter.owner.map(|owner| ByteArray(owner.0.0)).as_ref(),
filter.order_uid.map(|uid| ByteArray(uid.0)).as_ref(),
)
.into_inner()
Expand Down
Loading