Skip to content

Commit

Permalink
Move JSON structs into api module (#3167)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Feb 24, 2024
1 parent bac8a16 commit 8650ff1
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 292 deletions.
175 changes: 175 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -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<InscriptionId>,
}

impl Block {
pub(crate) fn new(
block: bitcoin::Block,
height: Height,
best_height: Height,
inscriptions: Vec<InscriptionId>,
) -> 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<u64>,
pub merkle_root: TxMerkleNode,
pub next_block: Option<BlockHash>,
pub nonce: u32,
pub previous_block: Option<BlockHash>,
pub target: BlockHash,
pub timestamp: u64,
pub transaction_count: u64,
pub version: u32,
}

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

#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct Inscription {
pub address: Option<String>,
pub charms: Vec<String>,
pub children: Vec<InscriptionId>,
pub content_length: Option<usize>,
pub content_type: Option<String>,
pub genesis_fee: u64,
pub genesis_height: u32,
pub inscription_id: InscriptionId,
pub inscription_number: i32,
pub next: Option<InscriptionId>,
pub output_value: Option<u64>,
pub parent: Option<InscriptionId>,
pub previous: Option<InscriptionId>,
pub rune: Option<SpacedRune>,
pub sat: Option<ordinals::Sat>,
pub satpoint: SatPoint,
pub timestamp: i64,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct InscriptionRecursive {
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<ordinals::Sat>,
pub satpoint: SatPoint,
pub timestamp: i64,
pub value: Option<u64>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Inscriptions {
pub inscriptions: Vec<InscriptionId>,
pub more: bool,
pub page_index: u32,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub address: Option<String>,
pub indexed: bool,
pub inscriptions: Vec<InscriptionId>,
pub runes: Vec<(SpacedRune, Pile)>,
pub sat_ranges: Option<Vec<(u64, u64)>>,
pub script_pubkey: String,
pub spent: bool,
pub transaction: String,
pub value: u64,
}

impl Output {
pub fn new(
chain: Chain,
inscriptions: Vec<InscriptionId>,
outpoint: OutPoint,
output: TxOut,
indexed: bool,
runes: Vec<(SpacedRune, Pile)>,
sat_ranges: Option<Vec<(u64, u64)>>,
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<SatPoint>,
pub timestamp: i64,
pub inscriptions: Vec<InscriptionId>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct SatInscription {
pub id: Option<InscriptionId>,
}

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct SatInscriptions {
pub ids: Vec<InscriptionId>,
pub more: bool,
pub page: u64,
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ use {
time::{Duration, Instant, SystemTime},
},
sysinfo::System,
templates::{InscriptionJson, OutputJson, RuneJson, StatusJson},
tokio::{runtime::Runtime, task},
};

Expand Down Expand Up @@ -103,6 +102,7 @@ macro_rules! tprintln {
};
}

pub mod api;
pub mod arguments;
mod blocktime;
pub mod chain;
Expand Down
Loading

0 comments on commit 8650ff1

Please sign in to comment.