diff --git a/src/main.rs b/src/main.rs index 5652283..b071a7b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -386,6 +386,16 @@ fn list_pools_by_apy(conn: &State) -> Result, Custom }))) } +#[get("/contract/count")] +fn contract_count(conn: &State) -> Result, Custom> { + let db = conn + .get() + .map_err(|e| Custom(Status::InternalServerError, format!("Error: {}", e)))?; + let count = rpc::contract_count(&db) + .map_err(|e| Custom(Status::BadRequest, format!("Error: {}", e)))?; + Ok(Json(json!(count))) +} + fn set_panic_hook() { panic::set_hook(Box::new(|panic_info| { error!("A thread panicked, terminating the program."); @@ -511,7 +521,13 @@ fn launch() -> _ { .manage(dbpool) .mount( "/cauldron/", - routes![tvl, list_by_volume, price_history, list_pools_by_apy], + routes![ + tvl, + list_by_volume, + price_history, + list_pools_by_apy, + contract_count + ], ) .attach(cors) } diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index bd563cc..cb3b171 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -5,6 +5,7 @@ use anyhow::{bail, Result}; use rusqlite::{params, Connection}; +use serde::Serialize; use std::time::{SystemTime, UNIX_EPOCH}; pub fn get_token_tvl( @@ -400,3 +401,25 @@ pub fn pools_by_apy(connection: &Connection) -> Result> { Ok(pools) } + +#[derive(Serialize)] +pub struct ContractCount { + active: u64, + ended: u64, +} + +pub fn contract_count(db: &Connection) -> Result { + let active: u64 = db.query_row( + "SELECT COUNT(*) FROM pool WHERE withdrawn_in_utxo IS NULL", + [], + |row| row.get(0), + )?; + + let ended: u64 = db.query_row( + "SELECT COUNT(*) FROM pool WHERE withdrawn_in_utxo IS NOT NULL", + [], + |row| row.get(0), + )?; + + Ok(ContractCount { active, ended }) +}