riglr-evm-common 0.3.0

Common EVM utilities shared across Riglr crates
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
//! Common EVM types shared across riglr crates
//!
//! This module provides shared type definitions and configuration structs
//! that are needed by both riglr-evm-tools and riglr-cross-chain-tools
//! to avoid duplication and circular dependencies.

use alloy::primitives::{Address, Bytes, U256};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::time::Duration;

/// Configuration for EVM operations shared across crates
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct EvmConfig {
    /// RPC endpoint URL for the EVM chain
    pub rpc_url: String,
    /// Chain ID (e.g., 1 for Ethereum, 137 for Polygon)
    pub chain_id: u64,
    /// Request timeout duration
    pub timeout_seconds: u64,
    /// Maximum gas limit for transactions
    pub max_gas_limit: Option<u64>,
    /// Default gas price multiplier (1.0 = exact, 1.1 = 10% higher)
    pub gas_price_multiplier: Option<f64>,
}

impl Default for EvmConfig {
    fn default() -> Self {
        Self {
            rpc_url: "https://eth.llamarpc.com".to_string(),
            chain_id: 1, // Ethereum mainnet
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: Some(1.1), // 10% buffer
        }
    }
}

impl EvmConfig {
    /// Create config for a specific chain ID
    pub fn for_chain(chain_id: u64) -> Result<Self, crate::error::EvmCommonError> {
        let rpc_url = crate::chain::chain_id_to_rpc_url(chain_id)?;

        Ok(Self {
            rpc_url,
            chain_id,
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: Some(1.1),
        })
    }

    /// Get timeout as Duration
    pub fn timeout(&self) -> Duration {
        Duration::from_secs(self.timeout_seconds)
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<(), crate::error::EvmCommonError> {
        if self.rpc_url.is_empty() {
            return Err(crate::error::EvmCommonError::InvalidConfig(
                "RPC URL cannot be empty".to_string(),
            ));
        }

        if !self.rpc_url.starts_with("http://")
            && !self.rpc_url.starts_with("https://")
            && !self.rpc_url.starts_with("wss://")
        {
            return Err(crate::error::EvmCommonError::InvalidConfig(
                "RPC URL must start with http://, https://, or wss://".to_string(),
            ));
        }

        if self.chain_id == 0 {
            return Err(crate::error::EvmCommonError::InvalidConfig(
                "Chain ID cannot be 0".to_string(),
            ));
        }

        if self.timeout_seconds == 0 {
            return Err(crate::error::EvmCommonError::InvalidConfig(
                "Timeout cannot be 0".to_string(),
            ));
        }

        if let Some(multiplier) = self.gas_price_multiplier {
            if multiplier <= 0.0 {
                return Err(crate::error::EvmCommonError::InvalidConfig(
                    "Gas price multiplier must be positive".to_string(),
                ));
            }
        }

        Ok(())
    }
}

/// Common EVM account metadata
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct EvmAccount {
    /// Address of the account (hex format with 0x prefix)
    pub address: String,
    /// Chain ID where this account exists
    pub chain_id: u64,
    /// Optional account alias/name
    pub name: Option<String>,
}

impl EvmAccount {
    /// Create new EVM account with validation
    pub fn new(address: &str, chain_id: u64) -> Result<Self, crate::error::EvmCommonError> {
        // Validate address format
        crate::address::validate_evm_address(address)?;

        Ok(Self {
            address: address.to_string(),
            chain_id,
            name: None,
        })
    }

    /// Create new EVM account with name
    pub fn with_name(
        address: &str,
        chain_id: u64,
        name: String,
    ) -> Result<Self, crate::error::EvmCommonError> {
        let mut account = Self::new(address, chain_id)?;
        account.name = Some(name);
        Ok(account)
    }

    /// Get address as Alloy Address type
    pub fn to_address(&self) -> Result<Address, crate::error::EvmCommonError> {
        crate::address::parse_evm_address(&self.address)
    }

    /// Format address for display (checksummed)
    pub fn display_address(&self) -> Result<String, crate::error::EvmCommonError> {
        let addr = self.to_address()?;
        Ok(format!("0x{:x}", addr))
    }
}

/// EVM transaction data for cross-chain operations
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct EvmTransactionData {
    /// Target contract address
    pub to: String,
    /// Transaction data (hex encoded)
    pub data: String,
    /// Value to send (in wei, hex encoded)
    pub value: String,
    /// Gas limit (hex encoded)
    pub gas_limit: String,
    /// Gas price (in wei, hex encoded)
    pub gas_price: String,
    /// Chain ID for this transaction
    pub chain_id: u64,
}

impl EvmTransactionData {
    /// Create new EVM transaction data with validation
    pub fn new(
        to: &str,
        data: &str,
        value: U256,
        gas_limit: u64,
        gas_price: U256,
        chain_id: u64,
    ) -> Result<Self, crate::error::EvmCommonError> {
        // Validate to address
        crate::address::validate_evm_address(to)?;

        // Validate data is hex
        if !data.starts_with("0x") {
            return Err(crate::error::EvmCommonError::InvalidData(
                "Transaction data must start with 0x".to_string(),
            ));
        }

        Ok(Self {
            to: to.to_string(),
            data: data.to_string(),
            value: format!("0x{:x}", value),
            gas_limit: format!("0x{:x}", gas_limit),
            gas_price: format!("0x{:x}", gas_price),
            chain_id,
        })
    }

    /// Parse to address as Alloy Address
    pub fn to_address(&self) -> Result<Address, crate::error::EvmCommonError> {
        crate::address::parse_evm_address(&self.to)
    }

    /// Parse data as Alloy Bytes
    pub fn data_bytes(&self) -> Result<Bytes, crate::error::EvmCommonError> {
        let hex_str = self.data.strip_prefix("0x").unwrap_or(&self.data);
        alloy::hex::decode(hex_str)
            .map_err(|e| {
                crate::error::EvmCommonError::InvalidData(format!("Invalid hex data: {}", e))
            })
            .map(Bytes::from)
    }

    /// Parse value as U256
    pub fn value_u256(&self) -> Result<U256, crate::error::EvmCommonError> {
        U256::from_str_radix(self.value.strip_prefix("0x").unwrap_or(&self.value), 16).map_err(
            |e| crate::error::EvmCommonError::InvalidData(format!("Invalid value format: {}", e)),
        )
    }

    /// Parse gas limit as u64
    pub fn gas_limit_u64(&self) -> Result<u64, crate::error::EvmCommonError> {
        u64::from_str_radix(
            self.gas_limit.strip_prefix("0x").unwrap_or(&self.gas_limit),
            16,
        )
        .map_err(|e| {
            crate::error::EvmCommonError::InvalidData(format!("Invalid gas limit format: {}", e))
        })
    }

    /// Parse gas price as U256
    pub fn gas_price_u256(&self) -> Result<U256, crate::error::EvmCommonError> {
        U256::from_str_radix(
            self.gas_price.strip_prefix("0x").unwrap_or(&self.gas_price),
            16,
        )
        .map_err(|e| {
            crate::error::EvmCommonError::InvalidData(format!("Invalid gas price format: {}", e))
        })
    }
}

/// Token information for ERC20 and native tokens
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct EvmToken {
    /// Contract address (0x0 for native token)
    pub address: String,
    /// Token symbol (e.g., "USDC", "ETH")
    pub symbol: String,
    /// Token name (e.g., "USD Coin", "Ethereum")
    pub name: String,
    /// Number of decimal places
    pub decimals: u8,
    /// Chain ID where this token exists
    pub chain_id: u64,
}

impl EvmToken {
    /// Create new EVM token with validation
    pub fn new(
        address: &str,
        symbol: String,
        name: String,
        decimals: u8,
        chain_id: u64,
    ) -> Result<Self, crate::error::EvmCommonError> {
        // Validate address (allow 0x0 for native token)
        if address != "0x0000000000000000000000000000000000000000" && address != "0x0" {
            crate::address::validate_evm_address(address)?;
        }

        Ok(Self {
            address: address.to_string(),
            symbol,
            name,
            decimals,
            chain_id,
        })
    }

    /// Check if this is a native token (ETH, MATIC, etc.)
    pub fn is_native(&self) -> bool {
        self.address == "0x0000000000000000000000000000000000000000" || self.address == "0x0"
    }

    /// Get contract address as Alloy Address (for non-native tokens)
    pub fn contract_address(&self) -> Result<Option<Address>, crate::error::EvmCommonError> {
        if self.is_native() {
            Ok(None)
        } else {
            Ok(Some(crate::address::parse_evm_address(&self.address)?))
        }
    }

    /// Convert amount from smallest unit to human-readable format
    pub fn format_amount(&self, raw_amount: U256) -> String {
        let divisor = U256::from(10).pow(U256::from(self.decimals));
        let whole = raw_amount / divisor;
        let remainder = raw_amount % divisor;

        if remainder == U256::ZERO {
            format!("{} {}", whole, self.symbol)
        } else {
            // Calculate decimal portion
            let decimal_str = format!("{:0width$}", remainder, width = self.decimals as usize);
            let decimal_trimmed = decimal_str.trim_end_matches('0');

            if decimal_trimmed.is_empty() {
                format!("{} {}", whole, self.symbol)
            } else {
                format!("{}.{} {}", whole, decimal_trimmed, self.symbol)
            }
        }
    }
}

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

    #[test]
    fn test_evm_config_validation() {
        let mut config = EvmConfig::default();
        assert!(config.validate().is_ok());

        // Test invalid URL
        config.rpc_url = "invalid-url".to_string();
        assert!(config.validate().is_err());

        // Test valid URL
        config.rpc_url = "https://eth.llamarpc.com".to_string();
        assert!(config.validate().is_ok());

        // Test invalid chain ID
        config.chain_id = 0;
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_evm_account_creation() {
        let account = EvmAccount::new("0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e", 1);
        assert!(account.is_ok());

        let account = account.unwrap();
        assert_eq!(account.chain_id, 1);
        assert!(account.name.is_none());

        // Test with name
        let account = EvmAccount::with_name(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            1,
            "Test".to_string(),
        );
        assert!(account.is_ok());
        assert_eq!(account.unwrap().name, Some("Test".to_string()));
    }

    #[test]
    fn test_evm_token() {
        // Test native token
        let eth = EvmToken::new("0x0", "ETH".to_string(), "Ethereum".to_string(), 18, 1);
        assert!(eth.is_ok());
        let eth = eth.unwrap();
        assert!(eth.is_native());

        // Test ERC20 token
        let usdc = EvmToken::new(
            "0xA0b86a33E6417c5d6d6bE6C2e0C6C3e5d6c7D8E9",
            "USDC".to_string(),
            "USD Coin".to_string(),
            6,
            1,
        );
        assert!(usdc.is_ok());
        let usdc = usdc.unwrap();
        assert!(!usdc.is_native());

        // Test amount formatting
        let amount = U256::from(1_000_000u64); // 1 USDC
        assert_eq!(usdc.format_amount(amount), "1 USDC");

        let small_amount = U256::from(500_000u64); // 0.5 USDC
        assert_eq!(usdc.format_amount(small_amount), "0.5 USDC");
    }

    // Additional comprehensive tests for 100% coverage

    #[test]
    fn test_evm_config_default() {
        let config = EvmConfig::default();
        assert_eq!(config.rpc_url, "https://eth.llamarpc.com");
        assert_eq!(config.chain_id, 1);
        assert_eq!(config.timeout_seconds, 30);
        assert_eq!(config.max_gas_limit, None);
        assert_eq!(config.gas_price_multiplier, Some(1.1));
    }

    #[test]
    fn test_evm_config_timeout() {
        let config = EvmConfig {
            rpc_url: "https://test.com".to_string(),
            chain_id: 1,
            timeout_seconds: 60,
            max_gas_limit: None,
            gas_price_multiplier: None,
        };
        assert_eq!(config.timeout(), Duration::from_secs(60));
    }

    #[test]
    fn test_evm_config_validate_empty_rpc_url() {
        let config = EvmConfig {
            rpc_url: "".to_string(),
            chain_id: 1,
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: None,
        };
        let result = config.validate();
        assert!(result.is_err());
        if let Err(crate::error::EvmCommonError::InvalidConfig(msg)) = result {
            assert_eq!(msg, "RPC URL cannot be empty");
        }
    }

    #[test]
    fn test_evm_config_validate_invalid_rpc_url_protocol() {
        let config = EvmConfig {
            rpc_url: "ftp://test.com".to_string(),
            chain_id: 1,
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: None,
        };
        let result = config.validate();
        assert!(result.is_err());
        if let Err(crate::error::EvmCommonError::InvalidConfig(msg)) = result {
            assert_eq!(msg, "RPC URL must start with http://, https://, or wss://");
        }
    }

    #[test]
    fn test_evm_config_validate_zero_chain_id() {
        let config = EvmConfig {
            rpc_url: "https://test.com".to_string(),
            chain_id: 0,
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: None,
        };
        let result = config.validate();
        assert!(result.is_err());
        if let Err(crate::error::EvmCommonError::InvalidConfig(msg)) = result {
            assert_eq!(msg, "Chain ID cannot be 0");
        }
    }

    #[test]
    fn test_evm_config_validate_zero_timeout() {
        let config = EvmConfig {
            rpc_url: "https://test.com".to_string(),
            chain_id: 1,
            timeout_seconds: 0,
            max_gas_limit: None,
            gas_price_multiplier: None,
        };
        let result = config.validate();
        assert!(result.is_err());
        if let Err(crate::error::EvmCommonError::InvalidConfig(msg)) = result {
            assert_eq!(msg, "Timeout cannot be 0");
        }
    }

    #[test]
    fn test_evm_config_validate_negative_gas_price_multiplier() {
        let config = EvmConfig {
            rpc_url: "https://test.com".to_string(),
            chain_id: 1,
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: Some(-1.0),
        };
        let result = config.validate();
        assert!(result.is_err());
        if let Err(crate::error::EvmCommonError::InvalidConfig(msg)) = result {
            assert_eq!(msg, "Gas price multiplier must be positive");
        }
    }

    #[test]
    fn test_evm_config_validate_zero_gas_price_multiplier() {
        let config = EvmConfig {
            rpc_url: "https://test.com".to_string(),
            chain_id: 1,
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: Some(0.0),
        };
        let result = config.validate();
        assert!(result.is_err());
        if let Err(crate::error::EvmCommonError::InvalidConfig(msg)) = result {
            assert_eq!(msg, "Gas price multiplier must be positive");
        }
    }

    #[test]
    fn test_evm_config_validate_valid_http_url() {
        let config = EvmConfig {
            rpc_url: "http://test.com".to_string(),
            chain_id: 1,
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: Some(1.0),
        };
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_evm_config_validate_valid_wss_url() {
        let config = EvmConfig {
            rpc_url: "wss://test.com".to_string(),
            chain_id: 1,
            timeout_seconds: 30,
            max_gas_limit: None,
            gas_price_multiplier: None,
        };
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_evm_config_for_chain_success() {
        // This test will depend on the chain module implementation
        // We're testing the happy path where a valid chain ID is provided
        match EvmConfig::for_chain(1) {
            Ok(config) => {
                assert_eq!(config.chain_id, 1);
                assert_eq!(config.timeout_seconds, 30);
                assert_eq!(config.gas_price_multiplier, Some(1.1));
            }
            Err(_) => {
                // If the chain module returns an error for chain ID 1,
                // that's fine - we're just testing the structure
            }
        }
    }

    #[test]
    fn test_evm_config_for_chain_invalid_chain() {
        // Test with an invalid chain ID that should fail
        let result = EvmConfig::for_chain(999999);
        // The result depends on the chain module implementation
        // but this tests the error path
        match result {
            Ok(_) => {
                // Chain module supports this chain ID
            }
            Err(_) => {
                // Expected error for unsupported chain ID
            }
        }
    }

    #[test]
    fn test_evm_account_new_invalid_address() {
        let result = EvmAccount::new("invalid-address", 1);
        assert!(result.is_err());
    }

    #[test]
    fn test_evm_account_with_name_invalid_address() {
        let result = EvmAccount::with_name("invalid-address", 1, "Test".to_string());
        assert!(result.is_err());
    }

    #[test]
    fn test_evm_account_to_address_success() {
        let account = EvmAccount::new("0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e", 1).unwrap();
        let address_result = account.to_address();
        assert!(address_result.is_ok());
    }

    #[test]
    fn test_evm_account_display_address_success() {
        let account = EvmAccount::new("0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e", 1).unwrap();
        let display_result = account.display_address();
        assert!(display_result.is_ok());
        let display = display_result.unwrap();
        assert!(display.starts_with("0x"));
    }

    #[test]
    fn test_evm_transaction_data_new_success() {
        let result = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        );
        assert!(result.is_ok());
        let tx_data = result.unwrap();
        assert_eq!(tx_data.to, "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e");
        assert_eq!(tx_data.data, "0x1234");
        assert_eq!(tx_data.value, "0x3e8");
        assert_eq!(tx_data.gas_limit, "0x5208");
        assert_eq!(tx_data.chain_id, 1);
    }

    #[test]
    fn test_evm_transaction_data_new_invalid_address() {
        let result = EvmTransactionData::new(
            "invalid-address",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_evm_transaction_data_new_invalid_data_format() {
        let result = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "1234", // Missing 0x prefix
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        );
        assert!(result.is_err());
        if let Err(crate::error::EvmCommonError::InvalidData(msg)) = result {
            assert_eq!(msg, "Transaction data must start with 0x");
        }
    }

    #[test]
    fn test_evm_transaction_data_to_address() {
        let tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        let address_result = tx_data.to_address();
        assert!(address_result.is_ok());
    }

    #[test]
    fn test_evm_transaction_data_data_bytes() {
        let tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        let bytes_result = tx_data.data_bytes();
        assert!(bytes_result.is_ok());
        let bytes = bytes_result.unwrap();
        assert_eq!(bytes.len(), 2); // 0x1234 = 2 bytes
    }

    #[test]
    fn test_evm_transaction_data_data_bytes_no_prefix() {
        // Test data parsing without 0x prefix
        let mut tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        tx_data.data = "abcd".to_string(); // No 0x prefix
        let bytes_result = tx_data.data_bytes();
        assert!(bytes_result.is_ok());
    }

    #[test]
    fn test_evm_transaction_data_data_bytes_invalid_hex() {
        let mut tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        tx_data.data = "0xgggg".to_string(); // Invalid hex
        let bytes_result = tx_data.data_bytes();
        assert!(bytes_result.is_err());
    }

    #[test]
    fn test_evm_transaction_data_value_u256() {
        let tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        let value_result = tx_data.value_u256();
        assert!(value_result.is_ok());
        assert_eq!(value_result.unwrap(), U256::from(1000));
    }

    #[test]
    fn test_evm_transaction_data_value_u256_no_prefix() {
        let mut tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        tx_data.value = "3e8".to_string(); // No 0x prefix
        let value_result = tx_data.value_u256();
        assert!(value_result.is_ok());
        assert_eq!(value_result.unwrap(), U256::from(1000));
    }

    #[test]
    fn test_evm_transaction_data_value_u256_invalid() {
        let mut tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        tx_data.value = "0xgggg".to_string(); // Invalid hex
        let value_result = tx_data.value_u256();
        assert!(value_result.is_err());
    }

    #[test]
    fn test_evm_transaction_data_gas_limit_u64() {
        let tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        let gas_limit_result = tx_data.gas_limit_u64();
        assert!(gas_limit_result.is_ok());
        assert_eq!(gas_limit_result.unwrap(), 21000);
    }

    #[test]
    fn test_evm_transaction_data_gas_limit_u64_no_prefix() {
        let mut tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        tx_data.gas_limit = "5208".to_string(); // No 0x prefix
        let gas_limit_result = tx_data.gas_limit_u64();
        assert!(gas_limit_result.is_ok());
        assert_eq!(gas_limit_result.unwrap(), 21000);
    }

    #[test]
    fn test_evm_transaction_data_gas_limit_u64_invalid() {
        let mut tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        tx_data.gas_limit = "0xgggg".to_string(); // Invalid hex
        let gas_limit_result = tx_data.gas_limit_u64();
        assert!(gas_limit_result.is_err());
    }

    #[test]
    fn test_evm_transaction_data_gas_price_u256() {
        let tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        let gas_price_result = tx_data.gas_price_u256();
        assert!(gas_price_result.is_ok());
        assert_eq!(gas_price_result.unwrap(), U256::from(20000000000u64));
    }

    #[test]
    fn test_evm_transaction_data_gas_price_u256_no_prefix() {
        let mut tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        tx_data.gas_price = "4a817c800".to_string(); // No 0x prefix
        let gas_price_result = tx_data.gas_price_u256();
        assert!(gas_price_result.is_ok());
        assert_eq!(gas_price_result.unwrap(), U256::from(20000000000u64));
    }

    #[test]
    fn test_evm_transaction_data_gas_price_u256_invalid() {
        let mut tx_data = EvmTransactionData::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "0x1234",
            U256::from(1000),
            21000,
            U256::from(20000000000u64),
            1,
        )
        .unwrap();

        tx_data.gas_price = "0xgggg".to_string(); // Invalid hex
        let gas_price_result = tx_data.gas_price_u256();
        assert!(gas_price_result.is_err());
    }

    #[test]
    fn test_evm_token_native_with_long_zero_address() {
        let token = EvmToken::new(
            "0x0000000000000000000000000000000000000000",
            "ETH".to_string(),
            "Ethereum".to_string(),
            18,
            1,
        )
        .unwrap();
        assert!(token.is_native());
    }

    #[test]
    fn test_evm_token_native_with_short_zero_address() {
        let token = EvmToken::new("0x0", "ETH".to_string(), "Ethereum".to_string(), 18, 1).unwrap();
        assert!(token.is_native());
    }

    #[test]
    fn test_evm_token_contract_address_native() {
        let token = EvmToken::new("0x0", "ETH".to_string(), "Ethereum".to_string(), 18, 1).unwrap();
        let contract_address = token.contract_address().unwrap();
        assert!(contract_address.is_none());
    }

    #[test]
    fn test_evm_token_contract_address_erc20() {
        let token = EvmToken::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "TEST".to_string(),
            "Test Token".to_string(),
            18,
            1,
        )
        .unwrap();
        let contract_address = token.contract_address().unwrap();
        assert!(contract_address.is_some());
    }

    #[test]
    fn test_evm_token_format_amount_zero() {
        let token = EvmToken::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "TEST".to_string(),
            "Test Token".to_string(),
            18,
            1,
        )
        .unwrap();
        let formatted = token.format_amount(U256::ZERO);
        assert_eq!(formatted, "0 TEST");
    }

    #[test]
    fn test_evm_token_format_amount_whole_number() {
        let token = EvmToken::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "TEST".to_string(),
            "Test Token".to_string(),
            18,
            1,
        )
        .unwrap();
        let amount = U256::from(10).pow(U256::from(18)) * U256::from(5); // 5 tokens
        let formatted = token.format_amount(amount);
        assert_eq!(formatted, "5 TEST");
    }

    #[test]
    fn test_evm_token_format_amount_decimal() {
        let token = EvmToken::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "TEST".to_string(),
            "Test Token".to_string(),
            6, // 6 decimals like USDC
            1,
        )
        .unwrap();
        let amount = U256::from(1_500_000); // 1.5 tokens
        let formatted = token.format_amount(amount);
        assert_eq!(formatted, "1.5 TEST");
    }

    #[test]
    fn test_evm_token_format_amount_trailing_zeros_trimmed() {
        let token = EvmToken::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "TEST".to_string(),
            "Test Token".to_string(),
            6,
            1,
        )
        .unwrap();
        let amount = U256::from(1_200_000); // 1.2 tokens (should trim trailing zeros)
        let formatted = token.format_amount(amount);
        assert_eq!(formatted, "1.2 TEST");
    }

    #[test]
    fn test_evm_token_format_amount_small_decimals() {
        let token = EvmToken::new(
            "0x742d35Cc67A5b747bE4C506C5e8b0A146d7b2E9e",
            "TEST".to_string(),
            "Test Token".to_string(),
            18,
            1,
        )
        .unwrap();
        let amount = U256::from(1); // Smallest possible amount
        let formatted = token.format_amount(amount);
        assert_eq!(formatted, "0.000000000000000001 TEST");
    }

    #[test]
    fn test_evm_token_new_invalid_address() {
        let result = EvmToken::new(
            "invalid-address",
            "TEST".to_string(),
            "Test Token".to_string(),
            18,
            1,
        );
        assert!(result.is_err());
    }
}