defi-amm-security
它能做什么
这份指南可以帮助你保护处理加密货币兑换(AMM)和流动性池的去中心化金融(DeFi)智能合约。它会检查可能导致资金损失的常见问题。
适用场景
- ✓编写新的加密货币交易所合约时。
- ✓审查现有合约的安全漏洞时。
- ✓为 DeFi 协议添加新的管理功能时。
技能链接
https://github.com/affaan-m/ECC/blob/main/skills/defi-amm-security/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“检查这个流动性池合约,确保它不存在重入攻击或价格操纵的漏洞。”
每周 5 个新技能,1 分钟读完。
工作原理
它的使用方式就像一份详细的检查清单。你可以将代码与安全示例和推荐的设计模式进行对照,从而避免已知的漏洞。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
DeFi AMM Security
Critical vulnerability patterns and hardened implementations for Solidity AMM contracts, LP vaults, and swap functions.
When to Use
- Writing or auditing a Solidity AMM or liquidity-pool contract
- Implementing swap, deposit, withdraw, mint, or burn flows that hold token balances
- Reviewing any contract that uses
token.balanceOf(address(this))in share or reserve math - Adding fee setters, pausers, oracle updates, or other admin functions to a DeFi protocol
How It Works
Use this as a checklist-plus-pattern library. Review every user entrypoint against the categories below and prefer the hardened examples over hand-rolled variants.
Execution Safety
The shell commands in this skill are local audit examples. Run them only in a trusted checkout or disposable sandbox, and do not splice untrusted contract names, paths, RPC URLs, private keys, or user-supplied flags into shell commands. Ask before installing tools or running long fuzzing/static-analysis jobs that may consume significant local or paid resources.
Never include secrets, private keys, seed phrases, API tokens, or mainnet signing credentials in command examples, logs, or reports.
Examples
Reentrancy: enforce CEI order
Vulnerable:
function withdraw(uint256 amount) external {
require(balances[msg.sender] >= amount);
token.transfer(msg.sender, amount);
balances[msg.sender] -= amount;
}
Safe:
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, "Insufficient");
balances[msg.sender] -= amount;
token.safeTransfer(msg.sender, amount);
}
Do not write your own guard when a hardened library exists.
Donation or inflation attacks
Using token.balanceOf(address(this)) directly for share math lets attackers manipulate the denominator by sending tokens to the contract outside the intended path.
// Vulnerable
function deposit(uint256 assets) external returns (uint256 shares) {
shares = (assets * totalShares) / token.balanceOf(address(this));
}
// Safe
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;
}
Track internal accounting and measure actual tokens received.
Oracle manipulation
Spot prices are flash-loan manipulable. Prefer 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);
Slippage protection
Every swap path needs caller-provided slippage and a deadline.
function swap(
uint256 amountIn,
uint256 amountOutMin,
uint256 deadline
) external returns (uint256 amountOut) {
require(block.timestamp <= deadline, "Expired");
amountOut = _calculateOut(amountIn);
require(amountOut >= amountOutMin, "Slippage exceeded");
_executeSwap(amountIn, amountOut);
}
Safe reserve math
import {FullMath} from "@uniswap/v3-core/contracts/libraries/FullMath.sol";
uint256 result = FullMath.mulDiv(a, b, c);
For large reserve math, avoid naive a * b / c when overflow risk exists.
Admin controls
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
contract MyAMM is Ownable2Step {
function setFee(uint256 fee) external onlyOwner { ... }
function pause() external onlyOwner { ... }
}
Prefer explicit acceptance for ownership transfer and gate every privileged path.
Security Checklist
- Reentrancy-exposed entrypoints use
nonReentrant - CEI ordering is respected
- Share math does not depend on raw
balanceOf(address(this)) - ERC-20 transfers use
SafeERC20
…
常见问题
什么是“重入”(reentrancy)?
这是一种攻击方式:合约 A 调用合约 B,合约 B 又在 A 的初始调用结束之前回调 A,从而可能盗取资金。
什么是“滑点操纵”(slippage manipulation)?
指你发起交易到交易完成之间,价格发生意外变化,导致实际到手的数量少于预期。
什么是“AMM 智能合约”?
它们是区块链上的程序,无需中介就能自动完成加密货币交易,例如 Uniswap 或 PancakeSwap。
详情
作者的原始描述
Security checklist for Solidity AMM contracts, liquidity pools, and swap flows. Covers reentrancy, CEI ordering, donation or inflation attacks, oracle manipulation, slippage, admin controls, and integer math. Use when auditing or writing Solidity AMM, liquidity pool, or swap code.
repo: affaan-m/ECC
热度
- GitHub 星标
- 280k
- 排名
- #299
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
accessibility
280k帮你的智能体创建人人都能使用的界面。确保网站和应用对残障人士友好,比如失明或行动不便的用户也能顺畅使用。
agent-architecture-audit
280k让你的智能体检查一切是否正常运行。它能找出并修复 AI 系统中的问题,确保系统按预期工作。
agent-eval
280k帮你的智能体比较不同的 AI 编程工具。它能衡量哪个工具在特定任务上更快、更便宜、更可靠。
agent-harness-construction
280k让你的智能体更好地理解如何完成任务。它会优化智能体使用的工具和指令,提高成功的几率。