Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sat recursive endpoints with index and pagination #2680

Merged
merged 20 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use {
templates::{
BlockHtml, BlockJson, BlocksHtml, ChildrenHtml, ClockSvg, HomeHtml, InputHtml,
InscriptionHtml, InscriptionJson, InscriptionsBlockHtml, InscriptionsHtml, InscriptionsJson,
OutputHtml, OutputJson, PageContent, PageHtml, PreviewAudioHtml, PreviewCodeHtml,
PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml,
PreviewUnknownHtml, PreviewVideoHtml, RangeHtml, RareTxt, RuneHtml, RunesHtml, SatHtml,
SatJson, TransactionHtml,
InscriptionsSatJson, OutputHtml, OutputJson, PageContent, PageHtml, PreviewAudioHtml,
PreviewCodeHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml,
PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml, RangeHtml, RareTxt, RuneHtml,
RunesHtml, SatHtml, SatJson, TransactionHtml,
},
},
axum::{
Expand Down Expand Up @@ -229,6 +229,11 @@ impl Server {
.route("/r/blockheight", get(Self::block_height))
.route("/r/blocktime", get(Self::block_time))
.route("/r/metadata/:inscription_id", get(Self::metadata))
.route("/r/inscriptions/sat/:number", get(Self::sat_inscriptions))
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
.route(
"/r/inscriptions/sat/:number/:from",
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
get(Self::sat_inscriptions_from),
)
.route("/range/:start/:end", get(Self::range))
.route("/rare.txt", get(Self::rare_txt))
.route("/rune/:rune", get(Self::rune))
Expand Down Expand Up @@ -488,6 +493,39 @@ impl Server {
})
}

async fn sat_inscriptions(
Extension(page_config): Extension<Arc<PageConfig>>,
Extension(index): Extension<Arc<Index>>,
Path(DeserializeFromStr(sat_number)): Path<DeserializeFromStr<u64>>,
) -> ServerResult<Response> {
Self::sat_inscriptions_inner(page_config, index, sat_number, 0, 100).await
}
async fn sat_inscriptions_from(
Extension(page_config): Extension<Arc<PageConfig>>,
Extension(index): Extension<Arc<Index>>,
Path((sat_number, page_index)): Path<(u64, u64)>,
) -> ServerResult<Response> {
Self::sat_inscriptions_inner(page_config, index, sat_number, page_index, 100).await
}

async fn sat_inscriptions_inner(
page_config: Arc<PageConfig>,
index: Arc<Index>,
sat_number: u64,
from: u64,
n: usize,
) -> ServerResult<Response> {
let ids = index.get_inscription_ids_by_sat(Sat(sat_number))?;
Ok(
Json(InscriptionsSatJson {
ids,
page: 0,
more: false,
})
.into_response(),
)
}

async fn ordinal(Path(sat): Path<String>) -> Redirect {
Redirect::to(&format!("/sat/{sat}"))
}
Expand Down
2 changes: 1 addition & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(crate) use {
rare::RareTxt,
rune::RuneHtml,
runes::RunesHtml,
sat::{SatHtml, SatJson},
sat::{InscriptionsSatJson, SatHtml, SatJson},
transaction::TransactionHtml,
};

Expand Down
7 changes: 7 additions & 0 deletions src/templates/sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub struct SatJson {
pub inscriptions: Vec<InscriptionId>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct InscriptionsSatJson {
pub ids: Vec<InscriptionId>,
pub more: bool,
pub page: u64,
}

impl PageContent for SatHtml {
fn title(&self) -> String {
format!("Sat {}", self.sat)
Expand Down
7 changes: 5 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ use {
inscription_id::InscriptionId,
rarity::Rarity,
templates::{
block::BlockJson, inscription::InscriptionJson, inscriptions::InscriptionsJson,
output::OutputJson, sat::SatJson,
block::BlockJson,
inscription::InscriptionJson,
inscriptions::InscriptionsJson,
output::OutputJson,
sat::{InscriptionsSatJson, SatJson},
},
SatPoint,
},
Expand Down
41 changes: 41 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,44 @@ fn all_endpoints_in_recursive_directory_return_json() {

assert!(server.request("/blockhash/2").json::<String>().is_err());
}

#[test]
fn sat_recursive_endpoint() {
let rpc_server = test_bitcoincore_rpc::spawn();
create_wallet(&rpc_server);

rpc_server.mine_blocks(2);

let server = TestServer::spawn_with_args(&rpc_server, &["--index-sats"]);

assert_eq!(
server
.request("/r/inscriptions/sat/5000000000")
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
.json::<InscriptionsSatJson>()
.unwrap(),
InscriptionsSatJson {
ids: vec![],
page: 0,
more: false
}
);

for _ in 0..10 {
inscribe(&rpc_server);
}

let server = TestServer::spawn_with_args(&rpc_server, &["--index-sats"]);

let response = server
.request("/r/inscriptions/sat/5000000000")
raphjaph marked this conversation as resolved.
Show resolved Hide resolved
.json::<InscriptionsSatJson>()
.unwrap();

assert_eq!(response.ids.len(), 100);
assert_eq!(response.more, false);
assert_eq!(response.page, 0);

inscribe(&rpc_server);


}