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に貼り付けるだけ。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
…
よくある質問
「リエントランシー」とは何ですか?
あるコントラクトが別のコントラクトを呼び出し、その相手が最初の呼び出しの処理を終える前に、呼び出し元を再び呼び出してしまう攻撃の一種です。資金が盗まれるおそれがあります。
「スリッページ操作」とは何ですか?
取引を開始してから完了するまでの間に価格が予想外に変わり、受け取れる量が想定より少なくなることです。
「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エージェントが誰にとっても使いやすいインターフェースを作れるように手助けします。目が見えない方や身体の動きに制限のある方など、障害のある人にとってもWebサイトやアプリが正しく動くようにします。
agent-architecture-audit
280kエージェントがすべて正しく動いているかを確認できるようにします。AIシステムの問題を見つけて修正し、期待どおりに動くようにします。
agent-eval
280kエージェントが異なるAIコーディングツールを比較できるようにします。特定のタスクにおいて、どれがより速く、安く、信頼性が高いかを測定します。
agent-harness-construction
280kエージェントがタスクの進め方をよりよく理解できるようにします。エージェントが使うツールや指示を最適化し、成功する可能性を高めます。
似ているスキル
reverse-engineer-anything
422kREAツールを使って、出荷済みのアプリやパッケージの中身を調べ、機能の説明、版の比較、再構築の手順をエージェントが支援します。
1password
394k1Password CLIをセットアップし、サインインして、秘密情報の値を会話に表示せずに読み取ったり注入したりするようエージェントを支援します。
healthcheck
394kOpenClawを動かすコンピューターやサーバーを、SSH、ファイアウォール、更新、バックアップなどのセキュリティリスクについて監査し、安全な修正案を提案するようエージェントを支援します。