Add RPC call to retrieve pool ID

Add a call that returns a pool ID given an utxo
This commit is contained in:
Dagur Valberg Johannsson 2026-01-09 15:28:36 +01:00
parent 46c3ff029a
commit 50cd23dd34
No known key found for this signature in database
GPG key ID: FD701804AEE88107
3 changed files with 65 additions and 2 deletions

View file

@ -530,6 +530,18 @@ pub fn db_pool_get_details(db: &Connection, pool: &PoolID) -> Result<(String, St
Ok(res) Ok(res)
} }
pub fn db_pool_id_from_utxo(conn: &Connection, utxo_hash: &OutPointHash) -> Result<Option<String>> {
let mut stmt = conn.prepare("SELECT pool FROM pool_history_entry WHERE utxo = ?")?;
let mut rows = stmt.query(params![utxo_hash.to_hex()])?;
if let Some(row) = rows.next()? {
let pool_id: String = row.get(0)?;
Ok(Some(pool_id))
} else {
Ok(None)
}
}
/// Get total volume in satoshis across all tokens for a given time period /// Get total volume in satoshis across all tokens for a given time period
pub fn get_total_volume_sats( pub fn get_total_volume_sats(
db: &Connection, db: &Connection,

View file

@ -325,6 +325,7 @@ fn launch() -> _ {
rpc::pool::list_pools_by_apy, rpc::pool::list_pools_by_apy,
rpc::pool::list_active_pools, rpc::pool::list_active_pools,
rpc::pool::pool_history, rpc::pool::pool_history,
rpc::pool::pool_id_from_utxo,
rpc::apy::aggregate_apy, rpc::apy::aggregate_apy,
rpc::contract::contract_count_token, rpc::contract::contract_count_token,
rpc::contract::contract_count_all, rpc::contract::contract_count_all,

View file

@ -7,7 +7,7 @@ use crate::{
cashaddr::utiladdr::p2pkh_hex_to_addr, cashaddr::utiladdr::p2pkh_hex_to_addr,
db::{ db::{
cauldron::{ cauldron::{
pool::{db_pool_get_details, db_pool_history}, pool::{db_pool_get_details, db_pool_history, db_pool_id_from_utxo},
poolvisitor::{ poolvisitor::{
db_visit_pool_entries, OptionalFields, OptionalPoolFields, PoolFilters, PoolVisitor, db_visit_pool_entries, OptionalFields, OptionalPoolFields, PoolFilters, PoolVisitor,
}, },
@ -18,7 +18,9 @@ use crate::{
timeutil::time_now, timeutil::time_now,
}; };
use anyhow::Result; use anyhow::Result;
use bitcoin_hashes::hex::FromHex; use bitcoin_hashes::hex::{FromHex, ToHex};
use bitcoincash::Txid;
use riftenlabs_defi::chainutil::compute_outpoint_hash;
use rocket::{get, http::Status, response::status::Custom, serde::json::Json, State}; use rocket::{get, http::Status, response::status::Custom, serde::json::Json, State};
use rusqlite::{params, Connection}; use rusqlite::{params, Connection};
use serde_json::{json, Value}; use serde_json::{json, Value};
@ -298,3 +300,51 @@ pub fn pool_history(
"owner_pkh": owner_pkh, "owner_pkh": owner_pkh,
}))) })))
} }
/// Get pool ID from a UTXO specified by transaction ID and input position.
///
/// - txid: Transaction ID (hex string)
/// - input_pos: Input position (vout) in the transaction
///
/// **Response Example:**
/// ```json
/// {
/// "pool_id": "a1b2c3d4e5f6..."
/// }
/// ```
#[get("/pool/id_from_utxo?<txid>&<n>")]
pub fn pool_id_from_utxo(
txid: &str,
n: u32,
conn: &State<DB>,
) -> Result<Json<Value>, Custom<String>> {
let db = conn
.cauldron_r
.get()
.map_err(|e| Custom(Status::InternalServerError, format!("Error: {e}")))?;
// Parse txid from hex
let txid = Txid::from_hex(txid)
.map_err(|e| Custom(Status::BadRequest, format!("Invalid txid: {e}")))?;
// Compute outpoint hash from txid and input position
let utxo_hash = compute_outpoint_hash(&txid, n);
// Query pool_history_entry table
let pool_id = db_pool_id_from_utxo(&db, &utxo_hash)
.map_err(|e| Custom(Status::InternalServerError, format!("Error: {e}")))?;
match pool_id {
Some(pool_id) => Ok(Json(json!({
"pool_id": pool_id
}))),
None => Err(Custom(
Status::NotFound,
format!(
"No pool found for UTXO: txid={}, input_pos={}",
txid.to_hex(),
n
),
)),
}
}