foundry_cheatcodes/
string.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
//! Implementations of [`String`](spec::Group::String) cheatcodes.

use crate::{Cheatcode, Cheatcodes, Result, Vm::*};
use alloy_dyn_abi::{DynSolType, DynSolValue};
use alloy_primitives::{hex, U256};
use alloy_sol_types::SolValue;

// address
impl Cheatcode for toString_0Call {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { value } = self;
        Ok(value.to_string().abi_encode())
    }
}

// bytes
impl Cheatcode for toString_1Call {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { value } = self;
        Ok(hex::encode_prefixed(value).abi_encode())
    }
}

// bytes32
impl Cheatcode for toString_2Call {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { value } = self;
        Ok(value.to_string().abi_encode())
    }
}

// bool
impl Cheatcode for toString_3Call {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { value } = self;
        Ok(value.to_string().abi_encode())
    }
}

// uint256
impl Cheatcode for toString_4Call {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { value } = self;
        Ok(value.to_string().abi_encode())
    }
}

// int256
impl Cheatcode for toString_5Call {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { value } = self;
        Ok(value.to_string().abi_encode())
    }
}

impl Cheatcode for parseBytesCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { stringifiedValue } = self;
        parse(stringifiedValue, &DynSolType::Bytes)
    }
}

impl Cheatcode for parseAddressCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { stringifiedValue } = self;
        parse(stringifiedValue, &DynSolType::Address)
    }
}

impl Cheatcode for parseUintCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { stringifiedValue } = self;
        parse(stringifiedValue, &DynSolType::Uint(256))
    }
}

impl Cheatcode for parseIntCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { stringifiedValue } = self;
        parse(stringifiedValue, &DynSolType::Int(256))
    }
}

impl Cheatcode for parseBytes32Call {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { stringifiedValue } = self;
        parse(stringifiedValue, &DynSolType::FixedBytes(32))
    }
}

impl Cheatcode for parseBoolCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { stringifiedValue } = self;
        parse(stringifiedValue, &DynSolType::Bool)
    }
}

// toLowercase
impl Cheatcode for toLowercaseCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { input } = self;
        Ok(input.to_lowercase().abi_encode())
    }
}

// toUppercase
impl Cheatcode for toUppercaseCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { input } = self;
        Ok(input.to_uppercase().abi_encode())
    }
}

// trim
impl Cheatcode for trimCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { input } = self;
        Ok(input.trim().abi_encode())
    }
}

// Replace
impl Cheatcode for replaceCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { input, from, to } = self;
        Ok(input.replace(from, to).abi_encode())
    }
}

// Split
impl Cheatcode for splitCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { input, delimiter } = self;
        let parts: Vec<&str> = input.split(delimiter).collect();
        Ok(parts.abi_encode())
    }
}

// indexOf
impl Cheatcode for indexOfCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { input, key } = self;
        Ok(input.find(key).map(U256::from).unwrap_or(U256::MAX).abi_encode())
    }
}

// contains
impl Cheatcode for containsCall {
    fn apply(&self, _state: &mut Cheatcodes) -> Result {
        let Self { subject, search } = self;
        Ok(subject.contains(search).abi_encode())
    }
}

pub(super) fn parse(s: &str, ty: &DynSolType) -> Result {
    parse_value(s, ty).map(|v| v.abi_encode())
}

pub(super) fn parse_array<I, S>(values: I, ty: &DynSolType) -> Result
where
    I: IntoIterator<Item = S>,
    S: AsRef<str>,
{
    let mut values = values.into_iter();
    match values.next() {
        Some(first) if !first.as_ref().is_empty() => std::iter::once(first)
            .chain(values)
            .map(|s| parse_value(s.as_ref(), ty))
            .collect::<Result<Vec<_>, _>>()
            .map(|vec| DynSolValue::Array(vec).abi_encode()),
        // return the empty encoded Bytes when values is empty or the first element is empty
        _ => Ok("".abi_encode()),
    }
}

#[instrument(target = "cheatcodes", level = "debug", skip(ty), fields(%ty), ret)]
pub(super) fn parse_value(s: &str, ty: &DynSolType) -> Result<DynSolValue> {
    match ty.coerce_str(s) {
        Ok(value) => Ok(value),
        Err(e) => match parse_value_fallback(s, ty) {
            Some(Ok(value)) => Ok(value),
            Some(Err(e2)) => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e2}")),
            None => Err(fmt_err!("failed parsing {s:?} as type `{ty}`: {e}")),
        },
    }
}

// More lenient parsers than `coerce_str`.
fn parse_value_fallback(s: &str, ty: &DynSolType) -> Option<Result<DynSolValue, &'static str>> {
    match ty {
        DynSolType::Bool => {
            let b = match s {
                "1" => true,
                "0" => false,
                s if s.eq_ignore_ascii_case("true") => true,
                s if s.eq_ignore_ascii_case("false") => false,
                _ => return None,
            };
            return Some(Ok(DynSolValue::Bool(b)));
        }
        DynSolType::Int(_) |
        DynSolType::Uint(_) |
        DynSolType::FixedBytes(_) |
        DynSolType::Bytes => {
            if !s.starts_with("0x") && s.chars().all(|c| c.is_ascii_hexdigit()) {
                return Some(Err("missing hex prefix (\"0x\") for hex string"));
            }
        }
        _ => {}
    }
    None
}