PrefGen: A Preference-Driven Methodology for Secure Yet Gas-Efficient Smart Contract Generation

Zhiyuan Peng, Xin Yin, Zijie Zhou, Chenhao Ying, Chao Ni, Yuan Luo

TL;DR

This paper introduces PrefGen, a novel framework that uses a preference-driven approach to generate smart contracts that are not only functionally correct but also gas-efficient and secure. By extending existing LLM training methods to incorporate quantifiable blockchain metrics, PrefGen significantly improves upon current models, producing production-ready smart contracts that address critical real-world deployment needs.

What Problem Does This Paper Address?

The deployment of smart contracts on blockchains like Ethereum is heavily reliant on two key factors: gas efficiency (which directly impacts transaction costs and user adoption) and security (to prevent catastrophic financial losses from exploits). While Large Language Models (LLMs) have shown promise in generating functionally correct code, they often struggle to balance these crucial aspects. Existing LLM training methods, like Supervised Fine-Tuning (SFT) and Direct Preference Optimization (DPO), tend to treat functional correctness, gas optimization, and security as separate objectives. This can lead to contracts that work but are prohibitively expensive to run or contain hidden vulnerabilities. The paper aims to bridge this gap by creating a holistic approach to smart contract generation that optimizes for all three dimensions simultaneously.

Methods

PrefGen employs a two-phase training pipeline:

  1. Supervised Fine-Tuning (SFT): This initial phase focuses on ensuring functional correctness. LLMs are trained on verified Solidity contracts with comprehensive test suites. The goal is to teach the model to generate code that passes all functional requirements.
  2. Direct Preference Optimization (DPO) with Multi-Objective Rewards: This is the core innovation. PrefGen extends standard DPO by incorporating quantifiable blockchain-specific metrics into the reward system. Instead of just learning from human preferences between two code snippets, PrefGen uses rewards that directly penalize high gas consumption and security vulnerabilities. This allows the model to learn to favor solutions that minimize these issues while maintaining correctness. The framework also uses a PageRank-inspired algorithm to rank and select high-quality training data, ensuring that the model learns from the most reliable examples.

Datasets

The paper constructs a new dataset for training and evaluation. This dataset is built through a three-stage process:

  1. Base Dataset Collection: Crawling 19 diverse, high-star GitHub projects and extracting function samples and their associated tests.
  2. Multi-Model Code Generation: Using 16 mainstream LLMs to generate a large number of candidate function samples.
  3. Preference-Based Sample Selection: Employing the PageRank algorithm to rank and select high-quality samples, resulting in preference pairs for correctness, security, and gas optimization.

The final dataset comprises 7,586 preference pairs. The evaluation is performed using the SolEval benchmark [2], which includes metrics for functional correctness (Pass@k), syntactic correctness (Compile@k), gas efficiency (Gas@k), and security (Secure@k).

Key Insights and Ideas

  • Holistic Optimization is Crucial: The paper strongly argues that treating functional correctness, gas efficiency, and security as independent objectives is insufficient for real-world smart contract deployment. A truly effective solution must balance all three.
  • Quantifiable Metrics Drive Better Performance: Simply relying on human preferences isn't enough. Incorporating concrete, quantifiable metrics like gas consumption and vulnerability status directly into the training reward signal is key to achieving superior results.
  • PageRank for Data Quality: Using a PageRank-inspired algorithm to rank and select training data is vital for ensuring that the LLM learns from the most relevant and high-quality examples, leading to better overall performance.
  • Trade-offs are Manageable: While there are inherent trade-offs between these objectives, PrefGen demonstrates that these can be effectively managed through its multi-objective optimization framework, leading to contracts that excel across the board.

Key Results

PrefGen significantly outperforms baseline approaches (pretrained models, SFT, DPO, and SFT+DPO) on the Qwen-7B model:

  • Overall Performance: Achieves 66.7% Pass@5, 58.9% Gas@5, and 62.5% Secure@5.
  • Gas Efficiency: Generates ERC-20 contracts with a 12% reduction in gas costs compared to baseline models.
  • Security: Completely eliminates critical security vulnerabilities like reentrancy attacks.
  • Training Efficiency: Despite requiring more memory, PrefGen dramatically reduces training time by approximately 90% compared to SFT+DPO.
  • Data Scaling: The model shows strong scaling properties, with 50% of the data achieving about 90% of the performance gain for Pass@5, offering flexibility for resource-constrained scenarios. However, gas efficiency and security continue to benefit significantly from larger datasets.

Limitations and Future Work

  • Limitations:
    • The evaluation focuses on Ethereum-compatible Solidity contracts, which might limit generalizability to other blockchain platforms.
  • The metrics used, while comprehensive, might not capture every nuance of developer preferences.
  • Future Work:
    • Migrating the multi-objective optimization approach to other programming languages like Python and Java.
  • Further exploring the balance between data quality and quantity for optimal training.

Summary updated

BibTeX
@inproceedings{peng2025prefgen,
  author = {Zhiyuan Peng and Xin Yin and Zijie Zhou and Chenhao Ying and Chao Ni and Yuan Luo},
  title = {{PrefGen: A Preference-Driven Methodology for Secure Yet Gas-Efficient Smart Contract Generation}},
  booktitle = {IEEE/ACM International Conference on Automated Software Engineering (ASE)},
  year = {2025},
  url = {https://openreview.net/forum?id=L3fItZfKhH},
}
Figure