A comprehensive DeFi looping and collateral tool with React Flow visualization, Aave V3 integration, and flash loan functionality.
┌───────────────────────────────────────────────────────────────┐
│ UI Layer │
│ ┌───────────────────┐ ┌───────────────────────────────────┐ │
│ │ Sidebar / Presets │ │ Drag & Drop Canvas (React Flow) │ │
│ └───────────────────┘ └───────────────────────────────────┘ │
└───────────────────────────────────────────────────────────────┘
│
▼
┌───────────────────────────────────────────────────────────────┐
│ Strategy Engine (Client) │
│ - Manage steps and recursions │
│ - Multi-wallet simulations │
│ - Health factor safety checks │
└───────────────────────────────────────────────────────────────┘
│
▼
┌───────────────────────────────────────────────────────────────┐
│ DeFi Connector (API / SDK integration) │
│ - Aave V3 SDK: borrow/supply/repay/flashLoans │
│ - 1inch/Uniswap SDK: swaps │
│ - Graph API: market data │
└───────────────────────────────────────────────────────────────┘
- Drag & Drop Strategy Builder with React Flow
- Multi-wallet Support with connection management
- Preset Strategies (Safe Loop, Aggressive Loop, Debt Swap)
- Flash Loan Integration for atomic debt restructuring
- Real-time Simulation with health factor monitoring
- 3x Safe Loop Example with step-by-step visualization
- Modern UI with Tailwind CSS and Lucide icons
- Node.js 18+
- npm or yarn
# Clone the repository
git clone <repository-url>
cd defi-leverage-calc
# Install dependencies
npm install
# Start development server
npm run dev
The application will be available at http://localhost:3000
src/
├── components/
│ ├── StrategyCanvas.tsx # Main React Flow canvas
│ ├── StrategyNode.tsx # Custom node components
│ ├── WalletPanel.tsx # Multi-wallet interface
│ └── PresetSidebar.tsx # Strategy presets
├── services/
│ └── defiService.ts # DeFi integration logic
├── types/
│ └── index.ts # TypeScript definitions
├── App.tsx # Main application
└── main.tsx # Entry point
- Initial Deposit: 100 ETH
- Collateral Ratio: 75% LTV
- Health Factor: > 1.8
- Loops: 3 iterations
- Flash Loan Rescue: Enabled
Loop 1: Collateral 175.00, Debt 75.00, HF 1.75
Loop 2: Collateral 237.50, Debt 137.50, HF 1.82
Loop 3: Collateral 295.00, Debt 195.00, HF 1.82
Create a .env
file:
VITE_AAVE_POOL_ADDRESS=0x...
VITE_GRAPH_API_URL=https://api.thegraph.com/...
VITE_CHAIN_ID=1
Replace placeholder functions in src/services/defiService.ts
with actual Aave SDK calls:
// Example Aave V3 flash loan
import { Pool } from '@aave/contract-helpers';
const pool = new Pool(provider, {
POOL_ADDRESSES_PROVIDER: '0x...',
});
await pool.flashLoan({
user: userAddress,
assets: [asset],
amounts: [amount],
modes: [0], // 0 = no debt, 1 = stable, 2 = variable
onBehalfOf: userAddress,
params: '0x',
referralCode: '0',
});
- Extend the
NodeData
interface insrc/types/index.ts
- Add node styling in
src/components/StrategyNode.tsx
- Update the node types mapping in
StrategyCanvas.tsx
- Add preset configuration to
src/components/PresetSidebar.tsx
- Implement strategy logic in
src/services/defiService.ts
- Update simulation functions as needed
# Run tests
npm test
# Run with coverage
npm run test:coverage
simulateSafeLoop(strategy)
- Run loop strategy simulationexecuteFlashLoan(amount, asset, actions)
- Execute flash loangetMarketData()
- Fetch current market datacalculateHealthFactor(collateral, debt, ratio)
- Calculate HF
handleSimulation()
- Run strategy simulationhandleReset()
- Reset canvas to initial stateonConnect(params)
- Handle node connections
- Health Factor Monitoring: Automatic HF calculation and alerts
- Liquidation Protection: Real-time liquidation price tracking
- Flash Loan Rescue: Atomic debt restructuring capabilities
- Multi-wallet Safety: Isolated position management
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit changes (
git commit -m 'Add amazing feature'
) - Push to branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
This tool is for educational and development purposes. DeFi strategies involve significant risks including liquidation and loss of funds. Always test thoroughly on testnets before mainnet deployment.
- Aave V3 Documentation
- React Flow Documentation
- Tailwind CSS
- Vite # Atomic-Leverage-Calc