Works with the AI Agents you already use
Foundry Invariant Fuzzing
by BanditXBT
Foundry fuzz, fork and stateful invariant testing with handlers and ghost variables for DeFi logic.
$3.15
About this skill
Unit tests prove examples; invariants prove the protocol cannot reach a bad state through any call sequence.
- Unit/regression: one test per known behavior and per fixed bug (
test_RevertWhen_...,test_..._Succeeds). Usevm.expectRevert(abi.encodeWithSelector(...))with exact errors. - Stateless fuzz:
function testFuzz_X(uint256 a, ...); constrain withbound(x, min, max)(notvm.assumefor wide filters: it discards runs). Properties: round-trip (deposit→withdraw ≤ deposit), monotonicity, rounding direction, "never reverts for valid input", conservation of value. - Fork tests:
vm.createSelectFork(rpc, blockNumber)pinned to a block for determinism; use for real tokens (USDT/USDC quirks, fee-on-transfer), real oracles, integration with live protocols. Run per target chain. - Stateful invariants:
invariant_*functions checked after random call sequences. - Formal/symbolic (when valuable): Halmos for bounded symbolic checks on arithmetic-heavy functions (e.g., margin math); Echidna/Medusa for coverage-guided campaigns with corpus reuse.
- Write 5-10 protocol-level properties first (solvency, conservation, access, monotonic accumulators). For derivatives:
Σ user equity + insurance ≥ 0 and ≤ vault assets,OI_long == OI_short(or within skew cap),funding index never decreases unexpectedly,healthy account stays healthy after unrelated actions,liquidation never increases account risk,no position opens below initial margin. - Handler pattern: a
Handlercontract exposes bounded actor actions (open,close,addCollateral,liquidate,warp time,move oracle price,donate,pause). UsetargetContract(handler); bound all inputs; pick actors from a fixed set (vm.startPrank(actors[seed % n])). - Ghost variables track expected totals off-protocol (sum of deposits, realized PnL) to compare against on-chain state.
- Include adversarial actions (donation, dust amounts, zero values, price jumps, time jumps, reentrant token) so the fuzzer explores them; guard against false positives from unreachable states.
- Avoid revert-hiding: set
fail_on_revert = falseonly with handlers that already bound inputs; track call and revert counts in the handler (ghost counters) to ensure meaningful calls happen, and inspect withforge test -vvv.
- List invariants → write handler → run small → inspect call distribution → increase runs/depth.
- On failure: shrink, replay with
-vvvv, minimal repro test, find the root cause by minimizing the call sequence and inspecting state at the first violated invariant (not just the last call), fix, keep the test. - Mutation check: break the contract on purpose (flip a
<, remove a check, change rounding direction); the suite must fail. Usevertigo-rs/gambitif available; otherwise manual. - Gas and size:
forge snapshot,forge build --sizes(24 KB limit); keep gas regressions in CI. - Coverage:
forge coverage --report lcovto find unexercised branches; coverage guides, invariants decide.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 13 days ago
- Software
- Foundry
Permissions
No special permissions declared or detected
What's inside
Frequently Asked Questions
New in Security
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Security.