Skip to content

Commit

Permalink
feat(serve): use new globaldb table names
Browse files Browse the repository at this point in the history
  • Loading branch information
alextes committed Aug 21, 2024
1 parent bdeac61 commit 5d7763a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 53 deletions.
15 changes: 5 additions & 10 deletions src/serve/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use serde::Serialize;
use sqlx::{PgPool, Row};
use std::collections::HashMap;

use crate::env::ToNetwork;

use super::{env::APP_CONFIG, internal_error, ApiResponse, AppState};
use super::{internal_error, ApiResponse, AppState};

#[derive(Serialize, Clone)]
#[serde(rename_all = "camelCase")]
Expand All @@ -27,20 +25,17 @@ pub struct PubkeyBlockCount {
}

async fn fetch_pubkey_block_counts(relay_pool: &PgPool) -> Result<Vec<PubkeyBlockCount>> {
let query = format!(
"
let query = "
SELECT
builder_pubkey AS pubkey,
COUNT(*) AS block_count
FROM
{}_payload_delivered
payload_delivered
GROUP BY
builder_pubkey
",
&APP_CONFIG.env.to_network().to_string()
);
";

sqlx::query(&query)
sqlx::query(query)
.fetch_all(relay_pool)
.await
.map(|rows| {
Expand Down
47 changes: 18 additions & 29 deletions src/serve/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use chrono::{DateTime, TimeZone, Utc};
use serde::Serialize;
use sqlx::Row;

use crate::env::ToNetwork;

use super::{internal_error, ApiResponse, AppState, APP_CONFIG};
use super::{internal_error, ApiResponse, AppState};

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -29,17 +27,14 @@ pub struct PayloadStatsBody {
}

pub async fn delivered_payloads(State(state): State<AppState>) -> ApiResponse<PayloadsBody> {
let query = format!(
"
let query = "
select inserted_at, block_number, (value / 10^18) as value
from {}_payload_delivered
from payload_delivered
order by inserted_at desc
limit 30
",
&APP_CONFIG.env.to_network().to_string()
);
";

sqlx::query(&query)
sqlx::query(query)
.fetch_all(&state.relay_db_pool)
.await
.map(|rows| {
Expand All @@ -58,17 +53,14 @@ pub async fn delivered_payloads(State(state): State<AppState>) -> ApiResponse<Pa
}

pub async fn payload_stats(State(state): State<AppState>) -> ApiResponse<PayloadStatsBody> {
let query = format!(
"
select count(*) as count,
sum(value) / 10^18 as value,
(select min(inserted_at) from {network}_payload_delivered) as first_payload_at
from {network}_payload_delivered
",
network = &APP_CONFIG.env.to_network().to_string(),
);
let query = "
select count(*) as count,
sum(value) / 10^18 as value,
(select min(inserted_at) from payload_delivered) as first_payload_at
from payload_delivered
";

sqlx::query(&query)
sqlx::query(query)
.fetch_one(&state.relay_db_pool)
.await
.map(|row| {
Expand All @@ -82,19 +74,16 @@ pub async fn payload_stats(State(state): State<AppState>) -> ApiResponse<Payload
}

pub async fn top_payloads(State(state): State<AppState>) -> ApiResponse<PayloadsBody> {
let query = format!(
"
let query = "
select inserted_at,
block_number,
(value / 10^18) as value
from {}_payload_delivered
block_number,
(value / 10^18) as value
from payload_delivered
order by value desc
limit 10
",
&APP_CONFIG.env.to_network().to_string()
);
";

sqlx::query(&query)
sqlx::query(query)
.fetch_all(&state.relay_db_pool)
.await
.map(|rows| {
Expand Down
24 changes: 10 additions & 14 deletions src/serve/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ pub struct ValidatorStatsBody {
}

async fn get_registered_validator_count(relay_pool: &PgPool) -> Result<i64> {
sqlx::query(&format!(
sqlx::query(
"
select count(*) as validator_count
from (select distinct pubkey from {}_validator_registration) as sq
select count(*) as validator_count
from (select distinct pubkey from validator_registration) as sq
",
&APP_CONFIG.env.to_network().to_string()
))
)
.fetch_one(relay_pool)
.await
.map(|row| row.get::<i64, _>("validator_count"))
Expand Down Expand Up @@ -56,16 +55,13 @@ pub async fn check_validator_registration(
State(state): State<AppState>,
Path(pubkey): Path<String>,
) -> ApiResponse<RegistrationStatusBody> {
let query = format!(
"
select exists (
select pubkey from {}_validator_registration where pubkey = $1
)
",
&APP_CONFIG.env.to_network().to_string()
);
let query = "
select exists (
select pubkey from validator_registration where pubkey = $1
)
";

sqlx::query(&query)
sqlx::query(query)
.bind(pubkey)
.fetch_one(&state.relay_db_pool)
.await
Expand Down

0 comments on commit 5d7763a

Please sign in to comment.