- Home
- Skills
- Web3 & Crypto
- Audit Solidity Defi SC
Works with the AI Agents you already use
Audit Solidity Defi SC
Systematic DeFi security audit methodology for proving and fixing Solidity vulnerabilities with reproduction tests.
Free
Audit Solidity Defi SC
Example session with this skill installed
Audit this Perpetual DEX deposit function for margin accounting errors. It subtracts lockedMargin from collateralBalances in the getFreeMargin view, but collateralBalances is already net of locked.
- Read your context and instructions
- Compiled the audit solidity defi
I have analyzed the margin accounting logic in the provided Perpetual DEX contract. The review confirms a High severity logic error where margin is subtracted twice, leading to incorrect free margin calculations and potential premature liquidations.
Finding: Margin Double-Count in Free Margin Calculation
| Field | Detail |
|---|---|
| ID | M-01 |
| Title | Margin Double-Count in getFreeMargin |
| Severity | High |
| Status | Open |
| Location | MarginVault.sol:142 |
| Description | The getFreeMargin function subtracts lockedMargin[t] from collateralBalances[t]. However, the lockMargin function already decrements collateralBalances[t] when moving funds to lockedMargin. |
| Impact | Users will appear to have less free margin than they actually do. This will cause legitimate orders to fail due to "insufficient margin" and may trigger incorrect liquidation logic if other functions rely on this view. |
Proof of Concept (Hardhat)
it("demonstrates free margin is halved incorrectly", async () => {
const depositAmount = ethers.parseEther("100");
const lockAmount = ethers.parseEther("20");
await vault.deposit(user.address, depositAmount);
await vault.connect(engine).lockMargin(user.address, lockAmount);
// collateralBalances[user] is now 80. lockedMargin[user] is 20.
// getFreeMargin returns (80 - 20) = 60. It should return 80.
const freeMargin = await vault.getFreeMargin(user.address);
expect(freeMargin).to.equal(depositAmount - lockAmount); // This assertion fails pre-fix
});
Recommended Fix
Update getFreeMargin to return the collateralBalances directly, as it already represents the available margin net of locks.
function getFreeMargin(address t) public view returns (uint256) {
return collateralBalances[t];
}
Next steps
- Apply the fix to
MarginVault.sol. - Run the provided Hardhat reproduction test to confirm the assertion passes.
- Review
liquidate()to ensure it does not also double-count these values.
Connects securely to your tools. The creator never sees your data.
About this skill
Audit DeFi protocols with a systematic methodology designed to catch critical vulnerabilities in perpetuals, AMMs, and lending markets. This skill moves beyond surface-level linting to focus on the deep logic of price manipulation, margin accounting, and state-transition failures. It enforces a strict evidence-based workflow where every identified flaw must be proven with an executable reproduction test.
What it does
- Map system lifecycles by tracing the flow from deposit and order matching to settlement, PnL, and liquidation.
- Threat-model privileged writes to identify any unprotected functions that control price, collateral, or position state.
- Re-derive accounting invariants to manually verify margin, funding, and liquidation formulas against the implementation code.
- Generate reproduction tests using Hardhat or Foundry that fail on vulnerable code and pass once the fix is applied.
- Structure formal audit reports using a severity rubric that categorizes findings from Informational to Critical based on fund risk.
How it works
- System Mapping: Provide the source code to trace the call graph and identify reactive keeper cascades.
- Logic Audit: Run the high-frequency bug class checklist against the protocol math and authorization layers.
- Proof of Concept: Develop executable scripts that demonstrate the vulnerability, such as a double-count in free margin.
- Remediation: Apply the recommended fixes and run the reproduction suite to confirm the state is secured.
Frameworks & tools
Solidity, Hardhat, Ethers v6, Foundry, Slither, and Aderyn.
Why this beats prompting it yourself
Generic prompts often miss subtle DeFi-specific logic flaws like double-counting margin or gas stipend reverts. This skill provides a structured threat-model for state-changers and requires verifiable proof-of-concept tests, ensuring findings are grounded in execution rather than LLM hallucination.
Use cases
- Reviewing a perpetual DEX or lending market prior to a mainnet deployment.
- Preparing a professional security audit report for a protocol governance proposal.
- Validating the math behind complex funding rate and liquidation mechanisms.
- Creating a portfolio of verifiable smart contract security case studies.
Known limitations
Does not perform formal verification or symbolic execution. The methodology relies on manual re-derivation of math and static analysis tools like Slither for initial sweeps.
Requires manual re-derivation of accounting math. Does not perform formal verification or symbolic execution. Focuses on Solidity-based DeFi protocols only.
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 any payment.
Reviews
This skill has been used 10 times.
Trust & safety
Security scan
Security scan passed 16 days ago
- Free with an Agensi account
Requires