How to Build a Crypto Arbitrage Flash Loan Bot
A flash loan arbitrage bot borrows crypto, buys low on one exchange and sells high on another, then repays the loan, all inside one transaction.
On this page
Short answer
A flash loan arbitrage bot is a smart contract that borrows funds from a lending protocol, uses them to buy an asset cheaply on one decentralised exchange and sell it higher on another, then repays the loan plus a fee, all inside a single transaction. If the trade does not cover the loan and fee, the whole transaction reverts and nothing is lost except gas.
What is a flash loan arbitrage bot?
A flash loan arbitrage bot is a smart contract that borrows crypto with no collateral, trades it for a profit, and repays the loan before the transaction ends. The loan only exists inside one transaction, so the lender takes no credit risk: either the money comes back with the fee, or the entire transaction is cancelled.
Arbitrage is the price gap itself. The same token often trades at slightly different prices on two decentralised exchanges, because each exchange sets prices from its own liquidity pool. The bot buys where the token is cheap and sells where it is dear, keeping the difference.
The bot is not a wallet and not a person. It is code that a lending protocol calls back into, and it must be a contract: a protocol cannot call a regular wallet. If you want the background first, what flash loans are explains the mechanism in plain terms.
How does a flash loan arbitrage bot work?
A flash loan arbitrage bot works in five steps that all run inside one transaction, in a fixed order enforced by the lending protocol. The pool lends first, your code trades second, and the pool checks repayment last.
| Step | What happens | Who acts |
|---|---|---|
| 1. Request | Your contract calls flashLoan on the pool | Your contract |
| 2. Lend | Pool sends the borrowed tokens to your contract | Lending pool |
| 3. Callback | Pool calls executeOperation on your contract | Lending pool |
| 4. Trade | Your code buys low on one DEX, sells high on another | Your contract |
| 5. Repay | Your contract approves the pool to pull back loan plus fee | Your contract |
After your callback returns, the pool checks its balance. If it has grown by at least the loan plus the fee, the transaction confirms. If not, everything reverts, including the swaps. That check is what makes the loan safe for the lender and what makes a failed trade cost you only gas.
What do you need before you build?
Before you build, you need a development setup, a test network with lending and exchange contracts, and a clear picture of the costs. Most of the work is testing, not writing the contract itself.
| Item | What to use | Why it matters |
|---|---|---|
| Language | Solidity | The language of EVM lending and DEX contracts |
| Framework | Foundry or Hardhat | Local chain, tests and deployment scripts |
| Lending pool | Aave V3 on a testnet | Flash loans with a 0.05% premium |
| Exchanges | Two DEXs with the same pair | The price gap is the whole strategy |
| Price feed | Off-chain scan plus on-chain check | Find the gap before you pay gas |
| Wallet | A funded testnet wallet | Pays gas for deployment and tests |
Aave V3 charges a 0.05% premium on flash loans, down from 0.09% on V2. That fee is set by governance and can change, so read it from the pool at runtime instead of hardcoding it. Your other costs are gas and slippage on the two swaps, and both come out of the same profit.
How do you build the bot, step by step?
You build the bot as one contract with a callback, a swap path and a repayment check, then you test it on a fork of the live chain before it ever touches real money.
- Write the receiver contract. Implement the flash loan receiver interface with an executeOperation callback that holds your trade logic.
- Request the loan. Call flashLoan on the pool with the asset, the amount and your contract as the receiver.
- Code the two swaps. In the callback, swap the borrowed asset for the target token on the cheaper DEX, then swap it back on the dearer one.
- Check the profit before repaying. Require that the balance after the swaps covers the loan, the fee and your minimum profit, or revert early and save gas.
- Approve repayment. Approve the pool to pull the loan plus the fee, then return from the callback.
- Test on a mainnet fork. Run the contract against real pool and DEX state on a local fork, with real prices and real slippage.
- Audit, then deploy small. Get a smart contract audit of the contract, deploy it, and run small trades first while you watch gas and reverts.
Keep the contract narrow. One pair, two exchanges, one strategy. A contract that tries to handle many pairs and many venues is harder to test and harder to audit, and every extra path is a place for a bug to hide.
Example (hypothetical): one arbitrage trade, worked out
Example (hypothetical): your bot borrows 100,000 USDC from Aave V3 and spots ether at $3,000 on one DEX and $3,030 on another. It buys ether with the loan, then sells it back for USDC on the second exchange.
The arithmetic: 100,000 divided by 3,000 gives 33.3333 ether. Selling 33.3333 ether at $3,030 gives about 101,000 USDC, so the gross profit is about 1,000 USDC. The Aave fee is 0.05% of 100,000, which is 50 USDC. If gas costs 30 USDC, the net profit is 1,000 minus 50 minus 30, which is 920 USDC.
If the price gap closes while the transaction is waiting, the second swap returns less than the loan plus the fee, the repayment check fails and the whole transaction reverts. You lose the 30 USDC of gas and nothing else. That is the normal outcome for most attempted trades, which is why the bot must check the gap off-chain first.
What are the common mistakes?
The most common mistakes are trading on stale prices, ignoring slippage, and assuming a gap that looks good on paper survives the cost of the loan and gas.
- Trusting an off-chain price. The gap you saw a minute ago may be gone by the time your transaction is mined. Re-check the price inside the contract and revert if it moved.
- Forgetting slippage. A large swap moves the pool price against you. Size the trade so the swap itself does not eat the gap.
- Hardcoding the fee. The flash loan premium is a governance parameter. Read it from the pool each time.
- No minimum profit check. Without a require that the balance covers loan, fee and a floor, you can pay gas for trades that earn almost nothing.
- Skipping the fork test. Unit tests with fake prices prove nothing about real pools. Test against a fork of the live chain.
- Leaving the contract open. Anyone can call a public function. Restrict who can trigger the bot and who can withdraw funds.
How do you run the bot safely?
You run the bot safely by keeping it small, watched and capped, and by treating every deployment as a contract that holds real funds and needs the same care as any other financial code.
Start with a single pair on one chain, with a cap on the loan size and a daily gas budget. Log every attempt, every revert and every profit, so you can see whether the bot earns more than it spends on gas. Most bots do not, and the logs are what tell you.
Keep the contract upgradable only through a timelock or a multisig, and never leave an owner key on a hot wallet. If the strategy stops working, pause it and withdraw the funds rather than letting it keep paying gas. For teams building this as a product, DeFi development covers the lending, swap and audit work around the bot itself.
Key takeaways
- A flash loan arbitrage bot borrows, trades and repays inside one transaction, or the whole trade reverts
- Aave V3 charges a 0.05% premium; gas and slippage come out of the same profit
- The contract must be a receiver with an executeOperation callback, not a wallet
- Check the price gap and the minimum profit inside the contract before repaying
- Test on a mainnet fork with real prices, then audit and start small
Frequently asked questions
Do I need collateral to use a flash loan?
No. A flash loan needs no collateral because it is borrowed and repaid inside the same transaction. The lending protocol checks its balance at the end of the transaction, and if the loan plus the fee is not back, the whole transaction reverts as if it never happened.
How much does a flash loan cost on Aave?
Aave V3 charges a premium of 0.05% of the borrowed amount, down from 0.09% on V2. You also pay network gas for the transaction and slippage on any swaps, so the real cost is the fee plus gas plus slippage, all taken from the trade profit.
Can a flash loan arbitrage bot lose money?
Yes, it can lose gas. When a trade fails to cover the loan and fee, the transaction reverts and you lose only the gas you paid. A bot that submits many losing trades can spend more on gas than it ever earns, which is the most common way these bots lose money.
Which exchanges do flash loan arbitrage bots use?
Any two decentralised exchanges that list the same trading pair with different prices, such as Uniswap and SushiSwap on the same chain. The gap comes from each exchange pricing from its own liquidity pool, so the pair and the chain matter more than the brand names.
Do I need a smart contract, or can I use a wallet?
You need a smart contract. The lending pool calls a callback function on your contract while it holds the funds, and a regular wallet has no code to call. The contract requests the loan, runs the swaps and approves repayment in that callback.
Is flash loan arbitrage still profitable?
It can be, but only for small, fast gaps that survive the fee, gas and slippage. Large gaps are usually taken by faster bots within seconds, so a new bot needs cheap gas, a fast price scan and a strict minimum profit check to earn anything at all.