@intentsolutionsio/flash-loan-simulator 1.0.0 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -12
- package/agents/flashloan-agent.md +17 -0
- package/package.json +1 -1
- package/skills/simulating-flash-loans/ARD.md +21 -7
- package/skills/simulating-flash-loans/PRD.md +9 -3
- package/skills/simulating-flash-loans/SKILL.md +29 -8
- package/skills/simulating-flash-loans/references/errors.md +47 -2
- package/skills/simulating-flash-loans/references/examples.md +12 -0
- package/skills/simulating-flash-loans/references/implementation.md +6 -0
- package/skills/simulating-flash-loans/scripts/flash_simulator.py +29 -85
- package/skills/simulating-flash-loans/scripts/formatters.py +19 -51
- package/skills/simulating-flash-loans/scripts/profit_calculator.py +16 -16
- package/skills/simulating-flash-loans/scripts/protocol_adapters.py +2 -8
- package/skills/simulating-flash-loans/scripts/risk_assessor.py +6 -17
- package/skills/simulating-flash-loans/scripts/strategy_engine.py +6 -14
package/README.md
CHANGED
|
@@ -35,12 +35,14 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
35
35
|
### Why Free RPCs Work for Flash Loan Simulations
|
|
36
36
|
|
|
37
37
|
**Simulation Requirements:**
|
|
38
|
+
|
|
38
39
|
- Read blockchain state (getBlock, getTransaction, call)
|
|
39
40
|
- Query DeFi protocol data (Aave, Uniswap, Balancer)
|
|
40
41
|
- Gas estimation (estimateGas)
|
|
41
42
|
- Historical data for backtesting
|
|
42
43
|
|
|
43
44
|
**Free RPCs provide:**
|
|
45
|
+
|
|
44
46
|
- 25-100 requests/second (more than enough for simulations)
|
|
45
47
|
- Archive node access (historical data)
|
|
46
48
|
- Multi-chain support (Ethereum, Polygon, Arbitrum)
|
|
@@ -55,14 +57,16 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
55
57
|
**What:** Public RPC endpoints for 15+ chains
|
|
56
58
|
|
|
57
59
|
**Limits:**
|
|
60
|
+
|
|
58
61
|
- Ethereum: 30 requests/second
|
|
59
62
|
- No rate limits on queries
|
|
60
63
|
- Archive data available
|
|
61
64
|
|
|
62
65
|
**Setup:**
|
|
66
|
+
|
|
63
67
|
```json
|
|
64
68
|
{
|
|
65
|
-
"rpcEndpoint": "
|
|
69
|
+
"rpcEndpoint": "eth"
|
|
66
70
|
}
|
|
67
71
|
```
|
|
68
72
|
|
|
@@ -75,11 +79,13 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
75
79
|
**What:** Free tier with 100K requests/day
|
|
76
80
|
|
|
77
81
|
**Limits:**
|
|
82
|
+
|
|
78
83
|
- 100,000 requests/day (sufficient for development)
|
|
79
84
|
- 10 requests/second
|
|
80
85
|
- Requires free account (no credit card)
|
|
81
86
|
|
|
82
87
|
**Setup:**
|
|
88
|
+
|
|
83
89
|
```json
|
|
84
90
|
{
|
|
85
91
|
"rpcEndpoint": "https://mainnet.infura.io/v3/YOUR_FREE_KEY"
|
|
@@ -95,11 +101,13 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
95
101
|
**What:** 3M requests/month free
|
|
96
102
|
|
|
97
103
|
**Limits:**
|
|
104
|
+
|
|
98
105
|
- 3,000,000 requests/month
|
|
99
106
|
- 25 requests/second
|
|
100
107
|
- Free account (no card)
|
|
101
108
|
|
|
102
109
|
**Setup:**
|
|
110
|
+
|
|
103
111
|
```json
|
|
104
112
|
{
|
|
105
113
|
"rpcEndpoint": "https://nd-XXX-XXX-XXX.p2pify.com/YOUR_FREE_KEY"
|
|
@@ -115,11 +123,13 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
115
123
|
**What:** Community-run public endpoints
|
|
116
124
|
|
|
117
125
|
**Limits:**
|
|
126
|
+
|
|
118
127
|
- Variable rate limits
|
|
119
128
|
- May be slower during peak times
|
|
120
129
|
- No signup required
|
|
121
130
|
|
|
122
131
|
**Setup:**
|
|
132
|
+
|
|
123
133
|
```json
|
|
124
134
|
{
|
|
125
135
|
"rpcEndpoint": "https://eth.llamarpc.com"
|
|
@@ -129,6 +139,7 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
129
139
|
**Cost:** $0 (completely free)
|
|
130
140
|
|
|
131
141
|
**Alternatives:**
|
|
142
|
+
|
|
132
143
|
- `https://ethereum.publicnode.com`
|
|
133
144
|
- `https://cloudflare-eth.com`
|
|
134
145
|
- `https://rpc.flashbots.net` (MEV-protected)
|
|
@@ -149,7 +160,7 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
149
160
|
"gasPrice": "auto",
|
|
150
161
|
"minProfitUSD": 50
|
|
151
162
|
},
|
|
152
|
-
"rpcEndpoint": "
|
|
163
|
+
"rpcEndpoint": "eth"
|
|
153
164
|
}
|
|
154
165
|
```
|
|
155
166
|
|
|
@@ -164,6 +175,7 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
164
175
|
```
|
|
165
176
|
|
|
166
177
|
**Setup Steps:**
|
|
178
|
+
|
|
167
179
|
1. Sign up at [infura.io](https://infura.io) (free, no card)
|
|
168
180
|
2. Create project
|
|
169
181
|
3. Copy API key
|
|
@@ -176,10 +188,10 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
176
188
|
```json
|
|
177
189
|
{
|
|
178
190
|
"networks": {
|
|
179
|
-
"ethereum": "
|
|
180
|
-
"polygon": "
|
|
181
|
-
"arbitrum": "
|
|
182
|
-
"optimism": "
|
|
191
|
+
"ethereum": "eth",
|
|
192
|
+
"polygon": "polygon",
|
|
193
|
+
"arbitrum": "arbitrum",
|
|
194
|
+
"optimism": "optimism"
|
|
183
195
|
}
|
|
184
196
|
}
|
|
185
197
|
```
|
|
@@ -191,10 +203,12 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
191
203
|
#### Paid Approach (Alchemy)
|
|
192
204
|
|
|
193
205
|
**Annual Subscriptions:**
|
|
206
|
+
|
|
194
207
|
- Alchemy Growth: $49/mo → $588/year
|
|
195
208
|
- Alchemy Scale: $199/mo → $2,388/year (if high usage)
|
|
196
209
|
|
|
197
210
|
**Advantages:**
|
|
211
|
+
|
|
198
212
|
- 660 requests/second
|
|
199
213
|
- Advanced analytics dashboard
|
|
200
214
|
- Dedicated support
|
|
@@ -202,12 +216,14 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
202
216
|
#### Free Approach (Public RPCs)
|
|
203
217
|
|
|
204
218
|
**Annual Subscriptions:**
|
|
219
|
+
|
|
205
220
|
- Ankr: $0
|
|
206
221
|
- Infura free tier: $0
|
|
207
222
|
- Public RPCs: $0
|
|
208
223
|
- **Total: $0/year**
|
|
209
224
|
|
|
210
225
|
**Advantages:**
|
|
226
|
+
|
|
211
227
|
- 25-100 requests/second (sufficient for simulations)
|
|
212
228
|
- Archive data for backtesting
|
|
213
229
|
- No rate limit concerns for development
|
|
@@ -218,7 +234,7 @@ Simulate and analyze flash loan strategies including arbitrage, liquidations, an
|
|
|
218
234
|
|
|
219
235
|
#### Flash Loan Arbitrage Simulation
|
|
220
236
|
|
|
221
|
-
```
|
|
237
|
+
```text
|
|
222
238
|
const { ethers } = require('ethers');
|
|
223
239
|
|
|
224
240
|
// Before (Alchemy - Paid)
|
|
@@ -228,7 +244,7 @@ const provider = new ethers.providers.JsonRpcProvider(
|
|
|
228
244
|
|
|
229
245
|
// After (Ankr - FREE)
|
|
230
246
|
const provider = new ethers.providers.JsonRpcProvider(
|
|
231
|
-
'
|
|
247
|
+
'eth' // $0/mo
|
|
232
248
|
);
|
|
233
249
|
|
|
234
250
|
// Same simulation capability, zero cost
|
|
@@ -241,7 +257,7 @@ const profit = calculateArbitrage(uniswapPrice, sushiswapPrice);
|
|
|
241
257
|
|
|
242
258
|
#### Aave Liquidation Simulation
|
|
243
259
|
|
|
244
|
-
```
|
|
260
|
+
```text
|
|
245
261
|
// Using free Infura RPC
|
|
246
262
|
const provider = new ethers.providers.JsonRpcProvider(
|
|
247
263
|
`https://mainnet.infura.io/v3/${INFURA_FREE_KEY}` // $0 (free tier)
|
|
@@ -274,6 +290,7 @@ if (healthFactor < 1e18) {
|
|
|
274
290
|
### When Free RPCs Are NOT Enough
|
|
275
291
|
|
|
276
292
|
**Use paid Alchemy if:**
|
|
293
|
+
|
|
277
294
|
- You're deploying live flash loan bots (need <50ms latency)
|
|
278
295
|
- You need >100 requests/second
|
|
279
296
|
- You require webhooks for mempool monitoring
|
|
@@ -287,7 +304,7 @@ if (healthFactor < 1e18) {
|
|
|
287
304
|
|
|
288
305
|
```javascript
|
|
289
306
|
const RPC_ENDPOINTS = {
|
|
290
|
-
development: '
|
|
307
|
+
development: 'eth', // $0
|
|
291
308
|
production: `https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}` // $49/mo
|
|
292
309
|
};
|
|
293
310
|
|
|
@@ -300,7 +317,7 @@ const provider = new ethers.providers.JsonRpcProvider(
|
|
|
300
317
|
|
|
301
318
|
### Resources
|
|
302
319
|
|
|
303
|
-
- **Ankr RPCs:**
|
|
320
|
+
- **Ankr RPCs:** rpc.ankr.com (FREE, no signup)
|
|
304
321
|
- **Infura Free:** [infura.io/pricing](https://infura.io/pricing) (100K req/day)
|
|
305
322
|
- **Chainstack Free:** [chainstack.com/pricing](https://chainstack.com/pricing) (3M req/month)
|
|
306
323
|
- **Public RPCs List:** [chainlist.org](https://chainlist.org) (community endpoints)
|
|
@@ -312,6 +329,7 @@ const provider = new ethers.providers.JsonRpcProvider(
|
|
|
312
329
|
## Usage
|
|
313
330
|
|
|
314
331
|
The flash loan agent automatically activates when you discuss:
|
|
332
|
+
|
|
315
333
|
- Flash loan strategies and simulations
|
|
316
334
|
- DeFi arbitrage opportunities
|
|
317
335
|
- Liquidation strategies
|
|
@@ -382,6 +400,7 @@ Create a `.flashloan-config.json` file:
|
|
|
382
400
|
## Risk Warnings
|
|
383
401
|
|
|
384
402
|
️ **Critical Considerations**:
|
|
403
|
+
|
|
385
404
|
- Smart contract code must be thoroughly audited
|
|
386
405
|
- Gas costs can eliminate profitability
|
|
387
406
|
- Front-running by MEV bots is common
|
|
@@ -404,7 +423,7 @@ MIT License - See LICENSE file for details
|
|
|
404
423
|
## Support
|
|
405
424
|
|
|
406
425
|
- GitHub Issues: [Report bugs](https://github.com/jeremylongshore/claude-code-plugins/issues)
|
|
407
|
-
- Documentation:
|
|
426
|
+
- Documentation: Full docs
|
|
408
427
|
|
|
409
428
|
---
|
|
410
429
|
|
|
@@ -10,6 +10,7 @@ You are a specialized agent for simulating flash loan strategies, analyzing DeFi
|
|
|
10
10
|
## Your Capabilities
|
|
11
11
|
|
|
12
12
|
### Flash Loan Simulation
|
|
13
|
+
|
|
13
14
|
- Simulate flash loan transactions across Aave, dYdX, Uniswap V3, and Balancer
|
|
14
15
|
- Calculate optimal loan amounts for various strategies
|
|
15
16
|
- Model multi-step transactions with gas costs
|
|
@@ -17,6 +18,7 @@ You are a specialized agent for simulating flash loan strategies, analyzing DeFi
|
|
|
17
18
|
- Test strategies against historical data
|
|
18
19
|
|
|
19
20
|
### Arbitrage Analysis
|
|
21
|
+
|
|
20
22
|
- **DEX arbitrage**: Identify price discrepancies across Uniswap, SushiSwap, Curve, Balancer
|
|
21
23
|
- **Liquidation arbitrage**: Simulate profitable liquidations on lending protocols
|
|
22
24
|
- **Collateral swap**: Optimize position refinancing across protocols
|
|
@@ -24,6 +26,7 @@ You are a specialized agent for simulating flash loan strategies, analyzing DeFi
|
|
|
24
26
|
- **Cross-chain arbitrage**: Simulate bridge-based arbitrage (with flash loans)
|
|
25
27
|
|
|
26
28
|
### Strategy Types
|
|
29
|
+
|
|
27
30
|
1. **Simple Arbitrage**: Buy low on DEX A, sell high on DEX B
|
|
28
31
|
2. **Liquidation**: Flash loan to liquidate undercollateralized positions
|
|
29
32
|
3. **Collateral Swap**: Refinance positions at better rates
|
|
@@ -32,6 +35,7 @@ You are a specialized agent for simulating flash loan strategies, analyzing DeFi
|
|
|
32
35
|
6. **Wash Trading Prevention**: Analyze for circular trading patterns
|
|
33
36
|
|
|
34
37
|
### Risk Analysis
|
|
38
|
+
|
|
35
39
|
- Gas cost modeling with EIP-1559 dynamics
|
|
36
40
|
- Slippage estimation based on liquidity depth
|
|
37
41
|
- Front-running risk assessment
|
|
@@ -42,6 +46,7 @@ You are a specialized agent for simulating flash loan strategies, analyzing DeFi
|
|
|
42
46
|
## When to Activate
|
|
43
47
|
|
|
44
48
|
Activate this agent when users need to:
|
|
49
|
+
|
|
45
50
|
- Simulate flash loan strategies before execution
|
|
46
51
|
- Analyze DEX arbitrage opportunities
|
|
47
52
|
- Calculate optimal liquidation strategies
|
|
@@ -54,6 +59,7 @@ Activate this agent when users need to:
|
|
|
54
59
|
## Approach
|
|
55
60
|
|
|
56
61
|
### Simulation Methodology
|
|
62
|
+
|
|
57
63
|
1. **Strategy Definition**: Define the flash loan strategy and steps
|
|
58
64
|
2. **Data Collection**: Gather current prices, liquidity, and protocol parameters
|
|
59
65
|
3. **Transaction Modeling**: Build the multi-step transaction flow
|
|
@@ -63,7 +69,9 @@ Activate this agent when users need to:
|
|
|
63
69
|
7. **Optimization**: Suggest improvements to maximize profitability
|
|
64
70
|
|
|
65
71
|
### Output Format
|
|
72
|
+
|
|
66
73
|
Present simulations in structured format:
|
|
74
|
+
|
|
67
75
|
```
|
|
68
76
|
FLASH LOAN STRATEGY SIMULATION
|
|
69
77
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
@@ -149,6 +157,7 @@ contract FlashLoanArbitrage {
|
|
|
149
157
|
}
|
|
150
158
|
}
|
|
151
159
|
```
|
|
160
|
+
|
|
152
161
|
```
|
|
153
162
|
|
|
154
163
|
## Flash Loan Providers
|
|
@@ -192,22 +201,30 @@ Always include comprehensive risk warnings:
|
|
|
192
201
|
|
|
193
202
|
### 1. Simple DEX Arbitrage
|
|
194
203
|
```
|
|
204
|
+
|
|
195
205
|
Borrow 1000 ETH → Buy USDC on Uniswap → Sell USDC on SushiSwap → Repay ETH
|
|
206
|
+
|
|
196
207
|
```
|
|
197
208
|
|
|
198
209
|
### 2. Liquidation on Aave
|
|
199
210
|
```
|
|
211
|
+
|
|
200
212
|
Borrow collateral asset → Liquidate undercollateralized position → Sell collateral → Repay loan
|
|
213
|
+
|
|
201
214
|
```
|
|
202
215
|
|
|
203
216
|
### 3. Collateral Swap
|
|
204
217
|
```
|
|
218
|
+
|
|
205
219
|
Borrow new collateral → Deposit to protocol → Withdraw old collateral → Swap → Repay loan
|
|
220
|
+
|
|
206
221
|
```
|
|
207
222
|
|
|
208
223
|
### 4. Triangular Arbitrage
|
|
209
224
|
```
|
|
225
|
+
|
|
210
226
|
Borrow ETH → ETH to USDC → USDC to DAI → DAI to ETH (at profit) → Repay
|
|
227
|
+
|
|
211
228
|
```
|
|
212
229
|
|
|
213
230
|
## Simulation Tools
|
package/package.json
CHANGED
|
@@ -60,33 +60,43 @@ The flash loan simulator uses a **strategy-protocol abstraction pattern** where
|
|
|
60
60
|
## Progressive Disclosure Strategy
|
|
61
61
|
|
|
62
62
|
### Level 1: Quick Profitability Check (Default)
|
|
63
|
+
|
|
63
64
|
```bash
|
|
64
65
|
python flash_simulator.py arbitrage ETH USDC 100 --provider aave
|
|
65
66
|
```
|
|
67
|
+
|
|
66
68
|
Output: Simple profit/loss with recommendation.
|
|
67
69
|
|
|
68
70
|
### Level 2: Detailed Breakdown
|
|
71
|
+
|
|
69
72
|
```bash
|
|
70
73
|
python flash_simulator.py arbitrage ETH USDC 100 --breakdown
|
|
71
74
|
```
|
|
75
|
+
|
|
72
76
|
Output: Step-by-step costs and revenues.
|
|
73
77
|
|
|
74
78
|
### Level 3: Multi-Provider Comparison
|
|
79
|
+
|
|
75
80
|
```bash
|
|
76
81
|
python flash_simulator.py arbitrage ETH USDC 100 --compare-providers
|
|
77
82
|
```
|
|
83
|
+
|
|
78
84
|
Output: Aave vs dYdX vs Balancer comparison.
|
|
79
85
|
|
|
80
86
|
### Level 4: Risk Analysis
|
|
87
|
+
|
|
81
88
|
```bash
|
|
82
89
|
python flash_simulator.py arbitrage ETH USDC 100 --risk-analysis
|
|
83
90
|
```
|
|
91
|
+
|
|
84
92
|
Output: MEV competition, execution risk, protocol risk scores.
|
|
85
93
|
|
|
86
94
|
### Level 5: Full Simulation
|
|
95
|
+
|
|
87
96
|
```bash
|
|
88
97
|
python flash_simulator.py arbitrage ETH USDC 100 --full --output json
|
|
89
98
|
```
|
|
99
|
+
|
|
90
100
|
Output: Complete analysis with all details.
|
|
91
101
|
|
|
92
102
|
## Tool Permission Strategy
|
|
@@ -96,11 +106,13 @@ allowed-tools: Read, Write, Edit, Grep, Glob, Bash(crypto:flashloan-*)
|
|
|
96
106
|
```
|
|
97
107
|
|
|
98
108
|
### Rationale
|
|
109
|
+
|
|
99
110
|
- **Read/Write/Edit**: Configuration and output management
|
|
100
111
|
- **Grep/Glob**: Search for contract addresses and ABIs
|
|
101
112
|
- **Bash(crypto:flashloan-*)**: Scoped to flash loan simulation scripts
|
|
102
113
|
|
|
103
114
|
### Prohibited
|
|
115
|
+
|
|
104
116
|
- No unrestricted Bash execution
|
|
105
117
|
- No private key or wallet access
|
|
106
118
|
- No actual transaction signing
|
|
@@ -161,7 +173,6 @@ class FlashLoanProvider(ABC):
|
|
|
161
173
|
"""Simulate flash loan execution."""
|
|
162
174
|
pass
|
|
163
175
|
|
|
164
|
-
|
|
165
176
|
class AaveV3Provider(FlashLoanProvider):
|
|
166
177
|
"""Aave V3 flash loan provider adapter."""
|
|
167
178
|
|
|
@@ -176,7 +187,6 @@ class AaveV3Provider(FlashLoanProvider):
|
|
|
176
187
|
def get_fee(self, asset: str, amount: Decimal) -> Decimal:
|
|
177
188
|
return amount * self.FEE_RATE
|
|
178
189
|
|
|
179
|
-
|
|
180
190
|
class DydxProvider(FlashLoanProvider):
|
|
181
191
|
"""dYdX flash loan provider adapter (0% fee)."""
|
|
182
192
|
|
|
@@ -187,7 +197,6 @@ class DydxProvider(FlashLoanProvider):
|
|
|
187
197
|
def get_fee(self, asset: str, amount: Decimal) -> Decimal:
|
|
188
198
|
return Decimal("0") # dYdX has 0% flash loan fee
|
|
189
199
|
|
|
190
|
-
|
|
191
200
|
class BalancerProvider(FlashLoanProvider):
|
|
192
201
|
"""Balancer flash loan provider adapter."""
|
|
193
202
|
|
|
@@ -210,7 +219,6 @@ class FlashLoanStrategy(ABC):
|
|
|
210
219
|
"""Calculate net profit after all costs."""
|
|
211
220
|
pass
|
|
212
221
|
|
|
213
|
-
|
|
214
222
|
class SimpleArbitrageStrategy(FlashLoanStrategy):
|
|
215
223
|
"""Two-DEX arbitrage: buy low, sell high."""
|
|
216
224
|
|
|
@@ -222,7 +230,6 @@ class SimpleArbitrageStrategy(FlashLoanStrategy):
|
|
|
222
230
|
# 5. Keep profit
|
|
223
231
|
pass
|
|
224
232
|
|
|
225
|
-
|
|
226
233
|
class LiquidationStrategy(FlashLoanStrategy):
|
|
227
234
|
"""Liquidate undercollateralized positions."""
|
|
228
235
|
|
|
@@ -342,12 +349,11 @@ class SimulationError(Exception):
|
|
|
342
349
|
self.recoverable = recoverable
|
|
343
350
|
self.hint = hint
|
|
344
351
|
|
|
345
|
-
|
|
346
352
|
class RPCManager:
|
|
347
353
|
"""RPC connection manager with automatic fallback."""
|
|
348
354
|
|
|
349
355
|
PROVIDERS = [
|
|
350
|
-
"
|
|
356
|
+
"eth",
|
|
351
357
|
"https://eth.llamarpc.com",
|
|
352
358
|
"https://ethereum.publicnode.com",
|
|
353
359
|
]
|
|
@@ -404,17 +410,20 @@ python flash_simulator.py --opportunities opportunities.json --simulate-all
|
|
|
404
410
|
## Testing Strategy
|
|
405
411
|
|
|
406
412
|
### Unit Tests
|
|
413
|
+
|
|
407
414
|
- Protocol adapter fee calculations
|
|
408
415
|
- Profit calculation math
|
|
409
416
|
- Risk scoring algorithms
|
|
410
417
|
- Gas estimation accuracy
|
|
411
418
|
|
|
412
419
|
### Integration Tests
|
|
420
|
+
|
|
413
421
|
- RPC connectivity (mocked)
|
|
414
422
|
- Multi-provider comparison
|
|
415
423
|
- Strategy composition
|
|
416
424
|
|
|
417
425
|
### Acceptance Tests
|
|
426
|
+
|
|
418
427
|
- Simulation matches historical actual profits (±10%)
|
|
419
428
|
- All strategy types complete without error
|
|
420
429
|
- Free RPC tier sufficient for basic usage
|
|
@@ -422,12 +431,14 @@ python flash_simulator.py --opportunities opportunities.json --simulate-all
|
|
|
422
431
|
## Security & Compliance
|
|
423
432
|
|
|
424
433
|
### Safety Measures
|
|
434
|
+
|
|
425
435
|
- **No execution**: Simulation only, never signs transactions
|
|
426
436
|
- **No private keys**: Tool cannot access wallets
|
|
427
437
|
- **Read-only RPC**: Only view functions, no state changes
|
|
428
438
|
- **Educational disclaimer**: Clear warnings about real risks
|
|
429
439
|
|
|
430
440
|
### Data Handling
|
|
441
|
+
|
|
431
442
|
- No sensitive data storage
|
|
432
443
|
- API keys in environment variables only
|
|
433
444
|
- No transaction broadcasting
|
|
@@ -435,6 +446,7 @@ python flash_simulator.py --opportunities opportunities.json --simulate-all
|
|
|
435
446
|
## Dependencies
|
|
436
447
|
|
|
437
448
|
### Python Packages
|
|
449
|
+
|
|
438
450
|
```
|
|
439
451
|
web3>=6.0.0 # Ethereum interaction
|
|
440
452
|
httpx>=0.24.0 # Async HTTP client
|
|
@@ -443,11 +455,13 @@ rich>=13.0 # Terminal formatting
|
|
|
443
455
|
```
|
|
444
456
|
|
|
445
457
|
### External Services
|
|
458
|
+
|
|
446
459
|
- Free RPC endpoints (Ankr, Chainstack free tier, Infura free tier)
|
|
447
460
|
- DeFiLlama API (protocol TVL)
|
|
448
461
|
- Etherscan API (gas oracle)
|
|
449
462
|
|
|
450
463
|
### Protocol Contracts (Read-Only)
|
|
464
|
+
|
|
451
465
|
- Aave V3 Pool
|
|
452
466
|
- dYdX Solo Margin
|
|
453
467
|
- Balancer Vault
|
|
@@ -20,18 +20,21 @@ Flash loans are powerful DeFi primitives that allow borrowing any amount without
|
|
|
20
20
|
## Target Users
|
|
21
21
|
|
|
22
22
|
### Persona 1: DeFi Developer
|
|
23
|
+
|
|
23
24
|
- **Profile**: Building flash loan arbitrage bots or liquidation systems
|
|
24
25
|
- **Pain Points**: Testing on mainnet is expensive; needs simulation before deployment
|
|
25
26
|
- **Goals**: Validate profitability; optimize gas usage; identify edge cases
|
|
26
27
|
- **Usage Pattern**: Daily simulation runs during development cycle
|
|
27
28
|
|
|
28
29
|
### Persona 2: Arbitrage Researcher
|
|
30
|
+
|
|
29
31
|
- **Profile**: Analyzes DeFi protocols for arbitrage opportunities
|
|
30
32
|
- **Pain Points**: Manual calculation of multi-hop arbitrage is error-prone
|
|
31
33
|
- **Goals**: Quickly evaluate opportunities; backtest historical scenarios
|
|
32
34
|
- **Usage Pattern**: Continuous opportunity scanning and analysis
|
|
33
35
|
|
|
34
36
|
### Persona 3: Smart Contract Auditor
|
|
37
|
+
|
|
35
38
|
- **Profile**: Reviews DeFi protocols for vulnerabilities
|
|
36
39
|
- **Pain Points**: Needs to simulate attack vectors without executing them
|
|
37
40
|
- **Goals**: Understand flash loan attack scenarios; verify protocol safety
|
|
@@ -63,13 +66,13 @@ Flash loans are powerful DeFi primitives that allow borrowing any amount without
|
|
|
63
66
|
|
|
64
67
|
### Important (P1)
|
|
65
68
|
|
|
66
|
-
|
|
69
|
+
1. **As a researcher**, I want to simulate triangular arbitrage, so that I can evaluate multi-hop strategies.
|
|
67
70
|
- **Acceptance Criteria**:
|
|
68
71
|
- Support 3+ DEX paths
|
|
69
72
|
- Calculate cumulative slippage
|
|
70
73
|
- Show optimal route and amounts
|
|
71
74
|
|
|
72
|
-
|
|
75
|
+
2. **As a developer**, I want to backtest strategies against historical data, so that I can validate my approach.
|
|
73
76
|
- **Acceptance Criteria**:
|
|
74
77
|
- Input: Strategy parameters, date range
|
|
75
78
|
- Output: Historical profit/loss per opportunity
|
|
@@ -77,7 +80,7 @@ Flash loans are powerful DeFi primitives that allow borrowing any amount without
|
|
|
77
80
|
|
|
78
81
|
### Nice-to-Have (P2)
|
|
79
82
|
|
|
80
|
-
|
|
83
|
+
1. **As an auditor**, I want to simulate flash loan attack scenarios, so that I can test protocol security.
|
|
81
84
|
- **Acceptance Criteria**:
|
|
82
85
|
- Template attack patterns (price manipulation, governance, etc.)
|
|
83
86
|
- Safe simulation without actual execution
|
|
@@ -196,12 +199,14 @@ Flash loans are powerful DeFi primitives that allow borrowing any amount without
|
|
|
196
199
|
## Constraints & Assumptions
|
|
197
200
|
|
|
198
201
|
### Constraints
|
|
202
|
+
|
|
199
203
|
- Free RPC endpoints have rate limits (25-100 req/sec)
|
|
200
204
|
- Historical simulations require archive node access
|
|
201
205
|
- Gas estimates are approximations (actual may vary ±20%)
|
|
202
206
|
- Price data has latency (not real-time MEV-competitive)
|
|
203
207
|
|
|
204
208
|
### Assumptions
|
|
209
|
+
|
|
205
210
|
- User understands DeFi and flash loan concepts
|
|
206
211
|
- User has access to RPC endpoint (free or paid)
|
|
207
212
|
- Simulation results are for analysis, not execution
|
|
@@ -227,6 +232,7 @@ Flash loans are powerful DeFi primitives that allow borrowing any amount without
|
|
|
227
232
|
- Protocol exploits may have legal implications
|
|
228
233
|
|
|
229
234
|
Users should:
|
|
235
|
+
|
|
230
236
|
- Never deploy unaudited code
|
|
231
237
|
- Start with testnets before mainnet
|
|
232
238
|
- Understand the risks fully
|
|
@@ -1,16 +1,23 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: simulating-flash-loans
|
|
3
|
-
description:
|
|
4
|
-
|
|
5
|
-
Use when simulating flash loans, analyzing arbitrage profitability, evaluating liquidation opportunities, or comparing flash loan providers.
|
|
6
|
-
Trigger with phrases like "simulate flash loan", "flash loan arbitrage", "liquidation profit", "compare Aave dYdX", "flash loan strategy", or "DeFi arbitrage simulation".
|
|
3
|
+
description: 'Simulate flash loan strategies with profitability calculations and risk
|
|
4
|
+
assessment across Aave, dYdX, and Balancer.
|
|
7
5
|
|
|
6
|
+
Use when simulating flash loans, analyzing arbitrage profitability, evaluating liquidation
|
|
7
|
+
opportunities, or comparing flash loan providers.
|
|
8
|
+
|
|
9
|
+
Trigger with phrases like "simulate flash loan", "flash loan arbitrage", "liquidation
|
|
10
|
+
profit", "compare Aave dYdX", "flash loan strategy", or "DeFi arbitrage simulation".
|
|
11
|
+
|
|
12
|
+
'
|
|
8
13
|
allowed-tools: Read, Write, Edit, Grep, Glob, Bash(crypto:flashloan-*)
|
|
9
14
|
version: 1.0.0
|
|
10
15
|
author: Jeremy Longshore <jeremy@intentsolutions.io>
|
|
11
16
|
license: MIT
|
|
12
|
-
|
|
13
|
-
|
|
17
|
+
tags:
|
|
18
|
+
- crypto
|
|
19
|
+
- simulating-flash
|
|
20
|
+
compatibility: Designed for Claude Code, also compatible with Codex and OpenClaw
|
|
14
21
|
---
|
|
15
22
|
# Simulating Flash Loans
|
|
16
23
|
|
|
@@ -32,29 +39,40 @@ Simulate flash loan strategies across Aave V3, dYdX, and Balancer with profitabi
|
|
|
32
39
|
## Instructions
|
|
33
40
|
|
|
34
41
|
1. Simulate a two-DEX arbitrage with automatic fee and gas calculation:
|
|
42
|
+
|
|
35
43
|
```bash
|
|
36
44
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py arbitrage ETH USDC 100 \
|
|
37
45
|
--dex-buy uniswap --dex-sell sushiswap
|
|
38
46
|
```
|
|
47
|
+
|
|
39
48
|
2. Compare flash loan providers to find the cheapest for your strategy:
|
|
49
|
+
|
|
40
50
|
```bash
|
|
41
51
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py arbitrage ETH USDC 100 --compare-providers
|
|
42
52
|
```
|
|
53
|
+
|
|
43
54
|
3. Analyze liquidation profitability on lending protocols:
|
|
55
|
+
|
|
44
56
|
```bash
|
|
45
57
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py liquidation \
|
|
46
58
|
--protocol aave --health-factor 0.95
|
|
47
59
|
```
|
|
60
|
+
|
|
48
61
|
4. Simulate triangular arbitrage with multi-hop circular paths:
|
|
62
|
+
|
|
49
63
|
```bash
|
|
50
64
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py triangular \
|
|
51
65
|
ETH USDC WBTC ETH --amount 50
|
|
52
66
|
```
|
|
67
|
+
|
|
53
68
|
5. Add risk assessment (MEV competition, execution, protocol, liquidity) to any simulation:
|
|
69
|
+
|
|
54
70
|
```bash
|
|
55
71
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py arbitrage ETH USDC 100 --risk-analysis
|
|
56
72
|
```
|
|
73
|
+
|
|
57
74
|
6. Run full analysis combining all features:
|
|
75
|
+
|
|
58
76
|
```bash
|
|
59
77
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py arbitrage ETH USDC 100 \
|
|
60
78
|
--full --output json > simulation.json
|
|
@@ -83,17 +101,20 @@ See `${CLAUDE_SKILL_DIR}/references/errors.md` for comprehensive error handling.
|
|
|
83
101
|
## Examples
|
|
84
102
|
|
|
85
103
|
**Basic arbitrage simulation:**
|
|
104
|
+
|
|
86
105
|
```bash
|
|
87
106
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py arbitrage ETH USDC 100 \
|
|
88
107
|
--dex-buy uniswap --dex-sell sushiswap
|
|
89
108
|
```
|
|
90
109
|
|
|
91
110
|
**Find cheapest provider:**
|
|
111
|
+
|
|
92
112
|
```bash
|
|
93
113
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py arbitrage ETH USDC 100 --compare-providers
|
|
94
114
|
```
|
|
95
115
|
|
|
96
116
|
**Liquidation opportunity scan:**
|
|
117
|
+
|
|
97
118
|
```bash
|
|
98
119
|
python ${CLAUDE_SKILL_DIR}/scripts/flash_simulator.py liquidation --protocol aave --health-factor 0.95
|
|
99
120
|
```
|
|
@@ -104,6 +125,6 @@ See `${CLAUDE_SKILL_DIR}/references/examples.md` for multi-provider comparison a
|
|
|
104
125
|
|
|
105
126
|
- `${CLAUDE_SKILL_DIR}/references/implementation.md` - Provider comparison, strategy details, risk scoring, output modes
|
|
106
127
|
- [Aave V3 Flash Loans](https://docs.aave.com/developers/guides/flash-loans)
|
|
107
|
-
-
|
|
108
|
-
-
|
|
128
|
+
- dYdX Flash Loans
|
|
129
|
+
- Balancer Flash Loans
|
|
109
130
|
- [Flashbots Protect](https://docs.flashbots.net/flashbots-protect/overview)
|