Skip to content

Commit

Permalink
some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
longbowlu committed Oct 12, 2024
1 parent abd8be9 commit 24fe1c4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions crates/sui-deepbook-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ clap.workspace = true
mysten-metrics.workspace = true
prometheus.workspace = true
serde_yaml.workspace = true
sui-bridge.workspace = true
sui-sdk.workspace = true
sui-json-rpc-types.workspace = true
sui-data-ingestion-core.workspace = true
Expand All @@ -33,12 +32,10 @@ sui-config.workspace = true
sui-indexer-builder.workspace = true
tempfile.workspace = true
axum.workspace = true
bigdecimal = "0.4.0"
bigdecimal = { version = "0.4.5" }

[dev-dependencies]
sui-types = { workspace = true, features = ["test-utils"] }
sui-test-transaction-builder.workspace = true
test-cluster.workspace = true
hex-literal = "0.3.4"

[[bin]]
Expand Down
7 changes: 7 additions & 0 deletions crates/sui-deepbook-indexer/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ pub struct OrderFill {
pub onchain_timestamp: i64,
}

#[derive(Queryable)]
pub struct OrderFillSummary {
pub maker_balance_manager_id: String,
pub taker_balance_manager_id: String,
pub base_quantity: i64,
}

#[derive(Queryable, Selectable, Insertable, Identifiable, Debug)]
#[diesel(table_name = flashloans, primary_key(digest))]
pub struct Flashloan {
Expand Down
39 changes: 20 additions & 19 deletions crates/sui-deepbook-indexer/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{
error::DeepBookError,
models::{OrderFill, Pools},
models::{OrderFillSummary, Pools},
schema,
sui_deepbook_indexer::PgDeepbookPersistent,
};
Expand All @@ -14,16 +14,13 @@ use axum::{
routing::get,
Json, Router,
};
use bigdecimal::BigDecimal;
use bigdecimal::ToPrimitive;
use diesel::dsl::sum;
use diesel::prelude::*;
use diesel::BoolExpressionMethods;
use diesel::QueryDsl;
use diesel::{ExpressionMethods, SelectableHelper};
use diesel_async::RunQueryDsl;
use std::net::SocketAddr;
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::{net::TcpListener, task::JoinHandle};
use tracing::info;

pub const GET_POOLS_PATH: &str = "/get_pools";
pub const GET_24HR_VOLUME_PATH: &str = "/get_24hr_volume/:pool_id";
Expand Down Expand Up @@ -90,25 +87,20 @@ async fn get_pools(
async fn get_24hr_volume(
Path(pool_id): Path<String>,
State(state): State<PgDeepbookPersistent>,
) -> Result<Json<i64>, DeepBookError> {
) -> Result<Json<u64>, DeepBookError> {
let connection = &mut state.pool.get().await?;
let unix_ts = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as i64;
let day_ago = unix_ts - 24 * 60 * 60 * 1000;
info!("day_ago: {}", day_ago);
let total_vol: BigDecimal = schema::order_fills::table
.select(sum(schema::order_fills::base_quantity).nullable())
let vols: Vec<i64> = schema::order_fills::table
.select(schema::order_fills::base_quantity)
.filter(schema::order_fills::pool_id.eq(pool_id))
.filter(schema::order_fills::onchain_timestamp.gt(day_ago))
.first::<Option<BigDecimal>>(connection)
.await?
.unwrap_or_default();

let total_vol = total_vol.to_i64().unwrap_or_default();

Ok(Json(total_vol))
.load(connection)
.await?;
Ok(Json(vols.into_iter().map(|v| v as u64).sum()))
}

async fn get_24hr_volume_by_balance_manager_id(
Expand All @@ -121,10 +113,19 @@ async fn get_24hr_volume_by_balance_manager_id(
.unwrap()
.as_millis() as i64;
let day_ago = unix_ts - 24 * 60 * 60 * 1000;
let results: Vec<OrderFill> = schema::order_fills::table
.select(OrderFill::as_select())
let results: Vec<OrderFillSummary> = schema::order_fills::table
.select((
schema::order_fills::maker_balance_manager_id,
schema::order_fills::taker_balance_manager_id,
schema::order_fills::base_quantity,
))
.filter(schema::order_fills::pool_id.eq(pool_id))
.filter(schema::order_fills::onchain_timestamp.gt(day_ago))
.filter(
schema::order_fills::maker_balance_manager_id
.eq(&balance_manager_id)
.or(schema::order_fills::taker_balance_manager_id.eq(&balance_manager_id)),
)
.load(connection)
.await?;

Expand Down

0 comments on commit 24fe1c4

Please sign in to comment.