use crate::{BlockNumber, Capacity, CellOutput, JsonBytes, OutPoint, Script, Uint32, Uint64};
use ckb_types::H256;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, JsonSchema)]
pub struct IndexerTip {
pub block_hash: H256,
pub block_number: BlockNumber,
}
#[derive(Serialize, JsonSchema)]
pub struct IndexerCell {
pub output: CellOutput,
pub output_data: Option<JsonBytes>,
pub out_point: OutPoint,
pub block_number: BlockNumber,
pub tx_index: Uint32,
}
#[derive(Serialize, JsonSchema)]
pub struct IndexerPagination<T> {
pub objects: Vec<T>,
pub last_cursor: JsonBytes,
}
impl<T> IndexerPagination<T> {
pub fn new(objects: Vec<T>, last_cursor: JsonBytes) -> Self {
IndexerPagination {
objects,
last_cursor,
}
}
}
#[derive(Deserialize, JsonSchema)]
pub struct IndexerSearchKey {
pub script: Script,
pub script_type: IndexerScriptType,
pub script_search_mode: Option<IndexerSearchMode>,
pub filter: Option<IndexerSearchKeyFilter>,
pub with_data: Option<bool>,
pub group_by_transaction: Option<bool>,
}
impl Default for IndexerSearchKey {
fn default() -> Self {
Self {
script: Script::default(),
script_type: IndexerScriptType::Lock,
script_search_mode: None,
filter: None,
with_data: None,
group_by_transaction: None,
}
}
}
#[derive(Deserialize, PartialEq, Eq, JsonSchema, Clone, Copy)]
#[serde(rename_all = "snake_case")]
#[derive(Default)]
pub enum IndexerSearchMode {
#[default]
Prefix,
Exact,
Partial,
}
#[derive(Deserialize, Default, JsonSchema)]
#[serde(transparent)]
pub struct IndexerRange {
inner: [Uint64; 2],
}
impl IndexerRange {
pub fn new<U>(start: U, end: U) -> Self
where
U: Into<Uint64>,
{
IndexerRange {
inner: [start.into(), end.into()],
}
}
pub fn start(&self) -> Uint64 {
self.inner[0]
}
pub fn end(&self) -> Uint64 {
self.inner[1]
}
}
#[derive(Deserialize, Default, JsonSchema)]
pub struct IndexerSearchKeyFilter {
pub script: Option<Script>,
pub script_len_range: Option<IndexerRange>,
pub output_data: Option<JsonBytes>,
pub output_data_filter_mode: Option<IndexerSearchMode>,
pub output_data_len_range: Option<IndexerRange>,
pub output_capacity_range: Option<IndexerRange>,
pub block_range: Option<IndexerRange>,
}
#[derive(Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum IndexerScriptType {
Lock,
Type,
}
#[derive(Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum IndexerOrder {
Desc,
Asc,
}
#[derive(Serialize, JsonSchema)]
pub struct IndexerCellsCapacity {
pub capacity: Capacity,
pub block_hash: H256,
pub block_number: BlockNumber,
}
#[derive(Serialize, JsonSchema, Debug)]
#[serde(untagged)]
pub enum IndexerTx {
Ungrouped(IndexerTxWithCell),
Grouped(IndexerTxWithCells),
}
impl IndexerTx {
pub fn tx_hash(&self) -> H256 {
match self {
IndexerTx::Ungrouped(tx) => tx.tx_hash.clone(),
IndexerTx::Grouped(tx) => tx.tx_hash.clone(),
}
}
}
#[derive(Serialize, JsonSchema, Debug)]
pub struct IndexerTxWithCell {
pub tx_hash: H256,
pub block_number: BlockNumber,
pub tx_index: Uint32,
pub io_index: Uint32,
pub io_type: IndexerCellType,
}
#[derive(Serialize, JsonSchema, Debug)]
pub struct IndexerTxWithCells {
pub tx_hash: H256,
pub block_number: BlockNumber,
pub tx_index: Uint32,
pub cells: Vec<(IndexerCellType, Uint32)>,
}
#[derive(Serialize, Clone, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub enum IndexerCellType {
Input,
Output,
}