Skip to content

Commit

Permalink
add previous slot info field
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Leshiy committed Apr 5, 2024
1 parent a966b82 commit e0cbd89
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
36 changes: 34 additions & 2 deletions catalyst-gateway/bin/src/event_db/follower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl EventDB {
let rows = conn
.query(
include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/current_slot_no.sql"
"../../../event-db/queries/follower/select_slot_index_by_datetime/current_slot_info.sql"
),
&[&network, &date_time],
)
Expand All @@ -78,6 +78,38 @@ impl EventDB {
Ok((slot_number, block_hash, block_time))
}

/// Get previous slot info for the provided date-time and network
pub(crate) async fn previous_slot_info(
&self, date_time: DateTime, network: Network,
) -> Result<(SlotNumber, BlockHash, DateTime), Error> {
let conn = self.pool.get().await?;

let network = match network {
Network::Mainnet => "mainnet".to_string(),
Network::Preview => "preview".to_string(),
Network::Preprod => "preprod".to_string(),
Network::Testnet => "testnet".to_string(),
};

let rows = conn
.query(
include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/previous_slot_info.sql"
),
&[&network, &date_time],
)
.await?;

let Some(row) = rows.first() else {
return Err(Error::NotFound);
};

let slot_number: SlotNumber = row.try_get("slot_no")?;
let block_hash = hex::encode(row.try_get::<_, Vec<u8>>("block_hash")?);
let block_time = row.try_get("block_time")?;
Ok((slot_number, block_hash, block_time))
}

/// Get next slot info for the provided date-time and network
pub(crate) async fn next_slot_info(
&self, date_time: DateTime, network: Network,
Expand All @@ -94,7 +126,7 @@ impl EventDB {
let rows = conn
.query(
include_str!(
"../../../event-db/queries/follower/select_slot_index_by_datetime/next_slot_no.sql"
"../../../event-db/queries/follower/select_slot_index_by_datetime/next_slot_info.sql"
),
&[&network, &date_time],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ pub(crate) async fn endpoint(
Err(err) => return server_error_response!("{err}"),
};

let previous = match event_db
.previous_slot_info(date_time, network.clone().into())
.await
{
Ok((slot_number, block_hash, block_time)) => {
Some(Slot {
slot_number,
block_hash,
block_time,
})
},
Err(DBError::NotFound) => None,
Err(err) => return server_error_response!("{err}"),
};

let next = match event_db.next_slot_info(date_time, network.into()).await {
Ok((slot_number, block_hash, block_time)) => {
Some(Slot {
Expand All @@ -80,5 +95,9 @@ pub(crate) async fn endpoint(
Err(err) => return server_error_response!("{err}"),
};

T200(OK(Json(SlotInfo { current, next })))
T200(OK(Json(SlotInfo {
previous,
current,
next,
})))
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ impl Example for Slot {
#[derive(Object)]
#[oai(example = true)]
pub(crate) struct SlotInfo {
/// Previous slot info.
pub(crate) previous: Option<Slot>,

/// Current slot info.
pub(crate) current: Option<Slot>,

Expand All @@ -48,6 +51,7 @@ pub(crate) struct SlotInfo {
impl Example for SlotInfo {
fn example() -> Self {
Self {
previous: Some(Slot::example()),
current: Some(Slot::example()),
next: Some(Slot::example()),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SELECT
cardano_slot_index.slot_no,
cardano_slot_index.block_hash,
cardano_slot_index.block_time

FROM cardano_slot_index

WHERE
cardano_slot_index.network = $1
AND cardano_slot_index.block_time = $2

ORDER by cardano_slot_index.slot_no DESC

LIMIT 1;

0 comments on commit e0cbd89

Please sign in to comment.