SmartBERTv2 β Solidity vulnerability detection (single-label, with Safe)
Function-level classifier for Solidity smart contracts, fine-tuned from web3se/SmartBERT-v2. It assigns each function exactly one of five classes. It is the model used by the Solscan Audit proof-of-concept extension for Visual Studio Code.
| Index | Class |
|---|---|
| 0 | Re-entrancy |
| 1 | Timestamp-Dependency |
| 2 | Unhandled-Exceptions |
| 3 | tx.origin |
| 4 | Safe (no vulnerability) |
Files
best_model.onnx: encoder and classification head. Inputsinput_idsandattention_mask(int64, shape[batch, seq]); outputlogits([batch, 5]). Apply softmax and take the most probable class.tokenizer/: the SmartBERTv2 tokenizer (tokenizer.json,tokenizer_config.json).
Input preparation
The model expects the same text it saw during training:
- Normalize the function: remove comments, format it with js-beautify (indent 4) and drop empty lines and trailing spaces.
- Replace every newline and tab with a space.
- Tokenize with
truncation=True, max_length=510(including<s>and</s>).
Architecture and training
- Head:
[CLS]βLinear(768, 100)β GELU β Dropout βLinear(100, 5); cross-entropy loss. - Data: Solidity functions from SmartBugs Wild and Slither Audited Smart Contracts, labeled with Slither, with a Safe class for functions without findings (dataset v5.0, single-label).
- Validation: stratified 10-fold cross-validation grouped by contract, with the hyperparameters selected by Bayesian optimization. This checkpoint is the fold with the lowest validation loss (fold 8, epoch 2).
Results of this checkpoint
| Set | F1 macro | F1 macro (vulnerabilities) | Accuracy |
|---|---|---|---|
| Validation (fold 8) | 0.961 | 0.963 | 0.963 |
| SolidiFI + Safe (5,650 functions) | 0.919 | 0.958 | 0.938 |
Limitations
- Labels come from Slither and inherit its errors.
- Each function is classified on its own: vulnerabilities that span several functions or contracts are not detected.
- Functions longer than 510 tokens are truncated.
- Only the four vulnerability classes above are covered.