Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    Audit Solidity Defi SC

    0

    Systematic DeFi security audit methodology for proving and fixing Solidity vulnerabilities with reproduction tests.

    Free

    10 uses4.0 (1 review)
    Audit Solidity Defi SC

    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

    FieldDetail
    IDM-01
    TitleMargin Double-Count in getFreeMargin
    SeverityHigh
    StatusOpen
    LocationMarginVault.sol:142
    DescriptionThe getFreeMargin function subtracts lockedMargin[t] from collateralBalances[t]. However, the lockMargin function already decrements collateralBalances[t] when moving funds to lockedMargin.
    ImpactUsers 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

    1. Apply the fix to MarginVault.sol.
    2. Run the provided Hardhat reproduction test to confirm the assertion passes.
    3. 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

    1. System Mapping: Provide the source code to trace the call graph and identify reactive keeper cascades.
    2. Logic Audit: Run the high-frequency bug class checklist against the protocol math and authorization layers.
    3. Proof of Concept: Develop executable scripts that demonstrate the vulnerability, such as a double-count in free margin.
    4. 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. 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. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before any payment.

    Reviews

    4.0
    1 review
    5
    0
    4
    1
    3
    0
    2
    0
    1
    0

    This skill has been used 10 times.

    Trust & safety

    Security scan

    Security scan passed 16 days ago

    • Free with an Agensi account

    Requires

    Git

    Permissions

    Terminal / Shell
    Listed19 days ago
    Updated16 days ago

    What's inside

    Frequently Asked Questions

    This skill performs a deep logic audit of DeFi-specific functions like margin accounting and price manipulation, requiring an executable reproduction test (Foundry/Hardhat) for every finding to eliminate false positives.

    The skill is designed to work with any agentic AI platform that supports code execution and file system access, as it needs to run Slither, Aderyn, and local testing frameworks.

    You receive the full systematic auditing workflow, the DeFi-specific bug class checklists, and the standardized reporting templates used to categorize findings by severity.

    While the skill identifies logical vulnerabilities and math errors, it does not perform formal verification (mathematical proofs) or symbolic execution; it focuses on evidence-based reproduction tests.

    You need a local or containerized environment with Node.js (for Hardhat/Ethers) and Rust (for Foundry) installed, as the skill generates and executes scripts to prove vulnerabilities.

    New in Web3 & Crypto