scope-bca-core 0.8.6

Scope Blockchain Analysis - Core library for blockchain data analysis
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! # Proxy Pattern Detection
//!
//! Detects common Ethereum proxy patterns by analyzing bytecode,
//! storage slots, and source code metadata.
//!
//! ## Supported Patterns
//!
//! - **EIP-1967** - Transparent Proxy (implementation slot: `0x360894...`)
//! - **EIP-1822** - UUPS (Universal Upgradeable Proxy Standard)
//! - **Transparent Proxy** - OpenZeppelin TransparentUpgradeableProxy
//! - **Beacon Proxy** - EIP-1967 beacon slot
//! - **Minimal Proxy** (EIP-1167) - Clone factory pattern (bytecode detection)
//! - **Diamond** (EIP-2535) - Multi-facet proxy
//!
//! ## Detection Methods
//!
//! 1. **Etherscan metadata** - `Proxy` and `Implementation` fields from getsourcecode
//! 2. **Storage slot reads** - Check well-known EIP-1967 slots
//! 3. **Bytecode patterns** - EIP-1167 minimal proxy prefix/suffix

use crate::contract::source::ContractSource;
use crate::error::Result;
use serde::{Deserialize, Serialize};

/// Well-known EIP-1967 storage slots.
/// Implementation slot: bytes32(uint256(keccak256('eip1967.proxy.implementation')) - 1)
pub const EIP1967_IMPL_SLOT: &str =
    "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc";
/// Admin slot: bytes32(uint256(keccak256('eip1967.proxy.admin')) - 1)
pub const EIP1967_ADMIN_SLOT: &str =
    "0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103";
/// Beacon slot: bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)
pub const EIP1967_BEACON_SLOT: &str =
    "0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50";

/// EIP-1167 minimal proxy bytecode prefix (first 10 bytes).
const MINIMAL_PROXY_PREFIX: &str = "363d3d373d3d3d363d73";
/// EIP-1167 minimal proxy bytecode suffix (last 15 bytes).
const MINIMAL_PROXY_SUFFIX: &str = "5af43d82803e903d91602b57fd5bf3";

/// Result of proxy pattern detection.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProxyInfo {
    /// Whether the contract is a proxy.
    pub is_proxy: bool,
    /// Type of proxy pattern detected.
    pub proxy_type: String,
    /// Implementation contract address (if resolvable).
    pub implementation_address: Option<String>,
    /// Admin address that can upgrade the proxy (if detectable).
    pub admin_address: Option<String>,
    /// Beacon address (for beacon proxies).
    pub beacon_address: Option<String>,
    /// Detailed findings about the proxy pattern.
    pub details: Vec<String>,
}

impl Default for ProxyInfo {
    fn default() -> Self {
        Self {
            is_proxy: false,
            proxy_type: "None".to_string(),
            implementation_address: None,
            admin_address: None,
            beacon_address: None,
            details: Vec::new(),
        }
    }
}

/// Detect proxy patterns using all available data sources.
///
/// Checks (in order):
/// 1. Etherscan source metadata (Proxy/Implementation fields)
/// 2. Bytecode analysis (EIP-1167 minimal proxy)
/// 3. Storage slot reads (EIP-1967 implementation/admin/beacon)
/// 4. Source code patterns (if verified)
pub async fn detect_proxy(
    address: &str,
    _chain: &str,
    bytecode: &str,
    source: Option<&ContractSource>,
    client: &dyn crate::chains::ChainClient,
    _http_client: &dyn crate::http::HttpClient,
) -> Result<ProxyInfo> {
    let mut info = ProxyInfo::default();

    // 1. Check Etherscan metadata
    if let Some(src) = source
        && src.is_proxy
    {
        info.is_proxy = true;
        info.proxy_type = "Etherscan-detected proxy".to_string();
        info.implementation_address = src.implementation_address.clone();
        info.details
            .push("Etherscan flags this contract as a proxy.".to_string());
    }

    // 2. Check EIP-1167 minimal proxy (bytecode-only detection)
    let code = bytecode.trim_start_matches("0x").to_lowercase();
    if code.starts_with(MINIMAL_PROXY_PREFIX) && code.ends_with(MINIMAL_PROXY_SUFFIX) {
        info.is_proxy = true;
        info.proxy_type = "EIP-1167 Minimal Proxy (Clone)".to_string();
        // Extract implementation address from bytecode (20 bytes after prefix)
        if code.len() >= MINIMAL_PROXY_PREFIX.len() + 40 {
            let impl_addr = &code[MINIMAL_PROXY_PREFIX.len()..MINIMAL_PROXY_PREFIX.len() + 40];
            info.implementation_address = Some(format!("0x{}", impl_addr));
        }
        info.details
            .push("EIP-1167 minimal proxy detected from bytecode pattern.".to_string());
        return Ok(info);
    }

    // 3. Check EIP-1967 storage slots
    if let Ok(impl_slot) = client.get_storage_at(address, EIP1967_IMPL_SLOT).await {
        let impl_addr = extract_address_from_slot(&impl_slot);
        if let Some(addr) = impl_addr {
            info.is_proxy = true;
            info.implementation_address = Some(addr.clone());
            info.details
                .push(format!("EIP-1967 implementation slot points to {}", addr));

            // Determine proxy subtype
            if let Some(src) = source {
                let code_lower = src.source_code.to_lowercase();
                if code_lower.contains("uups") || code_lower.contains("_upgradeto") {
                    info.proxy_type = "UUPS (EIP-1822)".to_string();
                } else {
                    info.proxy_type = "Transparent Proxy (EIP-1967)".to_string();
                }
            } else {
                info.proxy_type = "EIP-1967 Proxy".to_string();
            }
        }
    }

    // Check admin slot
    if let Ok(admin_slot) = client.get_storage_at(address, EIP1967_ADMIN_SLOT).await {
        let admin_addr = extract_address_from_slot(&admin_slot);
        if let Some(addr) = admin_addr {
            info.admin_address = Some(addr.clone());
            info.details
                .push(format!("EIP-1967 admin slot points to {}", addr));
        }
    }

    // Check beacon slot
    if let Ok(beacon_slot) = client.get_storage_at(address, EIP1967_BEACON_SLOT).await {
        let beacon_addr = extract_address_from_slot(&beacon_slot);
        if let Some(addr) = beacon_addr {
            info.is_proxy = true;
            info.proxy_type = "Beacon Proxy (EIP-1967)".to_string();
            info.beacon_address = Some(addr.clone());
            info.details
                .push(format!("EIP-1967 beacon slot points to {}", addr));
        }
    }

    // 4. Source code pattern analysis
    if let Some(src) = source {
        detect_proxy_from_source(src, &mut info);
    }

    // Check bytecode for DELEGATECALL pattern (generic proxy indicator)
    if !info.is_proxy && code.contains("f4") {
        // 0xf4 = DELEGATECALL opcode; not definitive alone but notable
        if let Some(src) = source
            && src.source_code.contains("delegatecall")
        {
            info.details.push(
                "Contract uses delegatecall (may be a proxy or proxy-like pattern).".to_string(),
            );
        }
    }

    Ok(info)
}

/// Extract an Ethereum address from a 32-byte storage slot value.
/// Returns None if the slot is empty (all zeros).
fn extract_address_from_slot(slot_value: &str) -> Option<String> {
    let hex = slot_value.trim_start_matches("0x").to_lowercase();
    if hex.len() < 40 {
        return None;
    }
    // Address is in the last 40 hex chars of the 32-byte slot
    let addr = &hex[hex.len() - 40..];
    // Check if it's not the zero address
    if addr == "0000000000000000000000000000000000000000" {
        return None;
    }
    Some(format!("0x{}", addr))
}

/// Detect proxy patterns from source code analysis.
fn detect_proxy_from_source(source: &ContractSource, info: &mut ProxyInfo) {
    let code = &source.source_code;
    let code_lower = code.to_lowercase();

    // Diamond proxy (EIP-2535)
    if code_lower.contains("diamondcut")
        || code_lower.contains("idiamond")
        || code_lower.contains("facetcut")
    {
        info.is_proxy = true;
        info.proxy_type = "Diamond Proxy (EIP-2535)".to_string();
        info.details
            .push("Diamond/multi-facet proxy pattern detected in source.".to_string());
    }

    // OpenZeppelin upgradeable patterns
    if code_lower.contains("transparentupgradeableproxy") {
        info.is_proxy = true;
        info.proxy_type = "OpenZeppelin TransparentUpgradeableProxy".to_string();
        info.details
            .push("OpenZeppelin TransparentUpgradeableProxy import detected.".to_string());
    }

    if code_lower.contains("uupsupgradeable") {
        info.is_proxy = true;
        info.proxy_type = "UUPS (OpenZeppelin UUPSUpgradeable)".to_string();
        info.details
            .push("OpenZeppelin UUPSUpgradeable import detected.".to_string());
    }

    if code_lower.contains("beaconproxy") || code_lower.contains("upgradeablebeacon") {
        info.is_proxy = true;
        info.proxy_type = "Beacon Proxy (OpenZeppelin)".to_string();
        info.details
            .push("OpenZeppelin BeaconProxy pattern detected in source.".to_string());
    }

    // Generic proxy/upgrade patterns
    if code_lower.contains("_setimplementation") || code_lower.contains("upgradeto(") {
        if !info.is_proxy {
            info.is_proxy = true;
            info.proxy_type = "Upgradeable Proxy (custom)".to_string();
        }
        info.details
            .push("Upgrade mechanism (upgradeTo/_setImplementation) found in source.".to_string());
    }
}

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

    #[test]
    fn test_extract_address_from_slot() {
        let slot = "0x000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7";
        let addr = extract_address_from_slot(slot);
        assert_eq!(
            addr,
            Some("0xdac17f958d2ee523a2206206994597c13d831ec7".to_string())
        );
    }

    #[test]
    fn test_extract_address_from_zero_slot() {
        let slot = "0x0000000000000000000000000000000000000000000000000000000000000000";
        let addr = extract_address_from_slot(slot);
        assert_eq!(addr, None);
    }

    #[test]
    fn test_minimal_proxy_detection() {
        let prefix = MINIMAL_PROXY_PREFIX;
        let suffix = MINIMAL_PROXY_SUFFIX;
        let impl_addr = "bebebebebebebebebebebebebebebebebebebebe";
        let bytecode = format!("0x{}{}{}", prefix, impl_addr, suffix);
        let code = bytecode.trim_start_matches("0x").to_lowercase();
        assert!(code.starts_with(MINIMAL_PROXY_PREFIX));
        assert!(code.ends_with(MINIMAL_PROXY_SUFFIX));
    }

    #[test]
    fn test_detect_proxy_from_source_diamond() {
        let src = ContractSource {
            contract_name: "DiamondProxy".to_string(),
            source_code: "contract DiamondProxy { function diamondCut(...) {} }".to_string(),
            abi: "[]".to_string(),
            compiler_version: "v0.8.19".to_string(),
            optimization_used: true,
            optimization_runs: 200,
            evm_version: "paris".to_string(),
            license_type: "MIT".to_string(),
            is_proxy: false,
            implementation_address: None,
            constructor_arguments: String::new(),
            library: String::new(),
            swarm_source: String::new(),
            parsed_abi: vec![],
        };
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(info.is_proxy);
        assert!(info.proxy_type.contains("Diamond"));
    }

    #[test]
    fn test_detect_proxy_from_source_uups() {
        let src = ContractSource {
            contract_name: "UUPSToken".to_string(),
            source_code: "import UUPSUpgradeable; contract Token is UUPSUpgradeable {}".to_string(),
            abi: "[]".to_string(),
            compiler_version: "v0.8.19".to_string(),
            optimization_used: true,
            optimization_runs: 200,
            evm_version: "paris".to_string(),
            license_type: "MIT".to_string(),
            is_proxy: false,
            implementation_address: None,
            constructor_arguments: String::new(),
            library: String::new(),
            swarm_source: String::new(),
            parsed_abi: vec![],
        };
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(info.is_proxy);
        assert!(info.proxy_type.contains("UUPS"));
    }

    fn make_source(code: &str) -> ContractSource {
        ContractSource {
            contract_name: "Test".to_string(),
            source_code: code.to_string(),
            abi: "[]".to_string(),
            compiler_version: "v0.8.19".to_string(),
            optimization_used: true,
            optimization_runs: 200,
            evm_version: "paris".to_string(),
            license_type: "MIT".to_string(),
            is_proxy: false,
            implementation_address: None,
            constructor_arguments: String::new(),
            library: String::new(),
            swarm_source: String::new(),
            parsed_abi: vec![],
        }
    }

    #[test]
    fn test_extract_address_short_hex() {
        assert_eq!(extract_address_from_slot("0xabc"), None);
    }

    #[test]
    fn test_extract_address_no_prefix() {
        let slot = "000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7";
        let addr = extract_address_from_slot(slot);
        assert_eq!(
            addr,
            Some("0xdac17f958d2ee523a2206206994597c13d831ec7".to_string())
        );
    }

    #[test]
    fn test_detect_transparent_upgradeable() {
        let src = make_source(
            "import TransparentUpgradeableProxy; contract P is TransparentUpgradeableProxy {}",
        );
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(info.is_proxy);
        assert!(info.proxy_type.contains("TransparentUpgradeableProxy"));
    }

    #[test]
    fn test_detect_beacon_proxy() {
        let src =
            make_source("import BeaconProxy; contract P is BeaconProxy { UpgradeableBeacon b; }");
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(info.is_proxy);
        assert!(info.proxy_type.contains("Beacon"));
    }

    #[test]
    fn test_detect_upgrade_to_pattern() {
        let src = make_source("function upgradeTo(address impl) { _setImplementation(impl); }");
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(info.is_proxy);
        assert!(info.details.iter().any(|d| d.contains("upgradeTo")));
    }

    #[test]
    fn test_non_proxy_source() {
        let src = make_source("contract Token { function transfer() {} }");
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(!info.is_proxy);
    }

    #[test]
    fn test_proxy_info_default() {
        let info = ProxyInfo::default();
        assert!(!info.is_proxy);
        assert_eq!(info.proxy_type, "None");
        assert!(info.implementation_address.is_none());
        assert!(info.admin_address.is_none());
        assert!(info.beacon_address.is_none());
        assert!(info.details.is_empty());
    }

    #[test]
    fn test_detect_proxy_from_source_idiamond() {
        let src = make_source("import IDiamond; contract D is IDiamond { }");
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(info.is_proxy);
        assert!(info.proxy_type.contains("Diamond"));
    }

    #[test]
    fn test_detect_proxy_from_source_facetcut() {
        let src = make_source(
            "struct FacetCut { address target; } function addFacet(FacetCut[] calldata)",
        );
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(info.is_proxy);
        assert!(info.proxy_type.contains("Diamond"));
    }

    #[test]
    fn test_detect_proxy_from_source_set_implementation() {
        let src =
            make_source("function _setImplementation(address impl) internal { _impl = impl; }");
        let mut info = ProxyInfo::default();
        detect_proxy_from_source(&src, &mut info);
        assert!(info.is_proxy);
        assert!(
            info.details
                .iter()
                .any(|d| d.contains("_setImplementation"))
        );
    }

    #[test]
    fn test_extract_address_from_slot_uppercase_hex() {
        let slot = "0x000000000000000000000000DAC17F958D2EE523A2206206994597C13D831EC7";
        let addr = extract_address_from_slot(slot);
        assert_eq!(
            addr,
            Some("0xdac17f958d2ee523a2206206994597c13d831ec7".to_string())
        );
    }

    #[test]
    fn test_proxy_info_serialization() {
        let info = ProxyInfo {
            is_proxy: true,
            proxy_type: "EIP-1967".to_string(),
            implementation_address: Some("0x1234567890123456789012345678901234567890".to_string()),
            admin_address: Some("0xadmin".to_string()),
            beacon_address: None,
            details: vec!["Detail 1".to_string()],
        };
        let json = serde_json::to_string(&info).unwrap();
        let restored: ProxyInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.is_proxy, info.is_proxy);
        assert_eq!(restored.implementation_address, info.implementation_address);
        assert_eq!(restored.details.len(), 1);
    }

    #[tokio::test]
    async fn test_detect_proxy_minimal_eip1167() {
        let prefix = MINIMAL_PROXY_PREFIX;
        let suffix = MINIMAL_PROXY_SUFFIX;
        let impl_addr = "bebebebebebebebebebebebebebebebebebebebe";
        let bytecode = format!("0x{}{}{}", prefix, impl_addr, suffix);
        let client = crate::chains::mocks::MockChainClient::new("ethereum", "ETH");
        let http = crate::http::NativeHttpClient::new().unwrap();
        let result = detect_proxy("0xproxy", "ethereum", &bytecode, None, &client, &http)
            .await
            .unwrap();
        assert!(result.is_proxy);
        assert!(result.proxy_type.contains("EIP-1167"));
        assert_eq!(
            result.implementation_address,
            Some("0xbebebebebebebebebebebebebebebebebebebebe".to_string())
        );
    }

    #[tokio::test]
    async fn test_detect_proxy_etherscan_metadata() {
        let src = ContractSource {
            contract_name: "Proxy".to_string(),
            source_code: "contract Proxy {}".to_string(),
            abi: "[]".to_string(),
            compiler_version: "v0.8.19".to_string(),
            optimization_used: true,
            optimization_runs: 200,
            evm_version: "paris".to_string(),
            license_type: "MIT".to_string(),
            is_proxy: true,
            implementation_address: Some("0x1234567890123456789012345678901234567890".to_string()),
            constructor_arguments: String::new(),
            library: String::new(),
            swarm_source: String::new(),
            parsed_abi: vec![],
        };
        let bytecode = "0x6080604052348015600f57600080fd5b50"; // Non-minimal-proxy bytecode
        let client = crate::chains::mocks::MockChainClient::new("ethereum", "ETH");
        let http = crate::http::NativeHttpClient::new().unwrap();
        let result = detect_proxy("0xproxy", "ethereum", bytecode, Some(&src), &client, &http)
            .await
            .unwrap();
        assert!(result.is_proxy);
        assert!(result.proxy_type.contains("Etherscan"));
        assert_eq!(
            result.implementation_address,
            Some("0x1234567890123456789012345678901234567890".to_string())
        );
    }

    #[test]
    fn test_extract_address_from_slot_exactly_40_chars() {
        let slot_str =
            "0x".to_string() + &"0".repeat(24) + "dac17f958d2ee523a2206206994597c13d831ec7";
        let addr = extract_address_from_slot(&slot_str);
        assert_eq!(
            addr,
            Some("0xdac17f958d2ee523a2206206994597c13d831ec7".to_string())
        );
    }

    /// Mock that returns configurable storage slot data for EIP-1967 proxy tests.
    struct StorageMockClient {
        inner: crate::chains::mocks::MockChainClient,
        impl_slot_value: Option<String>,
        admin_slot_value: Option<String>,
        beacon_slot_value: Option<String>,
    }

    impl StorageMockClient {
        fn with_impl_slot(addr: &str) -> Self {
            let padded = format!("0x{}{}", "0".repeat(24), addr.trim_start_matches("0x"));
            Self {
                inner: crate::chains::mocks::MockChainClient::new("ethereum", "ETH"),
                impl_slot_value: Some(padded),
                admin_slot_value: None,
                beacon_slot_value: None,
            }
        }

        fn with_all_slots(impl_addr: &str, admin_addr: &str, beacon_addr: &str) -> Self {
            let pad = |a: &str| format!("0x{}{}", "0".repeat(24), a.trim_start_matches("0x"));
            Self {
                inner: crate::chains::mocks::MockChainClient::new("ethereum", "ETH"),
                impl_slot_value: Some(pad(impl_addr)),
                admin_slot_value: Some(pad(admin_addr)),
                beacon_slot_value: Some(pad(beacon_addr)),
            }
        }
    }

    #[async_trait::async_trait]
    impl crate::chains::ChainClient for StorageMockClient {
        fn chain_name(&self) -> &str {
            self.inner.chain_name()
        }

        fn native_token_symbol(&self) -> &str {
            self.inner.native_token_symbol()
        }

        async fn get_balance(&self, a: &str) -> crate::error::Result<crate::chains::Balance> {
            self.inner.get_balance(a).await
        }

        async fn enrich_balance_usd(&self, b: &mut crate::chains::Balance) {
            self.inner.enrich_balance_usd(b).await
        }

        async fn get_transaction(
            &self,
            h: &str,
        ) -> crate::error::Result<crate::chains::Transaction> {
            self.inner.get_transaction(h).await
        }

        async fn get_transactions(
            &self,
            a: &str,
            limit: u32,
        ) -> crate::error::Result<Vec<crate::chains::Transaction>> {
            self.inner.get_transactions(a, limit).await
        }

        async fn get_block_number(&self) -> crate::error::Result<u64> {
            self.inner.get_block_number().await
        }

        async fn get_token_balances(
            &self,
            a: &str,
        ) -> crate::error::Result<Vec<crate::chains::TokenBalance>> {
            self.inner.get_token_balances(a).await
        }

        async fn get_storage_at(&self, _address: &str, slot: &str) -> crate::error::Result<String> {
            if slot == EIP1967_IMPL_SLOT
                && let Some(ref v) = self.impl_slot_value
            {
                return Ok(v.clone());
            }
            if slot == EIP1967_ADMIN_SLOT
                && let Some(ref v) = self.admin_slot_value
            {
                return Ok(v.clone());
            }
            if slot == EIP1967_BEACON_SLOT
                && let Some(ref v) = self.beacon_slot_value
            {
                return Ok(v.clone());
            }
            Err(crate::error::ScopeError::Chain(
                "No storage mock".to_string(),
            ))
        }
    }

    #[tokio::test]
    async fn test_detect_proxy_eip1967_transparent() {
        let client = StorageMockClient::with_impl_slot("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48");
        let http = crate::http::NativeHttpClient::new().unwrap();
        let result = detect_proxy("0xproxy", "ethereum", "0x6080604052", None, &client, &http)
            .await
            .unwrap();
        assert!(result.is_proxy);
        assert!(result.proxy_type.contains("EIP-1967"));
        assert_eq!(
            result.implementation_address,
            Some("0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string())
        );
    }

    #[tokio::test]
    async fn test_detect_proxy_eip1967_uups_with_source() {
        let src = ContractSource {
            contract_name: "UUPSProxy".to_string(),
            source_code: "contract UUPSProxy { function _upgradeTo(address) {} }".to_string(),
            abi: "[]".to_string(),
            compiler_version: "v0.8.19".to_string(),
            optimization_used: true,
            optimization_runs: 200,
            evm_version: "paris".to_string(),
            license_type: "MIT".to_string(),
            is_proxy: false,
            implementation_address: None,
            constructor_arguments: String::new(),
            library: String::new(),
            swarm_source: String::new(),
            parsed_abi: vec![],
        };
        let client = StorageMockClient::with_impl_slot("a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48");
        let http = crate::http::NativeHttpClient::new().unwrap();
        let result = detect_proxy(
            "0xproxy",
            "ethereum",
            "0x6080604052",
            Some(&src),
            &client,
            &http,
        )
        .await
        .unwrap();
        assert!(result.is_proxy);
        assert!(result.proxy_type.contains("UUPS"));
    }

    #[tokio::test]
    async fn test_detect_proxy_eip1967_admin_and_beacon_slots() {
        let client = StorageMockClient::with_all_slots(
            "a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
            "b0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
            "c0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
        );
        let http = crate::http::NativeHttpClient::new().unwrap();
        let result = detect_proxy("0xproxy", "ethereum", "0x6080604052", None, &client, &http)
            .await
            .unwrap();
        assert!(result.is_proxy);
        assert!(result.admin_address.is_some());
        assert!(result.beacon_address.is_some());
        assert!(result.proxy_type.contains("Beacon"));
    }

    #[tokio::test]
    async fn test_detect_proxy_delegatecall_in_source() {
        let src = ContractSource {
            contract_name: "DelegateProxy".to_string(),
            source_code: "contract D { function run() { delegatecall(...); } }".to_string(),
            abi: "[]".to_string(),
            compiler_version: "v0.8.19".to_string(),
            optimization_used: true,
            optimization_runs: 200,
            evm_version: "paris".to_string(),
            license_type: "MIT".to_string(),
            is_proxy: false,
            implementation_address: None,
            constructor_arguments: String::new(),
            library: String::new(),
            swarm_source: String::new(),
            parsed_abi: vec![],
        };
        let client = crate::chains::mocks::MockChainClient::new("ethereum", "ETH");
        let http = crate::http::NativeHttpClient::new().unwrap();
        let result = detect_proxy(
            "0xproxy",
            "ethereum",
            "0x6080f4604052",
            Some(&src),
            &client,
            &http,
        )
        .await
        .unwrap();
        assert!(result.details.iter().any(|d| d.contains("delegatecall")));
    }
}