Skip to content

Commit

Permalink
Output as json
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo committed Nov 13, 2024
1 parent f246353 commit e611a4f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/ain-dftx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ bitcoin.workspace = true
hex.workspace = true
bitflags = "2.4.1"
lazy_static.workspace = true
serde.workspace = true
1 change: 1 addition & 0 deletions lib/ain-dftx/src/types/balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ain_macros::ConsensusEncoding;
use bitcoin::{io, ScriptBuf, VarInt};
use serde::Serialize;

use super::common::CompactVec;

Expand Down
3 changes: 2 additions & 1 deletion lib/ain-dftx/src/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bitcoin::{
consensus::{Decodable, Encodable},
io::{self, ErrorKind},
};
use serde::Serialize;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct CompactVec<T>(Vec<T>);
Expand Down Expand Up @@ -110,7 +111,7 @@ impl Decodable for RawBytes {
/// In the rust-bitcoin library, variable-length integers are implemented as CompactSize.
/// See [issue #1016](https://github.com/rust-bitcoin/rust-bitcoin/issues/1016)

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Serialize)]
pub struct VarInt(pub u64);

impl Encodable for VarInt {
Expand Down
6 changes: 3 additions & 3 deletions lib/ain-dftx/src/types/price.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::common::CompactVec;
use ain_macros::ConsensusEncoding;
use bitcoin::io;

use super::common::CompactVec;
use serde::Serialize;

#[derive(ConsensusEncoding, Debug, PartialEq, Eq, Clone)]
pub struct CurrencyPair {
pub token: String,
pub currency: String,
}

#[derive(ConsensusEncoding, Debug, PartialEq, Eq, Clone)]
#[derive(ConsensusEncoding, Debug, PartialEq, Eq, Clone, Serialize)]
pub struct TokenAmount {
pub currency: String,
pub amount: i64,
Expand Down
4 changes: 3 additions & 1 deletion lib/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ edition = "2021"
[dependencies]
ain-macros.workspace = true
ain-dftx.workspace = true
bitcoin.workspace = true
bitcoin = { workspace = true, features = ["serde"] }
hex.workspace = true
serde_json.workspace = true
serde.workspace = true
30 changes: 23 additions & 7 deletions lib/parser/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use ain_dftx::balance::TokenBalanceVarInt;
use ain_dftx::common::VarInt;
use ain_dftx::price::TokenAmount;
use ain_macros::ConsensusEncoding;
use bitcoin::io;
use bitcoin::{consensus::Decodable, ScriptBuf};
use bitcoin::{io, Txid};
use serde::Serialize;
use std::io::BufRead;

#[derive(Debug)]
Expand All @@ -26,19 +30,19 @@ impl RawDbEntry {
}
}

#[derive(ConsensusEncoding, Debug, Clone, PartialEq, Eq)]
#[derive(ConsensusEncoding, Debug, Clone, PartialEq, Eq, Serialize)]
pub struct BalanceKey {
pub owner: ScriptBuf,
pub token_id: u32,
pub token_id: VarInt,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct PrefixedData<K, V> {
pub key: K,
pub value: V,
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum VMDomainEdge {
DVMToEVM,
EVMToDVM,
Expand All @@ -59,12 +63,24 @@ impl Decodable for VMDomainEdge {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct UndoKey {
height: u32,
id: Txid,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
struct Undo {
data: Vec<u8>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub enum Prefix {
ByBalance(PrefixedData<BalanceKey, i64>),
ByHeight(PrefixedData<ScriptBuf, u32>),
VMDomainTxEdge(PrefixedData<(VMDomainEdge, String), String>),
VMDomainBlockEdge(PrefixedData<(VMDomainEdge, String), String>),
Undo(PrefixedData<UndoKey, Undo>),
}

impl TryFrom<RawDbEntry> for Prefix {
Expand Down Expand Up @@ -104,7 +120,7 @@ fn process_line(line: &str) -> Result<(), Box<dyn std::error::Error>> {
let raw_entry = RawDbEntry::parse(line)?;

match Prefix::try_from(raw_entry) {
Ok(entry) => println!("{entry:?}"),
Ok(entry) => println!("{}", serde_json::to_string(&entry)?),
Err(_) => {}
}

Expand Down

0 comments on commit e611a4f

Please sign in to comment.