rpc: Add contract count

Fixes #2
This commit is contained in:
Dagur Valberg Johannsson 2024-03-18 12:34:46 +01:00
parent a4c194ff3e
commit e52314c138
No known key found for this signature in database
GPG key ID: FD701804AEE88107
2 changed files with 40 additions and 1 deletions

View file

@ -386,6 +386,16 @@ fn list_pools_by_apy(conn: &State<DBPool>) -> Result<Json<Value>, Custom<String>
})))
}
#[get("/contract/count")]
fn contract_count(conn: &State<DBPool>) -> Result<Json<Value>, Custom<String>> {
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)
}

View file

@ -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<Vec<PoolYield>> {
Ok(pools)
}
#[derive(Serialize)]
pub struct ContractCount {
active: u64,
ended: u64,
}
pub fn contract_count(db: &Connection) -> Result<ContractCount> {
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 })
}