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
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))
Comment on lines -98 to +99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can You provide more information backing this is a drop-in replacement? I looked around alloy's docs but couldn't untangle it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use alloy::primitives::U256;

fn main() {
    let mut buffer = vec![0; 32];
    ethcontract::U256::from_f64_lossy(3.3).to_big_endian(&mut buffer);

    assert_eq!(U256::from(3.3f64).to_be_bytes_vec(), buffer);

    println!(
        "{} {}",
        U256::from(3.3f64),
        ethcontract::U256::from_f64_lossy(3.3)
    );
}

I think that in ethcontract they explicitly state that the conversion is lossy because you're bound to lose decimals, in alloy they make it implicit because you shouldn't really expect the decimals to keep when converting to an integer type.

Hope this helps

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect! Thanks!

}
}
Loading