aptos-sdk 0.4.1

A user-friendly, idiomatic Rust SDK for the Aptos blockchain
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
//! Code generator for Move modules.

use crate::api::response::{MoveFunction, MoveModuleABI, MoveStructDef, MoveStructField};
use crate::codegen::move_parser::{EnrichedFunctionInfo, MoveModuleInfo};
use crate::codegen::types::{MoveTypeMapper, to_pascal_case, to_snake_case};
use crate::error::{AptosError, AptosResult};
use std::fmt::Write;

/// Sanitizes an ABI-derived string for safe embedding in generated Rust code.
///
/// # Security
///
/// Prevents code injection via crafted ABI JSON by:
/// - Replacing newlines (which could escape doc comments and inject code)
/// - Escaping double quotes (which could break string literals)
/// - Escaping backslashes (which could create escape sequences)
fn sanitize_abi_string(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace(['\n', '\r'], " ")
}

/// Configuration for code generation.
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)] // Config structs often have boolean options
pub struct GeneratorConfig {
    /// The module name for the generated code (defaults to module name from ABI).
    pub module_name: Option<String>,
    /// Whether to generate entry function wrappers.
    pub generate_entry_functions: bool,
    /// Whether to generate view function wrappers.
    pub generate_view_functions: bool,
    /// Whether to generate struct definitions.
    pub generate_structs: bool,
    /// Whether to generate event type helpers.
    pub generate_events: bool,
    /// Whether to generate async functions (vs sync).
    pub async_functions: bool,
    /// Custom type mapper.
    pub type_mapper: MoveTypeMapper,
    /// Whether to include the module address constant.
    pub include_address_constant: bool,
    /// Whether to generate builder pattern for entry functions.
    pub use_builder_pattern: bool,
}

impl Default for GeneratorConfig {
    fn default() -> Self {
        Self {
            module_name: None,
            generate_entry_functions: true,
            generate_view_functions: true,
            generate_structs: true,
            generate_events: true,
            async_functions: true,
            type_mapper: MoveTypeMapper::new(),
            include_address_constant: true,
            use_builder_pattern: false,
        }
    }
}

impl GeneratorConfig {
    /// Creates a new configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the module name.
    #[must_use]
    pub fn with_module_name(mut self, name: impl Into<String>) -> Self {
        self.module_name = Some(name.into());
        self
    }

    /// Enables or disables entry function generation.
    #[must_use]
    pub fn with_entry_functions(mut self, enabled: bool) -> Self {
        self.generate_entry_functions = enabled;
        self
    }

    /// Enables or disables view function generation.
    #[must_use]
    pub fn with_view_functions(mut self, enabled: bool) -> Self {
        self.generate_view_functions = enabled;
        self
    }

    /// Enables or disables struct generation.
    #[must_use]
    pub fn with_structs(mut self, enabled: bool) -> Self {
        self.generate_structs = enabled;
        self
    }

    /// Enables or disables event type generation.
    #[must_use]
    pub fn with_events(mut self, enabled: bool) -> Self {
        self.generate_events = enabled;
        self
    }

    /// Enables or disables async functions.
    #[must_use]
    pub fn with_async(mut self, enabled: bool) -> Self {
        self.async_functions = enabled;
        self
    }

    /// Enables builder pattern for entry functions.
    #[must_use]
    pub fn with_builder_pattern(mut self, enabled: bool) -> Self {
        self.use_builder_pattern = enabled;
        self
    }
}

/// Generates Rust code from a Move module ABI.
#[allow(missing_debug_implementations)] // Contains references that don't implement Debug
pub struct ModuleGenerator<'a> {
    abi: &'a MoveModuleABI,
    config: GeneratorConfig,
    source_info: Option<MoveModuleInfo>,
}

impl<'a> ModuleGenerator<'a> {
    /// Creates a new generator for the given ABI.
    #[must_use]
    pub fn new(abi: &'a MoveModuleABI, config: GeneratorConfig) -> Self {
        Self {
            abi,
            config,
            source_info: None,
        }
    }

    /// Adds Move source information for better parameter names and documentation.
    #[must_use]
    pub fn with_source_info(mut self, source_info: MoveModuleInfo) -> Self {
        self.source_info = Some(source_info);
        self
    }

    /// Gets enriched function info by combining ABI and source data.
    fn get_enriched_function(&self, func: &MoveFunction) -> EnrichedFunctionInfo {
        let source_func = self
            .source_info
            .as_ref()
            .and_then(|s| s.functions.get(&func.name));

        EnrichedFunctionInfo::from_abi_and_source(
            &func.name,
            &func.params,
            func.generic_type_params.len(),
            source_func,
        )
    }

    /// Generates the complete Rust module code.
    ///
    /// # Errors
    ///
    /// Returns an error if code generation fails (e.g., formatting errors).
    pub fn generate(&self) -> AptosResult<String> {
        let mut output = String::new();

        self.write_all(&mut output)
            .map_err(|e| AptosError::Internal(format!("Code generation failed: {e}")))?;

        Ok(output)
    }

    /// Writes all code to the output string.
    fn write_all(&self, output: &mut String) -> std::fmt::Result {
        // File header
        self.write_header(output)?;

        // Imports
        self.write_imports(output)?;

        // Module address constant
        if self.config.include_address_constant {
            self.write_address_constant(output)?;
        }

        // Struct definitions
        if self.config.generate_structs {
            self.write_structs(output)?;
        }

        // Event types
        if self.config.generate_events {
            self.write_events(output)?;
        }

        // Entry functions
        if self.config.generate_entry_functions {
            self.write_entry_functions(output)?;
        }

        // View functions
        if self.config.generate_view_functions {
            self.write_view_functions(output)?;
        }

        Ok(())
    }

    /// Writes event type helpers.
    fn write_events(&self, output: &mut String) -> std::fmt::Result {
        // Find structs that are likely events (heuristic: name ends with "Event")
        let event_structs: Vec<_> = self
            .abi
            .structs
            .iter()
            .filter(|s| s.name.ends_with("Event") && !s.is_native)
            .collect();

        if event_structs.is_empty() {
            return Ok(());
        }

        writeln!(output, "// =============================================")?;
        writeln!(output, "// Event Types")?;
        writeln!(output, "// =============================================")?;
        writeln!(output)?;

        // Generate event type enum
        writeln!(output, "/// All event types defined in this module.")?;
        writeln!(output, "#[derive(Debug, Clone, PartialEq)]")?;
        writeln!(output, "pub enum ModuleEvent {{")?;
        for struct_def in &event_structs {
            let variant_name = to_pascal_case(&struct_def.name);
            writeln!(output, "    {variant_name}({variant_name}),")?;
        }
        writeln!(output, "    /// Unknown event type.")?;
        writeln!(output, "    Unknown(serde_json::Value),")?;
        writeln!(output, "}}")?;
        writeln!(output)?;

        // Generate event type string constants
        writeln!(output, "/// Event type strings for this module.")?;
        writeln!(output, "pub mod event_types {{")?;
        for struct_def in &event_structs {
            let const_name = to_snake_case(&struct_def.name).to_uppercase();
            writeln!(
                output,
                "    pub const {}: &str = \"{}::{}::{}\";",
                const_name,
                sanitize_abi_string(&self.abi.address),
                sanitize_abi_string(&self.abi.name),
                sanitize_abi_string(&struct_def.name)
            )?;
        }
        writeln!(output, "}}")?;
        writeln!(output)?;

        // Generate parse_event function
        writeln!(output, "/// Parses a raw event into a typed ModuleEvent.")?;
        writeln!(output, "///")?;
        writeln!(output, "/// # Arguments")?;
        writeln!(output, "///")?;
        writeln!(output, "/// * `event_type` - The event type string")?;
        writeln!(output, "/// * `data` - The event data as JSON")?;
        writeln!(
            output,
            "pub fn parse_event(event_type: &str, data: serde_json::Value) -> AptosResult<ModuleEvent> {{"
        )?;
        writeln!(output, "    match event_type {{")?;
        for struct_def in &event_structs {
            let const_name = to_snake_case(&struct_def.name).to_uppercase();
            let variant_name = to_pascal_case(&struct_def.name);
            writeln!(output, "        event_types::{const_name} => {{")?;
            writeln!(
                output,
                "            let event: {variant_name} = serde_json::from_value(data)"
            )?;
            writeln!(
                output,
                "                .map_err(|e| AptosError::Internal(format!(\"Failed to parse {}: {{}}\", e)))?;",
                struct_def.name
            )?;
            writeln!(output, "            Ok(ModuleEvent::{variant_name}(event))")?;
            writeln!(output, "        }}")?;
        }
        writeln!(output, "        _ => Ok(ModuleEvent::Unknown(data)),")?;
        writeln!(output, "    }}")?;
        writeln!(output, "}}")?;
        writeln!(output)?;

        // Generate is_module_event helper
        writeln!(
            output,
            "/// Checks if an event type belongs to this module."
        )?;
        writeln!(
            output,
            "pub fn is_module_event(event_type: &str) -> bool {{"
        )?;
        writeln!(
            output,
            "    event_type.starts_with(\"{}::{}::\")",
            sanitize_abi_string(&self.abi.address),
            sanitize_abi_string(&self.abi.name)
        )?;
        writeln!(output, "}}")?;
        writeln!(output)
    }

    /// Writes the file header comment.
    fn write_header(&self, output: &mut String) -> std::fmt::Result {
        writeln!(
            output,
            "//! Generated Rust bindings for `{}::{}`.",
            sanitize_abi_string(&self.abi.address),
            sanitize_abi_string(&self.abi.name)
        )?;
        writeln!(output, "//!")?;
        writeln!(
            output,
            "//! This file was auto-generated by aptos-sdk codegen."
        )?;
        writeln!(output, "//! Do not edit manually.")?;
        writeln!(output)?;
        writeln!(output, "#![allow(dead_code)]")?;
        writeln!(output, "#![allow(unused_imports)]")?;
        writeln!(output)
    }

    /// Writes import statements.
    #[allow(clippy::unused_self)] // Some methods take &self for future extensibility
    fn write_imports(&self, output: &mut String) -> std::fmt::Result {
        writeln!(output, "use aptos_sdk::{{")?;
        writeln!(output, "    account::Account,")?;
        writeln!(output, "    error::{{AptosError, AptosResult}},")?;
        writeln!(
            output,
            "    transaction::{{EntryFunction, TransactionPayload}},"
        )?;
        writeln!(output, "    types::{{AccountAddress, TypeTag}},")?;
        writeln!(output, "    Aptos,")?;
        writeln!(output, "}};")?;
        writeln!(output, "use serde::{{Deserialize, Serialize}};")?;
        writeln!(output)
    }

    /// Writes the module address constant.
    fn write_address_constant(&self, output: &mut String) -> std::fmt::Result {
        writeln!(output, "/// The address where this module is deployed.")?;
        writeln!(
            output,
            "pub const MODULE_ADDRESS: &str = \"{}\";",
            sanitize_abi_string(&self.abi.address)
        )?;
        writeln!(output)?;
        writeln!(output, "/// The module name.")?;
        writeln!(
            output,
            "pub const MODULE_NAME: &str = \"{}\";",
            sanitize_abi_string(&self.abi.name)
        )?;
        writeln!(output)
    }

    /// Writes struct definitions.
    fn write_structs(&self, output: &mut String) -> std::fmt::Result {
        if self.abi.structs.is_empty() {
            return Ok(());
        }

        writeln!(output, "// =============================================")?;
        writeln!(output, "// Struct Definitions")?;
        writeln!(output, "// =============================================")?;
        writeln!(output)?;

        for struct_def in &self.abi.structs {
            self.write_struct(output, struct_def)?;
        }

        Ok(())
    }

    /// Writes a single struct definition.
    fn write_struct(&self, output: &mut String, struct_def: &MoveStructDef) -> std::fmt::Result {
        // Skip native structs
        if struct_def.is_native {
            return Ok(());
        }

        let rust_name = to_pascal_case(&struct_def.name);

        // Documentation
        // SECURITY: Sanitize ABI-derived strings to prevent code injection via newlines
        writeln!(
            output,
            "/// Move struct: `{}::{}`",
            sanitize_abi_string(&self.abi.name),
            sanitize_abi_string(&struct_def.name)
        )?;
        if !struct_def.abilities.is_empty() {
            let abilities = struct_def
                .abilities
                .iter()
                .map(|a| sanitize_abi_string(a))
                .collect::<Vec<_>>()
                .join(", ");
            writeln!(output, "///")?;
            writeln!(output, "/// Abilities: {abilities}")?;
        }

        // Derive macros
        writeln!(
            output,
            "#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]"
        )?;

        // Generic type parameters
        if struct_def.generic_type_params.is_empty() {
            writeln!(output, "pub struct {rust_name} {{")?;
        } else {
            let type_params: Vec<String> = struct_def
                .generic_type_params
                .iter()
                .enumerate()
                .map(|(i, _)| format!("T{i}"))
                .collect();

            writeln!(
                output,
                "pub struct {}<{}> {{",
                rust_name,
                type_params.join(", ")
            )?;
        }

        // Fields
        for field in &struct_def.fields {
            self.write_struct_field(output, field)?;
        }

        writeln!(output, "}}")?;
        writeln!(output)
    }

    /// Writes a struct field.
    fn write_struct_field(&self, output: &mut String, field: &MoveStructField) -> std::fmt::Result {
        let rust_type = self.config.type_mapper.map_type(&field.typ);
        let rust_name = to_snake_case(&field.name);

        // Handle reserved Rust keywords
        let rust_name = match rust_name.as_str() {
            "type" => "r#type".to_string(),
            "self" => "r#self".to_string(),
            "move" => "r#move".to_string(),
            _ => rust_name,
        };

        if let Some(doc) = &rust_type.doc {
            // SECURITY: Sanitize doc strings derived from ABI to prevent code injection
            writeln!(output, "    /// {}", sanitize_abi_string(doc))?;
        }

        // Add serde rename if field name differs
        if rust_name != field.name && !rust_name.starts_with("r#") {
            // SECURITY: Escape field names to prevent breaking the serde attribute syntax
            writeln!(
                output,
                "    #[serde(rename = \"{}\")]",
                sanitize_abi_string(&field.name)
            )?;
        }

        writeln!(output, "    pub {}: {},", rust_name, rust_type.path)
    }

    /// Writes entry function wrappers.
    fn write_entry_functions(&self, output: &mut String) -> std::fmt::Result {
        let entry_functions: Vec<_> = self
            .abi
            .exposed_functions
            .iter()
            .filter(|f| f.is_entry)
            .collect();

        if entry_functions.is_empty() {
            return Ok(());
        }

        writeln!(output, "// =============================================")?;
        writeln!(output, "// Entry Functions")?;
        writeln!(output, "// =============================================")?;
        writeln!(output)?;

        for func in entry_functions {
            self.write_entry_function(output, func)?;
        }

        Ok(())
    }

    /// Writes a single entry function wrapper.
    fn write_entry_function(&self, output: &mut String, func: &MoveFunction) -> std::fmt::Result {
        let rust_name = to_snake_case(&func.name);
        let enriched = self.get_enriched_function(func);

        // Get non-signer parameters with their enriched names
        let params: Vec<_> = enriched
            .non_signer_params()
            .into_iter()
            .map(|p| {
                let rust_type = self.config.type_mapper.map_type(&p.move_type);
                (p.name.clone(), p.move_type.clone(), rust_type)
            })
            .collect();

        // Documentation from source or generated
        // SECURITY: Sanitize ABI-derived strings to prevent code injection via newlines
        if let Some(doc) = &enriched.doc {
            for line in doc.lines() {
                writeln!(output, "/// {}", sanitize_abi_string(line))?;
            }
            writeln!(output, "///")?;
        } else {
            writeln!(
                output,
                "/// Entry function: `{}::{}`",
                sanitize_abi_string(&self.abi.name),
                sanitize_abi_string(&func.name)
            )?;
            writeln!(output, "///")?;
        }

        // Arguments documentation
        if !params.is_empty() {
            writeln!(output, "/// # Arguments")?;
            writeln!(output, "///")?;
            for (name, move_type, rust_type) in &params {
                writeln!(
                    output,
                    "/// * `{}` - {} (Move type: `{}`)",
                    sanitize_abi_string(name),
                    sanitize_abi_string(&rust_type.path),
                    sanitize_abi_string(move_type)
                )?;
            }
        }
        if !enriched.type_param_names.is_empty() {
            let type_params = enriched
                .type_param_names
                .iter()
                .map(|s| sanitize_abi_string(s))
                .collect::<Vec<_>>()
                .join(", ");
            writeln!(output, "/// * `type_args` - Type arguments: {type_params}")?;
        }

        // Function signature
        write!(output, "pub fn {rust_name}(")?;

        // Parameters with meaningful names
        let mut param_strs = Vec::new();
        for (name, _, rust_type) in &params {
            // Convert to snake_case and handle reserved words
            let safe_name = Self::safe_param_name(name);
            param_strs.push(format!("{}: {}", safe_name, rust_type.as_arg_type()));
        }
        if !enriched.type_param_names.is_empty() {
            param_strs.push("type_args: Vec<TypeTag>".to_string());
        }
        write!(output, "{}", param_strs.join(", "))?;

        writeln!(output, ") -> AptosResult<TransactionPayload> {{")?;

        // Function body
        // SECURITY: Sanitize ABI-derived values embedded in string literals
        writeln!(output, "    let function_id = format!(")?;
        writeln!(
            output,
            "        \"{}::{}::{}\",",
            sanitize_abi_string(&self.abi.address),
            sanitize_abi_string(&self.abi.name),
            sanitize_abi_string(&func.name)
        )?;
        writeln!(output, "    );")?;
        writeln!(output)?;

        // Build arguments using the parameter names
        writeln!(output, "    let args = vec![")?;
        for (name, move_type, _) in &params {
            let safe_name = Self::safe_param_name(name);
            let bcs_expr = self.config.type_mapper.to_bcs_arg(move_type, &safe_name);
            writeln!(output, "        {bcs_expr},")?;
        }
        writeln!(output, "    ];")?;
        writeln!(output)?;

        // Type arguments
        if func.generic_type_params.is_empty() {
            writeln!(output, "    let type_args = vec![];")?;
        }
        writeln!(output)?;

        // Create entry function
        writeln!(
            output,
            "    let entry_fn = EntryFunction::from_function_id(&function_id, type_args, args)?;"
        )?;
        writeln!(
            output,
            "    Ok(TransactionPayload::EntryFunction(entry_fn))"
        )?;
        writeln!(output, "}}")?;
        writeln!(output)
    }

    /// Writes view function wrappers.
    fn write_view_functions(&self, output: &mut String) -> std::fmt::Result {
        let view_functions: Vec<_> = self
            .abi
            .exposed_functions
            .iter()
            .filter(|f| f.is_view)
            .collect();

        if view_functions.is_empty() {
            return Ok(());
        }

        writeln!(output, "// =============================================")?;
        writeln!(output, "// View Functions")?;
        writeln!(output, "// =============================================")?;
        writeln!(output)?;

        for func in view_functions {
            self.write_view_function(output, func)?;
        }

        Ok(())
    }

    /// Converts a parameter name to a safe Rust identifier.
    fn safe_param_name(name: &str) -> String {
        let snake = to_snake_case(name);
        match snake.as_str() {
            "type" => "r#type".to_string(),
            "self" => "r#self".to_string(),
            "move" => "r#move".to_string(),
            "ref" => "r#ref".to_string(),
            "mut" => "r#mut".to_string(),
            "fn" => "r#fn".to_string(),
            "mod" => "r#mod".to_string(),
            "use" => "r#use".to_string(),
            "pub" => "r#pub".to_string(),
            "let" => "r#let".to_string(),
            "if" => "r#if".to_string(),
            "else" => "r#else".to_string(),
            "match" => "r#match".to_string(),
            "loop" => "r#loop".to_string(),
            "while" => "r#while".to_string(),
            "for" => "r#for".to_string(),
            "in" => "r#in".to_string(),
            "return" => "r#return".to_string(),
            "break" => "r#break".to_string(),
            "continue" => "r#continue".to_string(),
            "async" => "r#async".to_string(),
            "await" => "r#await".to_string(),
            "struct" => "r#struct".to_string(),
            "enum" => "r#enum".to_string(),
            "trait" => "r#trait".to_string(),
            "impl" => "r#impl".to_string(),
            "dyn" => "r#dyn".to_string(),
            "const" => "r#const".to_string(),
            "static" => "r#static".to_string(),
            "unsafe" => "r#unsafe".to_string(),
            "extern" => "r#extern".to_string(),
            "crate" => "r#crate".to_string(),
            "super" => "r#super".to_string(),
            "where" => "r#where".to_string(),
            "as" => "r#as".to_string(),
            "true" => "r#true".to_string(),
            "false" => "r#false".to_string(),
            _ => snake,
        }
    }

    /// Writes a single view function wrapper.
    fn write_view_function(&self, output: &mut String, func: &MoveFunction) -> std::fmt::Result {
        let rust_name = format!("view_{}", to_snake_case(&func.name));
        let enriched = self.get_enriched_function(func);

        // Get all parameters with their enriched names (view functions can have any params)
        let params: Vec<_> = enriched
            .params
            .iter()
            .map(|p| {
                let rust_type = self.config.type_mapper.map_type(&p.move_type);
                (p.name.clone(), p.move_type.clone(), rust_type)
            })
            .collect();

        // Return type
        let return_type = if func.returns.is_empty() {
            "()".to_string()
        } else if func.returns.len() == 1 {
            self.config.type_mapper.map_type(&func.returns[0]).path
        } else {
            let types: Vec<String> = func
                .returns
                .iter()
                .map(|r| self.config.type_mapper.map_type(r).path)
                .collect();
            format!("({})", types.join(", "))
        };

        // Documentation from source or generated
        // SECURITY: Sanitize ABI-derived strings to prevent code injection via newlines
        if let Some(doc) = &enriched.doc {
            for line in doc.lines() {
                writeln!(output, "/// {}", sanitize_abi_string(line))?;
            }
            writeln!(output, "///")?;
        } else {
            writeln!(
                output,
                "/// View function: `{}::{}`",
                sanitize_abi_string(&self.abi.name),
                sanitize_abi_string(&func.name)
            )?;
            writeln!(output, "///")?;
        }

        // Arguments documentation
        if !params.is_empty() {
            writeln!(output, "/// # Arguments")?;
            writeln!(output, "///")?;
            for (name, move_type, rust_type) in &params {
                writeln!(
                    output,
                    "/// * `{}` - {} (Move type: `{}`)",
                    sanitize_abi_string(name),
                    sanitize_abi_string(&rust_type.path),
                    sanitize_abi_string(move_type)
                )?;
            }
        }
        if !enriched.type_param_names.is_empty() {
            let type_params = enriched
                .type_param_names
                .iter()
                .map(|s| sanitize_abi_string(s))
                .collect::<Vec<_>>()
                .join(", ");
            writeln!(output, "/// * `type_args` - Type arguments: {type_params}")?;
        }
        if !func.returns.is_empty() {
            writeln!(output, "///")?;
            writeln!(output, "/// # Returns")?;
            writeln!(output, "///")?;
            writeln!(output, "/// `{}`", sanitize_abi_string(&return_type))?;
        }

        // Function signature
        let async_kw = if self.config.async_functions {
            "async "
        } else {
            ""
        };

        write!(output, "pub {async_kw}fn {rust_name}(aptos: &Aptos")?;

        // Parameters with meaningful names
        for (name, _, rust_type) in &params {
            let safe_name = Self::safe_param_name(name);
            write!(output, ", {}: {}", safe_name, rust_type.as_arg_type())?;
        }
        if !enriched.type_param_names.is_empty() {
            write!(output, ", type_args: Vec<String>")?;
        }

        writeln!(output, ") -> AptosResult<Vec<serde_json::Value>> {{")?;

        // Function body
        // SECURITY: Sanitize ABI-derived values embedded in string literals
        writeln!(output, "    let function_id = format!(")?;
        writeln!(
            output,
            "        \"{}::{}::{}\",",
            sanitize_abi_string(&self.abi.address),
            sanitize_abi_string(&self.abi.name),
            sanitize_abi_string(&func.name)
        )?;
        writeln!(output, "    );")?;
        writeln!(output)?;

        // Type arguments
        if enriched.type_param_names.is_empty() {
            writeln!(output, "    let type_args: Vec<String> = vec![];")?;
        }

        // Build view arguments as JSON using parameter names
        writeln!(output, "    let args = vec![")?;
        for (name, move_type, _) in &params {
            let safe_name = Self::safe_param_name(name);
            let arg_expr = self.view_arg_json_expr(move_type, &safe_name);
            writeln!(output, "        {arg_expr},")?;
        }
        writeln!(output, "    ];")?;
        writeln!(output)?;

        // Call view function
        let await_kw = if self.config.async_functions {
            ".await"
        } else {
            ""
        };
        writeln!(
            output,
            "    aptos.view(&function_id, type_args, args){await_kw}"
        )?;
        writeln!(output, "}}")?;
        writeln!(output)
    }

    /// Creates a JSON expression for a view function argument.
    #[allow(clippy::unused_self)] // Some methods take &self for future extensibility
    fn view_arg_json_expr(&self, move_type: &str, var_name: &str) -> String {
        match move_type {
            "address" => format!("serde_json::json!({var_name}.to_string())"),
            "bool" | "u8" | "u16" | "u32" | "u64" | "u128" => {
                format!("serde_json::json!({var_name}.to_string())")
            }
            _ if move_type.starts_with("vector<u8>") => {
                format!("serde_json::json!(::aptos_sdk::const_hex::encode({var_name}))")
            }
            "0x1::string::String" => format!("serde_json::json!({var_name})"),
            _ if move_type.ends_with("::string::String") => {
                format!("serde_json::json!({var_name})")
            }
            _ => format!("serde_json::json!({var_name})"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::response::{MoveFunctionGenericTypeParam, MoveStructGenericTypeParam};

    fn sample_abi() -> MoveModuleABI {
        MoveModuleABI {
            address: "0x1".to_string(),
            name: "coin".to_string(),
            exposed_functions: vec![
                MoveFunction {
                    name: "transfer".to_string(),
                    visibility: "public".to_string(),
                    is_entry: true,
                    is_view: false,
                    generic_type_params: vec![MoveFunctionGenericTypeParam {
                        constraints: vec![],
                    }],
                    params: vec![
                        "&signer".to_string(),
                        "address".to_string(),
                        "u64".to_string(),
                    ],
                    returns: vec![],
                },
                MoveFunction {
                    name: "balance".to_string(),
                    visibility: "public".to_string(),
                    is_entry: false,
                    is_view: true,
                    generic_type_params: vec![MoveFunctionGenericTypeParam {
                        constraints: vec![],
                    }],
                    params: vec!["address".to_string()],
                    returns: vec!["u64".to_string()],
                },
            ],
            structs: vec![MoveStructDef {
                name: "Coin".to_string(),
                is_native: false,
                abilities: vec!["store".to_string()],
                generic_type_params: vec![MoveStructGenericTypeParam {
                    constraints: vec![],
                }],
                fields: vec![MoveStructField {
                    name: "value".to_string(),
                    typ: "u64".to_string(),
                }],
            }],
        }
    }

    fn sample_abi_with_events() -> MoveModuleABI {
        MoveModuleABI {
            address: "0x1".to_string(),
            name: "token".to_string(),
            exposed_functions: vec![],
            structs: vec![
                MoveStructDef {
                    name: "MintEvent".to_string(),
                    is_native: false,
                    abilities: vec!["drop".to_string(), "store".to_string()],
                    generic_type_params: vec![],
                    fields: vec![
                        MoveStructField {
                            name: "id".to_string(),
                            typ: "u64".to_string(),
                        },
                        MoveStructField {
                            name: "creator".to_string(),
                            typ: "address".to_string(),
                        },
                    ],
                },
                MoveStructDef {
                    name: "BurnEvent".to_string(),
                    is_native: false,
                    abilities: vec!["drop".to_string(), "store".to_string()],
                    generic_type_params: vec![],
                    fields: vec![MoveStructField {
                        name: "id".to_string(),
                        typ: "u64".to_string(),
                    }],
                },
                MoveStructDef {
                    name: "Token".to_string(),
                    is_native: false,
                    abilities: vec!["key".to_string()],
                    generic_type_params: vec![],
                    fields: vec![MoveStructField {
                        name: "value".to_string(),
                        typ: "u64".to_string(),
                    }],
                },
            ],
        }
    }

    #[test]
    fn test_generate_module() {
        let abi = sample_abi();
        let generator = ModuleGenerator::new(&abi, GeneratorConfig::default());
        let code = generator.generate().unwrap();

        // Verify header
        assert!(code.contains("Generated Rust bindings"));
        assert!(code.contains("0x1::coin"));

        // Verify constants
        assert!(code.contains("MODULE_ADDRESS"));
        assert!(code.contains("MODULE_NAME"));

        // Verify struct
        assert!(code.contains("pub struct Coin"));
        assert!(code.contains("pub value: u64"));

        // Verify entry function
        assert!(code.contains("pub fn transfer"));

        // Verify view function
        assert!(code.contains("pub async fn view_balance"));
    }

    #[test]
    fn test_entry_function_excludes_signer() {
        let abi = sample_abi();
        let generator = ModuleGenerator::new(&abi, GeneratorConfig::default());
        let code = generator.generate().unwrap();

        // transfer should have 2 args (address, u64), not 3 (signer is excluded)
        // Without source info, names are generated from types: addr, amount
        assert!(code.contains("addr: AccountAddress"));
        assert!(code.contains("amount: u64"));
        // Should not have account (signer) as an argument
        assert!(!code.contains("account: AccountAddress"));
        // Function should exist
        assert!(code.contains("pub fn transfer("));
    }

    #[test]
    fn test_entry_function_with_source_info() {
        use crate::codegen::move_parser::MoveSourceParser;

        let abi = sample_abi();
        let source = r"
            module 0x1::coin {
                /// Transfers coins from sender to recipient.
                public entry fun transfer<CoinType>(
                    from: &signer,
                    to: address,
                    value: u64,
                ) { }
            }
        ";
        let source_info = MoveSourceParser::parse(source);

        let generator =
            ModuleGenerator::new(&abi, GeneratorConfig::default()).with_source_info(source_info);
        let code = generator.generate().unwrap();

        // With source info, should have the actual parameter names
        assert!(code.contains("to: AccountAddress"));
        assert!(code.contains("value: u64"));
        // Should have the documentation
        assert!(code.contains("Transfers coins from sender to recipient"));
    }

    #[test]
    fn test_generate_events() {
        let abi = sample_abi_with_events();
        let generator = ModuleGenerator::new(&abi, GeneratorConfig::default());
        let code = generator.generate().unwrap();

        // Should generate event enum
        assert!(code.contains("pub enum ModuleEvent"));
        assert!(code.contains("MintEvent(MintEvent)"));
        assert!(code.contains("BurnEvent(BurnEvent)"));
        assert!(code.contains("Unknown(serde_json::Value)"));

        // Should generate event type constants
        assert!(code.contains("pub mod event_types"));
        assert!(code.contains("MINT_EVENT"));
        assert!(code.contains("BURN_EVENT"));

        // Should generate parse_event function
        assert!(code.contains("pub fn parse_event"));
        assert!(code.contains("is_module_event"));

        // Non-event struct (Token) should not be in event enum
        assert!(!code.contains("Token(Token)"));
    }

    #[test]
    fn test_config_disable_events() {
        let abi = sample_abi_with_events();
        let config = GeneratorConfig::default().with_events(false);
        let generator = ModuleGenerator::new(&abi, config);
        let code = generator.generate().unwrap();

        // Should not generate event helpers
        assert!(!code.contains("pub enum ModuleEvent"));
        assert!(!code.contains("pub fn parse_event"));
    }

    #[test]
    fn test_config_disable_structs() {
        let abi = sample_abi();
        let config = GeneratorConfig::default().with_structs(false);
        let generator = ModuleGenerator::new(&abi, config);
        let code = generator.generate().unwrap();

        // Should not generate struct definitions
        assert!(!code.contains("pub struct Coin"));
    }

    #[test]
    fn test_config_sync_functions() {
        let abi = sample_abi();
        let config = GeneratorConfig::default().with_async(false);
        let generator = ModuleGenerator::new(&abi, config);
        let code = generator.generate().unwrap();

        // Should generate sync view function, not async
        assert!(code.contains("pub fn view_balance"));
        assert!(!code.contains("pub async fn view_balance"));
    }

    #[test]
    fn test_config_no_address_constant() {
        let abi = sample_abi();
        let config = GeneratorConfig {
            include_address_constant: false,
            ..Default::default()
        };
        let generator = ModuleGenerator::new(&abi, config);
        let code = generator.generate().unwrap();

        // Should not include module address constant
        assert!(!code.contains("MODULE_ADDRESS"));
    }

    #[test]
    fn test_generator_config_builder() {
        let config = GeneratorConfig::new()
            .with_module_name("custom_name")
            .with_entry_functions(false)
            .with_view_functions(false)
            .with_structs(false)
            .with_events(false)
            .with_async(false)
            .with_builder_pattern(true);

        assert_eq!(config.module_name, Some("custom_name".to_string()));
        assert!(!config.generate_entry_functions);
        assert!(!config.generate_view_functions);
        assert!(!config.generate_structs);
        assert!(!config.generate_events);
        assert!(!config.async_functions);
        assert!(config.use_builder_pattern);
    }

    #[test]
    fn test_empty_module() {
        let abi = MoveModuleABI {
            address: "0x1".to_string(),
            name: "empty".to_string(),
            exposed_functions: vec![],
            structs: vec![],
        };
        let generator = ModuleGenerator::new(&abi, GeneratorConfig::default());
        let code = generator.generate().unwrap();

        // Should still generate valid code
        assert!(code.contains("Generated Rust bindings"));
        assert!(code.contains("MODULE_ADDRESS"));
        assert!(code.contains("MODULE_NAME"));
    }
}