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

Recursive endpoint for retrieving details about an inscription #2628

Merged
merged 22 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
75 changes: 69 additions & 6 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ use {
templates::{
BlockHtml, BlockJson, BlocksHtml, BlocksJson, ChildrenHtml, ChildrenJson, ClockSvg,
CollectionsHtml, HomeHtml, InputHtml, InscriptionHtml, InscriptionJson,
InscriptionsBlockHtml, InscriptionsHtml, InscriptionsJson, OutputHtml, OutputJson,
PageContent, PageHtml, PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml,
PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml,
PreviewVideoHtml, RangeHtml, RareTxt, RuneBalancesHtml, RuneHtml, RuneJson, RunesHtml,
RunesJson, SatHtml, SatInscriptionJson, SatInscriptionsJson, SatJson, TransactionHtml,
TransactionJson,
InscriptionRecursiveJson, InscriptionsBlockHtml, InscriptionsHtml, InscriptionsJson,
OutputHtml, OutputJson, PageContent, PageHtml, PreviewAudioHtml, PreviewCodeHtml,
PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml,
PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml, RangeHtml, RareTxt, RuneBalancesHtml,
RuneHtml, RuneJson, RunesHtml, RunesJson, SatHtml, SatInscriptionJson, SatInscriptionsJson,
SatJson, TransactionHtml, TransactionJson,
},
},
axum::{
Expand Down Expand Up @@ -256,6 +256,10 @@ impl Server {
)
.route("/r/blockheight", get(Self::block_height))
.route("/r/blocktime", get(Self::block_time))
.route(
"/r/inscription/:inscription_id",
get(Self::inscription_recursive),
)
.route("/r/children/:inscription_id", get(Self::children_recursive))
.route(
"/r/children/:inscription_id/:page",
Expand Down Expand Up @@ -851,6 +855,65 @@ impl Server {
})
}

async fn inscription_recursive(
Extension(index): Extension<Arc<Index>>,
Path(inscription_id): Path<InscriptionId>,
) -> ServerResult<Response> {
lifofifoX marked this conversation as resolved.
Show resolved Hide resolved
task::block_in_place(|| {
let inscription = index
.get_inscription_by_id(inscription_id)?
.ok_or_not_found(|| format!("inscription {inscription_id}"))?;

let entry = index
.get_inscription_entry(inscription_id)
.unwrap()
.unwrap();

let satpoint = index
.get_inscription_satpoint_by_id(inscription_id)
.ok()
.flatten()
.unwrap();

let output = if satpoint.outpoint == unbound_outpoint() {
None
} else {
Some(
index
.get_transaction(satpoint.outpoint.txid)?
.ok_or_not_found(|| format!("inscription {inscription_id} current transaction"))?
.output
.into_iter()
.nth(satpoint.outpoint.vout.try_into().unwrap())
.ok_or_not_found(|| {
format!("inscription {inscription_id} current transaction output")
})?,
)
};

Ok(
Json(InscriptionRecursiveJson {
charms: Charm::ALL
.iter()
.filter(|charm| charm.is_set(entry.charms))
.map(|charm| charm.title().into())
.collect(),
content_type: inscription.content_type().map(|s| s.to_string()),
content_length: inscription.content_length(),
fee: entry.fee,
height: entry.height,
number: entry.inscription_number,
output: satpoint.outpoint,
value: output.as_ref().map(|o| o.value),
sat: entry.sat,
satpoint,
timestamp: timestamp(entry.timestamp).timestamp(),
})
.into_response(),
)
})
}

async fn status(
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
Expand Down
2 changes: 1 addition & 1 deletion src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) use {
home::HomeHtml,
iframe::Iframe,
input::InputHtml,
inscription::{InscriptionHtml, InscriptionJson},
inscription::{InscriptionHtml, InscriptionJson, InscriptionRecursiveJson},
inscriptions::{InscriptionsHtml, InscriptionsJson},
inscriptions_block::InscriptionsBlockHtml,
metadata::MetadataHtml,
Expand Down
15 changes: 15 additions & 0 deletions src/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ pub(crate) struct InscriptionHtml {
pub(crate) charms: u16,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct InscriptionRecursiveJson {
pub charms: Vec<String>,
pub content_type: Option<String>,
pub content_length: Option<usize>,
pub fee: u64,
pub height: u32,
pub number: i32,
pub output: OutPoint,
pub sat: Option<Sat>,
pub satpoint: SatPoint,
pub timestamp: i64,
pub value: Option<u64>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct InscriptionJson {
pub address: Option<String>,
Expand Down
5 changes: 3 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use {
subcommand::runes::RuneInfo,
templates::{
block::BlockJson, blocks::BlocksJson, inscription::InscriptionJson,
inscriptions::InscriptionsJson, output::OutputJson, rune::RuneJson, runes::RunesJson,
sat::SatJson, status::StatusJson, transaction::TransactionJson,
inscription::InscriptionRecursiveJson, inscriptions::InscriptionsJson, output::OutputJson,
rune::RuneJson, runes::RunesJson, sat::SatJson, status::StatusJson,
transaction::TransactionJson,
},
Edict, InscriptionId, Rune, RuneEntry, RuneId, Runestone, SatPoint,
},
Expand Down
51 changes: 51 additions & 0 deletions tests/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,57 @@ fn inscription_metadata() {
);
}

#[test]
fn recursive_inscription_endpoint() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();
let ord_rpc_server =
TestServer::spawn_with_server_args(&bitcoin_rpc_server, &["--index-sats"], &[]);

create_wallet(&bitcoin_rpc_server, &ord_rpc_server);

bitcoin_rpc_server.mine_blocks(1);

let output = CommandBuilder::new("wallet inscribe --fee-rate 1 --file foo.txt")
.write("foo.txt", "FOO")
.bitcoin_rpc_server(&bitcoin_rpc_server)
.ord_rpc_server(&ord_rpc_server)
.run_and_deserialize_output::<Inscribe>();

bitcoin_rpc_server.mine_blocks(1);

let inscription = output.inscriptions.first().unwrap();
let response = ord_rpc_server.request(format!("/r/inscription/{}", inscription.id));

assert_eq!(response.status(), StatusCode::OK);
assert_eq!(
response.headers().get("content-type").unwrap(),
"application/json"
);

let inscription_recursive_json: InscriptionRecursiveJson =
serde_json::from_str(&response.text().unwrap()).unwrap();

pretty_assert_eq!(
inscription_recursive_json,
InscriptionRecursiveJson {
charms: vec!["coin".into(), "uncommon".into()],
content_type: Some("text/plain;charset=utf-8".to_string()),
content_length: Some(3),
fee: 138,
height: 2,
number: 0,
output: inscription.location.outpoint,
sat: Some(ord::Sat(50 * COIN_VALUE)),
satpoint: SatPoint {
outpoint: inscription.location.outpoint,
offset: 0,
},
timestamp: 2,
value: Some(10000),
}
)
}

#[test]
fn inscriptions_page() {
let bitcoin_rpc_server = test_bitcoincore_rpc::spawn();
Expand Down
Loading