simulating-flash-loans
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSimulating Flash Loans
Flash Loan模拟
Overview
概述
This skill simulates flash loan strategies across major DeFi protocols (Aave V3, dYdX, Balancer) with profitability calculations, gas cost estimation, and risk assessment. It enables developers and researchers to evaluate flash loan opportunities without executing real transactions.
本工具支持在主流DeFi协议(Aave V3、dYdX、Balancer)上模拟flash loan策略,提供盈利能力计算、gas成本估算和风险评估功能,开发者和研究者无需执行真实交易即可评估flash loan机会。
Prerequisites
前置要求
Before using this skill, ensure you have:
- Python 3.9+ with ,
web3, andhttpxpackagesrich - RPC endpoint access (free public RPCs work fine)
- Understanding of flash loan mechanics and DeFi protocols
- Optional: Etherscan API key for better gas estimates
使用本工具前,请确保你已满足以下条件:
- 安装Python 3.9+及、
web3、httpx依赖包rich - 拥有RPC端点访问权限(免费公共RPC即可正常使用)
- 了解flash loan运行机制和DeFi协议相关知识
- 可选:Etherscan API密钥,用于获得更精准的gas估算结果
Instructions
使用说明
Step 1: Configure RPC Endpoint
步骤1:配置RPC端点
Configure your RPC in :
{baseDir}/config/settings.yamlyaml
undefined在中配置你的RPC:
{baseDir}/config/settings.yamlyaml
undefinedFree public RPCs (no signup required)
免费公共RPC(无需注册)
rpc_endpoints:
ethereum: "https://rpc.ankr.com/eth"
polygon: "https://rpc.ankr.com/polygon"
arbitrum: "https://rpc.ankr.com/arbitrum"
Or use environment variable:
```bash
export ETH_RPC_URL="https://rpc.ankr.com/eth"rpc_endpoints:
ethereum: "https://rpc.ankr.com/eth"
polygon: "https://rpc.ankr.com/polygon"
arbitrum: "https://rpc.ankr.com/arbitrum"
也可使用环境变量配置:
```bash
export ETH_RPC_URL="https://rpc.ankr.com/eth"Step 2: Simple Arbitrage Simulation
步骤2:简单套利模拟
Simulate a basic two-DEX arbitrage:
bash
python {baseDir}/scripts/flash_simulator.py arbitrage ETH USDC 100 \
--dex-buy uniswap --dex-sell sushiswapThis calculates:
- Gross profit from price difference
- Flash loan fee (provider-specific)
- Gas costs
- Net profit/loss
模拟基础的双DEX套利:
bash
python {baseDir}/scripts/flash_simulator.py arbitrage ETH USDC 100 \
--dex-buy uniswap --dex-sell sushiswap该命令将计算:
- 价差带来的总收益
- Flash loan手续费(依服务商而定)
- Gas成本
- 净收益/亏损
Step 3: Compare Flash Loan Providers
步骤3:对比Flash Loan服务商
Find the cheapest provider for your strategy:
bash
python {baseDir}/scripts/flash_simulator.py arbitrage ETH USDC 100 --compare-providersOutput shows:
| Provider | Fee | Net Profit |
|---|---|---|
| dYdX | 0% | $245.30 |
| Balancer | 0.01% | $242.80 |
| Aave V3 | 0.09% | $225.50 |
为你的策略找到成本最低的服务商:
bash
python {baseDir}/scripts/flash_simulator.py arbitrage ETH USDC 100 --compare-providers输出结果如下:
| 提供商 | 费率 | 净利润 |
|---|---|---|
| dYdX | 0% | $245.30 |
| Balancer | 0.01% | $242.80 |
| Aave V3 | 0.09% | $225.50 |
Step 4: Liquidation Profitability
步骤4:清算盈利能力分析
Analyze liquidation opportunities:
bash
python {baseDir}/scripts/flash_simulator.py liquidation \
--protocol aave --health-factor 0.95Shows positions below health factor threshold with:
- Collateral value
- Debt value
- Liquidation bonus
- Net profit after costs
分析清算机会:
bash
python {baseDir}/scripts/flash_simulator.py liquidation \
--protocol aave --health-factor 0.95将展示健康因子低于阈值的仓位,包含以下信息:
- 抵押品价值
- 债务价值
- 清算奖励
- 扣除成本后的净收益
Step 5: Triangular Arbitrage
步骤5:三角套利
Simulate multi-hop circular arbitrage:
bash
python {baseDir}/scripts/flash_simulator.py triangular \
ETH USDC WBTC ETH --amount 50Analyzes path profitability:
Path: ETH → USDC → WBTC → ETH
Gross: +0.15 ETH
Fees: -0.045 ETH (3 swaps)
Loan: -0.045 ETH (Aave fee)
Gas: -0.02 ETH
─────────────────────
Net: +0.04 ETH ($101.73)模拟多跳循环套利:
bash
python {baseDir}/scripts/flash_simulator.py triangular \
ETH USDC WBTC ETH --amount 50分析路径盈利能力:
Path: ETH → USDC → WBTC → ETH
Gross: +0.15 ETH
Fees: -0.045 ETH (3 swaps)
Loan: -0.045 ETH (Aave fee)
Gas: -0.02 ETH
─────────────────────
Net: +0.04 ETH ($101.73)Step 6: Risk Assessment
步骤6:风险评估
Add risk analysis to any simulation:
bash
python {baseDir}/scripts/flash_simulator.py arbitrage ETH USDC 100 --risk-analysisRisk factors scored:
- MEV Competition: Bot activity on this pair
- Execution Risk: Slippage and timing sensitivity
- Protocol Risk: Smart contract and oracle risks
- Liquidity Risk: Depth available for trade size
为任意模拟添加风险分析:
bash
python {baseDir}/scripts/flash_simulator.py arbitrage ETH USDC 100 --risk-analysis将对以下风险因子打分:
- MEV竞争:该交易对的机器人活跃度
- 执行风险:滑点和时间敏感度
- 协议风险:智能合约和预言机风险
- 流动性风险:对应交易规模的交易深度
Step 7: Full Analysis
步骤7:全量分析
Run complete analysis with all features:
bash
python {baseDir}/scripts/flash_simulator.py arbitrage ETH USDC 100 \
--full --output json > simulation.json运行包含所有功能的完整分析:
bash
python {baseDir}/scripts/flash_simulator.py arbitrage ETH USDC 100 \
--full --output json > simulation.jsonOutput
输出说明
The simulator provides:
Quick Mode:
- Net profit/loss
- Provider recommendation
- Go/No-Go verdict
Breakdown Mode:
- Step-by-step transaction flow
- Individual cost components
- Slippage estimates
Comparison Mode:
- All providers ranked
- Fee differences
- Liquidity availability
Risk Analysis:
- Competition score (0-100)
- Execution probability
- Protocol safety rating
- Overall viability grade
模拟器提供以下输出模式:
快速模式:
- 净收益/亏损
- 服务商推荐
- 可行/不可行判定
明细模式:
- 交易流分步说明
- 各成本项明细
- 滑点估算
对比模式:
- 所有服务商排名
- 费率差异
- 流动性可用性
风险分析:
- 竞争得分(0-100)
- 执行概率
- 协议安全评级
- 整体可行性等级
Flash Loan Providers
Flash Loan服务商
| Provider | Fee | Best For | Chains |
|---|---|---|---|
| dYdX | 0% | Maximum profit | Ethereum |
| Balancer | 0.01% | Pool tokens | ETH, Polygon, Arbitrum |
| Aave V3 | 0.09% | Any token | ETH, Polygon, Arbitrum, Optimism |
| Uniswap V3 | ~0.3% | Specific pairs | ETH, Polygon, Arbitrum |
| 提供商 | 费率 | 适用场景 | 支持链 |
|---|---|---|---|
| dYdX | 0% | 收益最大化 | Ethereum |
| Balancer | 0.01% | 池代币场景 | ETH、Polygon、Arbitrum |
| Aave V3 | 0.09% | 任意代币场景 | ETH、Polygon、Arbitrum、Optimism |
| Uniswap V3 | ~0.3% | 特定交易对 | ETH、Polygon、Arbitrum |
Supported Strategies
支持的策略
- Simple Arbitrage: Buy on DEX A, sell on DEX B
- Triangular Arbitrage: A→B→C→A circular path
- Liquidation: Repay debt, claim collateral bonus
- Collateral Swap: Replace collateral without closing
- Self-Liquidation: Efficiently close own position
- Debt Refinancing: Move debt to better rates
- 简单套利:在DEX A买入,在DEX B卖出
- 三角套利:A→B→C→A循环路径套利
- 清算:偿还债务,领取抵押品奖励
- 抵押品互换:无需平仓即可替换抵押品
- 自我清算:高效平仓自有仓位
- 债务再融资:将债务转移到利率更优的平台
Error Handling
错误处理
See for comprehensive error handling.
{baseDir}/references/errors.mdCommon issues:
- RPC Rate Limit: Switch to backup endpoint or wait
- Stale Prices: Data older than 30s triggers warning
- No Profitable Route: All routes lose money after costs
- Insufficient Liquidity: Trade size exceeds pool depth
完整错误处理说明参见。
{baseDir}/references/errors.md常见问题:
- RPC速率限制:切换备用端点或等待后重试
- 价格过时:数据超过30秒将触发警告
- 无盈利路径:扣除所有成本后所有路径均亏损
- 流动性不足:交易规模超过池子深度
Examples
示例
See for detailed examples including:
{baseDir}/references/examples.md- ETH/USDC arbitrage simulation
- Aave liquidation analysis
- Multi-provider comparison
- Historical backtesting
详细示例参见,包含:
{baseDir}/references/examples.md- ETH/USDC套利模拟
- Aave清算分析
- 多服务商对比
- 历史回测
Educational Disclaimer
教育免责声明
FOR EDUCATIONAL PURPOSES ONLY
Flash loan strategies involve significant risks:
- Smart contract bugs can cause total loss
- MEV bots compete for same opportunities
- Gas costs can exceed profits
- Protocol exploits may have legal implications
Never deploy unaudited code. Start with testnets. Consult legal counsel.
仅用于教育目的
Flash loan策略存在极高风险:
- 智能合约漏洞可能导致全额损失
- MEV机器人会竞争相同的机会
- Gas成本可能超过收益
- 协议漏洞可能引发法律风险
切勿部署未审计的代码,先在测试网测试,必要时咨询法律顾问。