Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Audit Solidity smart contracts for reentrancy, integer overflow, access control, and oracle manipulation.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: solidity-security3description: 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.4---56# Solidity Security78Master smart contract security best practices, vulnerability prevention, and secure Solidity development patterns.910## When to Use This Skill1112- Writing secure smart contracts13- Auditing existing contracts for vulnerabilities14- Implementing secure DeFi protocols15- Preventing reentrancy, overflow, and access control issues16- Optimizing gas usage while maintaining security17- Preparing contracts for professional audits18- Understanding common attack vectors1920## Detailed patterns and worked examples2122Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.2324## Testing for Security2526```javascript27// Hardhat test example28const { expect } = require("chai");29const { ethers } = require("hardhat");3031describe("Security Tests", function () {32it("Should prevent reentrancy attack", async function () {33const [attacker] = await ethers.getSigners();3435const VictimBank = await ethers.getContractFactory("SecureBank");36const bank = await VictimBank.deploy();3738const Attacker = await ethers.getContractFactory("ReentrancyAttacker");39const attackerContract = await Attacker.deploy(bank.address);4041// Deposit funds42await bank.deposit({ value: ethers.utils.parseEther("10") });4344// Attempt reentrancy attack45await expect(46attackerContract.attack({ value: ethers.utils.parseEther("1") }),47).to.be.revertedWith("ReentrancyGuard: reentrant call");48});4950it("Should prevent integer overflow", async function () {51const Token = await ethers.getContractFactory("SecureToken");52const token = await Token.deploy();5354// Attempt overflow55await expect(token.transfer(attacker.address, ethers.constants.MaxUint256))56.to.be.reverted;57});5859it("Should enforce access control", async function () {60const [owner, attacker] = await ethers.getSigners();6162const Contract = await ethers.getContractFactory("SecureContract");63const contract = await Contract.deploy();6465// Attempt unauthorized withdrawal66await expect(contract.connect(attacker).withdraw(100)).to.be.revertedWith(67"Ownable: caller is not the owner",68);69});70});71```7273## Audit Preparation7475```solidity76contract WellDocumentedContract {77/**78* @title Well Documented Contract79* @dev Example of proper documentation for audits80* @notice This contract handles user deposits and withdrawals81*/8283/// @notice Mapping of user balances84mapping(address => uint256) public balances;8586/**87* @dev Deposits ETH into the contract88* @notice Anyone can deposit funds89*/90function deposit() public payable {91require(msg.value > 0, "Must send ETH");92balances[msg.sender] += msg.value;93}9495/**96* @dev Withdraws user's balance97* @notice Follows CEI pattern to prevent reentrancy98* @param amount Amount to withdraw in wei99*/100function withdraw(uint256 amount) public {101// CHECKS102require(amount <= balances[msg.sender], "Insufficient balance");103104// EFFECTS105balances[msg.sender] -= amount;106107// INTERACTIONS108(bool success, ) = msg.sender.call{value: amount}("");109require(success, "Transfer failed");110}111}112```113