anvil_zksync_core/formatter/address.rs
1use anvil_zksync_common::address_map::{ContractType, KNOWN_ADDRESSES};
2use colored::Colorize;
3use zksync_types::H160;
4
5/// Converts a raw Ethereum address to a human-readable format.
6///
7/// If the address is known (such as a system contract, precompile, or popular contract),
8/// this function returns a formatted string with the name and address. Otherwise, returns None.
9///
10/// # Arguments
11///
12/// * `address` - The Ethereum address to format
13///
14/// # Returns
15///
16/// * `Option<String>` - A formatted string or None if the address is not known
17pub fn address_to_human_readable(address: H160) -> Option<String> {
18 format_known_address(address)
19}
20
21/// Formats a known address with appropriate styling based on its contract type.
22///
23/// # Arguments
24///
25/// * `address` - The Ethereum address to format
26///
27/// # Returns
28///
29/// * `Option<String>` - A colored string representation of the address if known, None otherwise
30fn format_known_address(address: H160) -> Option<String> {
31 KNOWN_ADDRESSES.get(&address).map(|known_address| {
32 let name = match known_address.contract_type {
33 ContractType::System => known_address.name.bold().bright_blue(),
34 ContractType::Precompile => known_address.name.bold().magenta(),
35 ContractType::Popular => known_address.name.bold().bright_green(),
36 ContractType::Unknown => known_address.name.dimmed(),
37 }
38 .to_string();
39
40 let formatted_address = format!("{address:#x}").dimmed();
41 format!("{name}{at}{formatted_address}", at = "@".dimmed())
42 })
43}