Smart contract generation model based on code annotation and AST-LSTM tuning

Chen Yong, Hu Defeng, Chao Xu, Nannan Chen, Fanfan Shen, Jianbo LiuResearch Square 2024

Abstract

With the wide application of smart contracts in many fields, the number, types, and complexity of smart contracts are showing a rapidly increasing trend. However, the development of smart contracts has its own unique programming language and security requirements, which are difficult for conventional software personnel to adapt to quickly, and how to realize the efficient development of smart contracts according to the application requirements is an important issue that needs to be solved for its further development. To this end, this paper proposes a smart contract generation method based on AST-LSTM characterization and code annotation tuning large language model, which adopts AST-LSTM model combining Abstract Syntax Tree (AST) and Tree-LSTM to vectorize the code as well as Sentence-Bert to vectorize the annotations and carry out a weighted analysis, and constructs a smart contract clustering analysis model to achieve accurate clustering of functionally similar smart contracts. Then the AST-LSTM+Transformer model is used to detect defects in the clustered code and correlate the related annotation information to construct a diverse Prompt feature prompt statement dataset. Finally, the LLaMA2-7B model is used as the basis for demand-specific smart contract generation with the help of Lora and P-Tuning v2 fine-tuning techniques. In this paper, with the help of BLEU, an auxiliary tool for bilingual translation quality assessment, and Mythril, VaaS, and other code security detection tools, we conducted comparative experiments with existing methods. The results of the experiment show that the average value of BLEU of the code generated by this paper’s method is improved by about 25%, and the code security is improved by about 9%, which will greatly promote the rapid development and exploitation of smart contracts with high-security requirements.

Introduction

Smart contracts serve as the foundational logic for decentralized finance (DeFi) and blockchain ecosystems. Despite their importance, creating secure and efficient smart contracts remains a significant challenge. Developers often struggle with a 'separation of concerns': business logic experts may lack technical Solidity proficiency, while developers might overlook subtle security vulnerabilities or business nuances.

Traditional automated generation tools typically rely on Recurrent Neural Networks (RNNs) that treat code as a linear sequence of text. This approach is fundamentally limited as it ignores the nested, hierarchical nature of programming logic. Furthermore, while general-purpose Large Language Models (LLMs) like GPT-4 are powerful, they often lack the domain-specific 'vertical' knowledge required to produce secure, defect-free smart contracts consistently. This paper addresses these gaps by introducing a structurally aware generation pipeline.

Methodology

The proposed framework consists of a three-part pipeline designed to bridge the gap between structural code analysis and LLM fine-tuning.

Overview of the three-stage smart contract generation framework, including clustering, dataset building, and model tuning.

1. Structural and Semantic Clustering

To ensure the training data is organized by intent and structure, the authors developed a dual-vectorization approach to group functionally similar contracts.

Detailed view of the smart contract clustering process guided by AST-LSTM and code annotation.
  • AST-LSTM: Instead of processing code line-by-line, the model converts Solidity source code into Abstract Syntax Trees (ASTs). An N-ary Tree-LSTM then processes these trees, preserving parent-child dependencies and the syntactic 'shape' of the code.
  • Sentence-BERT: Simultaneously, code annotations (comments) are vectorized using Sentence-BERT. Annotations act as a bridge between human requirements and machine logic.
  • K-means Clustering: By combining these vectors, the system groups contracts into hierarchical layers: Function, Contract, Interface, and Library.

2. Defect Detection and Dataset Construction

To prevent the LLM from learning 'bad habits,' a specialized defect detection model was integrated. This model combines AST-LSTM with a Transformer architecture to identify incomplete logic or known vulnerabilities. By using security tools like Mythril and VaaS as ground truth, the authors purged the dataset of low-quality code. The remaining high-quality samples were used to build a 'Prompt instruction dataset' featuring 10 distinct templates.

3. LLM Fine-Tuning

The LLaMA2-7B model serves as the base generator. To adapt it to the specific requirements of smart contract generation, the authors applied two efficient fine-tuning techniques: LoRA (Low-Rank Adaptation) and P-Tuning v2. These methods allow for task-specific adaptation with minimal parameter updates, optimizing the model's understanding of the instruction dataset.

Datasets

The researchers curated a custom dataset by crawling the Ethereum platform (Etherscan):

  • Initial Collection: 10,437 smart contract programs.
  • Refined Dataset: 6,739 samples, filtered for size (50KB–80KB) and high annotation density (at least 300 lines of comments).
  • Categorization: Data was organized into four hierarchical layers to maintain structural integrity.

Key Results

The fine-tuned models demonstrated significant performance gains over baselines:

  • Generation Quality: The average BLEU score improved by approximately 25%, indicating that the generated code more closely matches human-written reference contracts.
  • Security: Testing against Mythril and VaaS revealed a 9% improvement in security pass rates, validating the effectiveness of the pre-emptive defect detection filter.
  • Benchmarking: In specific tasks, such as generating ERC20 contracts, the model outperformed Code Llama and remained competitive with larger models like ChatGPT 3.5.

Conclusion

This research demonstrates that treating code as a structural tree rather than a linear sentence significantly enhances the performance of generative models. By integrating structural defect detection and semantic annotation analysis, the proposed framework produces more secure and accurate smart contracts. Future work will explore scaling the framework to larger models (e.g., LLaMA 13B/70B) and expanding support to other blockchain languages such as Rust and Move.

Summary updated

BibTeX
@inproceedings{yong2024smart,
  author = {Chen Yong and Hu Defeng and Chao Xu and Nannan Chen and Fanfan Shen and Jianbo Liu},
  title = {{Smart contract generation model based on code annotation and AST-LSTM tuning}},
  booktitle = {Research Square},
  year = {2024},
  doi = {10.21203/rs.3.rs-3995739/v1},
}
Figure