Skip to content

Commit

Permalink
[feat](tx): Displayed Decoded Input Data
Browse files Browse the repository at this point in the history
  • Loading branch information
woxjro committed Jan 9, 2024
1 parent b214a33 commit 6b971eb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 25 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ simplelog = "0.12.1"
toml = "0.8.4"
serde = "1.0.189"
url = "2.4.1"
tempfile = "3.9.0"

[dependencies.crossterm]
version = "0.26.1"
Expand Down
54 changes: 51 additions & 3 deletions src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ use ethers::{
providers::{Http, Middleware, Provider},
};
use futures::future::{join_all, try_join, try_join3};
use std::{error::Error, sync::Arc};
use std::{
fs::File,
io::Write,
process::Command,
{error::Error, sync::Arc},
};
use tempfile::tempdir;
use tokio::sync::Mutex;

const RATE_LIMIT: usize = 60;
Expand Down Expand Up @@ -168,7 +174,14 @@ impl<'a> Network<'a> {
app.is_loading = false;
}
IoEvent::GetTransactionWithReceipt { transaction_hash } => {
let res = Self::get_transaction_with_receipt(self.endpoint, transaction_hash).await;
let res = Self::get_transaction_with_receipt(
self.endpoint,
self.etherscan
.as_ref()
.and_then(|etherscan| etherscan.api_key.to_owned()),
transaction_hash,
)
.await;
let mut app = self.app.lock().await;
if let Ok(some) = res {
app.set_route(Route::new(RouteId::Transaction(some), ActiveBlock::Main));
Expand Down Expand Up @@ -363,17 +376,52 @@ impl<'a> Network<'a> {

async fn get_transaction_with_receipt(
endpoint: &'a str,
etherscan_api_key: Option<String>,
transaction_hash: TxHash,
) -> Result<Option<TransactionWithReceipt>, Box<dyn Error>> {
let provider = Provider::<Http>::try_from(endpoint)?;
let transaction = provider.get_transaction(transaction_hash).await?;
let transaction_receipt = provider.get_transaction_receipt(transaction_hash).await?;
if let Some(transaction) = transaction {
if let Some(transaction_receipt) = transaction_receipt {
let decoded_input_data = if let Some(api_key) = etherscan_api_key {
let client = Client::builder()
.with_api_key(api_key)
.chain(Chain::Mainnet)?
.build()?;

if let Some(to) = transaction.to {
let abi = client.contract_abi(to).await?;

let s = serde_json::to_string(&abi)?;

let dir = tempdir()?;
let file_path = dir.path().join("lazy-etherscan.tmp.abi.json");
let mut file = File::create(file_path.to_owned())?;
writeln!(file, "{}", s)?;

let output = Command::new("ethereum-input-data-decoder")
.args([
"--abi",
file_path.to_str().unwrap(),
&transaction.input.to_string(),
])
.output()?;

drop(file);
dir.close()?;
Some(String::from_utf8(output.stdout)?)
} else {
None
}
} else {
None
};

Ok(Some(TransactionWithReceipt {
transaction,
transaction_receipt,
decoded_input_data: None,
decoded_input_data,
}))
} else {
Ok(None)
Expand Down
63 changes: 41 additions & 22 deletions src/ui/home/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn render<B: Backend>(
let TransactionWithReceipt {
transaction,
transaction_receipt,
decoded_input_data: _,
decoded_input_data,
} = transaction_with_receipt;

let detail_block = Block::default()
Expand Down Expand Up @@ -286,7 +286,16 @@ pub fn render<B: Backend>(
]));
}

let raw_decoded_input_data: Vec<Line> = vec![Line::from("")];
let mut raw_decoded_input_data = vec![];

if let Some(decoded_input_data) = decoded_input_data {
for (idx, line) in decoded_input_data.split("\n").enumerate() {
raw_decoded_input_data.push(Line::from(vec![
Span::raw(format!("{:>3} ", idx + 1)).fg(Color::Gray),
Span::raw(line.to_string()).fg(Color::White),
]));
}
}

app.input_data_scroll_state = app
.input_data_scroll_state
Expand Down Expand Up @@ -435,29 +444,39 @@ pub fn render<B: Backend>(

let block = Block::default().padding(Padding::new(1, 1, 0, 1));
f.render_widget(
Paragraph::new(raw_input_data.to_owned())
.alignment(Alignment::Left)
.block(
Block::default()
.borders(Borders::RIGHT | Borders::LEFT | Borders::BOTTOM)
.border_style(
if let ActiveBlock::Main =
app.get_current_route().get_active_block()
Paragraph::new(
match app
.input_data_detail_list_state
.selected()
.map_or(SelectableInputDataDetailItem::InputData, |i| {
SelectableInputDataDetailItem::from(i)
}) {
SelectableInputDataDetailItem::InputData => raw_input_data.to_owned(),
SelectableInputDataDetailItem::DecodedInputData => {
raw_decoded_input_data.to_owned()
}
},
)
.alignment(Alignment::Left)
.block(
Block::default()
.borders(Borders::RIGHT | Borders::LEFT | Borders::BOTTOM)
.border_style(
if let ActiveBlock::Main = app.get_current_route().get_active_block() {
if let RouteId::InputDataOfTransaction(_) =
app.get_current_route().get_id()
{
if let RouteId::InputDataOfTransaction(_) =
app.get_current_route().get_id()
{
Style::default().fg(Color::Green)
} else {
Style::default().fg(Color::White)
}
Style::default().fg(Color::Green)
} else {
Style::default().fg(Color::White)
},
),
)
.scroll((app.input_data_scroll, 0))
.wrap(Wrap { trim: false }),
}
} else {
Style::default().fg(Color::White)
},
),
)
.scroll((app.input_data_scroll, 0))
.wrap(Wrap { trim: false }),
block.inner(chunks[1]),
);

Expand Down

0 comments on commit 6b971eb

Please sign in to comment.