Guiding LLM-based Smart Contract Generation with Finite State Machine

Hao Luo, Yuhao Lin, Xiao Yan, Xintong Hu, Yuxiang Wang, Qiming Zeng, Hao Wang, Jiawei Jiang

TL;DR

This paper introduces FSM-SCG, a novel framework that leverages Finite State Machines (FSMs) to guide Large Language Models (LLMs) in generating more effective and secure smart contracts. By translating user requirements into FSMs and then using these FSMs to direct LLM code generation, FSM-SCG significantly improves compilation success rates and reduces security vulnerabilities.

What Problem Does This Paper Address?

Developing smart contracts is a complex and high-stakes task. Traditionally, it requires manual coding and expert audits, creating a steep learning curve and inefficiency. While Large Language Models (LLMs) show promise for code generation, they struggle with the specific nuances of smart contracts, often producing code with low compilation success rates and introducing security vulnerabilities. This is partly due to the limited documentation and community support for smart contract programming languages and the general-purpose nature of many LLMs, which aren't inherently designed for blockchain security. The paper aims to bridge this gap by proposing a method to enhance LLM-based smart contract generation for improved effectiveness and security.

Methods

The core of FSM-SCG is its requirement-to-FSM-to-code (R2F2C) generation pattern. This approach breaks down the complex task into manageable steps:

  1. FSM Generation: User requirements are first translated into a Finite State Machine (FSM). The paper proposes an enhanced FSM called SmartFSM, which goes beyond the basic Mealy machine by explicitly representing contract structure, states, variables, functions, and events. This provides a more comprehensive intermediate representation.
  2. LLM Fine-tuning: To improve the LLM's ability to generate both FSMs and smart contracts, the authors construct a fine-tuning dataset and use a supervised full parameter fine-tuning (FPFT) method.
  3. Smart Contract Generation: The fine-tuned LLM uses the generated SmartFSM as a guide to produce the smart contract code.
  4. Feedback-driven Refinement: The generated code undergoes compilation checks (using py-solc-x) and security analysis (using Slither). Any identified compilation errors or security vulnerabilities are fed back to the LLM via specific prompts, guiding it to refine the code iteratively. This feedback loop is crucial for improving both effectiveness and security.

Datasets

The paper introduces a new open-source fine-tuning dataset for smart contract generation. This dataset was constructed by collecting smart contract source code from platforms like Etherscan and using GPT-4 to generate corresponding user requirements and FSMs. It comprises 30,000 items, each containing requirements (R), FSM (F), and code (C). From this, several sub-datasets are derived (R2F2C, R2F, F2C, C2F, R2C) to support various training tasks.

Key Insights and Ideas

  • FSMs as a Powerful Intermediate Representation: The paper strongly advocates for using FSMs, particularly their enhanced SmartFSM variant, as an intermediate step. This structured representation helps LLMs better understand and implement the logic and state transitions required for smart contracts, leading to more robust code.
  • The Importance of Feedback Loops: Iterative refinement through compilation and security checks is shown to be critical. Simply generating code once is insufficient; the feedback mechanism allows the LLM to learn from its mistakes and produce higher-quality, more secure outputs.
  • Fine-tuning is Essential for Domain Specialization: General-purpose LLMs struggle with the specific syntax and security considerations of smart contracts. Fine-tuning on a domain-specific dataset significantly boosts performance, enabling LLMs to grasp Solidity complexities and address security vulnerabilities more effectively.
  • SmartFSM's Superiority over Basic Mealy Machines: The proposed SmartFSM, with its explicit sections for basic information, states, variables, functions, and events, offers a richer representation than a standard Mealy machine, proving more effective for guiding smart contract generation.

Key Results

FSM-SCG demonstrates significant improvements across key metrics:

  • Compilation Success Rate (CPR): FSM-SCG achieved a CPR of 95.1% on the LlaMa3.1-8B model, which is 48% higher than the best baseline.
  • Vulnerability Risk Score (VRS): The average vulnerability risk score was reduced by approximately 68%.
  • Security Metrics: For open-source LLMs, FSM-SCG increased the percentage of smart contracts without security vulnerabilities (ZRCP) to 53.21% (Qwen2.5-7B) and 50.53% (LlaMa3.1-8B), while high-risk vulnerabilities (HRCP) decreased to 5.15% and 6.62%, respectively.

Limitations and Future Work

  • Limitations: The paper acknowledges that while FSM-SCG significantly improves performance, it still relies on LLMs, which can sometimes produce errors or overlook subtle security issues. The effectiveness of the fine-tuning process is also dependent on the quality and coverage of the training dataset.
  • Future Work: The authors suggest further research into more sophisticated feedback mechanisms and exploring other intermediate representations. They also highlight the need for continued development of LLMs specifically tailored for the blockchain domain to further enhance the effectiveness and security of generated smart contracts.

Summary updated

Abstract

Smart contract is a kind of self-executing code based on blockchain technology with a wide range of application scenarios, but the traditional generation method relies on manual coding and expert auditing, which has a high threshold and low efficiency. Although Large Language Models (LLMs) show great potential in programming tasks, they still face challenges in smart contract generation w.r.t. effectiveness and security. To solve these problems, we propose FSM-SCG, a smart contract generation framework based on finite state machine (FSM) and LLMs, which significantly improves the quality of the generated code by abstracting user requirements to generate FSM, guiding LLMs to generate smart contracts, and iteratively optimizing the code with the feedback of compilation and security checks. The experimental results show that FSM-SCG significantly improves the quality of smart contract generation. Compared to the best baseline, FSM-SCG improves the compilation success rate of generated smart contract code by at most 48%, and reduces the average vulnerability risk score by approximately 68%.
BibTeX
@inproceedings{luo2025guiding,
  author = {Hao Luo and Yuhao Lin and Xiao Yan and Xintong Hu and Yuxiang Wang and Qiming Zeng and Hao Wang and Jiawei Jiang},
  title = {{Guiding LLM-based Smart Contract Generation with Finite State Machine}},
  booktitle = {International Joint Conference on Artificial Intelligence},
  year = {2025},
  url = {https://openreview.net/forum?id=vnZFF5WOSt},
}
Figure