Title: Automated Smart Contract Code Generation Based on Graph RAG and LLM
Primary area: applications to computer vision, audio, language, and other modalities
Keywords: Smart Contract;Graph RAG;Code Generation;Prompt Engineering

Abstract: Smart contract code generation is pivotal for improving development efficiency and mitigating vulnerabilities. Although prior studies have leveraged large language models (LLMs) for this task, their quality still lags behind fine-tuned models such as CodeT5+ and CodeBERT. Existing attempts that combine LLMs with data-flow analysis often fail to adequately capture the hierarchical and control-flow structures of code, resulting in incomplete logic and degraded security. To address these limitations, we present GraphRAG-SCG, a retrieval-augmented generation framework that integrates graph representations with LLMs. GraphRAG-SCG constructs a dual-layer “semantic-control” graph index, dynamically injecting function-call graphs (FCGs), data-dependency graphs (DDGs), and business-constraint facts into an enriched prompt. Through lightweight graph traversal and embedding-based retrieval, the most semantically relevant subgraphs are identified and explicitly presented to the LLM, ensuring both structural consistency and contextual dependency during generation. Extensive experiments on a dataset of 40,000 real-world smart-contract requirement–code pairs demonstrate that GraphRAG-SCG significantly outperforms state-of-the-art baselines, achieving improvements of 13.1%, 5.8%, and 2.4% in RAGAS-Code, CodeBERTScore, and CodeBLEU, respectively, thus offering a new SOTA solution for automated smart-contract development.