
defi-protocols
Master DeFi protocol development including AMMs, lending, yield, and oracles
Master DeFi protocol development including AMMs, lending, yield, and oracles
DeFi Protocols Skill
Master DeFi protocol development including AMM mechanics, lending systems, yield optimization, and oracle integration.
Quick Start
# Invoke this skill for DeFi development
Skill("defi-protocols", protocol_type="amm", chain="ethereum")
Topics Covered
1. AMM (Automated Market Makers)
Build decentralized exchanges:
- Constant Product: x * y = k (Uniswap V2)
- Concentrated Liquidity: Tick-based (Uniswap V3)
- Stable Swaps: Curve invariant
- TWAP: Time-weighted average price
2. Lending Protocols
Create lending markets:
- Overcollateralized: Aave/Compound model
- Interest Rates: Utilization-based curves
- Liquidation: Health factor, incentives
- Isolated Markets: Risk segmentation
3. Yield Optimization
Maximize returns:
- Auto-compounding: Vault strategies
- Liquidity Mining: Reward distribution
- veTokens: Vote-escrowed governance
- Bribes: Gauge voting incentives
4. Oracle Integration
Secure price feeds:
- Chainlink: Decentralized oracles
- TWAP: Manipulation resistant
- Staleness Checks: Price freshness
- Fallbacks: Multi-oracle setups
Code Examples
AMM Swap Calculation
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
library SwapMath {
uint256 constant FEE_NUMERATOR = 997;
uint256 constant FEE_DENOMINATOR = 1000;
/// @notice Calculate output amount for constant product AMM
function getAmountOut(
uint256 amountIn,
uint256 reserveIn,
uint256 reserveOut
) internal pure returns (uint256) {
uint256 amountInWithFee = amountIn * FEE_NUMERATOR;
uint256 numerator = amountInWithFee * reserveOut;
uint256 denominator = reserveIn * FEE_DENOMINATOR + amountInWithFee;
return numerator / denominator;
}
/// @notice Calculate price impact percentage (basis points)
function getPriceImpact(
uint256 amountIn,
uint256 reserveIn
) internal pure returns (uint256) {
return (amountIn * 10000) / (reserveIn + amountIn);
}
}
Chainlink Oracle
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceOracle {
AggregatorV3Interface public immutable priceFeed;
uint256 public constant STALENESS_THRESHOLD = 1 hours;
error StalePrice();
error InvalidPrice();
constructor(address feed) {
priceFeed = AggregatorV3Interface(feed);
}
function getPrice() external view returns (uint256) {
(, int256 price,, uint256 updatedAt,) = priceFeed.latestRoundData();
if (block.timestamp - updatedAt > STALENESS_THRESHOLD) {
revert StalePrice();
}
if (price <= 0) revert InvalidPrice();
return uint256(price);
}
}
Flash Loan Callback
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@aave/v3-core/contracts/flashloan/base/FlashLoanSimpleReceiverBase.sol";
contract Arbitrage is FlashLoanSimpleReceiverBase {
function executeOperation(
address asset,
uint256 amount,
uint256 premium,
address,
bytes calldata
) external override returns (bool) {
// Execute arbitrage logic here
// Repay: amount + premium
IERC20(asset).approve(address(POOL), amount + premium);
return true;
}
}
DeFi Math Formulas
Constant Product AMM
Invariant: x * y = k
Output: dy = y - k/(x + dx)
Price: p = y/x
Slippage: dx/(x + dx) * 100%
Interest Rates
Utilization: U = borrows / (deposits + borrows)
Borrow Rate: R = base + slope * U (when U < optimal)
Supply Rate: R_supply = R_borrow * U * (1 - reserve_factor)
Health Factor
HF = (collateral * liquidation_threshold) / debt
Liquidation when HF < 1
Common Pitfalls
| Pitfall | Risk | Prevention |
|---|---|---|
| Spot price oracle | Flash loan manipulation | Use TWAP |
| No slippage check | Sandwich attacks | Enforce min output |
| Stale prices | Wrong liquidations | Check updatedAt |
| Reentrancy | Fund drainage | CEI + guards |
Security Checklist
- [ ] Oracle staleness validation
- [ ] Slippage protection
- [ ] Flash loan attack resistance
- [ ] Reentrancy guards
- [ ] Access control on admin
- [ ] Emergency pause mechanism
- [ ] Timelock on parameters
Troubleshooting
"Oracle price deviation"
# Compare oracle vs DEX price
cast call $ORACLE "latestRoundData()" --rpc-url $RPC
cast call $POOL "slot0()" --rpc-url $RPC
"Sandwich attack detected"
Add minimum output enforcement:
require(amountOut >= minAmountOut, "Slippage exceeded");
Protocol Addresses (Mainnet)
| Protocol | Contract |
|---|---|
| Uniswap V3 Router | 0xE592427A0AEce92De3Edee1F18E0157C05861564 |
| Aave V3 Pool | 0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2 |
| Chainlink ETH/USD | 0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419 |
Cross-References
- Bonded Agent:
04-defi-specialist - Related Skills:
solidity-development,smart-contract-security
Version History
| Version | Date | Changes |
|---|---|---|
| 2.0.0 | 2025-01 | Production-grade with math, security |
| 1.0.0 | 2024-12 | Initial release |
You Might Also Like
Related Skills

coding-agent
Run Codex CLI, Claude Code, OpenCode, or Pi Coding Agent via background process for programmatic control.
openclaw
add-uint-support
Add unsigned integer (uint) type support to PyTorch operators by updating AT_DISPATCH macros. Use when adding support for uint16, uint32, uint64 types to operators, kernels, or when user mentions enabling unsigned types, barebones unsigned types, or uint support.
pytorch
at-dispatch-v2
Convert PyTorch AT_DISPATCH macros to AT_DISPATCH_V2 format in ATen C++ code. Use when porting AT_DISPATCH_ALL_TYPES_AND*, AT_DISPATCH_FLOATING_TYPES*, or other dispatch macros to the new v2 API. For ATen kernel files, CUDA kernels, and native operator implementations.
pytorch
skill-writer
Guide users through creating Agent Skills for Claude Code. Use when the user wants to create, write, author, or design a new Skill, or needs help with SKILL.md files, frontmatter, or skill structure.
pytorch
implementing-jsc-classes-cpp
Implements JavaScript classes in C++ using JavaScriptCore. Use when creating new JS classes with C++ bindings, prototypes, or constructors.
oven-sh
implementing-jsc-classes-zig
Creates JavaScript classes using Bun's Zig bindings generator (.classes.ts). Use when implementing new JS APIs in Zig with JSC integration.
oven-sh