diff --git a/src/api.rs b/src/api.rs new file mode 100644 index 0000000000..e1db4f0b4a --- /dev/null +++ b/src/api.rs @@ -0,0 +1,175 @@ +use super::{ + target_as_block_hash, BlockHash, Chain, Deserialize, Height, InscriptionId, OutPoint, Pile, + Rarity, SatPoint, Serialize, SpacedRune, TxMerkleNode, TxOut, +}; + +pub use crate::templates::{ + BlocksHtml as Blocks, RuneHtml as Rune, RunesHtml as Runes, StatusHtml as Status, + TransactionHtml as Transaction, +}; + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct Block { + pub hash: BlockHash, + pub target: BlockHash, + pub best_height: u32, + pub height: u32, + pub inscriptions: Vec, +} + +impl Block { + pub(crate) fn new( + block: bitcoin::Block, + height: Height, + best_height: Height, + inscriptions: Vec, + ) -> Self { + Self { + hash: block.header.block_hash(), + target: target_as_block_hash(block.header.target()), + height: height.0, + best_height: best_height.0, + inscriptions, + } + } +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct BlockInfo { + pub bits: u32, + pub chainwork: u128, + pub confirmations: i32, + pub difficulty: f64, + pub hash: BlockHash, + pub height: u32, + pub median_time: Option, + pub merkle_root: TxMerkleNode, + pub next_block: Option, + pub nonce: u32, + pub previous_block: Option, + pub target: BlockHash, + pub timestamp: u64, + pub transaction_count: u64, + pub version: u32, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct Children { + pub ids: Vec, + pub more: bool, + pub page: usize, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] +pub struct Inscription { + pub address: Option, + pub charms: Vec, + pub children: Vec, + pub content_length: Option, + pub content_type: Option, + pub genesis_fee: u64, + pub genesis_height: u32, + pub inscription_id: InscriptionId, + pub inscription_number: i32, + pub next: Option, + pub output_value: Option, + pub parent: Option, + pub previous: Option, + pub rune: Option, + pub sat: Option, + pub satpoint: SatPoint, + pub timestamp: i64, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct InscriptionRecursive { + pub charms: Vec, + pub content_type: Option, + pub content_length: Option, + pub fee: u64, + pub height: u32, + pub number: i32, + pub output: OutPoint, + pub sat: Option, + pub satpoint: SatPoint, + pub timestamp: i64, + pub value: Option, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct Inscriptions { + pub inscriptions: Vec, + pub more: bool, + pub page_index: u32, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct Output { + pub address: Option, + pub indexed: bool, + pub inscriptions: Vec, + pub runes: Vec<(SpacedRune, Pile)>, + pub sat_ranges: Option>, + pub script_pubkey: String, + pub spent: bool, + pub transaction: String, + pub value: u64, +} + +impl Output { + pub fn new( + chain: Chain, + inscriptions: Vec, + outpoint: OutPoint, + output: TxOut, + indexed: bool, + runes: Vec<(SpacedRune, Pile)>, + sat_ranges: Option>, + spent: bool, + ) -> Self { + Self { + address: chain + .address_from_script(&output.script_pubkey) + .ok() + .map(|address| address.to_string()), + indexed, + inscriptions, + runes, + sat_ranges, + script_pubkey: output.script_pubkey.to_asm_string(), + spent, + transaction: outpoint.txid.to_string(), + value: output.value, + } + } +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct Sat { + pub number: u64, + pub decimal: String, + pub degree: String, + pub name: String, + pub block: u32, + pub cycle: u32, + pub epoch: u32, + pub period: u32, + pub offset: u64, + pub rarity: Rarity, + pub percentile: String, + pub satpoint: Option, + pub timestamp: i64, + pub inscriptions: Vec, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct SatInscription { + pub id: Option, +} + +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub struct SatInscriptions { + pub ids: Vec, + pub more: bool, + pub page: u64, +} diff --git a/src/lib.rs b/src/lib.rs index 4b09ba1832..ba7410dd55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,6 @@ use { time::{Duration, Instant, SystemTime}, }, sysinfo::System, - templates::{InscriptionJson, OutputJson, RuneJson, StatusJson}, tokio::{runtime::Runtime, task}, }; @@ -103,6 +102,7 @@ macro_rules! tprintln { }; } +pub mod api; pub mod arguments; mod blocktime; pub mod chain; diff --git a/src/subcommand/server.rs b/src/subcommand/server.rs index 0461c37bae..81eb198fda 100644 --- a/src/subcommand/server.rs +++ b/src/subcommand/server.rs @@ -8,14 +8,11 @@ use { crate::{ server_config::ServerConfig, templates::{ - BlockHtml, BlockInfoJson, BlockJson, BlocksHtml, BlocksJson, ChildrenHtml, ChildrenJson, - ClockSvg, CollectionsHtml, HomeHtml, InputHtml, InscriptionHtml, InscriptionJson, - 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, + BlockHtml, BlocksHtml, ChildrenHtml, ClockSvg, CollectionsHtml, HomeHtml, InputHtml, + InscriptionHtml, InscriptionsBlockHtml, InscriptionsHtml, OutputHtml, PageContent, PageHtml, + PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml, + PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml, + RangeHtml, RareTxt, RuneBalancesHtml, RuneHtml, RunesHtml, SatHtml, TransactionHtml, }, }, axum::{ @@ -539,7 +536,7 @@ impl Server { }); let blocktime = index.block_time(sat.height())?; Ok(if accept_json { - Json(SatJson { + Json(api::Sat { number: sat.0, decimal: sat.decimal().to_string(), degree: sat.degree().to_string(), @@ -618,7 +615,7 @@ impl Server { let spent = index.is_output_spent(outpoint)?; Ok(if accept_json { - Json(OutputJson::new( + Json(api::Output::new( server_config.chain, inscriptions, outpoint, @@ -683,7 +680,7 @@ impl Server { .ok_or_not_found(|| format!("rune {spaced_rune}"))?; Ok(if accept_json { - Json(RuneJson { entry, id, parent }).into_response() + Json(api::Rune { entry, id, parent }).into_response() } else { RuneHtml { entry, id, parent } .page(server_config) @@ -699,7 +696,7 @@ impl Server { ) -> ServerResult { task::block_in_place(|| { Ok(if accept_json { - Json(RunesJson { + Json(api::Runes { entries: index.runes()?, }) .into_response() @@ -760,7 +757,7 @@ impl Server { } Ok(if accept_json { - Json(BlocksJson::new(blocks, featured_blocks)).into_response() + Json(api::Blocks::new(blocks, featured_blocks)).into_response() } else { BlocksHtml::new(blocks, featured_blocks) .page(server_config) @@ -803,7 +800,7 @@ impl Server { Ok(if accept_json { let inscriptions = index.get_inscriptions_in_block(height)?; - Json(BlockJson::new( + Json(api::Block::new( block, Height(height), Self::index_height(&index)?, @@ -840,7 +837,7 @@ impl Server { let inscription_count = index.inscription_count(txid)?; Ok(if accept_json { - Json(TransactionJson { + Json(api::Transaction { chain: server_config.chain, etching: index.get_etching(txid)?, inscription_count, @@ -914,7 +911,7 @@ impl Server { }; Ok( - Json(InscriptionRecursiveJson { + Json(api::InscriptionRecursive { charms: Charm::ALL .iter() .filter(|charm| charm.is_set(entry.charms)) @@ -1143,7 +1140,7 @@ impl Server { async fn block_info( Extension(index): Extension>, Path(DeserializeFromStr(query)): Path>, - ) -> ServerResult> { + ) -> ServerResult> { task::block_in_place(|| { let hash = match query { BlockQuery::Hash(hash) => hash, @@ -1160,7 +1157,7 @@ impl Server { .block_header(hash)? .ok_or_not_found(|| format!("block {hash}"))?; - Ok(Json(BlockInfoJson { + Ok(Json(api::BlockInfo { bits: header.bits.to_consensus(), chainwork: chainwork(&info.chainwork), confirmations: info.confirmations, @@ -1443,7 +1440,7 @@ impl Server { .ok_or_not_found(|| format!("inscription {query}"))?; Ok(if accept_json { - Json(InscriptionJson { + Json(api::Inscription { inscription_id: info.entry.id, charms: Charm::ALL .iter() @@ -1598,7 +1595,7 @@ impl Server { let (ids, more) = index.get_children_by_sequence_number_paginated(parent_sequence_number, 100, page)?; - Ok(Json(ChildrenJson { ids, more, page }).into_response()) + Ok(Json(api::Children { ids, more, page }).into_response()) }) } @@ -1630,7 +1627,7 @@ impl Server { let next = more.then_some(page_index + 1); Ok(if accept_json { - Json(InscriptionsJson { + Json(api::Inscriptions { inscriptions, page_index, more, @@ -1689,7 +1686,7 @@ impl Server { } Ok(if accept_json { - Json(InscriptionsJson { + Json(api::Inscriptions { inscriptions, page_index, more, @@ -1712,14 +1709,14 @@ impl Server { async fn sat_inscriptions( Extension(index): Extension>, Path(sat): Path, - ) -> ServerResult> { + ) -> ServerResult> { Self::sat_inscriptions_paginated(Extension(index), Path((sat, 0))).await } async fn sat_inscriptions_paginated( Extension(index): Extension>, Path((sat, page)): Path<(u64, u64)>, - ) -> ServerResult> { + ) -> ServerResult> { task::block_in_place(|| { if !index.has_sat_index() { return Err(ServerError::NotFound( @@ -1729,14 +1726,14 @@ impl Server { let (ids, more) = index.get_inscription_ids_by_sat_paginated(Sat(sat), 100, page)?; - Ok(Json(SatInscriptionsJson { ids, more, page })) + Ok(Json(api::SatInscriptions { ids, more, page })) }) } async fn sat_inscription_at_index( Extension(index): Extension>, Path((DeserializeFromStr(sat), inscription_index)): Path<(DeserializeFromStr, isize)>, - ) -> ServerResult> { + ) -> ServerResult> { task::block_in_place(|| { if !index.has_sat_index() { return Err(ServerError::NotFound( @@ -1746,7 +1743,7 @@ impl Server { let id = index.get_inscription_id_by_sat_indexed(sat, inscription_index)?; - Ok(Json(SatInscriptionJson { id })) + Ok(Json(api::SatInscription { id })) }) } @@ -2764,8 +2761,8 @@ mod tests { ); assert_eq!( - server.get_json::(format!("/output/{output}")), - OutputJson { + server.get_json::(format!("/output/{output}")), + api::Output { value: 5000000000, script_pubkey: String::new(), address: None, @@ -4409,14 +4406,14 @@ next assert_eq!( server - .get_json::(format!("/inscription/{inscription_id}")) + .get_json::(format!("/inscription/{inscription_id}")) .parent, Some(parent_inscription_id), ); assert_eq!( server - .get_json::(format!("/inscription/{parent_inscription_id}")) + .get_json::(format!("/inscription/{parent_inscription_id}")) .children, [inscription_id], ); @@ -5087,8 +5084,8 @@ next let server = TestServer::new_with_regtest_with_index_sats(); assert_eq!( - server.get_json::("/r/sat/5000000000"), - SatInscriptionsJson { + server.get_json::("/r/sat/5000000000"), + api::SatInscriptions { ids: vec![], page: 0, more: false @@ -5096,8 +5093,8 @@ next ); assert_eq!( - server.get_json::("/r/sat/5000000000/at/0"), - SatInscriptionJson { id: None } + server.get_json::("/r/sat/5000000000/at/0"), + api::SatInscription { id: None } ); server.mine_blocks(1); @@ -5123,10 +5120,10 @@ next ids.push(InscriptionId { txid, index: 0 }); } - let paginated_response = server.get_json::("/r/sat/5000000000"); + let paginated_response = server.get_json::("/r/sat/5000000000"); let equivalent_paginated_response = - server.get_json::("/r/sat/5000000000/0"); + server.get_json::("/r/sat/5000000000/0"); assert_eq!(paginated_response.ids.len(), 100); assert!(paginated_response.more); @@ -5139,7 +5136,7 @@ next assert_eq!(paginated_response.more, equivalent_paginated_response.more); assert_eq!(paginated_response.page, equivalent_paginated_response.page); - let paginated_response = server.get_json::("/r/sat/5000000000/1"); + let paginated_response = server.get_json::("/r/sat/5000000000/1"); assert_eq!(paginated_response.ids.len(), 11); assert!(!paginated_response.more); @@ -5147,34 +5144,34 @@ next assert_eq!( server - .get_json::("/r/sat/5000000000/at/0") + .get_json::("/r/sat/5000000000/at/0") .id, Some(ids[0]) ); assert_eq!( server - .get_json::("/r/sat/5000000000/at/-111") + .get_json::("/r/sat/5000000000/at/-111") .id, Some(ids[0]) ); assert_eq!( server - .get_json::("/r/sat/5000000000/at/110") + .get_json::("/r/sat/5000000000/at/110") .id, Some(ids[110]) ); assert_eq!( server - .get_json::("/r/sat/5000000000/at/-1") + .get_json::("/r/sat/5000000000/at/-1") .id, Some(ids[110]) ); assert!(server - .get_json::("/r/sat/5000000000/at/111") + .get_json::("/r/sat/5000000000/at/111") .id .is_none()); } @@ -5203,7 +5200,7 @@ next server.mine_blocks(1); let children_json = - server.get_json::(format!("/r/children/{parent_inscription_id}")); + server.get_json::(format!("/r/children/{parent_inscription_id}")); assert_eq!(children_json.ids.len(), 0); let mut builder = script::Builder::new(); @@ -5233,7 +5230,7 @@ next let hundred_eleventh_child_inscription_id = InscriptionId { txid, index: 110 }; let children_json = - server.get_json::(format!("/r/children/{parent_inscription_id}")); + server.get_json::(format!("/r/children/{parent_inscription_id}")); assert_eq!(children_json.ids.len(), 100); assert_eq!(children_json.ids[0], first_child_inscription_id); @@ -5242,7 +5239,7 @@ next assert_eq!(children_json.page, 0); let children_json = - server.get_json::(format!("/r/children/{parent_inscription_id}/1")); + server.get_json::(format!("/r/children/{parent_inscription_id}/1")); assert_eq!(children_json.ids.len(), 11); assert_eq!(children_json.ids[0], hundred_first_child_inscription_id); @@ -5372,8 +5369,8 @@ next let server = TestServer::new(); pretty_assert_eq!( - server.get_json::("/r/blockinfo/0"), - BlockInfoJson { + server.get_json::("/r/blockinfo/0"), + api::BlockInfo { bits: 486604799, chainwork: 0, confirmations: 0, @@ -5399,8 +5396,8 @@ next server.mine_blocks(1); pretty_assert_eq!( - server.get_json::("/r/blockinfo/1"), - BlockInfoJson { + server.get_json::("/r/blockinfo/1"), + api::BlockInfo { bits: 0, chainwork: 0, confirmations: 0, diff --git a/src/templates.rs b/src/templates.rs index 2dedc4bb12..e0474c34a3 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -1,32 +1,32 @@ use {super::*, boilerplate::Boilerplate}; pub(crate) use { - block::{BlockHtml, BlockInfoJson, BlockJson}, - blocks::{BlocksHtml, BlocksJson}, - children::{ChildrenHtml, ChildrenJson}, + block::BlockHtml, + children::ChildrenHtml, clock::ClockSvg, collections::CollectionsHtml, home::HomeHtml, iframe::Iframe, input::InputHtml, - inscription::{InscriptionHtml, InscriptionJson, InscriptionRecursiveJson}, - inscriptions::{InscriptionsHtml, InscriptionsJson}, + inscription::InscriptionHtml, + inscriptions::InscriptionsHtml, inscriptions_block::InscriptionsBlockHtml, metadata::MetadataHtml, - output::{OutputHtml, OutputJson}, + output::OutputHtml, preview::{ PreviewAudioHtml, PreviewCodeHtml, PreviewFontHtml, PreviewImageHtml, PreviewMarkdownHtml, PreviewModelHtml, PreviewPdfHtml, PreviewTextHtml, PreviewUnknownHtml, PreviewVideoHtml, }, range::RangeHtml, rare::RareTxt, - rune::{RuneHtml, RuneJson}, rune_balances::RuneBalancesHtml, - runes::{RunesHtml, RunesJson}, - sat::{SatHtml, SatInscriptionJson, SatInscriptionsJson, SatJson}, + sat::SatHtml, server_config::ServerConfig, - status::{StatusHtml, StatusJson}, - transaction::{TransactionHtml, TransactionJson}, +}; + +pub use { + blocks::BlocksHtml, rune::RuneHtml, runes::RunesHtml, status::StatusHtml, + transaction::TransactionHtml, }; pub mod block; diff --git a/src/templates/block.rs b/src/templates/block.rs index fb4bfba846..f5d1294e74 100644 --- a/src/templates/block.rs +++ b/src/templates/block.rs @@ -31,51 +31,6 @@ impl BlockHtml { } } -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct BlockJson { - pub hash: BlockHash, - pub target: BlockHash, - pub best_height: u32, - pub height: u32, - pub inscriptions: Vec, -} - -impl BlockJson { - pub(crate) fn new( - block: Block, - height: Height, - best_height: Height, - inscriptions: Vec, - ) -> Self { - Self { - hash: block.header.block_hash(), - target: target_as_block_hash(block.header.target()), - height: height.0, - best_height: best_height.0, - inscriptions, - } - } -} - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct BlockInfoJson { - pub bits: u32, - pub chainwork: u128, - pub confirmations: i32, - pub difficulty: f64, - pub hash: BlockHash, - pub height: u32, - pub median_time: Option, - pub merkle_root: TxMerkleNode, - pub next_block: Option, - pub nonce: u32, - pub previous_block: Option, - pub target: BlockHash, - pub timestamp: u64, - pub transaction_count: u64, - pub version: u32, -} - impl PageContent for BlockHtml { fn title(&self) -> String { format!("Block {}", self.height) diff --git a/src/templates/blocks.rs b/src/templates/blocks.rs index 6a63b9e2d8..8bf1747d8f 100644 --- a/src/templates/blocks.rs +++ b/src/templates/blocks.rs @@ -1,7 +1,5 @@ use super::*; -pub type BlocksJson = BlocksHtml; - #[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)] pub struct BlocksHtml { pub last: u32, diff --git a/src/templates/children.rs b/src/templates/children.rs index d495abdcea..ac3621d57a 100644 --- a/src/templates/children.rs +++ b/src/templates/children.rs @@ -9,13 +9,6 @@ pub(crate) struct ChildrenHtml { pub(crate) next_page: Option, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct ChildrenJson { - pub ids: Vec, - pub more: bool, - pub page: usize, -} - impl PageContent for ChildrenHtml { fn title(&self) -> String { format!("Inscription {} Children", self.parent_number) diff --git a/src/templates/inscription.rs b/src/templates/inscription.rs index b23d5dd890..229ea90472 100644 --- a/src/templates/inscription.rs +++ b/src/templates/inscription.rs @@ -20,42 +20,6 @@ pub(crate) struct InscriptionHtml { pub(crate) charms: u16, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct InscriptionRecursiveJson { - pub charms: Vec, - pub content_type: Option, - pub content_length: Option, - pub fee: u64, - pub height: u32, - pub number: i32, - pub output: OutPoint, - pub sat: Option, - pub satpoint: SatPoint, - pub timestamp: i64, - pub value: Option, -} - -#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] -pub struct InscriptionJson { - pub address: Option, - pub charms: Vec, - pub children: Vec, - pub content_length: Option, - pub content_type: Option, - pub genesis_fee: u64, - pub genesis_height: u32, - pub inscription_id: InscriptionId, - pub inscription_number: i32, - pub next: Option, - pub output_value: Option, - pub parent: Option, - pub previous: Option, - pub rune: Option, - pub sat: Option, - pub satpoint: SatPoint, - pub timestamp: i64, -} - impl PageContent for InscriptionHtml { fn title(&self) -> String { format!("Inscription {}", self.inscription_number) diff --git a/src/templates/inscriptions.rs b/src/templates/inscriptions.rs index 5d0d60065a..bed238b916 100644 --- a/src/templates/inscriptions.rs +++ b/src/templates/inscriptions.rs @@ -7,13 +7,6 @@ pub(crate) struct InscriptionsHtml { pub(crate) next: Option, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct InscriptionsJson { - pub inscriptions: Vec, - pub more: bool, - pub page_index: u32, -} - impl PageContent for InscriptionsHtml { fn title(&self) -> String { "Inscriptions".into() diff --git a/src/templates/output.rs b/src/templates/output.rs index 58fa66cfac..a929d4b83d 100644 --- a/src/templates/output.rs +++ b/src/templates/output.rs @@ -11,47 +11,6 @@ pub(crate) struct OutputHtml { pub(crate) spent: bool, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct OutputJson { - pub address: Option, - pub indexed: bool, - pub inscriptions: Vec, - pub runes: Vec<(SpacedRune, Pile)>, - pub sat_ranges: Option>, - pub script_pubkey: String, - pub spent: bool, - pub transaction: String, - pub value: u64, -} - -impl OutputJson { - pub fn new( - chain: Chain, - inscriptions: Vec, - outpoint: OutPoint, - output: TxOut, - indexed: bool, - runes: Vec<(SpacedRune, Pile)>, - sat_ranges: Option>, - spent: bool, - ) -> Self { - Self { - address: chain - .address_from_script(&output.script_pubkey) - .ok() - .map(|address| address.to_string()), - indexed, - inscriptions, - runes, - sat_ranges, - script_pubkey: output.script_pubkey.to_asm_string(), - spent, - transaction: outpoint.txid.to_string(), - value: output.value, - } - } -} - impl PageContent for OutputHtml { fn title(&self) -> String { format!("Output {}", self.outpoint) diff --git a/src/templates/rune.rs b/src/templates/rune.rs index 83651b4045..74729479ec 100644 --- a/src/templates/rune.rs +++ b/src/templates/rune.rs @@ -1,7 +1,5 @@ use super::*; -pub type RuneJson = RuneHtml; - #[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)] pub struct RuneHtml { pub entry: RuneEntry, diff --git a/src/templates/runes.rs b/src/templates/runes.rs index ace35fca32..c8e9951e1a 100644 --- a/src/templates/runes.rs +++ b/src/templates/runes.rs @@ -1,7 +1,5 @@ use super::*; -pub type RunesJson = RunesHtml; - #[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)] pub struct RunesHtml { pub entries: Vec<(RuneId, RuneEntry)>, diff --git a/src/templates/sat.rs b/src/templates/sat.rs index 6567e9cbfd..fd1c5bab7d 100644 --- a/src/templates/sat.rs +++ b/src/templates/sat.rs @@ -8,36 +8,6 @@ pub(crate) struct SatHtml { pub(crate) inscriptions: Vec, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct SatJson { - pub number: u64, - pub decimal: String, - pub degree: String, - pub name: String, - pub block: u32, - pub cycle: u32, - pub epoch: u32, - pub period: u32, - pub offset: u64, - pub rarity: Rarity, - pub percentile: String, - pub satpoint: Option, - pub timestamp: i64, - pub inscriptions: Vec, -} - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct SatInscriptionsJson { - pub ids: Vec, - pub more: bool, - pub page: u64, -} - -#[derive(Debug, PartialEq, Serialize, Deserialize)] -pub struct SatInscriptionJson { - pub id: Option, -} - impl PageContent for SatHtml { fn title(&self) -> String { format!("Sat {}", self.sat) diff --git a/src/templates/status.rs b/src/templates/status.rs index b36fda057e..b96cf26241 100644 --- a/src/templates/status.rs +++ b/src/templates/status.rs @@ -1,7 +1,5 @@ use super::*; -pub type StatusJson = StatusHtml; - #[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)] pub struct StatusHtml { pub blessed_inscriptions: u64, diff --git a/src/templates/transaction.rs b/src/templates/transaction.rs index a2f741bec3..b62e6efafd 100644 --- a/src/templates/transaction.rs +++ b/src/templates/transaction.rs @@ -1,7 +1,5 @@ use super::*; -pub type TransactionJson = TransactionHtml; - #[derive(Boilerplate, Debug, PartialEq, Serialize, Deserialize)] pub struct TransactionHtml { pub chain: Chain, diff --git a/src/wallet.rs b/src/wallet.rs index f4696f2e12..626fc604e1 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -47,9 +47,9 @@ pub(crate) struct Wallet { has_rune_index: bool, utxos: BTreeMap, locked_utxos: BTreeMap, - output_info: BTreeMap, + output_info: BTreeMap, inscriptions: BTreeMap>, - inscription_info: BTreeMap, + inscription_info: BTreeMap, } impl Wallet { @@ -181,14 +181,14 @@ impl Wallet { }) } - async fn get_output(ord_client: &OrdClient, output: OutPoint) -> Result { + async fn get_output(ord_client: &OrdClient, output: OutPoint) -> Result { let response = ord_client.get(&format!("/output/{output}")).await?; if !response.status().is_success() { bail!("wallet failed get output: {}", response.text().await?); } - let output_json: OutputJson = serde_json::from_str(&response.text().await?)?; + let output_json: api::Output = serde_json::from_str(&response.text().await?)?; if !output_json.indexed { bail!("output in wallet but not in ord server: {output}"); @@ -245,7 +245,7 @@ impl Wallet { async fn get_inscription_info( ord_client: &OrdClient, inscription_id: InscriptionId, - ) -> Result { + ) -> Result { let response = ord_client .get(&format!("/inscription/{inscription_id}")) .await?; @@ -257,7 +257,7 @@ impl Wallet { Ok(serde_json::from_str(&response.text().await?)?) } - async fn get_server_status(ord_client: &OrdClient) -> Result { + async fn get_server_status(ord_client: &OrdClient) -> Result { let response = ord_client.get("/status").await?; if !response.status().is_success() { @@ -329,7 +329,7 @@ impl Wallet { &self.inscriptions } - pub(crate) fn inscription_info(&self) -> BTreeMap { + pub(crate) fn inscription_info(&self) -> BTreeMap { self.inscription_info.clone() } @@ -440,7 +440,7 @@ impl Wallet { return Ok(None); } - let rune_json: RuneJson = serde_json::from_str(&response.text()?)?; + let rune_json: api::Rune = serde_json::from_str(&response.text()?)?; Ok(Some((rune_json.id, rune_json.entry, rune_json.parent))) } diff --git a/tests/json_api.rs b/tests/json_api.rs index d95f624932..6d514b4eaf 100644 --- a/tests/json_api.rs +++ b/tests/json_api.rs @@ -9,14 +9,14 @@ fn get_sat_without_sat_index() { assert_eq!(response.status(), StatusCode::OK); - let mut sat_json: SatJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let mut sat_json: api::Sat = serde_json::from_str(&response.text().unwrap()).unwrap(); // this is a hack to ignore the timestamp, since it changes for every request sat_json.timestamp = 0; pretty_assert_eq!( sat_json, - SatJson { + api::Sat { number: 2099999997689999, decimal: "6929999.0".into(), degree: "5°209999′1007″0‴".into(), @@ -50,11 +50,11 @@ fn get_sat_with_inscription_and_sat_index() { assert_eq!(response.status(), StatusCode::OK); - let sat_json: SatJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let sat_json: api::Sat = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( sat_json, - SatJson { + api::Sat { number: 50 * COIN_VALUE, decimal: "1.0".into(), degree: "0°1′1″0‴".into(), @@ -106,11 +106,11 @@ fn get_sat_with_inscription_on_common_sat_and_more_inscriptions() { assert_eq!(response.status(), StatusCode::OK); - let sat_json: SatJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let sat_json: api::Sat = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( sat_json, - SatJson { + api::Sat { number: 3 * 50 * COIN_VALUE + 1, decimal: "3.1".into(), degree: "0°3′3″1‴".into(), @@ -144,14 +144,14 @@ fn get_inscription() { assert_eq!(response.status(), StatusCode::OK); - let mut inscription_json: InscriptionJson = + let mut inscription_json: api::Inscription = serde_json::from_str(&response.text().unwrap()).unwrap(); assert_regex_match!(inscription_json.address.unwrap(), r"bc1p.*"); inscription_json.address = None; pretty_assert_eq!( inscription_json, - InscriptionJson { + api::Inscription { address: None, charms: vec!["coin".into(), "uncommon".into()], children: Vec::new(), @@ -210,7 +210,7 @@ fn get_inscriptions() { let response = ord_rpc_server.json_request("/inscriptions"); assert_eq!(response.status(), StatusCode::OK); - let inscriptions_json: InscriptionsJson = + let inscriptions_json: api::Inscriptions = serde_json::from_str(&response.text().unwrap()).unwrap(); assert_eq!(inscriptions_json.inscriptions.len(), 100); @@ -219,7 +219,7 @@ fn get_inscriptions() { let response = ord_rpc_server.json_request("/inscriptions/1"); assert_eq!(response.status(), StatusCode::OK); - let inscriptions_json: InscriptionsJson = + let inscriptions_json: api::Inscriptions = serde_json::from_str(&response.text().unwrap()).unwrap(); assert_eq!(inscriptions_json.inscriptions.len(), 50); @@ -272,7 +272,7 @@ fn get_inscriptions_in_block() { let response = ord_rpc_server.json_request(format!("/inscriptions/block/{}", 11)); assert_eq!(response.status(), StatusCode::OK); - let inscriptions_json: InscriptionsJson = + let inscriptions_json: api::Inscriptions = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( @@ -318,7 +318,7 @@ fn get_output() { assert_eq!(response.status(), StatusCode::OK); assert!( - !serde_json::from_str::(&response.text().unwrap()) + !serde_json::from_str::(&response.text().unwrap()) .unwrap() .indexed ); @@ -328,11 +328,11 @@ fn get_output() { let response = server.json_request(format!("/output/{}:0", txid)); assert_eq!(response.status(), StatusCode::OK); - let output_json: OutputJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let output_json: api::Output = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( output_json, - OutputJson { + api::Output { address: None, inscriptions: vec![ InscriptionId { txid, index: 0 }, @@ -376,11 +376,11 @@ fn get_block() { assert_eq!(response.status(), StatusCode::OK); - let block_json: BlockJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let block_json: api::Block = serde_json::from_str(&response.text().unwrap()).unwrap(); assert_eq!( block_json, - BlockJson { + api::Block { hash: "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f" .parse::() .unwrap(), @@ -413,11 +413,11 @@ fn get_blocks() { assert_eq!(response.status(), StatusCode::OK); - let blocks_json: BlocksJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let blocks_json: api::Blocks = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( blocks_json, - BlocksJson { + api::Blocks { last: 101, blocks: blocks.clone(), featured_blocks: blocks @@ -444,8 +444,8 @@ fn get_transaction() { assert_eq!(response.status(), StatusCode::OK); assert_eq!( - serde_json::from_str::(&response.text().unwrap()).unwrap(), - TransactionJson { + serde_json::from_str::(&response.text().unwrap()).unwrap(), + api::Transaction { chain: Chain::Mainnet, etching: None, inscription_count: 0, @@ -476,7 +476,7 @@ fn get_status() { assert_eq!(response.status(), StatusCode::OK); - let mut status_json: StatusJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let mut status_json: api::Status = serde_json::from_str(&response.text().unwrap()).unwrap(); let dummy_started = "2012-12-12 12:12:12+00:00" .parse::>() @@ -489,7 +489,7 @@ fn get_status() { pretty_assert_eq!( status_json, - StatusJson { + api::Status { blessed_inscriptions: 1, chain: Chain::Regtest, content_type_counts: vec![(Some("text/plain;charset=utf-8".into()), 1)], @@ -531,11 +531,11 @@ fn get_runes() { let response = ord_rpc_server.json_request(format!("/rune/{}", a.rune)); assert_eq!(response.status(), StatusCode::OK); - let rune_json: RuneJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let rune_json: api::Rune = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( rune_json, - RuneJson { + api::Rune { entry: RuneEntry { burned: 0, mint: None, @@ -561,11 +561,11 @@ fn get_runes() { assert_eq!(response.status(), StatusCode::OK); - let runes_json: RunesJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let runes_json: api::Runes = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( runes_json, - RunesJson { + api::Runes { entries: vec![ ( RuneId { diff --git a/tests/lib.rs b/tests/lib.rs index f1f3c924b3..fc0933d4eb 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -11,16 +11,8 @@ use { chrono::{DateTime, Utc}, executable_path::executable_path, ord::{ - chain::Chain, - outgoing::Outgoing, - subcommand::runes::RuneInfo, - templates::{ - block::BlockJson, blocks::BlocksJson, inscription::InscriptionJson, - inscription::InscriptionRecursiveJson, inscriptions::InscriptionsJson, output::OutputJson, - rune::RuneJson, runes::RunesJson, sat::SatJson, status::StatusJson, - transaction::TransactionJson, - }, - Edict, InscriptionId, Rune, RuneEntry, RuneId, Runestone, + api, chain::Chain, outgoing::Outgoing, subcommand::runes::RuneInfo, Edict, InscriptionId, Rune, + RuneEntry, RuneId, Runestone, }, ordinals::{Rarity, Sat, SatPoint}, pretty_assertions::assert_eq as pretty_assert_eq, diff --git a/tests/server.rs b/tests/server.rs index d1b1034f05..6e30e5bdf6 100644 --- a/tests/server.rs +++ b/tests/server.rs @@ -308,12 +308,12 @@ fn recursive_inscription_endpoint() { "application/json" ); - let inscription_recursive_json: InscriptionRecursiveJson = + let inscription_recursive_json: api::InscriptionRecursive = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( inscription_recursive_json, - InscriptionRecursiveJson { + api::InscriptionRecursive { charms: vec!["coin".into(), "uncommon".into()], content_type: Some("text/plain;charset=utf-8".to_string()), content_length: Some(3), diff --git a/tests/wallet/inscribe.rs b/tests/wallet/inscribe.rs index e6e613e3d2..143e2318a2 100644 --- a/tests/wallet/inscribe.rs +++ b/tests/wallet/inscribe.rs @@ -2207,7 +2207,7 @@ fn batch_inscribe_with_satpoints_with_parent() { offset: 0, }; - let sat_1 = serde_json::from_str::( + let sat_1 = serde_json::from_str::( &ord_rpc_server .json_request(format!("/output/{}", satpoint_1.outpoint)) .text() @@ -2218,7 +2218,7 @@ fn batch_inscribe_with_satpoints_with_parent() { .unwrap()[0] .0; - let sat_2 = serde_json::from_str::( + let sat_2 = serde_json::from_str::( &ord_rpc_server .json_request(format!("/output/{}", satpoint_2.outpoint)) .text() @@ -2229,7 +2229,7 @@ fn batch_inscribe_with_satpoints_with_parent() { .unwrap()[0] .0; - let sat_3 = serde_json::from_str::( + let sat_3 = serde_json::from_str::( &ord_rpc_server .json_request(format!("/output/{}", satpoint_3.outpoint)) .text() @@ -2389,7 +2389,7 @@ fn batch_inscribe_with_satpoints_with_different_sizes() { offset: 0, }; - let output_1 = serde_json::from_str::( + let output_1 = serde_json::from_str::( &ord_rpc_server .json_request(format!("/output/{}", satpoint_1.outpoint)) .text() @@ -2398,7 +2398,7 @@ fn batch_inscribe_with_satpoints_with_different_sizes() { .unwrap(); assert_eq!(output_1.value, 25 * COIN_VALUE); - let output_2 = serde_json::from_str::( + let output_2 = serde_json::from_str::( &ord_rpc_server .json_request(format!("/output/{}", satpoint_2.outpoint)) .text() @@ -2407,7 +2407,7 @@ fn batch_inscribe_with_satpoints_with_different_sizes() { .unwrap(); assert_eq!(output_2.value, COIN_VALUE); - let output_3 = serde_json::from_str::( + let output_3 = serde_json::from_str::( &ord_rpc_server .json_request(format!("/output/{}", satpoint_3.outpoint)) .text() diff --git a/tests/wallet/send.rs b/tests/wallet/send.rs index 7308431897..1aefd8abdd 100644 --- a/tests/wallet/send.rs +++ b/tests/wallet/send.rs @@ -280,11 +280,11 @@ fn splitting_merged_inscriptions_is_possible() { let response = ord_rpc_server.json_request(format!("/output/{}:0", reveal_txid)); assert_eq!(response.status(), StatusCode::OK); - let output_json: OutputJson = serde_json::from_str(&response.text().unwrap()).unwrap(); + let output_json: api::Output = serde_json::from_str(&response.text().unwrap()).unwrap(); pretty_assert_eq!( output_json, - OutputJson { + api::Output { address: None, inscriptions: vec![ InscriptionId {