What Are Flash Loans?
A flash loan is an uncollateralized loan that must be borrowed and repaid within the exact same transaction block on the blockchain. If the borrower cannot repay the full amount plus fees before the transaction closes, the entire operation reverts as if it never happened — zero risk to the lender, zero loss of funds.
This atomic property makes flash loans uniquely powerful for AI agents: you can temporarily control millions of dollars of capital to execute complex multi-step strategies, then repay instantly. The only cost is the 0.09% fee on the borrowed amount — there is no interest, no duration risk, and no collateral to lock up.
Flash loans were pioneered by Aave and have become a core primitive in DeFi. Purple Flea exposes Aave V3's flash loan infrastructure via a simple REST API, so your agent can execute flash loan strategies without writing a single line of Solidity.
Atomic safety: If your strategy does not generate enough profit to cover repayment, the transaction reverts automatically. Your agent loses only the gas cost — it cannot lose more than it put in.
Flash Loan Use Cases for AI Agents
Flash loans unlock strategies that require temporary access to large capital pools. Here are the three most profitable use cases autonomous agents run today.
DEX Arbitrage
Borrow $1M USDC via flash loan. Buy ETH on Uniswap where the price is slightly lower. Sell ETH on Curve where the price is slightly higher. Repay the $1M + 0.09% fee. Keep the spread. Entire operation executes atomically in one block — the price gap cannot close during execution.
Liquidation Sniping
Monitor lending protocols (Aave, Compound, MakerDAO) for underwater positions. When a position is liquidatable, flash loan the required capital, repay the borrower's debt, seize the discounted collateral, sell it, repay the flash loan, and keep the 5–8% liquidation bounty.
Collateral Swap
Convert your lending position's collateral from ETH to WBTC (or any supported token) without closing the position. Flash loan the debt amount, repay the loan to unlock collateral, deposit new collateral, reborrow to repay the flash loan — all in one atomic transaction.
How Purple Flea Flash Loans Work
Purple Flea's flash loan API abstracts the complexity of interacting with Aave V3 directly. You don't need to deploy a smart contract or know the callback interface. Instead, you describe your strategy via the API, and Purple Flea constructs and broadcasts the atomic transaction on your behalf.
Under the hood, Purple Flea's executor contract implements the IFlashLoanSimpleReceiver interface required by Aave V3. When the flash loan is initiated, Aave transfers funds to the executor, which runs your strategy steps (swaps, liquidations, deposits) and then repays Aave — all within the same transaction. If any step fails, everything reverts.
No Solidity required: Purple Flea's API handles all on-chain transaction construction. Your agent only needs to call the REST endpoint with your strategy parameters — the protocol handles the rest atomically.
Supported Tokens & Limits
Purple Flea supports the five most liquid Aave V3 markets for flash loans. Limits reflect Aave V3's available liquidity on Ethereum mainnet and are subject to market conditions.
| Token | Max Flash Loan | Fee (0.09%) | Common Use |
|---|---|---|---|
| USDC | $50,000,000 | $45,000 | Arbitrage, liquidations |
| WETH | 20,000 ETH | 18 ETH | Collateral swaps, liquidations |
| WBTC | 500 WBTC | 0.45 WBTC | Collateral swaps |
| DAI | $30,000,000 | $27,000 | Arbitrage, debt repayment |
| USDT | $20,000,000 | $18,000 | Arbitrage |
Code Examples
Basic Flash Loan Call
Initiate a flash loan and define the strategy steps your agent wants to execute with the borrowed capital.
// Purple Flea Flash Loan API — basic borrow const response = await fetch('https://api.purpleflea.com/v1/flash-loan', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.PURPLE_FLEA_API_KEY, }, body: JSON.stringify({ token: 'USDC', amount: '1000000', // $1,000,000 USDC strategy: 'arbitrage', // strategy type steps: [ { action: 'swap', dex: 'uniswap_v3', token_in: 'USDC', token_out: 'WETH', amount_in: '1000000', }, { action: 'swap', dex: 'curve', token_in: 'WETH', token_out: 'USDC', amount_in: 'all', // use all WETH received } ], min_profit_usd: '500', // revert if profit < $500 }), }); const result = await response.json(); console.log(result.profit_usd); // e.g. "1243.50" console.log(result.tx_hash); // on-chain transaction hash
Arbitrage Bot — Full Example
Monitor price feeds and execute flash loan arbitrage automatically when a profitable spread is detected.
import { PurpleFleaClient } from '@purple-flea/sdk'; const client = new PurpleFleaClient({ apiKey: process.env.PURPLE_FLEA_API_KEY }); async function scanArbitrage() { // 1. Fetch prices from both DEXes const [uniPrice, curvePrice] = await Promise.all([ client.getPrice({ dex: 'uniswap_v3', pair: 'USDC/WETH' }), client.getPrice({ dex: 'curve', pair: 'USDC/WETH' }), ]); const spread = (curvePrice - uniPrice) / uniPrice; const MIN_SPREAD = 0.002; // 0.2% minimum spread if (spread > MIN_SPREAD) { console.log(`Spread detected: ${(spread * 100).toFixed(3)}%`); // 2. Execute flash loan arbitrage const result = await client.flashLoan({ token: 'USDC', amount: '500000', // $500k for this trade strategy: 'arbitrage', steps: [ { action: 'swap', dex: 'uniswap_v3', token_in: 'USDC', token_out: 'WETH', amount_in: 'all' }, { action: 'swap', dex: 'curve', token_in: 'WETH', token_out: 'USDC', amount_in: 'all' }, ], min_profit_usd: '200', // abort if profit too low }); console.log('Profit: $' + result.profit_usd); } } // Poll every 15 seconds setInterval(scanArbitrage, 15000);
Liquidation Snipe
Monitor Aave for underwater positions and execute flash-loan-powered liquidations to earn the bounty.
// Monitor health factors and liquidate when < 1.0 const positions = await client.getLiquidatablePositions({ protocol: 'aave_v3', max_health_factor: 1.0, min_debt_usd: 10000, // only worthwhile positions }); for (const pos of positions) { const result = await client.flashLoan({ token: pos.debt_token, amount: pos.debt_amount, strategy: 'liquidate', steps: [ { action: 'liquidate', protocol: 'aave_v3', borrower: pos.borrower_address, debt_token: pos.debt_token, collateral_token: pos.collateral_token, }, { action: 'swap', // sell seized collateral for repayment dex: 'uniswap_v3', token_in: pos.collateral_token, token_out: pos.debt_token, amount_in: 'all', }, ], }); console.log(`Liquidated ${pos.borrower_address}: +$${result.profit_usd}`); }
Safety & Atomicity Guarantees
Purple Flea's flash loan API is built on Aave V3's battle-tested smart contract infrastructure. Every flash loan strategy executes atomically — either the entire sequence of steps succeeds and the loan is repaid, or the entire transaction reverts and the blockchain state is unchanged.
Purple Flea adds an additional profit guard parameter (min_profit_usd): if your strategy does not generate at least this amount in profit after fees, the transaction is deliberately reverted before broadcast. This protects agents from marginal trades where gas costs would exceed profits.
For production agents we recommend simulating every flash loan call via the simulate: true flag before execution. Simulation runs the full strategy via eth_call and returns expected profit, gas cost, and failure reason without submitting a transaction.