Skip to content

Commit

Permalink
feat: added blocks query for endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
0xterminator committed Feb 18, 2025
1 parent 5f37242 commit b62eec7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
9 changes: 9 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
sha2 = "0.10"
sqlx = { version = "0.8.3", default-features = false, features = [
"chrono",
"macros",
"postgres",
"runtime-tokio",
Expand Down
1 change: 1 addition & 0 deletions crates/domains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version.workspace = true

[dependencies]
async-trait.workspace = true
chrono.workspace = true
fuel-core.workspace = true
fuel-core-types.workspace = true
fuel-streams-store.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/domains/src/blocks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod db_item;
mod packets;
pub mod queryable;
mod record_impl;
pub mod subjects;
pub mod types;
Expand Down
106 changes: 106 additions & 0 deletions crates/domains/src/blocks/queryable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use std::{fmt::Display, str::FromStr};

use chrono::{DateTime, TimeZone, Utc};
use fuel_streams_subject::subject::*;
use fuel_streams_types::*;
use serde::{Deserialize, Serialize};

use super::types::*;

#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum TimeRange {
#[serde(rename = "1h")]
OneHour,
#[serde(rename = "12h")]
TwelveHours,
#[serde(rename = "1d")]
OneDay,
#[serde(rename = "7d")]
SevenDays,
#[serde(rename = "30d")]
ThirtyDays,
#[serde(rename = "90d")]
NinetyDays,
#[serde(rename = "180d")]
OneEightyDays,
#[serde(rename = "1y")]
OneYear,
#[default]
#[serde(rename = "all")]
All,
}

impl Display for TimeRange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TimeRange::OneHour => write!(f, "1h"),
TimeRange::TwelveHours => write!(f, "12h"),
TimeRange::OneDay => write!(f, "1d"),
TimeRange::SevenDays => write!(f, "7d"),
TimeRange::ThirtyDays => write!(f, "30d"),
TimeRange::NinetyDays => write!(f, "90d"),
TimeRange::OneEightyDays => write!(f, "180d"),
TimeRange::OneYear => write!(f, "1y"),
TimeRange::All => write!(f, "all"),
}
}
}

impl FromStr for TimeRange {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"1h" => Ok(TimeRange::OneHour),
"12h" => Ok(TimeRange::TwelveHours),
"1d" => Ok(TimeRange::OneDay),
"7d" => Ok(TimeRange::SevenDays),
"30d" => Ok(TimeRange::ThirtyDays),
"90d" => Ok(TimeRange::NinetyDays),
"180d" => Ok(TimeRange::OneEightyDays),
"1y" => Ok(TimeRange::OneYear),
"all" => Ok(TimeRange::All),
_ => Err(format!("Invalid TimeRange: {}", s)),
}
}
}

#[derive(
Subject, Debug, Clone, Default, Serialize, Deserialize, Eq, PartialEq,
)]
#[subject(id = "blocks")]
#[subject(entity = "Block")]
#[subject(query_all = "blocks.>")]
#[subject(format = "blocks.{producer}.{height}")]
pub struct BlocksQuery {
#[subject(
sql_column = "producer_address",
description = "The address of the producer that created the block"
)]
pub producer: Option<Address>,
#[subject(
sql_column = "block_height",
description = "The height of the block as unsigned 64 bit integer"
)]
pub height: Option<BlockHeight>,
#[subject(
sql_column = "timestamp",
description = "The timestamp at which the block was added"
)]
pub timestamp: Option<DateTime<Utc>>,
#[subject(description = "The timestamp at which the block was added")]
pub time_range: Option<TimeRange>,
}

impl From<&Block> for BlocksQuery {
fn from(block: &Block) -> Self {
BlocksQuery {
producer: Some(block.producer.to_owned()),
height: Some(block.height.to_owned()),
timestamp: Some(
Utc.timestamp_opt(block.header.time.clone().to_unix(), 0)
.unwrap(),
),
time_range: Some(TimeRange::default()),
}
}
}
4 changes: 2 additions & 2 deletions services/api/src/server/handlers/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ async fn select_one(tx: &Pool<Postgres>) -> Result<i32, sqlx::Error> {

pub async fn get_blocks(
req: HttpRequest,
req_body: web::Json<GetBlocksTestRequest>,
req_query: web::Query<GetBlocksTestRequest>,
state: web::Data<ServerState>,
) -> actix_web::Result<HttpResponse> {
let _api_key = ApiKey::from_req(&req)?;
let _req = req_body.into_inner();
let _req = req_query.into_inner();
let one = select_one(&state.db.pool).await.map_err(Error::Sqlx)?;
Ok(HttpResponse::Ok().json(GetBlocksTestResponse { value: one }))
}

0 comments on commit b62eec7

Please sign in to comment.