Blog
Artificial Intelligence16 min read
Fine-Tuning a Small LLM to Generate Solidity Tests
B
BADJO Dibéa Koffi
Published on May 11, 2026
The Testing Gap
Smart contracts handle real money. Yet most projects have inadequate test coverage — not because developers are lazy, but because writing comprehensive fuzz tests is hard.
The Approach
I fine-tuned Mistral 7B on 1,847 Solidity contracts paired with their Foundry test suites.
Building the Dataset
Scraped from OpenZeppelin, Uniswap V3, Aave V3, Compound V2, and 1,200+ audited contracts from Code4rena.
Filtered aggressively:
- Only contracts with > 80% coverage
- Only test files with fuzz tests
- Only passing suites
Fine-Tuning with LoRA
from peft import LoraConfig, get_peft_model
lora_config = LoraConfig(
r=16,
lora_alpha=32,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
lora_dropout=0.05,
task_type="CAUSAL_LM"
)
model = get_peft_model(model, lora_config)
# Trainable params: 13.6M (0.19% of 7.24B)Training: 4 hours on a single A100. Cost: ~$16.
Results
| Metric | Base Mistral | Fine-tuned |
|---|---|---|
| Compilable tests | 34% | 89% |
| Tests that pass | 12% | 71% |
| Edge cases found | 2% | 38% |
| Fuzz tests generated | 0% | 64% |
Example Output
function testFuzz_TransferExceedsBalance(uint256 amount) public {
vm.assume(amount > 0 && amount <= type(uint128).max);
token.mint(alice, amount - 1);
vm.prank(alice);
vm.expectRevert("ERC20: transfer amount exceeds balance");
token.transfer(bob, amount);
}Real, useful tests — not boilerplate.
What's Next
Building a VS Code extension that suggests tests as you write Solidity — running locally via Ollama, no API costs.
fine-tuningsolidityfoundryllm
Comments
Stay Updated
Get my latest articles delivered straight to your inbox.