foundry_evm_abi/console/
mod.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
use alloy_primitives::{hex, I256, U256};
use alloy_sol_types::sol;
use derive_more::Display;
use itertools::Itertools;

mod hardhat;
pub use hardhat::*;

// TODO: Use `UiFmt`

sol! {
#[sol(abi)]
#[derive(Display)]
interface Console {
    #[display("{val}")]
    event log(string val);

    #[display("{}", hex::encode_prefixed(val))]
    event logs(bytes val);

    #[display("{val}")]
    event log_address(address val);

    #[display("{val}")]
    event log_bytes32(bytes32 val);

    #[display("{val}")]
    event log_int(int val);

    #[display("{val}")]
    event log_uint(uint val);

    #[display("{}", hex::encode_prefixed(val))]
    event log_bytes(bytes val);

    #[display("{val}")]
    event log_string(string val);

    #[display("[{}]", val.iter().format(", "))]
    event log_array(uint256[] val);

    #[display("[{}]", val.iter().format(", "))]
    event log_array(int256[] val);

    #[display("[{}]", val.iter().format(", "))]
    event log_array(address[] val);

    #[display("{key}: {val}")]
    event log_named_address(string key, address val);

    #[display("{key}: {val}")]
    event log_named_bytes32(string key, bytes32 val);

    #[display("{key}: {}", format_units_int(val, decimals))]
    event log_named_decimal_int(string key, int val, uint decimals);

    #[display("{key}: {}", format_units_uint(val, decimals))]
    event log_named_decimal_uint(string key, uint val, uint decimals);

    #[display("{key}: {val}")]
    event log_named_int(string key, int val);

    #[display("{key}: {val}")]
    event log_named_uint(string key, uint val);

    #[display("{key}: {}", hex::encode_prefixed(val))]
    event log_named_bytes(string key, bytes val);

    #[display("{key}: {val}")]
    event log_named_string(string key, string val);

    #[display("{key}: [{}]", val.iter().format(", "))]
    event log_named_array(string key, uint256[] val);

    #[display("{key}: [{}]", val.iter().format(", "))]
    event log_named_array(string key, int256[] val);

    #[display("{key}: [{}]", val.iter().format(", "))]
    event log_named_array(string key, address[] val);
}
}

pub fn format_units_int(x: &I256, decimals: &U256) -> String {
    let (sign, x) = x.into_sign_and_abs();
    format!("{sign}{}", format_units_uint(&x, decimals))
}

pub fn format_units_uint(x: &U256, decimals: &U256) -> String {
    match alloy_primitives::utils::Unit::new(decimals.saturating_to::<u8>()) {
        Some(units) => alloy_primitives::utils::ParseUnits::U256(*x).format_units(units),
        None => x.to_string(),
    }
}