parent
a4c194ff3e
commit
e52314c138
2 changed files with 40 additions and 1 deletions
18
src/main.rs
18
src/main.rs
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 })
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue