Skip to content

Commit

Permalink
feat(ordzaar): methods and apis (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
joundy authored Mar 1, 2024
1 parent 0ae58b8 commit 769bdbb
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ mod index;
mod inscriptions;
mod object;
mod options;
mod ordzaar;
mod outgoing;
pub mod rarity;
mod representation;
Expand Down
42 changes: 42 additions & 0 deletions src/ordzaar/inscriptions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use super::*;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct InscriptionData {
pub inscription_id: InscriptionId,
pub number: i32,
pub sequence: u32,
pub genesis_height: u32,
pub genesis_fee: u64,
pub sat: Option<Sat>,
pub satpoint: SatPoint,
pub timestamp: i64,
}

impl InscriptionData {
pub fn new(
genesis_fee: u64,
genesis_height: u32,
inscription_id: InscriptionId,
number: i32,
sequence: u32,
sat: Option<Sat>,
satpoint: SatPoint,
timestamp: DateTime<Utc>,
) -> Self {
Self {
inscription_id,
number,
sequence,
genesis_height,
genesis_fee,
sat,
satpoint,
timestamp: timestamp.timestamp(),
}
}
}

#[derive(Deserialize)]
pub struct InscriptionIds {
pub ids: Vec<InscriptionId>,
}
4 changes: 4 additions & 0 deletions src/ordzaar/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use super::*;

pub mod inscriptions;
pub mod ordinals;
92 changes: 92 additions & 0 deletions src/ordzaar/ordinals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use super::*;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct OrdinalJson {
pub number: u64,
pub decimal: String,
pub degree: String,
pub name: String,
pub height: u32,
pub cycle: u32,
pub epoch: u32,
pub period: u32,
pub offset: u64,
pub rarity: Rarity,
pub output: OutPoint,
pub start: u64,
pub end: u64,
pub size: u64,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub output: OutPoint,
pub start: u64,
pub end: u64,
pub size: u64,
pub offset: u64,
pub rarity: Rarity,
pub name: String,
}

pub fn get_ordinals(index: &Index, outpoint: OutPoint) -> Result<Vec<OrdinalJson>> {
match index.list(outpoint)? {
Some(crate::index::List::Unspent(ranges)) => {
let mut ordinals = Vec::new();
for Output {
output,
start,
end,
size,
offset,
rarity,
name,
} in list(outpoint, ranges)
{
let sat = Sat(start);
ordinals.push(OrdinalJson {
number: sat.n(),
decimal: sat.decimal().to_string(),
degree: sat.degree().to_string(),
name,
height: sat.height().0,
cycle: sat.cycle(),
epoch: sat.epoch().0,
period: sat.period(),
offset,
rarity,
output,
start,
end,
size,
});
}
Ok(ordinals)
}
Some(crate::index::List::Spent) => Ok(Vec::new()),
None => Ok(Vec::new()),
}
}

fn list(outpoint: OutPoint, ranges: Vec<(u64, u64)>) -> Vec<Output> {
let mut offset = 0;
ranges
.into_iter()
.map(|(start, end)| {
let size = end - start;
let output = Output {
output: outpoint,
start,
end,
size,
offset,
name: Sat(start).name(),
rarity: Sat(start).rarity(),
};

offset += size;

output
})
.collect()
}
58 changes: 57 additions & 1 deletion src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use {
},
super::*,
crate::{
ordzaar::inscriptions::{InscriptionData, InscriptionIds},
ordzaar::ordinals::get_ordinals,
server_config::ServerConfig,
templates::{
BlockHtml, BlockJson, BlocksHtml, ChildrenHtml, ChildrenJson, ClockSvg, CollectionsHtml,
Expand All @@ -24,7 +26,7 @@ use {
headers::UserAgent,
http::{header, HeaderMap, HeaderValue, StatusCode, Uri},
response::{IntoResponse, Redirect, Response},
routing::get,
routing::{get, post},
Router, TypedHeader,
},
axum_server::Handle,
Expand Down Expand Up @@ -274,6 +276,10 @@ impl Server {
.route("/static/*path", get(Self::static_asset))
.route("/status", get(Self::status))
.route("/tx/:txid", get(Self::transaction))
// ---- Ordzaar routes ----
.route("/inscriptions", post(Self::ordzaar_inscriptions_from_ids))
.route("/ordinals/:outpoint", get(Self::ordzaar_ordinals_from_outpoint))
// ---- Ordzaar routes ----
.layer(Extension(index))
.layer(Extension(server_config.clone()))
.layer(Extension(config))
Expand Down Expand Up @@ -338,6 +344,52 @@ impl Server {
})
}

// ---- Ordzaar methods ----
async fn ordzaar_ordinals_from_outpoint(
Extension(index): Extension<Arc<Index>>,
Path(outpoint): Path<OutPoint>,
) -> ServerResult<Response> {
index
.get_transaction(outpoint.txid)?
.ok_or_not_found(|| format!("output {outpoint}"))?
.output
.into_iter()
.nth(outpoint.vout as usize)
.ok_or_not_found(|| format!("output {outpoint}"))?;
Ok(Json(get_ordinals(&index, outpoint)?).into_response())
}

async fn ordzaar_inscriptions_from_ids(
Extension(index): Extension<Arc<Index>>,
Json(payload): Json<InscriptionIds>,
) -> ServerResult<Response> {
let mut inscriptions: Vec<InscriptionData> = Vec::new();
for id in payload.ids {
let entry = match index.get_inscription_entry(id)? {
Some(entry) => entry,
None => continue,
};

let satpoint = match index.get_inscription_satpoint_by_id(id)? {
Some(satpoint) => satpoint,
None => continue,
};

inscriptions.push(InscriptionData::new(
entry.fee,
entry.height,
id,
entry.inscription_number,
entry.sequence_number,
entry.sat,
satpoint,
timestamp(entry.timestamp),
));
}
Ok(Json(inscriptions).into_response())
}
// ---- Ordzaar methods ----

fn spawn(
&self,
router: Router,
Expand Down Expand Up @@ -1315,6 +1367,10 @@ impl Server {
previous: info.previous,
next: info.next,
rune: info.rune,

// ---- Ordzaar ----
inscription_sequence: info.entry.sequence_number,
// ---- Ordzaar ----
})
.into_response()
} else {
Expand Down
4 changes: 4 additions & 0 deletions src/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ pub struct InscriptionJson {
pub sat: Option<Sat>,
pub satpoint: SatPoint,
pub timestamp: i64,

// ---- Ordzaar ----
pub inscription_sequence: u32
// ---- Ordzaar ----
}

impl PageContent for InscriptionHtml {
Expand Down

0 comments on commit 769bdbb

Please sign in to comment.