tycho-simulation 0.520.0

Provides tools for interacting with protocol states, calculating spot prices, and quoting token swaps.
Documentation
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use std::{collections::HashMap, str::FromStr, sync::LazyLock};

use alloy::primitives::{Address, U256};

use super::utils::get_storage_slot_index_at_key;
use crate::evm::{ContractCompiler, SlotId};

pub(crate) type Overwrites = HashMap<SlotId, U256>;

pub static IMPLEMENTATION_SLOT: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0x6677C72CDEB41ACAF2B17EC8A6E275C4205F27DBFE4DE34EBAF2E928A7E610DB").unwrap()
});
static BALANCES_MAPPING_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0x474F5FD57EE674F7B6851BC6F07E751B49076DFB356356985B9DAF10E9ABC941").unwrap()
});
static HAS_CUSTOM_BALANCE_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0x7EAD8EDE9DBB385B0664952C7462C9938A5821E6F78E859DA2E683216E99411B").unwrap()
});
static CUSTOM_APPROVAL_MAPPING_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0x71A54E125991077003BEF7E7CA57369C919DAC6D2458895F1EAB4D03960F4AEB").unwrap()
});
static HAS_CUSTOM_APPROVAL_MAPPING_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0x9F0C1BC0E9C3078F9AD5FC59C8606416B3FABCBD4C8353FED22937C66C866CE3").unwrap()
});
#[cfg(test)]
static CUSTOM_NAME_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0xCC1E513FB5BDA80DC466AD9D44DF38805A8DEE4C82B3C6DF3D9B25D3D5355D1C").unwrap()
});
#[cfg(test)]
static CUSTOM_SYMBOL_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0xDC17DD3380A9A034A702A2B2B1C6C25D39EBF0E89796E0D15E1E04D23E3BB221").unwrap()
});
#[cfg(test)]
static CUSTOM_DECIMALS_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0xADD486B234562DE9AC745F036F538CDA2547EF6DBB4DA3FA1C017625F888A8E8").unwrap()
});
#[cfg(test)]
static CUSTOM_TOTAL_SUPPLY_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0x6014AF1E8E9BB2844581B2FA9E5E3620181C3192EEFD3258319AEC23538DA9F5").unwrap()
});
#[cfg(test)]
static HAS_CUSTOM_METADATA_POSITION: LazyLock<SlotId> = LazyLock::new(|| {
    U256::from_str("0x9F37243DE61714BE9CC00628D4B9BF9897AE670218AF52ADE6D192B4339D7616").unwrap()
});

pub(crate) struct TokenProxyOverwriteFactory {
    token_address: Address,
    overwrites: Overwrites,
    compiler: ContractCompiler,
}

impl TokenProxyOverwriteFactory {
    pub(crate) fn new(token_address: Address, proxy_address: Option<Address>) -> Self {
        let mut instance = Self {
            token_address,
            overwrites: HashMap::new(),
            compiler: ContractCompiler::Solidity,
        };

        if let Some(proxy_addr) = proxy_address {
            instance.set_original_address(proxy_addr);
        }

        instance
    }

    /// Sets the original address of the token contract in the implementation slot.
    pub(crate) fn set_original_address(&mut self, implementation: Address) {
        self.overwrites
            .insert(*IMPLEMENTATION_SLOT, U256::from_be_slice(implementation.as_slice()));
    }

    pub(crate) fn set_balance(&mut self, balance: U256, owner: Address) {
        // Set the balance in the custom storage slot
        let storage_index =
            get_storage_slot_index_at_key(owner, *BALANCES_MAPPING_POSITION, self.compiler);
        self.overwrites
            .insert(storage_index, balance);

        // Set the has_custom_balance flag to true
        let has_balance_index =
            get_storage_slot_index_at_key(owner, *HAS_CUSTOM_BALANCE_POSITION, self.compiler);
        self.overwrites
            .insert(has_balance_index, U256::from(1)); // true in Solidity
    }

    pub(crate) fn set_allowance(&mut self, allowance: U256, spender: Address, owner: Address) {
        // Set the allowance in the custom storage slot
        let owner_slot =
            get_storage_slot_index_at_key(owner, *CUSTOM_APPROVAL_MAPPING_POSITION, self.compiler);
        let storage_index = get_storage_slot_index_at_key(spender, owner_slot, self.compiler);
        self.overwrites
            .insert(storage_index, allowance);

        // Set the has_custom_approval flag to true
        let has_approval_index = get_storage_slot_index_at_key(
            owner,
            *HAS_CUSTOM_APPROVAL_MAPPING_POSITION,
            self.compiler,
        );
        self.overwrites
            .insert(has_approval_index, U256::from(1)); // true in Solidity
    }

    /// Marks `owner` as having custom approvals, without granting a specific allowance.
    ///
    /// `transferFrom` from this owner is then handled by the proxy's local balance bookkeeping
    /// instead of delegating to the implementation. The proxy's approval system is disabled by
    /// default, so the local branch does not check a per-spender amount — only this flag is needed.
    pub(crate) fn set_has_custom_approval(&mut self, owner: Address) {
        let has_approval_index = get_storage_slot_index_at_key(
            owner,
            *HAS_CUSTOM_APPROVAL_MAPPING_POSITION,
            self.compiler,
        );
        self.overwrites
            .insert(has_approval_index, U256::from(1)); // true in Solidity
    }

    #[cfg(test)]
    pub(crate) fn set_total_supply(&mut self, supply: U256) {
        self.overwrites
            .insert(*CUSTOM_TOTAL_SUPPLY_POSITION, supply);
    }

    /// Sets the has_custom_metadata flag for a given key
    #[cfg(test)]
    fn set_metadata_flag(&mut self, key: &str) {
        let key_bytes = string_to_storage_bytes(key);
        let mapping_slot_bytes: [u8; 32] = HAS_CUSTOM_METADATA_POSITION.to_be_bytes();
        let has_metadata_index = self
            .compiler
            .compute_map_slot(&key_bytes, &mapping_slot_bytes);
        self.overwrites
            .insert(has_metadata_index, U256::from(1)); // true in Solidity
    }

    #[cfg(test)]
    pub(crate) fn set_name(&mut self, name: &str) {
        // Store the name value
        let name_value = U256::from_be_bytes(string_to_storage_bytes(name));
        self.overwrites
            .insert(*CUSTOM_NAME_POSITION, name_value);

        // Set the has_custom_metadata flag for name to true
        self.set_metadata_flag("name");
    }

    #[cfg(test)]
    pub(crate) fn set_symbol(&mut self, symbol: &str) {
        // Store the symbol value
        let symbol_value = U256::from_be_bytes(string_to_storage_bytes(symbol));
        self.overwrites
            .insert(*CUSTOM_SYMBOL_POSITION, symbol_value);

        // Set the has_custom_metadata flag for symbol to true
        self.set_metadata_flag("symbol");
    }

    #[cfg(test)]
    pub(crate) fn set_decimals(&mut self, decimals: u8) {
        self.overwrites
            .insert(*CUSTOM_DECIMALS_POSITION, U256::from(decimals));

        // Set the has_custom_metadata flag for decimals to true
        self.set_metadata_flag("decimals");
    }

    pub(crate) fn get_overwrites(&self) -> HashMap<Address, Overwrites> {
        let mut result = HashMap::new();
        result.insert(self.token_address, self.overwrites.clone());
        result
    }
}

/// Converts a string to a 32-byte array for storage, truncating if necessary
pub fn string_to_storage_bytes(s: &str) -> [u8; 32] {
    let mut padded = [0u8; 32];
    let len = s.len().min(31);
    padded[..len].copy_from_slice(&s.as_bytes()[..len]);
    #[expect(
        clippy::cast_possible_truncation,
        reason = "len is at most 31, so len * 2 fits in a byte"
    )]
    let encoded_len = (len * 2) as u8; // Length * 2 for short strings
    padded[31] = encoded_len;
    padded
}

#[cfg(test)]
mod tests {
    use super::*;

    fn get_metadata_slot(key: &str) -> SlotId {
        let key_bytes = string_to_storage_bytes(key);
        let mapping_slot_bytes: [u8; 32] = HAS_CUSTOM_METADATA_POSITION.to_be_bytes();
        ContractCompiler::Solidity.compute_map_slot(&key_bytes, &mapping_slot_bytes)
    }

    #[test]
    fn test_token_proxy_factory_new() {
        let token_address = Address::random();
        let factory = TokenProxyOverwriteFactory::new(token_address, None);
        assert_eq!(factory.token_address, token_address);
        assert!(factory.overwrites.is_empty());
    }

    #[test]
    fn test_token_proxy_factory_with_implementation() {
        let token_address = Address::random();
        let implementation = Address::random();
        let factory = TokenProxyOverwriteFactory::new(token_address, Some(implementation));

        // Check if implementation was set correctly
        let mut expected_bytes = [0u8; 32];
        expected_bytes[12..].copy_from_slice(implementation.as_slice());
        let expected_value = U256::from_be_bytes(expected_bytes);

        assert_eq!(factory.overwrites[&*IMPLEMENTATION_SLOT], expected_value);
    }

    #[test]
    fn test_token_proxy_set_balance() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let owner = Address::random();
        let balance = U256::from(1000);

        factory.set_balance(balance, owner);

        // Check balance storage
        let storage_index =
            get_storage_slot_index_at_key(owner, *BALANCES_MAPPING_POSITION, factory.compiler);
        assert_eq!(factory.overwrites[&storage_index], balance);

        // Check has_custom_balance flag
        let has_balance_index =
            get_storage_slot_index_at_key(owner, *HAS_CUSTOM_BALANCE_POSITION, factory.compiler);
        assert_eq!(factory.overwrites[&has_balance_index], U256::from(1));
    }

    #[test]
    fn test_token_proxy_set_allowance() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let owner = Address::random();
        let spender = Address::random();
        let allowance = U256::from(500);

        factory.set_allowance(allowance, spender, owner);

        // Check allowance storage
        let owner_slot = get_storage_slot_index_at_key(
            owner,
            *CUSTOM_APPROVAL_MAPPING_POSITION,
            factory.compiler,
        );
        let storage_index = get_storage_slot_index_at_key(spender, owner_slot, factory.compiler);
        assert_eq!(factory.overwrites[&storage_index], allowance);

        // Check has_custom_approval flag
        let has_approval_index = get_storage_slot_index_at_key(
            owner,
            *HAS_CUSTOM_APPROVAL_MAPPING_POSITION,
            factory.compiler,
        );
        assert_eq!(factory.overwrites[&has_approval_index], U256::from(1));
    }

    #[test]
    fn test_token_proxy_set_total_supply() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let supply = U256::from(1_000_000);

        factory.set_total_supply(supply);

        assert_eq!(factory.overwrites[&*CUSTOM_TOTAL_SUPPLY_POSITION], supply);
    }

    #[test]
    fn test_token_proxy_set_name() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let name = "Test Token";

        factory.set_name(name);

        // Check name storage
        let mut expected_bytes = [0u8; 32];
        let name_bytes = name.as_bytes();
        expected_bytes[..name_bytes.len()].copy_from_slice(name_bytes);
        expected_bytes[31] = u8::try_from(name_bytes.len() * 2).unwrap(); // Length * 2 for short strings
        let expected_value = U256::from_be_bytes(expected_bytes);
        assert_eq!(factory.overwrites[&*CUSTOM_NAME_POSITION], expected_value);

        // Check has_custom_metadata flag
        let has_metadata_index = get_metadata_slot("name");
        assert_eq!(factory.overwrites[&has_metadata_index], U256::from(1));
    }

    #[test]
    fn test_token_proxy_set_symbol() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let symbol = "TEST";

        factory.set_symbol(symbol);

        // Check symbol storage
        let mut expected_bytes = [0u8; 32];
        let symbol_bytes = symbol.as_bytes();
        expected_bytes[..symbol_bytes.len()].copy_from_slice(symbol_bytes);
        expected_bytes[31] = u8::try_from(symbol_bytes.len() * 2).unwrap(); // Length * 2 for short strings
        let expected_value = U256::from_be_bytes(expected_bytes);
        assert_eq!(factory.overwrites[&*CUSTOM_SYMBOL_POSITION], expected_value);

        // Check has_custom_metadata flag
        let has_metadata_index = get_metadata_slot("symbol");
        assert_eq!(factory.overwrites[&has_metadata_index], U256::from(1));
    }

    #[test]
    fn test_token_proxy_set_decimals() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let decimals = 18u8;

        factory.set_decimals(decimals);

        assert_eq!(factory.overwrites[&*CUSTOM_DECIMALS_POSITION], U256::from(decimals));

        // Check has_custom_metadata flag
        let has_metadata_index = get_metadata_slot("decimals");
        assert_eq!(factory.overwrites[&has_metadata_index], U256::from(1));
    }

    #[test]
    fn test_token_proxy_get_overwrites() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let supply = U256::from(1_000_000);
        factory.set_total_supply(supply);

        let overwrites = factory.get_overwrites();

        assert_eq!(overwrites.len(), 1);
        assert!(overwrites.contains_key(&factory.token_address));
        assert_eq!(overwrites[&factory.token_address][&*CUSTOM_TOTAL_SUPPLY_POSITION], supply);
    }

    #[test]
    fn test_token_proxy_set_long_name_truncated() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let name = "This is a very long token name that exceeds 31 bytes";

        factory.set_name(name);

        // Check name storage for truncated string
        let mut expected_bytes = [0u8; 32];
        expected_bytes[..31].copy_from_slice(&name.as_bytes()[..31]);
        expected_bytes[31] = 62; // 31 * 2 for short strings
        let expected_value = U256::from_be_bytes(expected_bytes);
        assert_eq!(factory.overwrites[&*CUSTOM_NAME_POSITION], expected_value);

        // Check has_custom_metadata flag
        let has_metadata_index = get_metadata_slot("name");
        assert_eq!(factory.overwrites[&has_metadata_index], U256::from(1));
    }

    #[test]
    fn test_token_proxy_set_long_symbol_truncated() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);
        let symbol = "This is a very long token symbol that exceeds 31 bytes";

        factory.set_symbol(symbol);

        // Check symbol storage for truncated string
        let mut expected_bytes = [0u8; 32];
        expected_bytes[..31].copy_from_slice(&symbol.as_bytes()[..31]);
        expected_bytes[31] = 62; // 31 * 2 for short strings
        let expected_value = U256::from_be_bytes(expected_bytes);
        assert_eq!(factory.overwrites[&*CUSTOM_SYMBOL_POSITION], expected_value);

        // Check has_custom_metadata flag
        let has_metadata_index = get_metadata_slot("symbol");
        assert_eq!(factory.overwrites[&has_metadata_index], U256::from(1));
    }

    #[test]
    fn test_string_to_storage_bytes() {
        // Test short string
        let short = "Test";
        let bytes = string_to_storage_bytes(short);
        assert_eq!(bytes[..4], short.as_bytes()[..4]);
        assert_eq!(bytes[31], 8); // 4 * 2 for length

        // Test long string (should be truncated)
        let long = "This is a very long string that exceeds 31 bytes";
        let bytes = string_to_storage_bytes(long);
        assert_eq!(bytes[..31], long.as_bytes()[..31]);
        assert_eq!(bytes[31], 62); // 31 * 2 for length
    }

    #[test]
    fn test_set_metadata_flag() {
        let mut factory = TokenProxyOverwriteFactory::new(Address::random(), None);

        // Test setting metadata flag for a key
        factory.set_metadata_flag("test_key");

        // Verify the flag was set correctly
        let key_bytes = string_to_storage_bytes("test_key");
        let mapping_slot_bytes: [u8; 32] = HAS_CUSTOM_METADATA_POSITION.to_be_bytes();
        let has_metadata_index =
            ContractCompiler::Solidity.compute_map_slot(&key_bytes, &mapping_slot_bytes);
        assert_eq!(factory.overwrites[&has_metadata_index], U256::from(1));
    }
}