Skip to content

Commit

Permalink
Add JSON endpoints for Runes (#2941)
Browse files Browse the repository at this point in the history
  • Loading branch information
lugondev authored Jan 4, 2024
1 parent 09e1e19 commit 16aa7ef
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 89 deletions.
37 changes: 7 additions & 30 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use {
super::*,
crate::{
subcommand::{find::FindRangeOutput, server::InscriptionQuery},
templates::{RuneHtml, StatusHtml},
templates::StatusHtml,
},
bitcoin::block::Header,
bitcoincore_rpc::{json::GetBlockHeaderResult, Client},
Expand All @@ -30,7 +30,7 @@ use {
},
};

pub(crate) use self::entry::RuneEntry;
pub use self::entry::RuneEntry;

pub(crate) mod entry;
mod fetcher;
Expand Down Expand Up @@ -855,29 +855,10 @@ impl Index {
)
}

pub(crate) fn rune(&self, rune: Rune) -> Result<Option<(RuneId, RuneEntry)>> {
let rtx = self.database.begin_read()?;

let Some(id) = rtx
.open_table(RUNE_TO_RUNE_ID)?
.get(rune.0)?
.map(|guard| guard.value())
else {
return Ok(None);
};

let entry = RuneEntry::load(
rtx
.open_table(RUNE_ID_TO_RUNE_ENTRY)?
.get(id)?
.unwrap()
.value(),
);

Ok(Some((RuneId::load(id), entry)))
}

pub(crate) fn rune_html(&self, rune: Rune) -> Result<Option<RuneHtml>> {
pub(crate) fn rune(
&self,
rune: Rune,
) -> Result<Option<(RuneId, RuneEntry, Option<InscriptionId>)>> {
let rtx = self.database.begin_read()?;

let Some(id) = rtx
Expand Down Expand Up @@ -907,11 +888,7 @@ impl Index {
.is_some()
.then_some(parent);

Ok(Some(RuneHtml {
entry,
id: RuneId::load(id),
parent,
}))
Ok(Some((RuneId::load(id), entry, parent)))
}

pub(crate) fn runes(&self) -> Result<Vec<(RuneId, RuneEntry)>> {
Expand Down
30 changes: 15 additions & 15 deletions src/index/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ impl Entry for Header {
}
}

#[derive(Debug, PartialEq, Copy, Clone)]
pub(crate) struct RuneEntry {
pub(crate) burned: u128,
pub(crate) deadline: Option<u32>,
pub(crate) divisibility: u8,
pub(crate) end: Option<u32>,
pub(crate) etching: Txid,
pub(crate) limit: Option<u128>,
pub(crate) mints: u64,
pub(crate) number: u64,
pub(crate) rune: Rune,
pub(crate) spacers: u32,
pub(crate) supply: u128,
pub(crate) symbol: Option<char>,
pub(crate) timestamp: u32,
#[derive(Debug, PartialEq, Copy, Clone, Serialize, Deserialize)]
pub struct RuneEntry {
pub burned: u128,
pub deadline: Option<u32>,
pub divisibility: u8,
pub end: Option<u32>,
pub etching: Txid,
pub limit: Option<u128>,
pub mints: u64,
pub number: u64,
pub rune: Rune,
pub spacers: u32,
pub supply: u128,
pub symbol: Option<char>,
pub timestamp: u32,
}

pub(super) type RuneEntryValue = (
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use {
deserialize_from_str::DeserializeFromStr,
epoch::Epoch,
height::Height,
index::{List, RuneEntry},
index::List,
inscriptions::{media, teleburn, Charm, Media, ParsedEnvelope},
outgoing::Outgoing,
representation::Representation,
Expand Down Expand Up @@ -85,7 +85,7 @@ use {
pub use self::{
chain::Chain,
fee_rate::FeeRate,
index::Index,
index::{Index, RuneEntry},
inscriptions::{Envelope, Inscription, InscriptionId},
object::Object,
options::Options,
Expand Down
39 changes: 26 additions & 13 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use {
InscriptionsHtml, InscriptionsJson, OutputHtml, OutputJson, PageContent, PageHtml,
PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml,
PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml,
RangeHtml, RareTxt, RuneHtml, RunesHtml, SatHtml, SatInscriptionJson, SatInscriptionsJson,
SatJson, TransactionHtml,
RangeHtml, RareTxt, RuneHtml, RuneJson, RunesHtml, RunesJson, SatHtml, SatInscriptionJson,
SatInscriptionsJson, SatJson, TransactionHtml,
},
},
axum::{
Expand Down Expand Up @@ -617,31 +617,44 @@ impl Server {
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
Path(DeserializeFromStr(spaced_rune)): Path<DeserializeFromStr<SpacedRune>>,
) -> ServerResult<PageHtml<RuneHtml>> {
AcceptJson(accept_json): AcceptJson,
) -> ServerResult<Response> {
if !index.has_rune_index() {
return Err(ServerError::NotFound(
"this server has no rune index".to_string(),
));
}

Ok(
index
.rune_html(spaced_rune.rune)?
.ok_or_not_found(|| format!("rune {spaced_rune}"))?
.page(server_config),
)
let (id, entry, parent) = index
.rune(spaced_rune.rune)?
.ok_or_not_found(|| format!("rune {spaced_rune}"))?;

Ok(if accept_json {
Json(RuneJson { entry, id, parent }).into_response()
} else {
RuneHtml { entry, id, parent }
.page(server_config)
.into_response()
})
}

async fn runes(
Extension(server_config): Extension<Arc<ServerConfig>>,
Extension(index): Extension<Arc<Index>>,
) -> ServerResult<PageHtml<RunesHtml>> {
Ok(
AcceptJson(accept_json): AcceptJson,
) -> ServerResult<Response> {
Ok(if accept_json {
Json(RunesJson {
entries: index.runes()?,
})
.into_response()
} else {
RunesHtml {
entries: index.runes()?,
}
.page(server_config),
)
.page(server_config)
.into_response()
})
}

async fn home(
Expand Down
6 changes: 5 additions & 1 deletion src/subcommand/wallet/etch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) struct Etch {

#[derive(Serialize, Deserialize, Debug)]
pub struct Output {
pub rune: SpacedRune,
pub transaction: Txid,
}

Expand Down Expand Up @@ -123,6 +124,9 @@ impl Etch {

let transaction = client.send_raw_transaction(&signed_transaction)?;

Ok(Box::new(Output { transaction }))
Ok(Box::new(Output {
rune: self.rune,
transaction,
}))
}
}
2 changes: 1 addition & 1 deletion src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl Send {

Self::lock_non_cardinal_outputs(client, &inscriptions, &runic_outputs, unspent_outputs)?;

let (id, entry) = index
let (id, entry, _parent) = index
.rune(spaced_rune.rune)?
.with_context(|| format!("rune `{}` has not been etched", spaced_rune.rune))?;

Expand Down
8 changes: 4 additions & 4 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ pub(crate) use {
},
range::RangeHtml,
rare::RareTxt,
rune::RuneHtml,
runes::RunesHtml,
rune::{RuneHtml, RuneJson},
runes::{RunesHtml, RunesJson},
sat::{SatHtml, SatInscriptionJson, SatInscriptionsJson, SatJson},
server_config::ServerConfig,
status::StatusHtml,
Expand All @@ -44,8 +44,8 @@ pub mod output;
mod preview;
mod range;
mod rare;
mod rune;
mod runes;
pub mod rune;
pub mod runes;
pub mod sat;
pub mod status;
mod transaction;
Expand Down
12 changes: 7 additions & 5 deletions src/templates/rune.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::*;

#[derive(Boilerplate)]
pub(crate) struct RuneHtml {
pub(crate) entry: RuneEntry,
pub(crate) id: RuneId,
pub(crate) parent: Option<InscriptionId>,
pub type RuneJson = RuneHtml;

#[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)]
pub struct RuneHtml {
pub entry: RuneEntry,
pub id: RuneId,
pub parent: Option<InscriptionId>,
}

impl PageContent for RuneHtml {
Expand Down
8 changes: 5 additions & 3 deletions src/templates/runes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::*;

#[derive(Boilerplate)]
pub(crate) struct RunesHtml {
pub(crate) entries: Vec<(RuneId, RuneEntry)>,
pub type RunesJson = RunesHtml;

#[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)]
pub struct RunesHtml {
pub entries: Vec<(RuneId, RuneEntry)>,
}

impl PageContent for RunesHtml {
Expand Down
Loading

0 comments on commit 16aa7ef

Please sign in to comment.