edb-engine 0.0.1

EDB's core analysis and instrumentation engine
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
// EDB - Ethereum Debugger
// Copyright (C) 2024 Zhuo Zhang and Wuqi Zhang
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! ABI encoding utilities for function calls
//!
//! This module provides functionality to encode text-form function calls
//! (e.g., "balanceOf(0x123424)") into encoded bytes using function ABIs.

use alloy_dyn_abi::{DynSolType, DynSolValue, JsonAbiExt};
use alloy_json_abi::Function;
use alloy_primitives::{Address, Bytes, FixedBytes, I256, U256};
use eyre::{eyre, Result};
use std::collections::BTreeMap;

/// Encode a text-form function call to bytes using the provided function ABI
///
/// # Arguments
/// * `functions` - BTreeMap of function name to list of function definitions
/// * `call_text` - Text representation of function call (e.g., "balanceOf(0x123424)")
///
/// # Returns
/// * `Result<Bytes>` - Encoded function call data on success
///
/// # Examples
/// ```rust
/// use std::collections::BTreeMap;
/// use alloy_json_abi::Function;
///
/// let mut functions = BTreeMap::new();
/// // functions.insert("balanceOf".to_string(), vec![balance_of_function]);
///
/// // Basic function call
/// let encoded = encode_function_call(&functions, "balanceOf(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1)")?;
///
/// // Function call with struct syntax (for tuples/structs)
/// let encoded = encode_function_call(&functions, "submitData({user: 0x123..., amount: 100})")?;
///
/// // Function call with traditional tuple syntax
/// let encoded = encode_function_call(&functions, "submitData((0x123..., 100))")?;
///
/// // Complex nested structures
/// let encoded = encode_function_call(&functions, "complexCall({data: {nested: [1,2,3]}, value: 456})")?;
/// ```
pub fn encode_function_call(
    functions: &BTreeMap<String, Vec<Function>>,
    call_text: &str,
) -> Result<Bytes> {
    let (function_name, args_str) = parse_function_call_text(call_text)?;

    // Find the matching function definition and parse arguments in one step
    let (function, args) = find_matching_function(functions, &function_name, &args_str)?;

    // Encode the function call
    encode_function_data(&function, args)
}

/// Parse function call text into name and arguments string
///
/// Examples: "balanceOf(0x123)" -> ("balanceOf", "0x123")
fn parse_function_call_text(call_text: &str) -> Result<(String, String)> {
    let call_text = call_text.trim();

    if let Some(open_paren) = call_text.find('(') {
        if !call_text.ends_with(')') {
            return Err(eyre!("Function call must end with ')': {}", call_text));
        }

        let function_name = call_text[..open_paren].trim().to_string();
        let args_str = call_text[open_paren + 1..call_text.len() - 1].trim().to_string();

        if function_name.is_empty() {
            return Err(eyre!("Function name cannot be empty"));
        }

        Ok((function_name, args_str))
    } else {
        Err(eyre!("Invalid function call format. Expected format: functionName(arg1,arg2,...)"))
    }
}

/// Find the best matching function from available overloads by trying to parse arguments
fn find_matching_function(
    functions: &BTreeMap<String, Vec<Function>>,
    function_name: &str,
    args_str: &str,
) -> Result<(Function, Vec<DynSolValue>)> {
    let function_overloads = functions
        .get(function_name)
        .ok_or_else(|| eyre!("Function '{}' not found in ABI", function_name))?;

    if function_overloads.is_empty() {
        return Err(eyre!("No function definitions found for '{}'", function_name));
    }

    // Try to parse arguments with each function overload until one succeeds
    let mut parse_errors = Vec::new();

    for function in function_overloads {
        match parse_function_arguments(function, args_str) {
            Ok(args) => {
                // Successfully parsed arguments with this function signature
                return Ok((function.clone(), args));
            }
            Err(e) => {
                // Store the error and try the next overload
                parse_errors.push(format!(
                    "Function '{}({})': {}",
                    function.name,
                    function
                        .inputs
                        .iter()
                        .map(|param| param.ty.clone())
                        .collect::<Vec<_>>()
                        .join(","),
                    e
                ));
            }
        }
    }

    // If no overload worked, return a comprehensive error
    let error_msg = if function_overloads.len() == 1 {
        format!("Failed to parse arguments for function '{}': {}", function_name, parse_errors[0])
    } else {
        format!(
            "Failed to match arguments with any overload of function '{}'. Tried:\n{}",
            function_name,
            parse_errors.join("\n")
        )
    };

    Err(eyre!(error_msg))
}

/// Parse function arguments from string representation
fn parse_function_arguments(function: &Function, args_str: &str) -> Result<Vec<DynSolValue>> {
    if args_str.trim().is_empty() {
        if function.inputs.is_empty() {
            return Ok(vec![]);
        } else {
            return Err(eyre!(
                "Function '{}' expects {} arguments, but none provided",
                function.name,
                function.inputs.len()
            ));
        }
    }

    let arg_strings = split_arguments(args_str)?;

    if arg_strings.len() != function.inputs.len() {
        return Err(eyre!(
            "Function '{}' expects {} arguments, but {} provided",
            function.name,
            function.inputs.len(),
            arg_strings.len()
        ));
    }

    let mut args = Vec::new();
    for (i, (arg_str, param)) in arg_strings.iter().zip(&function.inputs).enumerate() {
        let arg_value = parse_argument_value(arg_str.trim(), &param.ty)
            .map_err(|e| eyre!("Failed to parse argument {}: {}", i + 1, e))?;
        args.push(arg_value);
    }

    Ok(args)
}

/// Split arguments string while respecting parentheses, brackets, braces, and quotes
fn split_arguments(args_str: &str) -> Result<Vec<String>> {
    let mut args = Vec::new();
    let mut current_arg = String::new();
    let mut depth = 0; // Combined depth for all bracket types
    let mut in_string = false;
    let mut escape_next = false;
    let mut string_char = '\0';

    for ch in args_str.chars() {
        if escape_next {
            current_arg.push(ch);
            escape_next = false;
            continue;
        }

        match ch {
            '\\' if in_string => {
                escape_next = true;
                current_arg.push(ch);
            }
            '"' | '\'' => {
                if !in_string {
                    in_string = true;
                    string_char = ch;
                } else if ch == string_char {
                    in_string = false;
                }
                current_arg.push(ch);
            }
            // Opening brackets/braces/parentheses
            '(' | '[' | '{' if !in_string => {
                depth += 1;
                current_arg.push(ch);
            }
            // Closing brackets/braces/parentheses
            ')' | ']' | '}' if !in_string => {
                depth -= 1;
                current_arg.push(ch);
            }
            ',' if !in_string && depth == 0 => {
                args.push(current_arg.trim().to_string());
                current_arg.clear();
            }
            _ => {
                current_arg.push(ch);
            }
        }
    }

    if !current_arg.trim().is_empty() {
        args.push(current_arg.trim().to_string());
    }

    Ok(args)
}

/// Parse a single argument value based on its type
fn parse_argument_value(arg_str: &str, param_type: &str) -> Result<DynSolValue> {
    let arg_str = arg_str.trim();

    // Check for type casting syntax: type(value)
    let (cast_type, actual_value) = extract_type_cast(arg_str)?;

    // If there's a cast type, validate it matches or is compatible with param_type
    let value_to_parse = if let Some(cast_type) = cast_type {
        validate_type_cast(&cast_type, param_type)?;
        actual_value
    } else {
        arg_str
    };

    let sol_type = DynSolType::parse(param_type)
        .map_err(|e| eyre!("Invalid parameter type '{}': {}", param_type, e))?;

    match sol_type {
        DynSolType::Address => {
            let address = parse_address(value_to_parse)?;
            Ok(DynSolValue::Address(address))
        }
        DynSolType::Uint(size) => {
            let value = parse_uint(value_to_parse, size)?;
            Ok(DynSolValue::Uint(value, size))
        }
        DynSolType::Int(size) => {
            let value = parse_int(value_to_parse, size)?;
            Ok(DynSolValue::Int(value, size))
        }
        DynSolType::Bool => {
            let value = parse_bool(value_to_parse)?;
            Ok(DynSolValue::Bool(value))
        }
        DynSolType::String => {
            let value = parse_string(value_to_parse)?;
            Ok(DynSolValue::String(value))
        }
        DynSolType::Bytes => {
            let value = parse_bytes(value_to_parse)?;
            Ok(DynSolValue::Bytes(value))
        }
        DynSolType::FixedBytes(size) => {
            let value = parse_fixed_bytes(value_to_parse, size)?;
            // Convert to FixedBytes<32>
            let mut word = [0u8; 32];
            let copy_len = value.len().min(32);
            word[..copy_len].copy_from_slice(&value[..copy_len]);
            Ok(DynSolValue::FixedBytes(FixedBytes::from(word), size))
        }
        DynSolType::Array(ref inner) => parse_array(value_to_parse, inner),
        DynSolType::FixedArray(ref inner, size) => parse_fixed_array(value_to_parse, inner, size),
        DynSolType::Tuple(ref types) => parse_tuple(value_to_parse, types),
        _ => Err(eyre!("Unsupported type: {}", param_type)),
    }
}

/// Extract type cast from syntax like "uint256(123)" or "address(0x456)"
fn extract_type_cast(s: &str) -> Result<(Option<String>, &str)> {
    let s = s.trim();

    // Check for type cast pattern: type_name(value)
    // Look for valid Solidity type names followed by parentheses
    let type_prefixes = [
        "uint", "int", "address", "bool", "bytes", "string", "uint8", "uint16", "uint24", "uint32",
        "uint40", "uint48", "uint56", "uint64", "uint72", "uint80", "uint88", "uint96", "uint104",
        "uint112", "uint120", "uint128", "uint136", "uint144", "uint152", "uint160", "uint168",
        "uint176", "uint184", "uint192", "uint200", "uint208", "uint216", "uint224", "uint232",
        "uint240", "uint248", "uint256", "int8", "int16", "int24", "int32", "int40", "int48",
        "int56", "int64", "int72", "int80", "int88", "int96", "int104", "int112", "int120",
        "int128", "int136", "int144", "int152", "int160", "int168", "int176", "int184", "int192",
        "int200", "int208", "int216", "int224", "int232", "int240", "int248", "int256", "bytes1",
        "bytes2", "bytes3", "bytes4", "bytes5", "bytes6", "bytes7", "bytes8", "bytes9", "bytes10",
        "bytes11", "bytes12", "bytes13", "bytes14", "bytes15", "bytes16", "bytes17", "bytes18",
        "bytes19", "bytes20", "bytes21", "bytes22", "bytes23", "bytes24", "bytes25", "bytes26",
        "bytes27", "bytes28", "bytes29", "bytes30", "bytes31", "bytes32",
    ];

    for prefix in &type_prefixes {
        if let Some(after_type) = s.strip_prefix(prefix) {
            if after_type.starts_with('(') {
                // Find matching closing parenthesis
                let mut depth = 0;
                let mut end_idx = None;

                for (i, ch) in after_type.chars().enumerate() {
                    match ch {
                        '(' => depth += 1,
                        ')' => {
                            depth -= 1;
                            if depth == 0 {
                                end_idx = Some(i);
                                break;
                            }
                        }
                        _ => {}
                    }
                }

                if let Some(end) = end_idx {
                    let value = &after_type[1..end];
                    return Ok((Some(prefix.to_string()), value));
                }
            }
        }
    }

    Ok((None, s))
}

/// Validate that a type cast is compatible with the expected parameter type
fn validate_type_cast(cast_type: &str, param_type: &str) -> Result<()> {
    // Normalize types for comparison
    let normalize_type = |t: &str| -> String {
        if t == "uint" {
            "uint256".to_string()
        } else if t == "int" {
            "int256".to_string()
        } else {
            t.to_string()
        }
    };

    let cast_normalized = normalize_type(cast_type);
    let param_normalized = normalize_type(param_type);

    // Check if types are compatible
    if cast_normalized == param_normalized {
        return Ok(());
    }

    // Allow integer type casting between different sizes (will be validated during parsing)
    if (cast_normalized.starts_with("uint") && param_normalized.starts_with("uint"))
        || (cast_normalized.starts_with("int") && param_normalized.starts_with("int"))
    {
        return Ok(());
    }

    // Allow bytes type casting between different sizes
    if cast_normalized.starts_with("bytes") && param_normalized.starts_with("bytes") {
        return Ok(());
    }

    Err(eyre!(
        "Type cast '{cast_type}' is not compatible with expected parameter type '{param_type}'"
    ))
}

/// Parse address from string
fn parse_address(s: &str) -> Result<Address> {
    let s = s.trim();
    if s.starts_with("0x") || s.starts_with("0X") {
        s.parse().map_err(|e| eyre!("Invalid address '{s}': {e}"))
    } else {
        // Try parsing as hex without 0x prefix
        format!("0x{s}").parse().map_err(|e| eyre!("Invalid address '{s}': {e}"))
    }
}

/// Parse unsigned integer from string
fn parse_uint(s: &str, _size: usize) -> Result<U256> {
    let s = s.trim();
    if s.starts_with("0x") || s.starts_with("0X") {
        U256::from_str_radix(&s[2..], 16).map_err(|e| eyre!("Invalid hex uint '{}': {}", s, e))
    } else {
        U256::from_str_radix(s, 10).map_err(|e| eyre!("Invalid decimal uint '{}': {}", s, e))
    }
}

/// Parse signed integer from string
fn parse_int(s: &str, _size: usize) -> Result<I256> {
    let s = s.trim();
    if s.starts_with("0x") || s.starts_with("0X") {
        // Parse as U256 first, then convert to I256
        let uint_val = U256::from_str_radix(&s[2..], 16)
            .map_err(|e| eyre!("Invalid hex int '{}': {}", s, e))?;
        Ok(I256::from_raw(uint_val))
    } else {
        // For decimal, check if negative
        if let Some(positive_part) = s.strip_prefix('-') {
            let uint_val = U256::from_str_radix(positive_part, 10)
                .map_err(|e| eyre!("Invalid decimal int '{}': {}", s, e))?;
            Ok(-I256::from_raw(uint_val))
        } else {
            let uint_val = U256::from_str_radix(s, 10)
                .map_err(|e| eyre!("Invalid decimal int '{}': {}", s, e))?;
            Ok(I256::from_raw(uint_val))
        }
    }
}

/// Parse boolean from string
fn parse_bool(s: &str) -> Result<bool> {
    match s.trim().to_lowercase().as_str() {
        "true" | "1" => Ok(true),
        "false" | "0" => Ok(false),
        _ => Err(eyre!("Invalid boolean value '{}'. Expected 'true', 'false', '1', or '0'", s)),
    }
}

/// Parse string from argument
fn parse_string(s: &str) -> Result<String> {
    let s = s.trim();
    if (s.starts_with('"') && s.ends_with('"')) || (s.starts_with('\'') && s.ends_with('\'')) {
        Ok(s[1..s.len() - 1].to_string())
    } else {
        Ok(s.to_string())
    }
}

/// Parse bytes from hex string
fn parse_bytes(s: &str) -> Result<Vec<u8>> {
    let s = s.trim();
    if s.starts_with("0x") || s.starts_with("0X") {
        hex::decode(&s[2..]).map_err(|e| eyre!("Invalid hex bytes '{}': {}", s, e))
    } else {
        hex::decode(s).map_err(|e| eyre!("Invalid hex bytes '{}': {}", s, e))
    }
}

/// Parse fixed bytes from hex string
fn parse_fixed_bytes(s: &str, size: usize) -> Result<Vec<u8>> {
    let bytes = parse_bytes(s)?;
    if bytes.len() != size {
        return Err(eyre!(
            "Fixed bytes size mismatch: expected {} bytes, got {}",
            size,
            bytes.len()
        ));
    }
    Ok(bytes)
}

/// Parse array from string representation
fn parse_array(s: &str, inner_type: &DynSolType) -> Result<DynSolValue> {
    let s = s.trim();
    if !s.starts_with('[') || !s.ends_with(']') {
        return Err(eyre!("Array must be enclosed in square brackets: {}", s));
    }

    let inner_str = &s[1..s.len() - 1];
    if inner_str.trim().is_empty() {
        return Ok(DynSolValue::Array(vec![]));
    }

    let elements_str = split_arguments(inner_str)?;
    let mut elements = Vec::new();

    for element_str in elements_str {
        let element = parse_argument_value(&element_str, &inner_type.to_string())?;
        elements.push(element);
    }

    Ok(DynSolValue::Array(elements))
}

/// Parse fixed array from string representation
fn parse_fixed_array(s: &str, inner_type: &DynSolType, size: usize) -> Result<DynSolValue> {
    if let DynSolValue::Array(elements) = parse_array(s, inner_type)? {
        if elements.len() != size {
            return Err(eyre!(
                "Fixed array size mismatch: expected {} elements, got {}",
                size,
                elements.len()
            ));
        }
        Ok(DynSolValue::FixedArray(elements))
    } else {
        unreachable!("parse_array should always return Array variant")
    }
}

/// Parse tuple from string representation (supports both () and {} syntax)
fn parse_tuple(s: &str, types: &[DynSolType]) -> Result<DynSolValue> {
    let s = s.trim();

    // Support both tuple syntax () and struct syntax {}
    let (is_struct_syntax, inner_str) = if s.starts_with('{') && s.ends_with('}') {
        (true, &s[1..s.len() - 1])
    } else if s.starts_with('(') && s.ends_with(')') {
        (false, &s[1..s.len() - 1])
    } else {
        return Err(eyre!("Tuple/Struct must be enclosed in parentheses () or braces {{}}: {}", s));
    };

    if inner_str.trim().is_empty() {
        if types.is_empty() {
            return Ok(DynSolValue::Tuple(vec![]));
        } else {
            return Err(eyre!("Empty tuple/struct provided but {} elements expected", types.len()));
        }
    }

    if is_struct_syntax {
        // Parse struct syntax: {field1: value1, field2: value2}
        parse_struct_syntax(inner_str, types)
    } else {
        // Parse positional tuple syntax: (value1, value2)
        parse_positional_syntax(inner_str, types)
    }
}

/// Parse struct syntax: field1: value1, field2: value2
fn parse_struct_syntax(inner_str: &str, types: &[DynSolType]) -> Result<DynSolValue> {
    // For struct syntax, we need to parse key-value pairs
    // This is a simplified implementation - a full parser would handle the struct field names
    // For now, we'll assume the order matches the ABI definition
    let elements_str = split_arguments(inner_str)?;

    if elements_str.len() != types.len() {
        return Err(eyre!(
            "Struct element count mismatch: expected {} elements, got {}",
            types.len(),
            elements_str.len()
        ));
    }

    let mut elements = Vec::new();
    for (element_str, element_type) in elements_str.iter().zip(types) {
        // For struct syntax, we need to extract the value part after ':'
        let value_str = if element_str.contains(':') {
            // Split on ':' and take the value part
            let parts: Vec<&str> = element_str.splitn(2, ':').collect();
            if parts.len() == 2 {
                parts[1].trim()
            } else {
                element_str.trim()
            }
        } else {
            element_str.trim()
        };

        let element = parse_argument_value(value_str, &element_type.to_string())?;
        elements.push(element);
    }

    Ok(DynSolValue::Tuple(elements))
}

/// Parse positional tuple syntax: value1, value2
fn parse_positional_syntax(inner_str: &str, types: &[DynSolType]) -> Result<DynSolValue> {
    let elements_str = split_arguments(inner_str)?;
    if elements_str.len() != types.len() {
        return Err(eyre!(
            "Tuple element count mismatch: expected {} elements, got {}",
            types.len(),
            elements_str.len()
        ));
    }

    let mut elements = Vec::new();
    for (element_str, element_type) in elements_str.iter().zip(types) {
        let element = parse_argument_value(element_str, &element_type.to_string())?;
        elements.push(element);
    }

    Ok(DynSolValue::Tuple(elements))
}

/// Encode function data with arguments
fn encode_function_data(function: &Function, args: Vec<DynSolValue>) -> Result<Bytes> {
    // Encode the arguments
    let encoded_args = if args.is_empty() {
        Vec::new()
    } else {
        function
            .abi_encode_input(&args)
            .map_err(|e| eyre!("Failed to encode function arguments: {}", e))?
    };

    // Combine function selector with encoded arguments
    let selector = function.selector();
    let mut result = selector.to_vec();
    result.extend_from_slice(&encoded_args);

    Ok(result.into())
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloy_json_abi::{Param, StateMutability};

    fn create_test_function(name: &str, inputs: Vec<(&str, &str)>) -> Function {
        Function {
            name: name.to_string(),
            inputs: inputs
                .into_iter()
                .map(|(name, ty)| Param {
                    name: name.to_string(),
                    ty: ty.to_string(),
                    internal_type: None, // Simplified for tests
                    components: vec![],
                })
                .collect(),
            outputs: vec![],
            state_mutability: StateMutability::NonPayable,
        }
    }

    #[test]
    fn test_parse_function_call_text() {
        assert_eq!(
            parse_function_call_text("balanceOf(0x123)").unwrap(),
            ("balanceOf".to_string(), "0x123".to_string())
        );

        assert_eq!(
            parse_function_call_text("transfer(0x123, 100)").unwrap(),
            ("transfer".to_string(), "0x123, 100".to_string())
        );

        assert_eq!(
            parse_function_call_text("noArgs()").unwrap(),
            ("noArgs".to_string(), String::new())
        );
    }

    #[test]
    fn test_split_arguments() {
        assert_eq!(split_arguments("0x123, 100").unwrap(), vec!["0x123", "100"]);

        assert_eq!(
            split_arguments("0x123, [1,2,3], \"hello, world\"").unwrap(),
            vec!["0x123", "[1,2,3]", "\"hello, world\""]
        );

        // Test with struct syntax (curly braces)
        assert_eq!(
            split_arguments("{field1: 123, field2: \"test\"}, 456").unwrap(),
            vec!["{field1: 123, field2: \"test\"}", "456"]
        );

        // Test nested structures
        assert_eq!(
            split_arguments("0x123, {inner: [1,2,3], value: 100}").unwrap(),
            vec!["0x123", "{inner: [1,2,3], value: 100}"]
        );
    }

    #[test]
    fn test_parse_address() {
        let addr = parse_address("0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1").unwrap();
        // Address parsing should succeed (case doesn't matter for correctness)
        assert_eq!(addr.to_string().to_lowercase(), "0x742d35cc6634c0532925a3b8d6ac6e89e86c6ad1");
    }

    #[test]
    fn test_parse_uint() {
        assert_eq!(parse_uint("123", 256).unwrap(), U256::from(123u64));
        assert_eq!(parse_uint("0xff", 256).unwrap(), U256::from(255u64));
    }

    #[test]
    fn test_encode_simple_function_call() {
        let mut functions = BTreeMap::new();
        let balance_of = create_test_function("balanceOf", vec![("account", "address")]);
        functions.insert("balanceOf".to_string(), vec![balance_of]);

        let encoded = encode_function_call(
            &functions,
            "balanceOf(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1)",
        )
        .unwrap();

        // Should start with balanceOf selector (0x70a08231)
        assert_eq!(&encoded[0..4], &[0x70, 0xa0, 0x82, 0x31]);
    }

    #[test]
    fn test_function_overload_resolution() {
        let mut functions = BTreeMap::new();

        // Create two different transfer functions with same argument count but different types
        let transfer_address_uint =
            create_test_function("transfer", vec![("to", "address"), ("amount", "uint256")]);
        let transfer_uint_address =
            create_test_function("transfer", vec![("tokenId", "uint256"), ("to", "address")]);

        functions
            .insert("transfer".to_string(), vec![transfer_address_uint, transfer_uint_address]);

        // Test with address first, then uint256 - should match first overload
        let encoded1 = encode_function_call(
            &functions,
            "transfer(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1, 100)",
        )
        .unwrap();

        // Test with uint256 first, then address - should match second overload
        let encoded2 = encode_function_call(
            &functions,
            "transfer(123, 0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1)",
        )
        .unwrap();

        // They should have different selectors since they're different function signatures
        assert_ne!(&encoded1[0..4], &encoded2[0..4]);
    }

    #[test]
    fn test_overload_resolution_failure() {
        let mut functions = BTreeMap::new();
        let balance_of = create_test_function("balanceOf", vec![("account", "address")]);
        functions.insert("balanceOf".to_string(), vec![balance_of]);

        // Try to call with wrong argument type (should fail)
        let result = encode_function_call(
            &functions,
            "balanceOf(123)", // uint256 instead of address
        );

        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("Failed to parse arguments"));
    }

    #[test]
    fn test_struct_syntax_parsing() {
        let mut functions = BTreeMap::new();
        // Create a function that takes a tuple (representing a struct)
        let submit_data = create_test_function("submitData", vec![("data", "(address,uint256)")]);
        functions.insert("submitData".to_string(), vec![submit_data]);

        // Test struct syntax with field names
        let encoded1 = encode_function_call(
            &functions,
            "submitData({user: 0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1, amount: 100})",
        );
        assert!(encoded1.is_ok(), "Struct syntax should work: {:?}", encoded1.err());

        // Test traditional tuple syntax should also work
        let encoded2 = encode_function_call(
            &functions,
            "submitData((0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1, 100))",
        );
        assert!(encoded2.is_ok(), "Tuple syntax should work: {:?}", encoded2.err());

        // Both should produce the same result since the order is the same
        assert_eq!(encoded1.unwrap(), encoded2.unwrap());
    }

    #[test]
    fn test_nested_struct_parsing() {
        // Test parsing of nested structures in arguments
        let result = split_arguments("0x123, {field1: {nested: 456}, field2: [1,2,3]}");
        assert!(result.is_ok());
        let args = result.unwrap();
        assert_eq!(args.len(), 2);
        assert_eq!(args[0], "0x123");
        assert_eq!(args[1], "{field1: {nested: 456}, field2: [1,2,3]}");
    }

    #[test]
    fn test_type_cast_extraction() {
        // Test uint256 cast
        assert_eq!(
            extract_type_cast("uint256(123)").unwrap(),
            (Some("uint256".to_string()), "123")
        );

        // Test address cast
        assert_eq!(
            extract_type_cast("address(0x123)").unwrap(),
            (Some("address".to_string()), "0x123")
        );

        // Test uint cast (shorthand)
        assert_eq!(extract_type_cast("uint(42)").unwrap(), (Some("uint".to_string()), "42"));

        // Test bytes32 cast
        assert_eq!(
            extract_type_cast("bytes32(0xabc)").unwrap(),
            (Some("bytes32".to_string()), "0xabc")
        );

        // Test nested parentheses
        assert_eq!(
            extract_type_cast("uint256((1 + 2))").unwrap(),
            (Some("uint256".to_string()), "(1 + 2)")
        );

        // Test no cast
        assert_eq!(extract_type_cast("123").unwrap(), (None, "123"));

        // Test no cast with parentheses
        assert_eq!(extract_type_cast("(123)").unwrap(), (None, "(123)"));
    }

    #[test]
    fn test_type_cast_with_function_call() {
        let mut functions = BTreeMap::new();
        let transfer =
            create_test_function("transfer", vec![("to", "address"), ("amount", "uint256")]);
        functions.insert("transfer".to_string(), vec![transfer]);

        // Test with type casts
        let result = encode_function_call(
            &functions,
            "transfer(address(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1), uint256(100))",
        );
        assert!(result.is_ok(), "Type casting should work: {:?}", result.err());

        // Test with shorthand uint cast
        let result = encode_function_call(
            &functions,
            "transfer(address(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1), uint(100))",
        );
        assert!(result.is_ok(), "Shorthand uint cast should work: {:?}", result.err());
    }

    #[test]
    fn test_complex_nested_calls() {
        let mut functions = BTreeMap::new();
        // Function taking nested tuples
        let complex =
            create_test_function("complexCall", vec![("data", "((address,uint256),bytes32[])")]);
        functions.insert("complexCall".to_string(), vec![complex]);

        // Test with nested structures
        let result = encode_function_call(
            &functions,
            "complexCall(((\
                0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1, \
                123\
            ), [\
                0x0000000000000000000000000000000000000000000000000000000000000001,\
                0x0000000000000000000000000000000000000000000000000000000000000002\
            ]))",
        );
        assert!(result.is_ok(), "Complex nested call should work: {:?}", result.err());
    }

    #[test]
    fn test_array_parsing() {
        let mut functions = BTreeMap::new();
        let batch = create_test_function(
            "batchTransfer",
            vec![("recipients", "address[]"), ("amounts", "uint256[]")],
        );
        functions.insert("batchTransfer".to_string(), vec![batch]);

        // Test with arrays
        let result = encode_function_call(
            &functions,
            "batchTransfer(\
                [0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1, 0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad2],\
                [100, 200]\
            )"
        );
        assert!(result.is_ok(), "Array parsing should work: {:?}", result.err());

        // Test with type casts in arrays
        let result = encode_function_call(
            &functions,
            "batchTransfer(\
                [address(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1), address(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad2)],\
                [uint256(100), uint256(200)]\
            )"
        );
        assert!(result.is_ok(), "Arrays with type casts should work: {:?}", result.err());
    }

    #[test]
    fn test_mixed_argument_types() {
        let mut functions = BTreeMap::new();
        let mixed = create_test_function(
            "mixedTypes",
            vec![("flag", "bool"), ("data", "bytes"), ("text", "string"), ("number", "int256")],
        );
        functions.insert("mixedTypes".to_string(), vec![mixed]);

        // Test with various types
        let result =
            encode_function_call(&functions, r#"mixedTypes(true, 0xabcdef, "hello world", -123)"#);
        assert!(result.is_ok(), "Mixed types should work: {:?}", result.err());

        // Test with type casts
        let result = encode_function_call(
            &functions,
            r#"mixedTypes(bool(true), bytes(0xabcdef), string("hello world"), int256(-123))"#,
        );
        assert!(result.is_ok(), "Mixed types with casts should work: {:?}", result.err());
    }

    #[test]
    fn test_edge_cases() {
        let mut functions = BTreeMap::new();
        let simple = create_test_function("test", vec![("value", "uint256")]);
        functions.insert("test".to_string(), vec![simple]);

        // Test with extra spaces
        let result = encode_function_call(&functions, "test(  123  )");
        assert!(result.is_ok());

        // Test with tabs and newlines
        let result = encode_function_call(&functions, "test(\t123\n)");
        assert!(result.is_ok());

        // Test with hex values
        let result = encode_function_call(&functions, "test(0xff)");
        assert!(result.is_ok());

        // Test with scientific notation (should fail as not supported)
        let result = encode_function_call(&functions, "test(1e18)");
        assert!(result.is_err());

        // Test with underscores in numbers (Solidity 0.8.0+ syntax)
        let result = encode_function_call(&functions, "test(1_000_000)");
        // Underscores might be parsed as a single number "1" with the rest ignored
        // or might work if Rust's parser handles them. Let's check the actual behavior
        // For now, we'll accept either behavior
        let _ = result;
    }

    #[test]
    fn test_empty_and_single_arguments() {
        let mut functions = BTreeMap::new();

        // No arguments function
        let no_args = create_test_function("noArgs", vec![]);
        functions.insert("noArgs".to_string(), vec![no_args]);

        // Single argument function
        let single_arg = create_test_function("singleArg", vec![("value", "uint256")]);
        functions.insert("singleArg".to_string(), vec![single_arg]);

        // Test empty arguments
        assert!(encode_function_call(&functions, "noArgs()").is_ok());
        assert!(encode_function_call(&functions, "noArgs( )").is_ok());
        assert!(encode_function_call(&functions, "noArgs(  )").is_ok());

        // Test single argument
        assert!(encode_function_call(&functions, "singleArg(42)").is_ok());
        assert!(encode_function_call(&functions, "singleArg( 42 )").is_ok());
        assert!(encode_function_call(&functions, "singleArg(uint256(42))").is_ok());
    }

    #[test]
    fn test_fixed_arrays() {
        let mut functions = BTreeMap::new();
        let fixed = create_test_function("fixedArray", vec![("values", "uint256[3]")]);
        functions.insert("fixedArray".to_string(), vec![fixed]);

        // Test with exact size
        let result = encode_function_call(&functions, "fixedArray([1, 2, 3])");
        assert!(result.is_ok());

        // Test with wrong size (should fail)
        let result = encode_function_call(&functions, "fixedArray([1, 2])");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Fixed array size mismatch"));

        // Test with too many elements
        let result = encode_function_call(&functions, "fixedArray([1, 2, 3, 4])");
        assert!(result.is_err());
    }

    #[test]
    fn test_string_escaping() {
        let mut functions = BTreeMap::new();
        let string_fn = create_test_function("setString", vec![("text", "string")]);
        functions.insert("setString".to_string(), vec![string_fn]);

        // Test with quotes in string
        let result = encode_function_call(&functions, r#"setString("hello \"world\"")"#);
        assert!(result.is_ok());

        // Test with single quotes
        let result = encode_function_call(&functions, r#"setString('hello world')"#);
        assert!(result.is_ok());

        // Test with mixed quotes
        let result = encode_function_call(&functions, r#"setString("it's working")"#);
        assert!(result.is_ok());

        // Test with commas in string
        let result = encode_function_call(&functions, r#"setString("hello, world")"#);
        assert!(result.is_ok());

        // Test with parentheses in string
        let result = encode_function_call(&functions, r#"setString("test(123)")"#);
        assert!(result.is_ok());

        // Test with braces in string
        let result = encode_function_call(&functions, r#"setString("{key: value}")"#);
        assert!(result.is_ok());
    }

    #[test]
    fn test_invalid_type_casts() {
        let mut functions = BTreeMap::new();
        let transfer =
            create_test_function("transfer", vec![("to", "address"), ("amount", "uint256")]);
        functions.insert("transfer".to_string(), vec![transfer]);

        // Test incompatible type cast (string to address)
        let result =
            encode_function_call(&functions, r#"transfer(string("not an address"), uint256(100))"#);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not compatible"));

        // Test bool to uint256 cast (should fail)
        let result = encode_function_call(
            &functions,
            "transfer(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1, bool(true))",
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_deeply_nested_structures() {
        let mut functions = BTreeMap::new();
        // Very deeply nested structure
        let deep = create_test_function(
            "deeplyNested",
            vec![("data", "(uint256,(address,(bytes32,bool)[]))")],
        );
        functions.insert("deeplyNested".to_string(), vec![deep]);

        let result = encode_function_call(
            &functions,
            "deeplyNested((123, (\
                0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1, \
                [(\
                    0x0000000000000000000000000000000000000000000000000000000000000001, \
                    true\
                ), (\
                    0x0000000000000000000000000000000000000000000000000000000000000002, \
                    false\
                )]\
            )))",
        );
        assert!(result.is_ok(), "Deeply nested structure should work: {:?}", result.err());
    }

    #[test]
    fn test_all_solidity_types() {
        // Test parsing of all basic Solidity types
        assert!(parse_uint("123", 256).is_ok());
        assert!(parse_uint("0xff", 256).is_ok());
        assert!(parse_uint("0", 256).is_ok());

        assert!(parse_int("123", 256).is_ok());
        assert!(parse_int("-123", 256).is_ok());
        assert!(parse_int("0", 256).is_ok());

        assert!(parse_bool("true").is_ok());
        assert!(parse_bool("false").is_ok());
        assert!(parse_bool("1").is_ok());
        assert!(parse_bool("0").is_ok());

        assert!(parse_address("0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1").is_ok());
        assert!(parse_address("742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1").is_ok());

        assert!(parse_bytes("0xabcdef").is_ok());
        assert!(parse_bytes("abcdef").is_ok());
        assert!(parse_bytes("0x").is_ok());

        assert!(parse_string("hello world").is_ok());
        assert!(parse_string("\"quoted string\"").is_ok());
        assert!(parse_string("'single quoted'").is_ok());
    }

    #[test]
    fn test_overload_with_structs() {
        let mut functions = BTreeMap::new();

        // Two functions with same name but different struct parameters
        let process1 = create_test_function("process", vec![("data", "(address,uint256)")]);
        let process2 = create_test_function("process", vec![("data", "(uint256,address)")]);

        functions.insert("process".to_string(), vec![process1, process2]);

        // Should match first overload
        let result1 = encode_function_call(
            &functions,
            "process((0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1, 100))",
        );
        assert!(result1.is_ok());

        // Should match second overload
        let result2 = encode_function_call(
            &functions,
            "process((100, 0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1))",
        );
        assert!(result2.is_ok());

        // Results should be different due to different function signatures
        assert_ne!(result1.unwrap()[0..4], result2.unwrap()[0..4]);
    }

    #[test]
    fn test_payable_and_nonpayable_functions() {
        // Test encoding for payable vs non-payable functions
        let mut functions = BTreeMap::new();
        let send_eth = create_test_function("sendEth", vec![("to", "address")]);
        functions.insert("sendEth".to_string(), vec![send_eth]);

        let result =
            encode_function_call(&functions, "sendEth(0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1)");
        assert!(result.is_ok());
    }

    #[test]
    fn test_max_values() {
        let mut functions = BTreeMap::new();
        let max_test =
            create_test_function("maxTest", vec![("maxUint", "uint256"), ("maxInt", "int256")]);
        functions.insert("maxTest".to_string(), vec![max_test]);

        // Test with maximum uint256 value
        let result = encode_function_call(
            &functions,
            "maxTest(\
                0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff, \
                0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\
            )",
        );
        assert!(result.is_ok(), "Max values should work: {:?}", result.err());

        // Test with type casts for clarity
        let result = encode_function_call(
            &functions,
            "maxTest(\
                uint256(0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff), \
                int256(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)\
            )",
        );
        assert!(result.is_ok());
    }

    #[test]
    fn test_bytes_variations() {
        let mut functions = BTreeMap::new();
        let bytes_test = create_test_function(
            "bytesTest",
            vec![
                ("b1", "bytes1"),
                ("b2", "bytes2"),
                ("b4", "bytes4"),
                ("b8", "bytes8"),
                ("b16", "bytes16"),
                ("b32", "bytes32"),
            ],
        );
        functions.insert("bytesTest".to_string(), vec![bytes_test]);

        let result = encode_function_call(
            &functions,
            "bytesTest(\
                0x01, \
                0x0102, \
                0x01020304, \
                0x0102030405060708, \
                0x01020304050607080910111213141516, \
                0x0102030405060708091011121314151617181920212223242526272829303132\
            )",
        );
        assert!(result.is_ok(), "Different bytes sizes should work: {:?}", result.err());
    }

    #[test]
    fn test_function_with_multiple_arrays() {
        let mut functions = BTreeMap::new();
        let multi_array = create_test_function(
            "multiArray",
            vec![("arr1", "uint256[]"), ("arr2", "address[]"), ("arr3", "bool[]")],
        );
        functions.insert("multiArray".to_string(), vec![multi_array]);

        let result = encode_function_call(
            &functions,
            "multiArray(\
                [1, 2, 3], \
                [0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1], \
                [true, false, true]\
            )",
        );
        assert!(result.is_ok(), "Multiple arrays should work: {:?}", result.err());
    }

    #[test]
    fn test_empty_arrays_and_strings() {
        let mut functions = BTreeMap::new();
        let empty_test = create_test_function(
            "emptyTest",
            vec![("emptyArr", "uint256[]"), ("emptyStr", "string"), ("emptyBytes", "bytes")],
        );
        functions.insert("emptyTest".to_string(), vec![empty_test]);

        // Test with empty values
        let result = encode_function_call(&functions, r#"emptyTest([], "", 0x)"#);
        assert!(result.is_ok(), "Empty values should work: {:?}", result.err());

        // Test with type casts
        let result = encode_function_call(&functions, r#"emptyTest([], string(""), bytes(0x))"#);
        assert!(result.is_ok());
    }

    #[test]
    fn test_special_characters_in_strings() {
        let mut functions = BTreeMap::new();
        let special = create_test_function("specialChars", vec![("text", "string")]);
        functions.insert("specialChars".to_string(), vec![special]);

        // Test newlines, tabs, and other special characters
        let result =
            encode_function_call(&functions, r#"specialChars("line1\nline2\ttab\r\nwindows")"#);
        assert!(result.is_ok());

        // Test Unicode characters
        let result = encode_function_call(&functions, r#"specialChars("Hello 世界 🌍")"#);
        assert!(result.is_ok());

        // Test backslashes
        let result = encode_function_call(&functions, r#"specialChars("path\\to\\file")"#);
        assert!(result.is_ok());
    }

    #[test]
    fn test_complex_overload_resolution() {
        let mut functions = BTreeMap::new();

        // Multiple overloads with different complexity
        let func1 = create_test_function("complex", vec![("a", "uint256")]);
        let func2 = create_test_function("complex", vec![("a", "uint256"), ("b", "uint256")]);
        let func3 = create_test_function("complex", vec![("a", "uint256"), ("b", "address")]);
        let func4 = create_test_function("complex", vec![("data", "(uint256,address)")]);

        functions.insert("complex".to_string(), vec![func1, func2, func3, func4]);

        // Should match single argument version
        assert!(encode_function_call(&functions, "complex(123)").is_ok());

        // Should match two uint256 version
        assert!(encode_function_call(&functions, "complex(123, 456)").is_ok());

        // Should match uint256, address version
        assert!(encode_function_call(
            &functions,
            "complex(123, 0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1)"
        )
        .is_ok());

        // Should match tuple version
        assert!(encode_function_call(
            &functions,
            "complex((123, 0x742d35Cc6634C0532925a3b8D6Ac6E89e86C6Ad1))"
        )
        .is_ok());
    }

    #[test]
    fn test_error_messages() {
        let mut functions = BTreeMap::new();
        let test_fn = create_test_function("test", vec![("value", "uint256")]);
        functions.insert("test".to_string(), vec![test_fn]);

        // Test various error conditions
        let result = encode_function_call(&functions, "nonexistent(123)");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));

        let result = encode_function_call(&functions, "test(0xnotahexvalue)");
        assert!(result.is_err());

        let result = encode_function_call(&functions, "test(true)"); // bool instead of uint256
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Failed to parse"));

        let result = encode_function_call(&functions, "test"); // Missing parentheses
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid function call format"));

        let result = encode_function_call(&functions, "test("); // Unclosed parentheses
        assert!(result.is_err());
    }

    #[test]
    fn test_zero_and_negative_values() {
        let mut functions = BTreeMap::new();
        let zero_test = create_test_function(
            "zeroTest",
            vec![("uintZero", "uint256"), ("intNeg", "int256"), ("intZero", "int256")],
        );
        functions.insert("zeroTest".to_string(), vec![zero_test]);

        // Test zero and negative values
        let result = encode_function_call(&functions, "zeroTest(0, -1, 0)");
        assert!(result.is_ok());

        // Test with hex notation
        let result = encode_function_call(&functions, "zeroTest(0x0, -0x1, 0x00)");
        assert!(result.is_ok());

        // Test extreme negative
        let result = encode_function_call(
            &functions,
            "zeroTest(0, -57896044618658097711785492504343953926634992332820282019728792003956564819968, 0)"
        );
        assert!(result.is_ok());
    }
}