From 6d32786a6eb4b8e9182de6f1b142c0b9750a37ea Mon Sep 17 00:00:00 2001 From: Lugon LQ <48004924+lugondev@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:33:12 +0700 Subject: [PATCH 1/5] bring back /runes/balances json --- src/subcommand/server.rs | 27 +++++++++++++++- tests/json_api.rs | 70 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index b1fe73ac89..15ccf76eb3 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -10,7 +10,8 @@ use { InscriptionHtml, InscriptionsBlockHtml, InscriptionsHtml, OutputHtml, PageContent, PageHtml, ParentsHtml, PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, - PreviewVideoHtml, RangeHtml, RareTxt, RuneHtml, RunesHtml, SatHtml, TransactionHtml, + PreviewVideoHtml, RangeHtml, RareTxt, RuneHtml, RunesHtml, SatHtml, + TransactionHtml, }, axum::{ body, @@ -255,6 +256,7 @@ impl Server { .route("/rune/:rune", get(Self::rune)) .route("/runes", get(Self::runes)) .route("/runes/:page", get(Self::runes_paginated)) + .route("/runes/balances", get(Self::runes_balances)) .route("/sat/:sat", get(Self::sat)) .route("/search", get(Self::search_by_query)) .route("/search/*query", get(Self::search_by_path)) @@ -740,6 +742,29 @@ impl Server { }) } + async fn runes_balances(Extension(index): Extension>) -> ServerResult { + task::block_in_place(|| { + let balances = index.get_rune_balance_map()?; + Ok( + Json( + balances + .into_iter() + .map(|(rune, balances)| { + ( + rune, + balances + .into_iter() + .map(|(outpoint, pile)| (outpoint, pile.amount)) + .collect(), + ) + }) + .collect::>>(), + ) + .into_response(), + ) + }) + } + async fn home( Extension(server_config): Extension>, Extension(index): Extension>, diff --git a/tests/json_api.rs b/tests/json_api.rs index a43b2aac81..cd42c19d6a 100644 --- a/tests/json_api.rs +++ b/tests/json_api.rs @@ -635,3 +635,73 @@ fn get_runes() { } ); } + +#[test] +fn get_runes_balances() { + let core = mockcore::builder().network(Network::Regtest).build(); + + let ord = TestServer::spawn_with_server_args(&core, &["--index-runes", "--regtest"], &[]); + + create_wallet(&core, &ord); + + core.mine_blocks(3); + + let rune0 = Rune(RUNE); + let rune1 = Rune(RUNE + 1); + let rune2 = Rune(RUNE + 2); + + let e0 = etch(&core, &ord, rune0); + let e1 = etch(&core, &ord, rune1); + let e2 = etch(&core, &ord, rune2); + + core.mine_blocks(1); + + let rune_balances: BTreeMap> = vec![ + ( + rune0, + vec![( + OutPoint { + txid: e0.output.reveal, + vout: 1, + }, + 1000, + )] + .into_iter() + .collect(), + ), + ( + rune1, + vec![( + OutPoint { + txid: e1.output.reveal, + vout: 1, + }, + 1000, + )] + .into_iter() + .collect(), + ), + ( + rune2, + vec![( + OutPoint { + txid: e2.output.reveal, + vout: 1, + }, + 1000, + )] + .into_iter() + .collect(), + ), + ] + .into_iter() + .collect(); + + let response = ord.json_request("/runes/balances"); + assert_eq!(response.status(), StatusCode::OK); + + let runes_balance_json: BTreeMap> = + serde_json::from_str(&response.text().unwrap()).unwrap(); + + pretty_assert_eq!(runes_balance_json, rune_balances); +} From e60f2d5048b05a5cf2abf704da708008d14e465b Mon Sep 17 00:00:00 2001 From: Lugon LQ <48004924+lugondev@users.noreply.github.com> Date: Wed, 17 Apr 2024 14:36:04 +0700 Subject: [PATCH 2/5] fix lint --- src/subcommand/server.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 15ccf76eb3..377cf54632 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -10,8 +10,7 @@ use { InscriptionHtml, InscriptionsBlockHtml, InscriptionsHtml, OutputHtml, PageContent, PageHtml, ParentsHtml, PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, - PreviewVideoHtml, RangeHtml, RareTxt, RuneHtml, RunesHtml, SatHtml, - TransactionHtml, + PreviewVideoHtml, RangeHtml, RareTxt, RuneHtml, RunesHtml, SatHtml, TransactionHtml, }, axum::{ body, From 2fe17d85e70d59328845ed14af1b9cd897da6576 Mon Sep 17 00:00:00 2001 From: Lugon LQ <48004924+lugondev@users.noreply.github.com> Date: Thu, 18 Apr 2024 12:06:58 +0700 Subject: [PATCH 3/5] test html_runes_balances_not_found --- src/subcommand/server.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 377cf54632..70ac7f2d78 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -741,10 +741,13 @@ impl Server { }) } - async fn runes_balances(Extension(index): Extension>) -> ServerResult { + async fn runes_balances( + Extension(index): Extension>, + AcceptJson(accept_json): AcceptJson, + ) -> ServerResult { task::block_in_place(|| { let balances = index.get_rune_balance_map()?; - Ok( + Ok(if accept_json { Json( balances .into_iter() @@ -759,8 +762,11 @@ impl Server { }) .collect::>>(), ) - .into_response(), - ) + .into_response() + } else { + // throw http error 404 + StatusCode::NOT_FOUND.into_response() + }) }) } @@ -2570,6 +2576,14 @@ mod tests { ); } + #[test] + fn html_runes_balances_not_found() { + TestServer::builder() + .chain(Chain::Regtest) + .build() + .assert_response("/runes/balances", StatusCode::NOT_FOUND, ""); + } + #[test] fn fallback() { let server = TestServer::new(); From 9fd6ee98c267b61bded4f09e235ff40ba1eb86c4 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 18 Apr 2024 18:50:10 -0400 Subject: [PATCH 4/5] revert --- src/subcommand/server.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 70ac7f2d78..d048be9714 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -764,7 +764,6 @@ impl Server { ) .into_response() } else { - // throw http error 404 StatusCode::NOT_FOUND.into_response() }) }) From b601a9144dd69ebd121b4900964172ebcb142856 Mon Sep 17 00:00:00 2001 From: raphjaph Date: Thu, 18 Apr 2024 18:51:43 -0400 Subject: [PATCH 5/5] Amend --- src/subcommand/server.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index d048be9714..e94a15f230 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -746,10 +746,10 @@ impl Server { AcceptJson(accept_json): AcceptJson, ) -> ServerResult { task::block_in_place(|| { - let balances = index.get_rune_balance_map()?; Ok(if accept_json { Json( - balances + index + .get_rune_balance_map()? .into_iter() .map(|(rune, balances)| { (