From 50cd23dd34dd7ed0cebde387ea60c99d89f3cac6 Mon Sep 17 00:00:00 2001 From: Dagur Valberg Johannsson Date: Fri, 9 Jan 2026 15:28:36 +0100 Subject: [PATCH] Add RPC call to retrieve pool ID Add a call that returns a pool ID given an utxo --- src/db/cauldron/pool.rs | 12 +++++++++ src/main.rs | 1 + src/rpc/pool.rs | 54 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/db/cauldron/pool.rs b/src/db/cauldron/pool.rs index e301ec9..9106a5a 100644 --- a/src/db/cauldron/pool.rs +++ b/src/db/cauldron/pool.rs @@ -530,6 +530,18 @@ pub fn db_pool_get_details(db: &Connection, pool: &PoolID) -> Result<(String, St Ok(res) } +pub fn db_pool_id_from_utxo(conn: &Connection, utxo_hash: &OutPointHash) -> Result> { + 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 pub fn get_total_volume_sats( db: &Connection, diff --git a/src/main.rs b/src/main.rs index ab45b6a..e251be1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -325,6 +325,7 @@ fn launch() -> _ { rpc::pool::list_pools_by_apy, rpc::pool::list_active_pools, rpc::pool::pool_history, + rpc::pool::pool_id_from_utxo, rpc::apy::aggregate_apy, rpc::contract::contract_count_token, rpc::contract::contract_count_all, diff --git a/src/rpc/pool.rs b/src/rpc/pool.rs index 8a1a0cb..e7b9f5d 100644 --- a/src/rpc/pool.rs +++ b/src/rpc/pool.rs @@ -7,7 +7,7 @@ use crate::{ cashaddr::utiladdr::p2pkh_hex_to_addr, db::{ cauldron::{ - pool::{db_pool_get_details, db_pool_history}, + pool::{db_pool_get_details, db_pool_history, db_pool_id_from_utxo}, poolvisitor::{ db_visit_pool_entries, OptionalFields, OptionalPoolFields, PoolFilters, PoolVisitor, }, @@ -18,7 +18,9 @@ use crate::{ timeutil::time_now, }; 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 rusqlite::{params, Connection}; use serde_json::{json, Value}; @@ -298,3 +300,51 @@ pub fn pool_history( "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?&")] +pub fn pool_id_from_utxo( + txid: &str, + n: u32, + conn: &State, +) -> Result, Custom> { + 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 + ), + )), + } +}