Abstract
Decentralized applications on blockchain platforms are realized through smart contracts. However, participants who lack programming knowledge often have difficulties reading the smart contract source codes, which leads to potential security risks and barriers to participation. Our objective is to translate the smart contract source codes into natural language descriptions to help people better understand, operate, and learn smart contracts. This paper proposes an automated translation tool for Solidity smart contracts, termed SolcTrans, based on an abstract syntax tree and formal grammar. We have investigated 3,000 smart contracts and determined the part of speeches of corresponding blockchain terms. Among them, we further filtered out contract snippets without detailed comments and left 811 snippets to evaluate the translation quality of SolcTrans. Experimental results show that even with a small corpus, SolcTrans can achieve similar performance to the state-of-the-art code comments generation models for other programming languages. In addition, SolcTrans has consistent performance when dealing with code snippets with different lengths and gas consumption. SolcTrans can correctly interpret Solidity codes and automatically convert them into comprehensible English text.
Introduction
As the Decentralized Finance (DeFi) ecosystem expands, millions of users are interacting with smart contracts. However, a significant technical barrier remains: most users cannot read Solidity, the primary language of the Ethereum blockchain. This lack of transparency creates security risks, as users may unknowingly interact with malicious code that differs from a project's marketing claims.
While automated code summarization exists for languages like Java and Python, these solutions typically rely on deep learning to generate high-level summaries. For smart contracts, users require low-level, line-by-line logical breakdowns to verify financial transactions. Furthermore, the scarcity of large-scale parallel corpora (code paired with human descriptions) in Solidity makes training traditional neural networks difficult. This paper introduces SolcTrans, a tool designed to bridge this gap by translating Solidity source code into readable English comments using a deterministic, rule-based approach.
Methodology
SolcTrans bypasses the need for massive training datasets by utilizing a pipeline based on Abstract Syntax Trees (AST) and Context-Free Grammar (CFG). The process is divided into three primary stages:
- AST Extraction and Preprocessing: The tool leverages the official Solidity compiler to generate an AST in JSON format. Redundant nodes, such as compiler versioning, are removed to isolate the core contract logic.
- AST Traversal and Lexicalization: The system performs a depth-first traversal of the AST. Standard programming constructs (e.g., loops, declarations) are mapped to English phrases. Solidity-specific global variables are handled via custom templates; for instance,
msg.senderis lexicalized as "user" andblock.timestampas "current timestamp of the block." - Surface Realization: Disjointed phrases are tagged with Parts of Speech (POS) using an expanded Penn Treebank Tagset tailored for blockchain terminology. Finally, a Probabilistic Context-Free Grammar (PCFG) aggregates these elements into grammatically coherent sentences.
Experimental Evaluation
To evaluate SolcTrans, a new dataset was constructed by crawling 3,000 open-source smart contracts from the top 100 Ethereum Dapps via Etherscan. After filtering for duplicates and ensuring the presence of detailed human comments for ground-truth comparison, a final evaluation set of 811 code-description pairs was established.
As shown in the distribution analysis in the figure above, the dataset covers a wide range of code lengths and computational complexities (measured in gas costs), ensuring a robust test of the tool's versatility across different contract types.
Results and Discussion
Experimental results indicate that SolcTrans provides accurate and efficient translations:
- Quantitative Performance: The tool achieved a BLEU-1 score of 0.3472 and a ROUGE-L score of 0.3784. While higher-order BLEU scores were lower, this primarily reflects differences in sentence structure between the tool and human references rather than a lack of semantic accuracy.
- Efficiency: SolcTrans is highly performant, capable of processing contracts up to 3,200 lines in less than 250 milliseconds.
- Complexity Invariance: A key finding is that translation quality remains stable regardless of a function's gas cost. This suggests that the AST-based approach is equally effective for simple transfers and complex nested logic.
- Qualitative Feedback: A user study involving 20 volunteers (split between programmers and non-programmers) confirmed that the generated comments significantly improved code comprehension for non-technical users.
Conclusion
SolcTrans demonstrates that rule-based translation pipelines are a highly effective alternative to deep learning in low-resource domains like Solidity. By providing deterministic, low-level explanations of smart contract logic, the tool enhances transparency and security for blockchain participants. Future work will focus on expanding the CFG to cover more complex syntax, such as exception handling, and further growing the Solidity corpus to support future machine learning research.
Summary updated