Audits Solidity AMM contracts for reentrancy, donation/inflation attacks, oracle manipulation, slippage, and admin controls. Provides checklists and secure patterns.
How this skill is triggered — by the user, by Claude, or both
Slash command
/everything-claude-code:defi-amm-securityThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Solidity AMM 合约、LP 金库和交换函数的关键漏洞模式和强化实现。
Solidity AMM 合约、LP 金库和交换函数的关键漏洞模式和强化实现。
token.balanceOf(address(this)) 的合约将此用作检查清单加模式库。对照下面的类别审查每个用户入口点,优先使用强化示例而非手动编写的变体。
此技能中的 shell 命令是本地审计示例。仅在受信任的检出或一次性沙箱中运行,不要将不受信任的合约名称、路径、RPC URL、私钥或用户提供的标志拼接到 shell 命令中。在安装工具或运行可能消耗大量本地或付费资源的长时间模糊/静态分析任务之前先询问。
绝不在命令示例、日志或报告中包含密钥、私钥、助记词、API token 或主网签名凭证。
有漏洞:
function withdraw(uint256 amount) external {
require(balances[msg.sender] >= amount);
token.transfer(msg.sender, amount);
balances[msg.sender] -= amount;
}
安全:
import {ReentrancyGuard} from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
using SafeERC20 for IERC20;
function withdraw(uint256 amount) external nonReentrant {
require(balances[msg.sender] >= amount, "余额不足");
balances[msg.sender] -= amount;
token.safeTransfer(msg.sender, amount);
}
当存在经过强化的库时,不要自己编写保护器。
直接使用 token.balanceOf(address(this)) 进行份额数学计算,会让攻击者通过在预期路径之外向合约发送代币来操纵分母。
// 有漏洞
function deposit(uint256 assets) external returns (uint256 shares) {
shares = (assets * totalShares) / token.balanceOf(address(this));
}
// 安全
uint256 private _totalAssets;
function deposit(uint256 assets) external nonReentrant returns (uint256 shares) {
uint256 balBefore = token.balanceOf(address(this));
token.safeTransferFrom(msg.sender, address(this), assets);
uint256 received = token.balanceOf(address(this)) - balBefore;
shares = totalShares == 0 ? received : (received * totalShares) / _totalAssets;
_totalAssets += received;
totalShares += shares;
}
跟踪内部记账并测量实际收到的代币。
现货价格可被闪电贷操纵。优先使用 TWAP。
uint32[] memory secondsAgos = new uint32[](2);
secondsAgos[0] = 1800;
secondsAgos[1] = 0;
(int56[] memory tickCumulatives,) = IUniswapV3Pool(pool).observe(secondsAgos);
int24 twapTick = int24(
(tickCumulatives[1] - tickCumulatives[0]) / int56(uint56(30 minutes))
);
uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(twapTick);
每条交换路径都需要调用方提供的滑点参数和截止时间。
function swap(
uint256 amountIn,
uint256 amountOutMin,
uint256 deadline
) external returns (uint256 amountOut) {
require(block.timestamp <= deadline, "已过期");
amountOut = _calculateOut(amountIn);
require(amountOut >= amountOutMin, "超出滑点");
_executeSwap(amountIn, amountOut);
}
import {FullMath} from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
uint256 result = FullMath.mulDiv(a, b, c);
对于大型储备数学,当存在溢出风险时避免使用简单的 a * b / c。
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
contract MyAMM is Ownable2Step {
function setFee(uint256 fee) external onlyOwner { ... }
function pause() external onlyOwner { ... }
}
优先使用显式接受的所有权转移,并为每个特权路径设置关卡。
nonReentrantbalanceOf(address(this))SafeERC20amountOutMin 和 deadlinemulDivpip install slither-analyzer
slither . --exclude-dependencies
echidna-test . --contract YourAMM --config echidna.yaml
forge test --fuzz-runs 10000
npx claudepluginhub aaione/everything-claude-code-zhSecurity checklist for Solidity AMM contracts covering reentrancy, CEI ordering, donation/inflation attacks, oracle manipulation, slippage, admin controls, and integer math.
Provides production-ready Solidity templates for DeFi protocols including staking rewards, AMMs, governance, lending, and flash loans. Use when building smart contract-based decentralized finance apps.
Provides Solidity templates for DeFi protocols: staking rewards, AMMs, governance, lending, flash loans. Use for building smart contract-based decentralized finance applications.