agentleFS
0xlayerghost0xlayerghost/solidity-agent-kit/skills/defi-security/SKILL.md

defi-security

Skill4 starsChanged 9 months agoMITView on GitHub

[AUTO-INVOKE] MUST be invoked BEFORE deploying DeFi contracts (DEX, lending, staking, LP, token). Covers anti-whale, anti-MEV, flash loan protection, launch checklists, and emergency response. Trigger: any deployment or security review of DeFi-related contracts.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Always respond in the same language the user is using: If the user asks in Chinese, respond in Chinese.

    line 10
  2. Distinctive:

    Before mainnet deployment, verify all items:

    line 57

≈ “Common” is an estimate: at least 1 in 5 of the 213,895 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

DeFi Security Principles

Language Rule

  • Always respond in the same language the user is using. If the user asks in Chinese, respond in Chinese. If in English, respond in English.

Scope: Only applicable to DeFi projects (DEX, lending, staking, LP, yield). Non-DeFi projects can ignore this skill.

Protection Decision Rules

ThreatRequired Protection
Whale manipulationDaily transaction caps + per-tx amount limits + cooldown window
MEV / sandwich attackEOA-only checks (msg.sender == tx.origin), or use commit-reveal pattern
ArbitrageReferral binding + liquidity distribution + fixed yield model + lock period
ReentrancyReentrancyGuard on all external-call functions (see solidity-security skill)
Flash loan attackCheck block.number change between operations, or use TWAP pricing
Price manipulationChainlink oracle or TWAP — never rely on spot AMM reserves for pricing
Approval exploitUse safeIncreaseAllowance / safeDecreaseAllowance, never raw approve for user flows
Governance attackVoting requires snapshot + minimum token holding period; timelock ≥ 48h on proposal execution
ERC4626 inflation attackFirst deposit must enforce minimum amount or use virtual shares to prevent share dilution via rounding
Cross-vault trust bypassRouter/Registry relay must verify vault authorization; never trust caller identity inside flash loan callbacks — EVMbench/noya H-08
Collateral ownership exploitLiquidation/staking operations must verify actual NFT/collateral ownership — EVMbench/benddao
Bonding curve manipulationID/pricing params in create operations must be fully determined before external calls — EVMbench/phi H-06
DEX pair _transfer TOCTOUNever distinguish operation type by balance/reserve checks in _transfer — both directions are exploitable: buy vs removeLiquidity (pair→user) and sell vs addLiquidity (user→pair); use address whitelist only; new projects prefer Uniswap V4 Hook
Per-address state bypassAny mapping(address => ...) restriction (cooldown, max tx, max balance) can be bypassed via multi-address or transfer-then-trade; propagate restriction state on wallet-to-wallet transfers using max(_lastTxTime[to], _lastTxTime[from]) — never block.timestamp (griefing) or direct assignment (overwrite shortening)

Anti-Whale Implementation Rules

  • Maximum single transaction amount: configurable via onlyOwner setter
  • Daily cumulative limit per address: track with mapping(address => mapping(uint256 => uint256)) (address → day → amount)
  • Cooldown between transactions: enforce minimum time gap with block.timestamp check
  • Whitelist for exempt addresses (deployer, LP pair, staking contract)

Flash Loan Protection Rules

  • For price-sensitive operations: require that block.number has changed since last interaction
  • For oracle-dependent calculations: use time-weighted average (TWAP) over minimum 30 minutes
  • For critical state changes: add minimum holding period before action (e.g., must hold tokens for N blocks)

Protocol Composability Risks

Source: EVMbench (OpenAI/Paradigm, Feb 2026) — vulnerability patterns from Code4rena audits

  • Cross-vault operations [noya H-08]: Registry/Router relay calls must verify vault-level authorization; prevent keeper from using flash loan to impersonate other vaults
  • Lending collateral [benddao]: Liquidation functions must verify msg.sender actually owns or is authorized to operate on target collateral
  • Bonding curve [phi H-06]: In create + auto-buy operations, ID assignment and pricing params must be fully determined before the buy transaction executes; prevent reentrancy from modifying them
  • Shared registries [noya H-08]: Permission propagation chains in shared registries must be verified hop-by-hop; never rely solely on "trusted sender" flags

Launch Checklist

Before mainnet deployment, verify all items:

  • All onlyOwner functions transferred to multisig wallet
  • Timelock contract deployed and configured (minimum 24h delay for critical changes)
  • Emergency pause mechanism tested — both pause and unpause functions work correctly
  • Daily limit parameters documented and set to reasonable values
  • Third-party security audit completed and all critical/high findings resolved
  • Testnet deployment running for minimum 7 days with no issues
  • Slippage, fee, and lock period parameters reviewed and documented
  • Initial liquidity plan documented (amount, lock duration, LP token handling)
  • Fuzz testing passes with high iterations (10000+) on all DeFi-critical functions

Emergency Response Procedure

StepAction
1. DetectMonitor alerts trigger (on-chain monitoring, community reports)
2. PauseDesignated address calls pause() — must respond within minutes
3. AssessTechnical lead analyzes root cause, estimates fund impact
4. CommunicatePost incident notice to community channels (Discord, Twitter, Telegram)
5. FixDeploy fix or prepare recovery plan
6. ResumeCall unpause() after fix verified on fork — or migrate to new contract
7. Post-mortemPublish detailed incident report within 48 hours

DeFi Testing Reference

Test ScenarioApproach
Fuzz test fund flowsRun fuzz tests on staking/pool contracts with high iterations (10000+)
Fork mainnet testingUse Foundry fork mode against mainnet RPC to test with real state
Simulate whale transactionUse Foundry cast CLI to simulate large-amount calls on a forked network

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.