Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Master smart contract security best practices to prevent common vulnerabilities and implement secure Solidity patterns. Use when writing smart contracts, auditing existing contracts, or implementing security measures for blockchain applications.
.claude/skills/dicklesworthstone-solidity-security/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 259% | 0% |
| case-20 | ✗→✓ | ▲ Improved | 403% | 0% |
| case-21 | ✗→✓ | ▲ Improved | 204% | 0% |
| case-02 | ✓→✓ | = Same ✓ | 193% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 92% | 0% |
Master smart contract security best practices, vulnerability prevention, and secure Solidity development patterns.
Attacker calls back into your contract before state is updated.
Vulnerable Code:
solidity// VULNERABLE TO REENTRANCY contract VulnerableBank { mapping(address => uint256) public balances; function withdraw() public { uint256 amount = balances[msg.sender]; // DANGER: External call before state update (bool success, ) = msg.sender.call{value: amount}(""); require(success); balances[msg.sender] = 0; // Too late! } }
Secure Pattern (Checks-Effects-Interactions):
soliditycontract SecureBank { mapping(address => uint256) public balances; function withdraw() public { uint256 amount = balances[msg.sender]; require(amount > 0, "Insufficient balance"); // EFFECTS: Update state BEFORE external call balances[msg.sender] = 0; // INTERACTIONS: External call last (bool success, ) = msg.sender.call{value: amount}(""); require(success, "Transfer failed"); } }
Alternative: ReentrancyGuard
solidityimport "@openzeppelin/contracts/security/ReentrancyGuard.sol"; contract SecureBank is ReentrancyGuard { mapping(address => uint256) public balances; function withdraw() public nonReentrant { uint256 amount = balances[msg.sender]; require(amount > 0, "Insufficient balance"); balances[msg.sender] = 0; (bool success, ) = msg.sender.call{value: amount}(""); require(success, "Transfer failed"); } }
Vulnerable Code (Solidity < 0.8.0):
solidity// VULNERABLE contract VulnerableToken { mapping(address => uint256) public balances; function transfer(address to, uint256 amount) public { // No overflow check - can wrap around balances[msg.sender] -= amount; // Can underflow! balances[to] += amount; // Can overflow! } }
Secure Pattern (Solidity >= 0.8.0):
solidity// Solidity 0.8+ has built-in overflow/underflow checks contract SecureToken { mapping(address => uint256) public balances; function transfer(address to, uint256 amount) public { // Automatically reverts on overflow/underflow balances[msg.sender] -= amount; balances[to] += amount; } }
For Solidity < 0.8.0, use SafeMath:
solidityimport "@openzeppelin/contracts/utils/math/SafeMath.sol"; contract SecureToken { using SafeMath for uint256; mapping(address => uint256) public balances; function transfer(address to, uint256 amount) public { balances[msg.sender] = balances[msg.sender].sub(amount); balances[to] = balances[to].add(amount); } }
Vulnerable Code:
solidity// VULNERABLE: Anyone can call critical functions contract VulnerableContract { address public owner; function withdraw(uint256 amount) public { // No access control! payable(msg.sender).transfer(amount); } }
Secure Pattern:
solidityimport "@openzeppelin/contracts/access/Ownable.sol"; contract SecureContract is Ownable { function withdraw(uint256 amount) public onlyOwner { payable(owner()).transfer(amount); } } // Or implement custom role-based access contract RoleBasedContract { mapping(address => bool) public admins; modifier onlyAdmin() { require(admins[msg.sender], "Not an admin"); _; } function criticalFunction() public onlyAdmin { // Protected function } }
Vulnerable:
solidity// VULNERABLE TO FRONT-RUNNING contract VulnerableDEX { function swap(uint256 amount, uint256 minOutput) public { // Attacker sees this in mempool and front-runs uint256 output = calculateOutput(amount); require(output >= minOutput, "Slippage too high"); // Perform swap } }
Mitigation:
soliditycontract SecureDEX { mapping(bytes32 => bool) public usedCommitments; // Step 1: Commit to trade function commitTrade(bytes32 commitment) public { usedCommitments[commitment] = true; } // Step 2: Reveal trade (next block) function revealTrade( uint256 amount, uint256 minOutput, bytes32 secret ) public { bytes32 commitment = keccak256(abi.encodePacked( msg.sender, amount, minOutput, secret )); require(usedCommitments[commitment], "Invalid commitment"); // Perform swap } }
soliditycontract SecurePattern { mapping(address => uint256) public balances; function withdraw(uint256 amount) public { // 1. CHECKS: Validate conditions require(amount <= balances[msg.sender], "Insufficient balance"); require(amount > 0, "Amount must be positive"); // 2. EFFECTS: Update state balances[msg.sender] -= amount; // 3. INTERACTIONS: External calls last (bool success, ) = msg.sender.call{value: amount}(""); require(success, "Transfer failed"); } }
solidity// Prefer this (pull) contract SecurePayment { mapping(address => uint256) public pendingWithdrawals; function recordPayment(address recipient, uint256 amount) internal { pendingWithdrawals[recipient] += amount; } function withdraw() public { uint256 amount = pendingWithdrawals[msg.sender]; require(amount > 0, "Nothing to withdraw"); pendingWithdrawals[msg.sender] = 0; payable(msg.sender).transfer(amount); } } // Over this (push) contract RiskyPayment { function distributePayments(address[] memory recipients, uint256[] memory amounts) public { for (uint i = 0; i < recipients.length; i++) { // If any transfer fails, entire batch fails payable(recipients[i]).transfer(amounts[i]); } } }
soliditycontract SecureContract { function transfer(address to, uint256 amount) public { // Validate inputs require(to != address(0), "Invalid recipient"); require(to != address(this), "Cannot send to contract"); require(amount > 0, "Amount must be positive"); require(amount <= balances[msg.sender], "Insufficient balance"); // Proceed with transfer balances[msg.sender] -= amount; balances[to] += amount; } }
solidityimport "@openzeppelin/contracts/security/Pausable.sol"; contract EmergencyStop is Pausable, Ownable { function criticalFunction() public whenNotPaused { // Function logic } function emergencyStop() public onlyOwner { _pause(); } function resume() public onlyOwner { _unpause(); } }
uint256 Instead of Smaller Typessolidity// More gas efficient contract GasEfficient { uint256 public value; // Optimal function set(uint256 _value) public { value = _value; } } // Less efficient contract GasInefficient { uint8 public value; // Still uses 256-bit slot function set(uint8 _value) public { value = _value; // Extra gas for type conversion } }
solidity// Gas efficient (3 variables in 1 slot) contract PackedStorage { uint128 public a; // Slot 0 uint64 public b; // Slot 0 uint64 public c; // Slot 0 uint256 public d; // Slot 1 } // Gas inefficient (each variable in separate slot) contract UnpackedStorage { uint256 public a; // Slot 0 uint256 public b; // Slot 1 uint256 public c; // Slot 2 uint256 public d; // Slot 3 }
calldata Instead of memory for Function Argumentssoliditycontract GasOptimized { // More gas efficient function processData(uint256[] calldata data) public pure returns (uint256) { return data[0]; } // Less efficient function processDataMemory(uint256[] memory data) public pure returns (uint256) { return data[0]; } }
soliditycontract EventStorage { // Emitting events is cheaper than storage event DataStored(address indexed user, uint256 indexed id, bytes data); function storeData(uint256 id, bytes calldata data) public { emit DataStored(msg.sender, id, data); // Don't store in contract storage unless needed } }
solidity// Security Checklist Contract contract SecurityChecklist { /** * [ ] Reentrancy protection (ReentrancyGuard or CEI pattern) * [ ] Integer overflow/underflow (Solidity 0.8+ or SafeMath) * [ ] Access control (Ownable, roles, modifiers) * [ ] Input validation (require statements) * [ ] Front-running mitigation (commit-reveal if applicable) * [ ] Gas optimization (packed storage, calldata) * [ ] Emergency stop mechanism (Pausable) * [ ] Pull over push pattern for payments * [ ] No delegatecall to untrusted contracts * [ ] No tx.origin for authentication (use msg.sender) * [ ] Proper event emission * [ ] External calls at end of function * [ ] Check return values of external calls * [ ] No hardcoded addresses * [ ] Upgrade mechanism (if proxy pattern) */ }
javascript// Hardhat test example const { expect } = require("chai"); const { ethers } = require("hardhat"); describe("Security Tests", function () { it("Should prevent reentrancy attack", async function () { const [attacker] = await ethers.getSigners(); const VictimBank = await ethers.getContractFactory("SecureBank"); const bank = await VictimBank.deploy(); const Attacker = await ethers.getContractFactory("ReentrancyAttacker"); const attackerContract = await Attacker.deploy(bank.address); // Deposit funds await bank.deposit({ value: ethers.utils.parseEther("10") }); // Attempt reentrancy attack await expect( attackerContract.attack({ value: ethers.utils.parseEther("1") }), ).to.be.revertedWith("ReentrancyGuard: reentrant call"); }); it("Should prevent integer overflow", async function () { const Token = await ethers.getContractFactory("SecureToken"); const token = await Token.deploy(); // Attempt overflow await expect(token.transfer(attacker.address, ethers.constants.MaxUint256)) .to.be.reverted; }); it("Should enforce access control", async function () { const [owner, attacker] = await ethers.getSigners(); const Contract = await ethers.getContractFactory("SecureContract"); const contract = await Contract.deploy(); // Attempt unauthorized withdrawal await expect(contract.connect(attacker).withdraw(100)).to.be.revertedWith( "Ownable: caller is not the owner", ); }); });
soliditycontract WellDocumentedContract { /** * @title Well Documented Contract * @dev Example of proper documentation for audits * @notice This contract handles user deposits and withdrawals */ /// @notice Mapping of user balances mapping(address => uint256) public balances; /** * @dev Deposits ETH into the contract * @notice Anyone can deposit funds */ function deposit() public payable { require(msg.value > 0, "Must send ETH"); balances[msg.sender] += msg.value; } /** * @dev Withdraws user's balance * @notice Follows CEI pattern to prevent reentrancy * @param amount Amount to withdraw in wei */ function withdraw(uint256 amount) public { // CHECKS require(amount <= balances[msg.sender], "Insufficient balance"); // EFFECTS balances[msg.sender] -= amount; // INTERACTIONS (bool success, ) = msg.sender.call{value: amount}(""); require(success, "Transfer failed"); } }
tx.origin for Authentication: Use msg.sender instead| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 15,261 | 36,998 | +142% | 1 | 1 | 0% | 2,155 | 7,740 | +259% | 0 | 0 | — |
case-02 | pass→pass | 13,855 | 10,801 | -22% | 1 | 1 | 0% | 1,993 | 5,840 | +193% | 0 | 0 | — |
case-03 | pass→pass | 31,482 | 36,512 | +16% | 1 | 1 | 0% | 5,285 | 10,164 | +92% | 0 | 0 | — |
case-04 | pass→pass | 13,239 | 15,786 | +19% | 1 | 1 | 0% | 2,330 | 6,458 | +177% | 0 | 0 | — |
case-05 | pass→pass | 12,062 | 12,392 | +3% | 1 | 1 | 0% | 1,970 | 5,696 | +189% | 0 | 0 | — |
case-10 | pass→pass | 6,185 | 5,816 | -6% | 1 | 1 | 0% | 1,049 | 4,668 | +345% | 0 | 0 | — |
case-06 | pass→pass | 10,869 | 9,821 | -10% | 1 | 1 | 0% | 2,191 | 5,464 | +149% | 0 | 0 | — |
case-07 | pass→pass | 3,571 | 5,058 | +42% | 1 | 1 | 0% | 564 | 4,616 | +718% | 0 | 0 | — |
case-08 | pass→pass | 19,866 | 21,910 | +10% | 1 | 1 | 0% | 3,672 | 7,625 | +108% | 0 | 0 | — |
case-09 | pass→pass | 12,810 | 12,872 | +0% | 1 | 1 | 0% | 2,346 | 5,937 | +153% | 0 | 0 | — |
case-15 | pass→pass | 11,056 | 11,639 | +5% | 1 | 1 | 0% | 1,856 | 5,456 | +194% | 0 | 0 | — |
case-11 | pass→pass | 9,919 | 7,147 | -28% | 1 | 1 | 0% | 1,608 | 4,813 | +199% | 0 | 0 | — |
case-12 | pass→pass | 12,269 | 13,189 | +7% | 1 | 1 | 0% | 2,023 | 5,786 | +186% | 0 | 0 | — |
case-13 | pass→pass | 12,500 | 12,012 | -4% | 1 | 1 | 0% | 1,935 | 5,515 | +185% | 0 | 0 | — |
case-14 | pass→pass | 7,444 | 5,526 | -26% | 1 | 1 | 0% | 1,253 | 4,650 | +271% | 0 | 0 | — |
case-16 | pass→pass | 7,638 | 7,012 | -8% | 1 | 1 | 0% | 1,432 | 4,984 | +248% | 0 | 0 | — |
case-17 | pass→pass | 16,630 | 19,267 | +16% | 1 | 1 | 0% | 1,517 | 5,392 | +255% | 0 | 0 | — |
case-18 | pass→pass | 8,691 | 12,754 | +47% | 1 | 1 | 0% | 1,382 | 5,949 | +330% | 0 | 0 | — |
case-19 | pass→pass | 3,595 | 3,751 | +4% | 1 | 1 | 0% | 578 | 4,273 | +639% | 0 | 0 | — |
case-20 | fail→pass | 10,130 | 15,888 | +57% | 1 | 1 | 0% | 1,253 | 6,297 | +403% | 0 | 0 | — |
case-21 | fail→pass | 19,756 | 24,709 | +25% | 1 | 1 | 0% | 2,847 | 8,656 | +204% | 0 | 0 | — |
case-22 | pass→pass | 18,693 | 22,174 | +19% | 1 | 1 | 0% | 3,649 | 8,027 | +120% | 0 | 0 | — |
case-23 | pass→pass | 12,069 | 14,766 | +22% | 1 | 1 | 0% | 2,598 | 6,614 | +155% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 23 cases were attempted. The headline lift of +13 percentage points is the difference between those two pass rates over the 23 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.