-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get token first trade block API (#3197)
# Description This is a pre-requisite for improving `internal buffers at risk` alert by eliminating false positives. The idea is to provide the token's first trade timestamp so https://github.com/cowprotocol/tenderly-alerts/ can then decide whether the token should be ignored. Currently, there is no way to return the timestamp since the `order_events` table gets cleaned up periodically. Technically, it can be used to get a timestamp, but the result will be useless if a token was deployed more than 30 days ago and wasn't traded for the last 30 days. Instead, the block number is returned, so Tenderly RPC must be used to fetch the block's timestamp. Otherwise, this would require access to an archive node from the orderbook side. ## Changes For some reason, this query takes around 20s on mainnet-prod for avg-popular tokens. Unfortunately, this couldn't be reproduced locally. I assume this is related to available resources on prod. I added indexes that improved the query speed ~x2.5 times. ## Caching To avoid querying the DB for the same tokens too often, I would consider introducing caching on the NGINX side rather than in memory since we often have multiple orderbook instances. Also, the first trade block never changes and can be kept in the NGINX cache forever. All the errors won't be kept in the cache. Requires an infra PR. ## How to test The query is tested on prod and locally. This would require further testing on prod by collecting metrics and adjusting resources. Potentially, `work_mem`, `max_parallel_workers_per_gather`, etc.
- Loading branch information
1 parent
07f4db6
commit 4d82068
Showing
6 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use { | ||
crate::database::Postgres, | ||
hyper::StatusCode, | ||
primitive_types::H160, | ||
std::convert::Infallible, | ||
warp::{reply, Filter, Rejection}, | ||
}; | ||
|
||
fn get_native_prices_request() -> impl Filter<Extract = (H160,), Error = Rejection> + Clone { | ||
warp::path!("v1" / "token" / H160 / "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| { | ||
let db = db.clone(); | ||
async move { | ||
let result = db.token_metadata(&token).await; | ||
let response = match result { | ||
Ok(metadata) => reply::with_status(reply::json(&metadata), StatusCode::OK), | ||
Err(err) => { | ||
tracing::error!(?err, ?token, "Failed to fetch token's first trade block"); | ||
crate::api::internal_error_reply() | ||
} | ||
}; | ||
|
||
Result::<_, Infallible>::Ok(response) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CREATE INDEX orders_sell_buy_tokens ON orders (sell_token, buy_token); | ||
|
||
CREATE INDEX jit_orders_sell_buy_tokens ON jit_orders (sell_token, buy_token); |