2026-01-21 12:34:59 +01:00
|
|
|
// Copyright (C) 2024-2026 Whiterun LLC
|
2024-05-09 11:25:27 +02:00
|
|
|
//
|
|
|
|
|
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
|
|
|
|
|
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
|
2025-09-17 11:11:21 +02:00
|
|
|
use bitcoin_hashes::hex::{FromHex, ToHex};
|
|
|
|
|
use bitcoincash::TokenID;
|
2026-01-21 08:27:57 +01:00
|
|
|
use rocket::{get, State};
|
2024-05-09 11:25:27 +02:00
|
|
|
use serde_json::json;
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
2025-09-17 11:11:21 +02:00
|
|
|
use crate::db::cauldron::tokenlist::db_utils::{
|
|
|
|
|
cache_first_pool_ts_if_empty, db_first_pool_creation_row, CachedSort,
|
|
|
|
|
};
|
2025-09-05 13:53:10 +00:00
|
|
|
use crate::db::cauldron::tokenlist::list_cached::{
|
|
|
|
|
db_list_tokens_cached, db_list_tokens_cached_by_ids, TokenListItemCached,
|
|
|
|
|
};
|
|
|
|
|
use crate::db::search::db_search_tokens_cached;
|
2024-05-09 11:25:27 +02:00
|
|
|
use crate::db::DB;
|
|
|
|
|
|
2026-02-17 17:47:43 +01:00
|
|
|
use super::err::{bad_request, db_error, not_found, ApiErrorCode, CachedApiResult};
|
2026-01-21 10:37:55 +01:00
|
|
|
use super::response::{cached_ok, CACHE_AGGREGATE, CACHE_IMMUTABLE};
|
2024-10-31 10:55:29 +00:00
|
|
|
|
|
|
|
|
#[get("/tokens/search_by_volume?<search_query>")]
|
2026-02-17 17:47:43 +01:00
|
|
|
pub async fn search_by_volume(search_query: &str, db: &State<DB>) -> CachedApiResult<Vec<Value>> {
|
|
|
|
|
use rayon::prelude::*;
|
|
|
|
|
|
|
|
|
|
let list: Vec<(String, Option<String>, Option<String>, u64)> =
|
|
|
|
|
crate::db::search::search_tokens_by_volume(
|
|
|
|
|
&db.cauldron_r,
|
|
|
|
|
&db.bcmr_r,
|
|
|
|
|
&db.crc20_r,
|
|
|
|
|
search_query,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(db_error)?;
|
|
|
|
|
|
|
|
|
|
let result: Vec<Value> = list
|
|
|
|
|
.into_par_iter()
|
|
|
|
|
.map(|(token_id, name, ticker, trade_volume)| {
|
|
|
|
|
json!({
|
|
|
|
|
"token_id": token_id,
|
|
|
|
|
"name": name,
|
|
|
|
|
"ticker": ticker,
|
|
|
|
|
"trade_volume": trade_volume
|
2024-10-31 10:55:29 +00:00
|
|
|
})
|
2026-02-17 17:47:43 +01:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
Ok(cached_ok(result, CACHE_AGGREGATE))
|
2024-10-31 10:55:29 +00:00
|
|
|
}
|
2025-09-05 13:53:10 +00:00
|
|
|
|
|
|
|
|
#[get("/tokens/search_cached?<q>&<limit>&<offset>&<by>&<order>")]
|
2026-02-17 17:47:43 +01:00
|
|
|
pub async fn search_cached(
|
2025-09-05 13:53:10 +00:00
|
|
|
db: &State<DB>,
|
|
|
|
|
q: Option<String>,
|
|
|
|
|
limit: Option<usize>,
|
|
|
|
|
offset: Option<usize>,
|
|
|
|
|
by: Option<String>,
|
|
|
|
|
order: Option<String>,
|
2026-01-21 10:37:55 +01:00
|
|
|
) -> CachedApiResult<Value> {
|
2025-09-05 13:53:10 +00:00
|
|
|
let sort = parse_cached_sort(by, order);
|
|
|
|
|
let limit = limit.unwrap_or(250);
|
|
|
|
|
let offset = offset.unwrap_or(0);
|
|
|
|
|
let query = q.unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
let items: Vec<TokenListItemCached> =
|
2026-02-17 17:47:43 +01:00
|
|
|
db_search_tokens_cached(&db.cauldron_r, &query, sort, limit, offset)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(db_error)?;
|
2025-09-05 13:53:10 +00:00
|
|
|
|
2026-01-21 10:37:55 +01:00
|
|
|
Ok(cached_ok(json!(items), CACHE_AGGREGATE))
|
2025-09-05 13:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parse_cached_sort(by: Option<String>, order: Option<String>) -> CachedSort {
|
|
|
|
|
let desc = matches!(order.as_deref(), Some("desc") | Some("DESC"));
|
|
|
|
|
match by.as_deref() {
|
|
|
|
|
Some("name") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::NameDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::NameAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some("symbol") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::SymbolDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::SymbolAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some("tvl") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::TvlDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::TvlAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some("volume") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::VolumeDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::VolumeAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some("change_24h_bp") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::Change24hDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::Change24hAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some("change_7d_bp") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::Change7dDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::Change7dAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// USD sorts
|
|
|
|
|
Some("price_usd") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::PriceUsdDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::PriceUsdAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some("change_24h_usd_bp") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::Change24hUsdDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::Change24hUsdAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Some("change_7d_usd_bp") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::Change7dUsdDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::Change7dUsdAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// APY (30d) sorts — accept a few aliases
|
|
|
|
|
Some("apy") | Some("apy_30d") | Some("apy_30d_bp") => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::Apy30dDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::Apy30dAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// default
|
|
|
|
|
_ => {
|
|
|
|
|
if desc {
|
|
|
|
|
CachedSort::ScoreDesc
|
|
|
|
|
} else {
|
|
|
|
|
CachedSort::ScoreAsc
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/tokens/list_cached?<limit>&<offset>&<by>&<order>")]
|
2026-02-17 17:47:43 +01:00
|
|
|
pub async fn list_cached(
|
2025-09-05 13:53:10 +00:00
|
|
|
db: &State<DB>,
|
|
|
|
|
limit: Option<usize>,
|
|
|
|
|
offset: Option<usize>,
|
|
|
|
|
by: Option<String>,
|
|
|
|
|
order: Option<String>,
|
2026-01-21 10:37:55 +01:00
|
|
|
) -> CachedApiResult<Value> {
|
2025-09-05 13:53:10 +00:00
|
|
|
let limit = limit.unwrap_or(250);
|
|
|
|
|
let offset = offset.unwrap_or(0);
|
|
|
|
|
|
|
|
|
|
let sort = parse_cached_sort(by, order);
|
|
|
|
|
let items: Vec<TokenListItemCached> =
|
2026-02-17 17:47:43 +01:00
|
|
|
db_list_tokens_cached(&db.cauldron_r, limit, offset, sort)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(db_error)?;
|
2025-09-05 13:53:10 +00:00
|
|
|
|
2026-01-21 10:37:55 +01:00
|
|
|
Ok(cached_ok(json!(items), CACHE_AGGREGATE))
|
2025-09-05 13:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[get("/tokens/list_cached_by_ids?<ids>&<by>&<order>")]
|
2026-02-17 17:47:43 +01:00
|
|
|
pub async fn list_cached_by_ids(
|
2025-09-05 13:53:10 +00:00
|
|
|
db: &State<DB>,
|
|
|
|
|
ids: &str,
|
|
|
|
|
by: Option<String>,
|
|
|
|
|
order: Option<String>,
|
2026-01-21 10:37:55 +01:00
|
|
|
) -> CachedApiResult<Value> {
|
2025-09-05 13:53:10 +00:00
|
|
|
// split, trim, and normalize (lowercase is typical for hex IDs in DB)
|
|
|
|
|
let token_ids: Vec<String> = ids
|
|
|
|
|
.split(',')
|
|
|
|
|
.map(|s| s.trim().to_lowercase())
|
|
|
|
|
.filter(|s| !s.is_empty())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
if token_ids.is_empty() {
|
2026-01-21 10:37:55 +01:00
|
|
|
return Ok(cached_ok(json!([]), CACHE_AGGREGATE));
|
2025-09-05 13:53:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let sort = parse_cached_sort(by, order);
|
2026-02-17 17:47:43 +01:00
|
|
|
let items = db_list_tokens_cached_by_ids(&db.cauldron_r, &token_ids, sort)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(db_error)?;
|
2025-09-05 13:53:10 +00:00
|
|
|
|
2026-01-21 10:37:55 +01:00
|
|
|
Ok(cached_ok(json!(items), CACHE_AGGREGATE))
|
2025-09-05 13:53:10 +00:00
|
|
|
}
|
2025-09-17 11:11:21 +02:00
|
|
|
|
|
|
|
|
#[get("/token/<token>/first_pool")]
|
2026-02-17 17:47:43 +01:00
|
|
|
pub async fn first_pool_creation(token: &str, dbp: &State<DB>) -> CachedApiResult<Value> {
|
2026-01-21 08:27:57 +01:00
|
|
|
let token = TokenID::from_hex(token).map_err(|e| {
|
|
|
|
|
bad_request(
|
|
|
|
|
ApiErrorCode::InvalidTokenId,
|
|
|
|
|
&format!("Invalid token id: {e}"),
|
|
|
|
|
)
|
|
|
|
|
})?;
|
2025-09-17 11:11:21 +02:00
|
|
|
let token_hex = token.to_hex();
|
|
|
|
|
|
2026-02-17 17:47:43 +01:00
|
|
|
match db_first_pool_creation_row(&dbp.cauldron_r, &token_hex).await {
|
2025-09-17 11:52:22 +02:00
|
|
|
Ok(Some((creation_utxo, txid, timestamp, block_height))) => {
|
2026-02-17 17:47:43 +01:00
|
|
|
// Opportunistically cache the timestamp (non-blocking)
|
|
|
|
|
let cauldron_w = dbp.cauldron_w.clone();
|
|
|
|
|
let token_hex_clone = token_hex.clone();
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
if let Err(e) =
|
|
|
|
|
cache_first_pool_ts_if_empty(&cauldron_w, &token_hex_clone, timestamp).await
|
|
|
|
|
{
|
|
|
|
|
log::warn!("Failed to cache first_pool_ts for {token_hex_clone}: {e}");
|
2026-02-02 21:01:50 +01:00
|
|
|
}
|
2026-02-17 17:47:43 +01:00
|
|
|
});
|
2025-09-17 11:11:21 +02:00
|
|
|
|
2026-01-21 10:37:55 +01:00
|
|
|
Ok(cached_ok(
|
|
|
|
|
json!({
|
|
|
|
|
"token": token_hex,
|
|
|
|
|
"creation_utxo": creation_utxo,
|
|
|
|
|
"txid": txid,
|
|
|
|
|
"timestamp": timestamp,
|
|
|
|
|
"block_height": block_height
|
|
|
|
|
}),
|
|
|
|
|
CACHE_IMMUTABLE,
|
|
|
|
|
))
|
2025-09-17 11:11:21 +02:00
|
|
|
}
|
2026-01-21 08:27:57 +01:00
|
|
|
Ok(None) => Err(not_found(ApiErrorCode::PoolNotFound, "No pools for token")),
|
|
|
|
|
Err(e) => Err(db_error(e)),
|
2025-09-17 11:11:21 +02:00
|
|
|
}
|
|
|
|
|
}
|