Skip to content

Commit

Permalink
timestamp added
Browse files Browse the repository at this point in the history
  • Loading branch information
leecchh committed Dec 2, 2024
1 parent 6193977 commit 10a95cb
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/sui-deepbook-indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sui-indexer-builder.workspace = true
tempfile.workspace = true
axum.workspace = true
bigdecimal = { version = "0.4.5" }
serde_json = { version = "1.0", features = ["preserve_order"] }

[dev-dependencies]
hex-literal = "0.3.4"
Expand Down
73 changes: 41 additions & 32 deletions crates/sui-deepbook-indexer/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use diesel_async::RunQueryDsl;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{collections::HashMap, net::SocketAddr};
use tokio::{net::TcpListener, task::JoinHandle};
use serde_json::Value;

use std::str::FromStr;
use sui_json_rpc_types::{SuiObjectData, SuiObjectDataOptions, SuiObjectResponse};
Expand Down Expand Up @@ -359,7 +360,7 @@ async fn get_historical_volume_by_balance_manager_id_with_interval(

async fn get_level2_ticks_from_mid(
Path(pool_id): Path<String>,
) -> Result<Json<HashMap<String, Vec<f64>>>, DeepBookError> {
) -> Result<Json<HashMap<String, Value>>, DeepBookError> {
let sui_client = SuiClientBuilder::default().build(SUI_MAINNET_URL).await?;
let mut ptb = ProgrammableTransactionBuilder::new();

Expand Down Expand Up @@ -471,47 +472,55 @@ async fn get_level2_ticks_from_mid(
let ask_parsed_quantities: Vec<u64> = bcs::from_bytes(&ask_quantities).unwrap();

let mut result = HashMap::new();
result.insert(
"bid_parsed_prices".to_string(),
bid_parsed_prices
.into_iter()
.map(|quantity| {
let factor = 10u64.pow((9 - *base_decimals + *quote_decimals).try_into().unwrap());
quantity as f64 / factor as f64
})
.collect::<Vec<f64>>(),
);
// Insert bid parsed quantities
result.insert(
"bid_parsed_quantities".to_string(),
bid_parsed_quantities
.into_iter()
.map(|quantity| {
let factor = 10u64.pow((*base_decimals).try_into().unwrap());
quantity as f64 / factor as f64
})
.collect::<Vec<f64>>(),
Value::Array(
bid_parsed_quantities
.into_iter()
.map(|quantity| {
let factor = 10u64.pow((*base_decimals).try_into().unwrap());
Value::from(quantity as f64 / factor as f64)
})
.collect(),
),
);

// Insert ask parsed prices
result.insert(
"ask_parsed_prices".to_string(),
ask_parsed_prices
.into_iter()
.map(|quantity| {
let factor = 10u64.pow((9 - *base_decimals + *quote_decimals).try_into().unwrap());
quantity as f64 / factor as f64
})
.collect::<Vec<f64>>(),
Value::Array(
ask_parsed_prices
.into_iter()
.map(|quantity| {
let factor = 10u64.pow((9 - *base_decimals + *quote_decimals).try_into().unwrap());
Value::from(quantity as f64 / factor as f64)
})
.collect(),
),
);

// Insert ask parsed quantities
result.insert(
"ask_parsed_quantities".to_string(),
ask_parsed_quantities
.into_iter()
.map(|quantity| {
let factor = 10u64.pow((*base_decimals).try_into().unwrap());
quantity as f64 / factor as f64
})
.collect::<Vec<f64>>(),
Value::Array(
ask_parsed_quantities
.into_iter()
.map(|quantity| {
let factor = 10u64.pow((*base_decimals).try_into().unwrap());
Value::from(quantity as f64 / factor as f64)
})
.collect(),
),
);

// Insert timestamp
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis() as i64;
result.insert("timestamp".to_string(), Value::from(timestamp));

Ok(Json(result))
}

Expand Down

0 comments on commit 10a95cb

Please sign in to comment.