Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add custom terminal format for ADA Handle assets #232

Merged
merged 2 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/mapper/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl EventWriter {
OutputAssetRecord {
policy: policy.to_hex(),
asset: asset.to_hex(),
asset_ascii: String::from_utf8(asset.to_vec()).ok(),
amount,
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl From<TxInputRecord> for EventData {
pub struct OutputAssetRecord {
pub policy: String,
pub asset: String,
pub asset_ascii: Option<String>,
pub amount: u64,
}

Expand Down Expand Up @@ -254,8 +255,6 @@ pub enum EventData {
block_slot: u64,
block_hash: String,
},
// // flow-control event to end the pipeline
// Finalize,
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
32 changes: 26 additions & 6 deletions src/sinks/terminal/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use std::fmt::{Display, Write};

use crossterm::style::{Attribute, Color, Stylize};

use crate::model::{
BlockRecord, CIP25AssetRecord, Event, EventData, MetadataRecord, MintRecord, OutputAssetRecord,
TransactionRecord, TxInputRecord, TxOutputRecord,
use crate::{
model::{
BlockRecord, CIP25AssetRecord, Event, EventData, MetadataRecord, MintRecord,
OutputAssetRecord, TransactionRecord, TxInputRecord, TxOutputRecord,
},
utils::Utils,
};

pub struct LogLine {
Expand All @@ -16,7 +19,7 @@ pub struct LogLine {
}

impl LogLine {
pub fn new(source: Event, max_width: usize) -> LogLine {
pub fn new(source: Event, max_width: usize, utils: &Utils) -> LogLine {
match &source.data {
EventData::Block(BlockRecord {
era,
Expand Down Expand Up @@ -110,14 +113,31 @@ impl LogLine {
EventData::OutputAsset(OutputAssetRecord {
policy,
asset,
asset_ascii,
..
}) if policy == &utils.well_known.adahandle_policy => LogLine {
prefix: "$HNDL",
color: Color::DarkGreen,
content: format!(
"{{ {} => {} }}",
asset_ascii.as_deref().unwrap_or(asset),
source.context.output_address.as_deref().unwrap_or_default(),
),
source,
max_width,
},
EventData::OutputAsset(OutputAssetRecord {
policy,
asset,
asset_ascii,
amount,
..
}) => LogLine {
prefix: "ASSET",
color: Color::Green,
content: format!(
"{{ policy: {}, asset: {}, amount: {} }}",
policy, asset, amount
policy, asset_ascii.as_deref().unwrap_or(asset), amount
),
source,
max_width,
Expand Down Expand Up @@ -288,7 +308,7 @@ impl Display for LogLine {
.unwrap_or_else(|| "--".to_string()),
)
.stylize()
.with(Color::DarkGrey)
.with(Color::Grey)
.attribute(Attribute::Dim)
.fmt(f)?;

Expand Down
2 changes: 1 addition & 1 deletion src/sinks/terminal/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn reducer_loop(
utils.track_sink_progress(&evt);

throttle.wait_turn();
let line = LogLine::new(evt, width as usize);
let line = LogLine::new(evt, width as usize, &utils);
stdout.execute(Print(line))?;
}

Expand Down
5 changes: 5 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct ChainWellKnownInfo {
pub shelley_known_hash: String,
pub shelley_known_time: u64,
pub address_hrp: String,
pub adahandle_policy: String,
}

impl ChainWellKnownInfo {
Expand All @@ -82,6 +83,8 @@ impl ChainWellKnownInfo {
.to_string(),
shelley_known_time: 1596059091,
address_hrp: "addr".to_string(),
adahandle_policy: "f0ff48bbb7bbe9d59a40f1ce90e9e9d0ff5002ec48f232b49ca0fb9a"
.to_string(),
}
}

Expand All @@ -101,6 +104,8 @@ impl ChainWellKnownInfo {
.to_string(),
shelley_known_time: 1595967616,
address_hrp: "addr_test".to_string(),
adahandle_policy: "8d18d786e92776c824607fd8e193ec535c79dc61ea2405ddf3b09fe3"
.to_string(),
}
}

Expand Down