Skip to content

Commit df88b1f

Browse files
committed
[refactor](app): impl ERC20Token
1 parent a05eec0 commit df88b1f

File tree

3 files changed

+78
-40
lines changed

3 files changed

+78
-40
lines changed

src/app.rs

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,17 @@ pub mod block;
22
pub mod event_handling;
33
pub mod statistics;
44
pub mod transaction;
5-
use crate::ethers::types::{BlockWithTransactionReceipts, TransactionWithReceipt};
6-
use crate::network::IoEvent;
7-
use crate::route::{ActiveBlock, Route};
8-
use crate::widget::StatefulList;
9-
use ethers::core::types::{Address, NameOrAddress, Transaction, TxHash, U64};
5+
use crate::{
6+
ethers::types::{BlockWithTransactionReceipts, ERC20Token, TransactionWithReceipt},
7+
network::IoEvent,
8+
route::{ActiveBlock, Route},
9+
widget::StatefulList,
10+
};
11+
use ethers::core::types::{NameOrAddress, Transaction, TxHash, U64};
1012
use ratatui::widgets::{ListState, TableState};
1113
use statistics::Statistics;
1214
use std::{fs::File, io::Read, sync::mpsc::Sender};
1315

14-
use serde::{Deserialize, Deserializer};
15-
16-
#[derive(Deserialize, Debug)]
17-
pub struct ERC20Token {
18-
pub name: String,
19-
pub ticker: String,
20-
#[serde(deserialize_with = "deserialize_address_from_string")]
21-
pub contract_address: Address,
22-
}
23-
24-
fn deserialize_address_from_string<'de, D>(deserializer: D) -> Result<Address, D::Error>
25-
where
26-
D: Deserializer<'de>,
27-
{
28-
let s: String = Deserialize::deserialize(deserializer)?;
29-
30-
s.parse::<Address>().map_err(serde::de::Error::custom)
31-
}
32-
3316
pub enum InputMode {
3417
Normal,
3518
Editing,
@@ -184,11 +167,7 @@ impl App {
184167
}
185168

186169
pub fn submit_message(&mut self) {
187-
if let Some(token) = self
188-
.erc20_tokens
189-
.iter()
190-
.find(|erc20_token| erc20_token.ticker == self.input)
191-
{
170+
if let Some(token) = ERC20Token::find_by_ticker(&self.erc20_tokens, &self.input) {
192171
self.dispatch(IoEvent::GetNameOrAddressInfo {
193172
name_or_address: NameOrAddress::Address(token.contract_address),
194173
})

src/ethers.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod types {
33
core::types::{Address, Block, Transaction, TransactionReceipt, U256},
44
etherscan::contract::ContractMetadata,
55
};
6+
use serde::{Deserialize, Deserializer};
67
use std::cmp::PartialEq;
78
use url::Url;
89

@@ -26,4 +27,37 @@ pub mod types {
2627
pub block: Block<T>,
2728
pub transaction_receipts: Option<Vec<TransactionReceipt>>,
2829
}
30+
31+
#[derive(Deserialize, Debug, Clone)]
32+
pub struct ERC20Token {
33+
pub name: String,
34+
pub ticker: String,
35+
#[serde(deserialize_with = "deserialize_address_from_string")]
36+
pub contract_address: Address,
37+
}
38+
39+
fn deserialize_address_from_string<'de, D>(deserializer: D) -> Result<Address, D::Error>
40+
where
41+
D: Deserializer<'de>,
42+
{
43+
let s: String = Deserialize::deserialize(deserializer)?;
44+
45+
s.parse::<Address>().map_err(serde::de::Error::custom)
46+
}
47+
48+
impl ERC20Token {
49+
pub fn find_by_address(erc20_tokens: &[Self], address: Address) -> Option<Self> {
50+
erc20_tokens
51+
.iter()
52+
.find(|erc20_token| erc20_token.contract_address == address)
53+
.map(|token| token.to_owned())
54+
}
55+
56+
pub fn find_by_ticker(erc20_tokens: &[Self], ticker: &str) -> Option<Self> {
57+
erc20_tokens
58+
.iter()
59+
.find(|erc20_token| erc20_token.ticker == ticker)
60+
.map(|token| token.to_owned())
61+
}
62+
}
2963
} /* types */

src/ui/home/block/transactions.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
use crate::app::App;
2-
use crate::ethers::types::BlockWithTransactionReceipts;
3-
use crate::route::{ActiveBlock, RouteId};
4-
use crate::widget::Spinner;
1+
use crate::{
2+
app::App,
3+
ethers::types::{BlockWithTransactionReceipts, ERC20Token},
4+
route::{ActiveBlock, RouteId},
5+
widget::Spinner,
6+
};
57
use ethers::core::{
68
types::{Transaction, U64},
79
utils::{format_ether, format_units},
@@ -68,12 +70,36 @@ pub fn render<B: Backend>(
6870
.to_string(),
6971
)
7072
.fg(Color::White),
71-
Cell::from(format!("{}", tx.from)).fg(Color::White),
72-
Cell::from(tx.to.map_or("".to_owned(), |to| format!("{to}"))).fg(Color::White),
73+
Cell::from(
74+
if let Some(token) = ERC20Token::find_by_address(&app.erc20_tokens, tx.from) {
75+
token.ticker.to_string()
76+
} else {
77+
format!("{}", tx.from)
78+
},
79+
)
80+
.fg(
81+
if ERC20Token::find_by_address(&app.erc20_tokens, tx.from).is_some() {
82+
Color::Cyan
83+
} else {
84+
Color::White
85+
},
86+
),
87+
Cell::from(tx.to.map_or("".to_owned(), |to| {
88+
if let Some(token) = ERC20Token::find_by_address(&app.erc20_tokens, to) {
89+
token.ticker.to_string()
90+
} else {
91+
format!("{}", to)
92+
}
93+
}))
94+
.fg(tx.to.map_or(Color::White, |to| {
95+
if ERC20Token::find_by_address(&app.erc20_tokens, to).is_some() {
96+
Color::Cyan
97+
} else {
98+
Color::White
99+
}
100+
})),
73101
Cell::from(format_ether(tx.value).to_string()).fg(Color::White),
74-
Cell::from(format!(
75-
"{}",
76-
if let Some(transaction_receipts) = transaction_receipts {
102+
Cell::from((if let Some(transaction_receipts) = transaction_receipts {
77103
if let Some(transaction_receipt) = transaction_receipts
78104
.iter()
79105
.find(|receipt| receipt.transaction_hash == tx.hash)
@@ -88,8 +114,7 @@ pub fn render<B: Backend>(
88114
}
89115
} else {
90116
Spinner::default().to_string()
91-
}
92-
))
117+
}).to_string())
93118
.fg(Color::White),
94119
Cell::from(
95120
format_units(tx.gas_price.unwrap(), "gwei")

0 commit comments

Comments
 (0)