From ea26b093209fec37a8a5e05d0323d4595f2b7a99 Mon Sep 17 00:00:00 2001 From: lifofifo Date: Sun, 14 Apr 2024 22:04:22 -0500 Subject: [PATCH] Add recursive endpoint to fetch balance of a rune token for an inscription UTXO --- src/subcommand/server.rs | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 7fd11bea93..db193a11b2 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -236,6 +236,10 @@ impl Server { "/r/inscription/:inscription_id", get(Self::inscription_recursive), ) + .route( + "/r/inscription/:inscription_id/rune/:rune_id", + get(Self::inscription_rune), + ) .route("/r/children/:inscription_id", get(Self::children_recursive)) .route( "/r/children/:inscription_id/:page", @@ -990,6 +994,47 @@ impl Server { }) } + async fn inscription_rune( + Extension(index): Extension>, + Path((inscription_id, rune_id)): Path<(InscriptionId, RuneId)>, + ) -> ServerResult { + task::block_in_place(|| { + index + .get_inscription_by_id(inscription_id)? + .ok_or_not_found(|| format!("inscription {inscription_id}"))?; + + let satpoint = index + .get_inscription_satpoint_by_id(inscription_id) + .ok() + .flatten() + .unwrap(); + + let rune = index + .get_rune_by_id(rune_id)? + .ok_or_not_found(|| format!("rune {rune_id}"))?; + + let (_, rune_entry, _) = index + .rune(rune)? + .ok_or_not_found(|| format!("rune {rune}"))?; + + let runes = index.get_rune_balances_for_outpoint(satpoint.outpoint)?; + + let pile = runes.into_iter() + .find_map(|(spaced_rune, pile)| { + if spaced_rune.rune == rune { + Some(pile) + } else { + None + } + }) + .unwrap_or_else(|| rune_entry.pile(0)); + + Ok( + Json(pile).into_response() + ) + }) + } + async fn status( Extension(server_config): Extension>, Extension(index): Extension>,