A Preference-Driven Methodology for High-Quality Solidity Code Generation
Zhiyuan Peng, Xin Yin, Chenhao Ying, Chao Ni, Yuan Luo · 2025
Read A Preference-Driven Methodology for High-Quality Solidity Code Generation by Zhiyuan Peng, Xin Yin, Chenhao Ying, Chao Ni, Yuan Luo (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2506-03006.txt.
Introduction
Zhiyuan Peng and co-authors present, in a 2025 paper, a training method that teaches language models to write better smart contracts. Models already produce Solidity code that runs. Real contracts, though, must also be cheap to execute and safe from attack, two demands in tension.
Their framework extends preference training beyond human taste. Scores for gas cost and security join correctness as training signals. Earlier methods chased each goal separately, yielding contracts cheap but risky, or safe but costly. Optimizing all three together is the paper's claim. Smart contracts hold real money, so code that is merely correct is not enough; the work targets deployment-grade quality.
Catalog record
| CREATOR | Zhiyuan Peng, Xin Yin, Chenhao Ying, Chao Ni, Yuan Luo |
|---|---|
| YEAR | 2025 |
| COLLECTION | PAPERS |
| FORMAT | TXT |
| FILE SIZE | 52 KB |
| VERSION | 1.0 |
| LICENSE | http://creativecommons.org/licenses/by/4.0/ |
| RIGHTS | CC BY |
| SOURCE | arXiv |
Source: https://arxiv.org/abs/2506-03006
Browse the full library · About the Tower of Babel
Full text
The opening of the work, from the library's hosted text:
A Preference-Driven Methodology for High-Quality Solidity Code Generation Zhiyuan Peng, Xin Yin, Chenhao Ying, Chao Ni, Yuan Luo Abstract While Large Language Models (LLMs) have demonstrated remarkable progress in generating functionally correct Solidity code, they continue to face critical challenges in producing gas-efficient and secure code, which are critical requirements for real-world smart contract deployment. Although recent advances leverage Supervised Fine-Tuning (SFT) and Direct Preference Optimization (DPO) for code preference alignment, existing approaches treat functional correctness, gas optimization, and security as independent objectives, resulting in contracts that may achieve operational soundness but suffer from prohibitive execution costs or dangerous vulnerabilities. To address these limitations, we propose PrefGen , a novel framework that extends standard DPO beyond human preferences to incorporate quantifiable blockchain-specific metrics, enabling holistic multi-objective optimization specifically tailored for smart contract generation. Our framework introduces a comprehensive evaluation methodology with four complementary metrics: Pass@k (functional correctness), Compile@k (syntactic correctness), Gas@k (gas efficiency), and Secure@k (security assessment), providing rigorous multi-dimensional contract evaluation. Through extensive experimentation, we demonstrate that PrefGen significantly outperforms existing approaches across all critical dimensions, achieving 66.7% Pass@5, 58.9% Gas@5, and 62.5% Secure@5, while generating production-ready smart contracts that are functionally correct, cost-efficient, and secure. \textsuperscript{1} \textsuperscript{1} footnotetext: Equal contribution. \textsuperscript{2} \textsuperscript{2} footnotetext: Corresponding authors. I Introduction Large Language Models (LLMs) have demonstrated remarkable capabilities in generating functionally correct Solidity code, as evidenced by recent benchmarks [ 1 , 2 ] . However, while functional correctness represents a fundamental requirement, human preferences for smart contracts extend far beyond mere operational soundness. In practice, developers and users demand contracts that simultaneously achieve security-critical and gas efficiency attributes that directly impact deployment costs and vulnerability exposure in real-world blockchain environments. This multi-dimensional preference presents a significant challenge for current LLM-based smart contract generation approaches. Security vulnerabilities in LLM-generated smart contracts pose substantial risks for blockchain deployment, as Solidity’s contract-oriented nature makes it susceptible to various exploits, including reentrancy attacks [ 3 ] , integer overflows [ 4 ] , and access control vulnerabilities [ 5 ] . The severity of these risks was starkly demonstrated in May 2025 when Cetus Protocol, the largest decentralized exchange on the Sui blockchain, suffered a devastating exploit that drained over $260 million through smart contract vulnerabilities involving manipulated price curves and flawed reserve calculations 1 1 1 https://www.theblock.co/post/355906/cetus-poses-community-vote-to-possibly-return-100-of-funds-to-users-affected-by-223-million-exploit . Previous work [ 2 ] also reveals that LLMs performing well on Pass@k may produce code with elevated vulnerability rates, indicating a fundamental misalignment between model optimization objectives and human security preferences. Equally critical is the gap in gas efficiency optimization, which represents another dimension of human preference that current LLMs largely overlook. Gas efficiency is a paramount concern for Ethereum deployment, where execution costs directly impact user adoption and economic viability [ 5 ] . While models like GPT-4 [ 6 ] and DeepSeek-R1 [ 7 ] excel at producing functionally correct contracts, they typically neglect gas consumption optimization, resulting in contracts that meet functional requirements but violate user preferences for cost-effectiveness [ 8 , 2 ] . The fundamental issue lies in the misalignment between current optimization objectives and genuine human preferences for smart contracts. Optimizing for one dimension often sacrifices others [ 8 , 9 ] , creating a critical trade-off that is particularly problematic in decentralized finance (DeFi) applications, where high gas costs can undermine economic feasibility [ 10 , 11 ] . Although recent advances leverage Supervised Fine-Tuning (SFT) and Direct Preference Optimization (DPO) for code preference alignment, existing approaches address correctness, efficiency, and security in isolation [ 2 , 9 ] , failing to capture the holistic preference optimization required for production-ready smart contracts that satisfy real-world user requirements [ 8 ] . To address these limitations, we introduce PrefGen , a novel framework that extends standard DPO beyond subjective preference pairs to incorporate quantifiable metrics that reflect genuine user priorities (i.e., gas-efficiency and security), enabling comprehensive preference-driven optimization tailored for smart contract development. Unlike existing methods that treat these objectives independently, PrefGen learns to favor solutions that minimize gas consumption while maintaining correctness and avoiding vulnerabilities through a custom DPO implementation that incorporates domain-specific reward signals. Specifically, gas efficiency rewards encourage lower consumption and security rewards penalize vulnerabilities, enabling simultaneous multi-objective optimization that aligns with real-world deployment preferences. PrefGen employs a systematic approach to construct preference datasets by generating multiple candidate implementations for each functional requirement and evaluating them across three critical dimensions that capture real-world deployment requirements: functional correctness (i.e., Pass@k), gas efficiency (i.e., Gas@k), and security (i.e., Secure@k). This multi-dimensional evaluation framework enables the creation of preference pairs that guide the model toward generating production-ready smart contracts that simultaneously satisfy functional, gas-efficiency, and security constraints, addressing the comprehensive spectrum of developer and user preferences in blockchain environments. Our comprehensive evaluation demonstrates that PrefGen significantly outperforms existing approaches across all three critical dimensions, bridging the gap between academic benchmarks and production deployment requirements. Compared to the pretrained Qwen-7B baseline (16.7% Pass@5, 0.0% Gas@5, 11.8% Secure@5), PrefGen achieves substantial improvements: 66.7% Pass@5, 58.9% Gas@5, and 62.5% Secure@5. Beyond these metrics, practical deployment scenarios demonstrate real-world impact: PrefGen generates ERC-20 contracts with 12% reduced gas costs compared to baseline models while completely eliminating critical security vulnerabilities such as reentrancy attacks, exemplifying the framework’s ability to deliver contracts that meet the stringent requirements of production blockchain environments. In summary, our contributions are: • Comprehensive Multi-Dimensional Evaluation: We establish a systematic evaluation framework with four complementary metrics to provide a rigorous multi-dimensional assessment of generated smart contracts. Our extensive evaluation on smart contracts across 16 SOTA LLMs reveals that even top-performing models struggle with gas efficiency and security, demonstrating substantial room for improvement. • Novel Preference-Driven Framework: We introduce PrefGen, a novel framework that extends DPO beyond subjective preference pairs to incorporate quantifiable blockchain-specific metrics (e.g., gas-efficiency and security). • Production-Ready Smart Contract Generation: PrefGen achieves breakthrough performance improvements: 66.7% Pass@5, 58.9% Gas@5, and 62.5% Secure@5 on Qwen-7B, with practical deployment benefits including 12% reduced gas costs in ERC-20 contracts and complete elimination of critical security vulnerabilities such as reentrancy attacks. Fig. 1: Overview of the PrefGen framework v.s SFT + DPO. II Background and Motivation II-A Background II-A 1 Security Vulnerabilities in Smart Contracts Smart contracts operate in an immutable and trustless environment where security vulnerabilities can lead to catastrophic financial losses. The critical nature of smart contract security was demonstrated in May 2025 when Cetus Protocol suffered a devastating exploit that drained over $260 million through manipulated price curves and flawed reserve calculations [ 12 ] . This incident exemplifies how implementation flaws can cascade into systemic failures, where billions of dollars in assets remain at risk. The immutable nature of blockchain deployments distinguishes smart contract security from traditional software development. Unlike conventional applications, smart contracts cannot be modified once deployed [ 13 , 14 ] , making prevention the only viable defense strategy. This constraint has resulted in cumulative losses exceeding billions of dollars [ 15 , 16 , 17 ] , highlighting the paramount importance of generating inherently secure code. Common vulnerability patterns include reentrancy attacks that exploit external calls to recursively drain funds [ 3 ] , integer overflow/underflow vulnerabilities from unchecked arithmetic operations [ 4