pcli/transaction_view_ext.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
use comfy_table::presets;
use comfy_table::Table;
use penumbra_asset::asset::Id;
use penumbra_asset::asset::Metadata;
use penumbra_asset::Value;
use penumbra_asset::ValueView;
use penumbra_dex::swap::SwapView;
use penumbra_dex::swap_claim::SwapClaimView;
use penumbra_fee::Fee;
use penumbra_keys::AddressView;
use penumbra_num::Amount;
use penumbra_shielded_pool::SpendView;
use penumbra_transaction::view::action_view::OutputView;
use penumbra_transaction::TransactionView;
// Issues identified:
// TODO: FeeView
// TODO: TradingPairView
// Implemented some helper functions which may make more sense as methods on existing Structs
// helper function to create a value view from a value and optional metadata
fn create_value_view(value: Value, metadata: Option<Metadata>) -> ValueView {
match metadata {
Some(metadata) => ValueView::KnownAssetId {
amount: value.amount,
metadata,
equivalent_values: Vec::new(),
extended_metadata: None,
},
None => ValueView::UnknownAssetId {
amount: value.amount,
asset_id: value.asset_id,
},
}
}
// a helper function to create pretty placeholders for encrypted information
fn format_opaque_bytes(bytes: &[u8]) -> String {
if bytes.len() < 8 {
return String::new();
} else {
/*
// TODO: Hm, this can allow the same color for both, should rejig things to avoid this
// Select foreground and background colors based on the first 8 bytes.
let fg_color_index = bytes[0] % 8;
let bg_color_index = bytes[4] % 8;
// ANSI escape codes for foreground and background colors.
let fg_color_code = 37; // 30 through 37 are foreground colors
let bg_color_code = 40; // 40 through 47 are background colors
*/
// to be more general, perhaps this should be configurable
// an opaque address needs less space than an opaque memo, etc
let max_bytes = 32;
let rem = if bytes.len() > max_bytes {
bytes[0..max_bytes].to_vec()
} else {
bytes.to_vec()
};
// Convert the rest of the bytes to hexadecimal.
let hex_str = hex::encode_upper(rem);
let opaque_chars: String = hex_str
.chars()
.map(|c| {
match c {
'0' => "\u{2595}",
'1' => "\u{2581}",
'2' => "\u{2582}",
'3' => "\u{2583}",
'4' => "\u{2584}",
'5' => "\u{2585}",
'6' => "\u{2586}",
'7' => "\u{2587}",
'8' => "\u{2588}",
'9' => "\u{2589}",
'A' => "\u{259A}",
'B' => "\u{259B}",
'C' => "\u{259C}",
'D' => "\u{259D}",
'E' => "\u{259E}",
'F' => "\u{259F}",
_ => "",
}
.to_string()
})
.collect();
//format!("\u{001b}[{};{}m{}", fg_color_code, bg_color_code, block_chars)
format!("{}", opaque_chars)
}
}
// feels like these functions should be extension traits of their respective structs
// propose moving this to core/keys/src/address/view.rs
fn format_address_view(address_view: &AddressView) -> String {
match address_view {
AddressView::Decoded {
address: _,
index,
wallet_id: _,
} => {
if !index.is_ephemeral() {
format!("[account {:?}]", index.account)
} else {
format!("[account {:?} (one-time address)]", index.account)
}
}
AddressView::Opaque { address } => {
// The address being opaque just means we can't see the internal structure,
// we should render the content so it can be copy-pasted.
format!("{}", address)
}
}
}
// feels like these functions should be extension traits of their respective structs
// propose moving this to core/asset/src/value.rs
fn format_value_view(value_view: &ValueView) -> String {
match value_view {
ValueView::KnownAssetId {
amount,
metadata: denom,
..
} => {
let unit = denom.default_unit();
format!("{}{}", unit.format_value(*amount), unit)
}
ValueView::UnknownAssetId { amount, asset_id } => {
format!("{}{}", amount, asset_id)
}
}
}
fn format_amount_range(
start: Amount,
stop: Amount,
asset_id: &Id,
metadata: Option<&Metadata>,
) -> String {
match metadata {
Some(denom) => {
let unit = denom.default_unit();
format!(
"({}..{}){}",
unit.format_value(start),
unit.format_value(stop),
unit
)
}
None => format!("({}..{}){}", start, stop, asset_id),
}
}
fn format_fee(fee: &Fee) -> String {
// TODO: Implement FeeView to show decrypted fee.
format!("{}", fee.amount())
}
fn format_asset_id(asset_id: &Id) -> String {
// TODO: Implement TradingPairView to show decrypted .asset_id()
let input = &asset_id.to_string();
let truncated = &input[0..10]; //passet1
let ellipsis = "...";
let end = &input[(input.len() - 3)..];
format!("{}{}{}", truncated, ellipsis, end)
}
// When handling ValueViews inside of a Visible variant of an ActionView, handling both cases might be needlessly verbose
// potentially this makes sense as a method on the ValueView enum
// propose moving this to core/asset/src/value.rs
fn value_view_amount(value_view: &ValueView) -> Amount {
match value_view {
ValueView::KnownAssetId { amount, .. } | ValueView::UnknownAssetId { amount, .. } => {
*amount
}
}
}
pub trait TransactionViewExt {
/// Render this transaction view on stdout.
fn render_terminal(&self);
}
impl TransactionViewExt for TransactionView {
fn render_terminal(&self) {
let fee = &self.body_view.transaction_parameters.fee;
// the denomination should be visible here... does a FeeView exist?
println!("Fee: {}", format_fee(&fee));
println!(
"Expiration Height: {}",
&self.body_view.transaction_parameters.expiry_height
);
if let Some(memo_view) = &self.body_view.memo_view {
match memo_view {
penumbra_transaction::MemoView::Visible {
plaintext,
ciphertext: _,
} => {
println!("Memo Sender: {}", &plaintext.return_address.address());
println!("Memo Text: \n{}\n", &plaintext.text);
}
penumbra_transaction::MemoView::Opaque { ciphertext } => {
println!("Encrypted Memo: \n{}\n", format_opaque_bytes(&ciphertext.0));
}
}
}
let mut actions_table = Table::new();
actions_table.load_preset(presets::NOTHING);
actions_table.set_header(vec!["Tx Action", "Description"]);
// Iterate over the ActionViews in the TxView & display as appropriate
for action_view in &self.body_view.action_views {
let action: String;
let row = match action_view {
penumbra_transaction::ActionView::Spend(spend) => {
match spend {
SpendView::Visible { spend: _, note } => {
action = format!(
"{} -> {}",
format_address_view(¬e.address),
format_value_view(¬e.value)
);
["Spend", &action]
}
SpendView::Opaque { spend } => {
let bytes = spend.body.nullifier.to_bytes(); // taken to be a unique value, for aesthetic reasons
action = format_opaque_bytes(&bytes);
["Spend", &action]
}
}
}
penumbra_transaction::ActionView::Output(output) => {
match output {
OutputView::Visible {
output: _,
note,
payload_key: _,
} => {
action = format!(
"{} -> {}",
format_value_view(¬e.value),
format_address_view(¬e.address),
);
["Output", &action]
}
OutputView::Opaque { output } => {
let bytes = output.body.note_payload.encrypted_note.0; // taken to be a unique value, for aesthetic reasons
action = format_opaque_bytes(&bytes);
["Output", &action]
}
}
}
penumbra_transaction::ActionView::Swap(swap) => {
// Typical swaps are one asset for another, but we can't know that for sure.
match swap {
SwapView::Visible { swap_plaintext, .. } => {
let (from_asset, from_value, to_asset) = match (
swap_plaintext.delta_1_i.value(),
swap_plaintext.delta_2_i.value(),
) {
(0, v) if v > 0 => (
swap_plaintext.trading_pair.asset_2(),
swap_plaintext.delta_2_i,
swap_plaintext.trading_pair.asset_1(),
),
(v, 0) if v > 0 => (
swap_plaintext.trading_pair.asset_1(),
swap_plaintext.delta_1_i,
swap_plaintext.trading_pair.asset_2(),
),
// The pathological case (both assets have output values).
_ => (
swap_plaintext.trading_pair.asset_1(),
swap_plaintext.delta_1_i,
swap_plaintext.trading_pair.asset_1(),
),
};
action = format!(
"{} {} for {} and paid claim fee {}",
from_value,
format_asset_id(&from_asset),
format_asset_id(&to_asset),
format_fee(&swap_plaintext.claim_fee),
);
["Swap", &action]
}
SwapView::Opaque { swap, .. } => {
action = format!(
"Opaque swap for trading pair: {} <=> {}",
format_asset_id(&swap.body.trading_pair.asset_1()),
format_asset_id(&swap.body.trading_pair.asset_2()),
);
["Swap", &action]
}
}
}
penumbra_transaction::ActionView::SwapClaim(swap_claim) => {
match swap_claim {
SwapClaimView::Visible {
swap_claim,
output_1,
output_2,
swap_tx: _,
} => {
// View service can't see SwapClaims: https://github.com/penumbra-zone/penumbra/issues/2547
dbg!(swap_claim);
let claimed_value = match (
value_view_amount(&output_1.value).value(),
value_view_amount(&output_2.value).value(),
) {
(0, v) if v > 0 => format_value_view(&output_2.value),
(v, 0) if v > 0 => format_value_view(&output_1.value),
// The pathological case (both assets have output values).
_ => format!(
"{} and {}",
format_value_view(&output_1.value),
format_value_view(&output_2.value),
),
};
action = format!(
"Claimed {} with fee {:?}",
claimed_value,
format_fee(&swap_claim.body.fee),
);
["Swap Claim", &action]
}
SwapClaimView::Opaque { swap_claim } => {
let bytes = swap_claim.body.nullifier.to_bytes(); // taken to be a unique value, for aesthetic reasons
action = format_opaque_bytes(&bytes);
["Swap Claim", &action]
}
}
}
penumbra_transaction::ActionView::Ics20Withdrawal(withdrawal) => {
let unit = withdrawal.denom.best_unit_for(withdrawal.amount);
action = format!(
"{}{} via {} to {}",
unit.format_value(withdrawal.amount),
unit,
withdrawal.source_channel,
withdrawal.destination_chain_address,
);
["Ics20 Withdrawal", &action]
}
penumbra_transaction::ActionView::PositionOpen(position_open) => {
let position = &position_open.position;
/* TODO: leaving this around since we may want it to render prices
let _unit_pair = DirectedUnitPair {
start: unit_1.clone(),
end: unit_2.clone(),
};
*/
action = format!(
"Reserves: ({} {}, {} {}) Fee: {} ID: {}",
position.reserves.r1,
format_asset_id(&position.phi.pair.asset_1()),
position.reserves.r2,
format_asset_id(&position.phi.pair.asset_2()),
position.phi.component.fee,
position.id(),
);
["Open Liquidity Position", &action]
}
penumbra_transaction::ActionView::PositionClose(_) => {
["Close Liquitity Position", ""]
}
penumbra_transaction::ActionView::PositionWithdraw(_) => {
["Withdraw Liquitity Position", ""]
}
penumbra_transaction::ActionView::ProposalDepositClaim(proposal_deposit_claim) => {
action = format!(
"Claim Deposit for Governance Proposal #{}",
proposal_deposit_claim.proposal
);
[&action, ""]
}
penumbra_transaction::ActionView::ProposalSubmit(proposal_submit) => {
action = format!(
"Submit Governance Proposal #{}",
proposal_submit.proposal.id
);
[&action, ""]
}
penumbra_transaction::ActionView::ProposalWithdraw(proposal_withdraw) => {
action = format!(
"Withdraw Governance Proposal #{}",
proposal_withdraw.proposal
);
[&action, ""]
}
penumbra_transaction::ActionView::IbcRelay(_) => ["IBC Relay", ""],
penumbra_transaction::ActionView::DelegatorVote(_) => ["Delegator Vote", ""],
penumbra_transaction::ActionView::ValidatorDefinition(_) => {
["Upload Validator Definition", ""]
}
penumbra_transaction::ActionView::ValidatorVote(_) => ["Validator Vote", ""],
penumbra_transaction::ActionView::CommunityPoolDeposit(_) => {
["Community Pool Deposit", ""]
}
penumbra_transaction::ActionView::CommunityPoolSpend(_) => {
["Community Pool Spend", ""]
}
penumbra_transaction::ActionView::CommunityPoolOutput(_) => {
["Community Pool Output", ""]
}
penumbra_transaction::ActionView::Delegate(_) => ["Delegation", ""],
penumbra_transaction::ActionView::Undelegate(_) => ["Undelegation", ""],
penumbra_transaction::ActionView::UndelegateClaim(_) => ["Undelegation Claim", ""],
penumbra_transaction::ActionView::ActionDutchAuctionSchedule(x) => {
let description = &x.action.description;
let input: String = format_value_view(&create_value_view(
description.input,
x.input_metadata.clone(),
));
let output: String = format_amount_range(
description.min_output,
description.max_output,
&description.output_id,
x.output_metadata.as_ref(),
);
let start = description.start_height;
let stop = description.end_height;
let steps = description.step_count;
let auction_id = x.auction_id;
action = format!(
"{} -> {}, blocks {}..{}, in {} steps ({})",
input, output, start, stop, steps, auction_id
);
["Dutch Auction Schedule", &action]
}
penumbra_transaction::ActionView::ActionDutchAuctionEnd(x) => {
action = format!("{}", x.auction_id);
["Dutch Auction End", &action]
}
penumbra_transaction::ActionView::ActionDutchAuctionWithdraw(x) => {
let inside = x
.reserves
.iter()
.map(|value| format_value_view(value))
.collect::<Vec<_>>()
.as_slice()
.join(", ");
action = format!("{} -> [{}]", x.action.auction_id, inside);
["Dutch Auction Withdraw", &action]
}
};
actions_table.add_row(row);
}
// Print table of actions and their descriptions
println!("{actions_table}");
}
}