# DeFi Protocols

> AMM math (x*y=k) and Lending pool architectures.

- Skill: `j4flmao/defi-protocols` (Agent Skill)
- Install (CLI): `npx skillmds@latest add j4flmao/defi-protocols`
- Raw SKILL.md: https://api.skillmd.com/api/skills/j4flmao/defi-protocols/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: j4flmao (https://skillmd.com/u/j4flmao)
- Updated: 2026-09-21
- Page: https://skillmd.com/skills/j4flmao/defi-protocols

---


# DeFi Protocols

## AMM Math (Constant Product)
The fundamental formula for DEXes like Uniswap V2 is $x \times y = k$.

```solidity
contract SimpleAMM {
    uint public reserve0;
    uint public reserve1;

    function swap(uint amountIn, bool isToken0) external returns (uint amountOut) {
        require(amountIn > 0, "Invalid amount");
        (uint reserveIn, uint reserveOut) = isToken0 ? (reserve0, reserve1) : (reserve1, reserve0);
        
        // amountOut = (amountIn * 997 * reserveOut) / (reserveIn * 1000 + amountIn * 997)
        uint amountInWithFee = amountIn * 997;
        amountOut = (amountInWithFee * reserveOut) / (reserveIn * 1000 + amountInWithFee);
        
        // Update reserves...
    }
}
```

## Lending Pool Architecture
Overcollateralized lending requires robust liquidation mechanisms when Health Factor < 1.

## Protocol Interactions
```mermaid
%%{init: {"theme": "default", "flowchart": {"useMaxWidth": true}}}%%
flowchart TD
    User[User] -->|Deposit Asset| LendingPool[Lending Pool]
    LendingPool -->|Mint aToken| User
    User -->|Borrow| Vault[Collateral Vault]
    Vault -->|Check Price| Oracle[Price Oracle]
    Oracle -->|Price Update| Liquidator[Liquidator Bot]
    Liquidator -->|Liquidate Undercollateralized| Vault
```

