Smart Contract para solicitar, recibir y gestionar Flash Loans en Aave V3
Operación atómica completa: pedir prestado → arbitrar → devolver → beneficio Todo dentro de un único bloque de transacción.
El Agente (este contrato) va al banco central (Aave), pide prestada una suma millonaria sin dejar ningún aval, cruza la calle hacia la casa de cambio (Uniswap) para ejecutar una operación mercantil, vuelve al banco, devuelve exactamente lo que pidió más una comisión mínima (0.09%), y guarda la ganancia.
La Regla de Oro: tiene que hacer absolutamente todo esto antes de que el reloj avance un solo segundo (dentro de un único bloque). Si no puede devolver el dinero, el banco viaja en el tiempo y actúa como si nunca hubiera ocurrido (revert).
Este repositorio contiene un Smart Contract desarrollado en Solidity diseñado para solicitar, recibir y gestionar préstamos relámpago (Flash Loans) interactuando directamente con el protocolo Aave V3.
El contrato inteligente (FlashLoanBot.sol) opera en la versión ^0.8.10 de Solidity y utiliza el framework Foundry.
La arquitectura del contrato implementa las siguientes características:
- Herencia de Protocolo: Hereda de
FlashLoanSimpleReceiverBase, el contrato base oficial de Aave V3. Esto le otorga la capacidad nativa de recibir préstamos delegados a través de la funciónexecuteOperation. - Integración de Interfaces: Define la interfaz
ISwapRouterpara permitir la comunicación cruzada con protocolos de intercambio (específicamente Uniswap V3, utilizandoexactInputSingle). - Dependencias Externas: Importa librerías estándar de
aave-v3-corepara la gestión de direcciones del pool yopenzeppelinpara el manejo seguro de tokens estándar ERC20. - Configuración de Red (Hardcoded): Contiene variables constantes con las direcciones reales de la red principal de Ethereum (Mainnet) para el enrutador de Uniswap, WETH y DAI.
- Inicialización: El constructor requiere la dirección del
PoolAddressesProviderde Aave en el momento del despliegue para vincularse correctamente al pool de liquidez principal.
Para comprender el papel de este contrato inteligente dentro de la blockchain, utilizaremos la analogía de un agente financiero exprés:
- El Agente (Este Contrato en Solidity): Su única misión es ir al banco central (Aave), pedir una suma millonaria de dinero prestado sin dejar ningún tipo de aval, cruzar la calle hacia la casa de cambio (Uniswap) para ejecutar una operación mercantil, volver al banco, devolver exactamente lo que pidió más una comisión mínima, y guardar la ganancia.
- La Regla de Oro (Flash Loan): La magia técnica reside en que el Agente tiene que hacer absolutamente todo esto antes de que el reloj del banco avance un solo segundo (dentro de un único bloque de transacciones). Si al terminar la operación no tiene el dinero suficiente para devolverle al banco, el banco viaja en el tiempo y hace como si el Agente nunca hubiera pedido el dinero (revirtiendo la transacción completa).
Dado que este contrato interactúa con las direcciones reales de Aave V3 y Uniswap V3 en la red principal de Ethereum, las pruebas y el desarrollo deben realizarse en un entorno bifurcado (fork).
- Asegúrate de tener configurado Foundry en tu entorno.
- Para simular o probar este contrato, necesitas levantar un nodo local bifurcando la Mainnet utilizando tu URL de Alchemy:
anvil --fork-url [https://eth-mainnet.g.alchemy.com/v2/TU_API_KEY](https://eth-mainnet.g.alchemy.com/v2/TU_API_KEY)
- Despliega el contrato en tu red local pasando la dirección del
PoolAddressesProviderde Aave V3 en Ethereum.
TÚ (EOA)
│
│ solicitarPrestamo(token, cantidad)
▼
┌─────────────────┐
│ FlashLoanBot │
│ (este contrato)│
└────────┬────────┘
│ POOL.flashLoanSimple()
▼
┌─────────────────┐
│ Aave V3 │◀─── ingresa fondos al contrato
│ (el banco) │
└────────┬────────┘
│ executeOperation() ← llamado automáticamente por Aave
▼
┌─────────────────┐
│ TUS OPERACIONES│ ← arbitraje, liquidaciones, etc.
│ DE ARBITRAJE │
└────────┬────────┘
│ approve(amount + premium)
▼
┌─────────────────┐
│ Aave V3 │◀─── se cobra automáticamente
│ (cobra + fee) │
└─────────────────┘
│
▼
✅ Beneficio retenido en el contrato
| Parámetro | Valor |
|---|---|
| Lenguaje | Solidity ^0.8.10 |
| Framework | Foundry |
| Protocolo de préstamo | Aave V3 |
| Protocolo de swap | Uniswap V3 |
| Red objetivo | Ethereum Mainnet |
| Comisión Flash Loan | 0.09% del capital |
| Par por defecto | WETH / DAI |
aave-v3-core
├── FlashLoanSimpleReceiverBase.sol ← clase base del receptor
├── IPoolAddressesProvider.sol ← dirección del pool de Aave
└── IERC20.sol ← manejo seguro de tokens ERC20
Uniswap V3
└── ISwapRouter ← interfaz para exactInputSingle
Flash_Loans/
├── 05_FlashLoanAgent/
│ ├── FlashLoanBot.sol # Contrato principal
│ ├── foundry.toml # Configuración de Foundry
│ └── test/ # Tests en entorno fork
└── README.md
Como el contrato usa las direcciones reales de Aave V3 y Uniswap V3 en Mainnet, las pruebas deben hacerse en un entorno bifurcado local.
1. Instalar Foundry
curl -L https://foundry.paradigm.xyz | bash
foundryup2. Levantar nodo local bifurcando Ethereum Mainnet
anvil --fork-url https://eth-mainnet.g.alchemy.com/v2/TU_API_KEY3. Desplegar el contrato
forge create FlashLoanBot \
--constructor-args 0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e \
--rpc-url http://localhost:8545 \
--private-key TU_PRIVATE_KEY
0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9ees la dirección delPoolAddressesProviderde Aave V3 en Ethereum Mainnet.
4. Ejecutar un Flash Loan de prueba
cast send TU_CONTRATO \
"solicitarPrestamo(address,uint256)" \
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 \
1000000000000000000 \
--rpc-url http://localhost:8545 \
--private-key TU_PRIVATE_KEY- Receptor base de Flash Loan (Aave V3)
- Integración de interfaz Uniswap V3
- Aprobación automática de devolución con fee
- Lógica de arbitraje real (WETH → DAI → WETH)
- Tests automatizados con Foundry en fork
- Cálculo de rentabilidad antes de disparar
- Integración con el radar off-chain
RealPriceBrain
Este proyecto es exclusivamente para fines educativos e investigación DeFi.
Los autores no son responsables de pérdidas financieras, incumplimientos regulatorios ni daños derivados del uso de este software. Al usarlo confirmas haber leído y aceptado estos términos.
Héctor Oviedo — Backend Developer & DeFi Researcher
The Agent (this contract) goes to the central bank (Aave), borrows a massive sum without any collateral, crosses the street to the exchange office (Uniswap) to execute a trade, returns to the bank, repays exactly what it borrowed plus a minimal fee (0.09%), and keeps the profit.
The Golden Rule: it has to do all of this before the clock advances a single second (within a single block). If it can't repay, the bank travels back in time and acts as if it never happened (revert).
This repository contains a Smart Contract developed in Solidity designed to request, receive, and manage Flash Loans by interacting directly with the Aave V3 protocol.
The smart contract (FlashLoanBot.sol) operates on Solidity version ^0.8.10 and utilizes the Foundry framework.
The contract architecture implements the following features:
- Protocol Inheritance: Inherits from
FlashLoanSimpleReceiverBase, the official base contract of Aave V3. This grants it the native capability to receive delegated loans via theexecuteOperationfunction. - Interface Integration: Defines the
ISwapRouterinterface to allow cross-communication with exchange protocols (specifically Uniswap V3, usingexactInputSingle). - External Dependencies: Imports standard libraries from
aave-v3-corefor pool address management andopenzeppelinfor the secure handling of standard ERC20 tokens. - Network Configuration (Hardcoded): Contains constant variables with the real addresses from the Ethereum Mainnet for the Uniswap router, WETH, and DAI.
- Initialization: The constructor requires the Aave
PoolAddressesProvideraddress at the time of deployment to correctly bind to the main liquidity pool.
To understand the role of this smart contract within the blockchain, we will use the analogy of an express financial agent:
- The Agent (This Solidity Contract): Its sole mission is to go to the central bank (Aave), borrow a millionaire sum of money without leaving any collateral, cross the street to the exchange office (Uniswap) to execute a trading operation, return to the bank, repay exactly what it borrowed plus a minimal fee, and keep the profit.
- The Golden Rule (Flash Loan): The technical magic lies in the fact that the Agent has to do absolutely all of this before the bank's clock advances a single second (within a single transaction block). If at the end of the operation it does not have enough money to repay the bank, the bank travels back in time and acts as if the Agent never asked for the money (reverting the entire transaction).
Since this contract interacts with the real Aave V3 and Uniswap V3 addresses on the Ethereum Mainnet, testing and development must be done in a forked environment.
- Ensure you have Foundry configured in your environment.
- To simulate or test this contract, you need to spin up a local node forking the Mainnet using your Alchemy URL:
anvil --fork-url [https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY](https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY)
- Deploy the contract on your local network by passing the Aave V3
PoolAddressesProvideraddress on Ethereum.
YOU (EOA)
│
│ solicitarPrestamo(token, amount)
▼
┌─────────────────┐
│ FlashLoanBot │
│ (this contract)│
└────────┬────────┘
│ POOL.flashLoanSimple()
▼
┌─────────────────┐
│ Aave V3 │◀─── funds deposited into contract
│ (the bank) │
└────────┬────────┘
│ executeOperation() ← auto-called by Aave
▼
┌─────────────────┐
│ YOUR ARBITRAGE │ ← swaps, liquidations, etc.
│ OPERATIONS │
└────────┬────────┘
│ approve(amount + premium)
▼
┌─────────────────┐
│ Aave V3 │◀─── auto-collects repayment
│ (collects fee) │
└─────────────────┘
│
▼
✅ Profit retained in contract
| Parameter | Value |
|---|---|
| Language | Solidity ^0.8.10 |
| Framework | Foundry |
| Lending Protocol | Aave V3 |
| Swap Protocol | Uniswap V3 |
| Target Network | Ethereum Mainnet |
| Flash Loan Fee | 0.09% of capital |
| Default Pair | WETH / DAI |
1. Install Foundry
curl -L https://foundry.paradigm.xyz | bash
foundryup2. Fork Ethereum Mainnet locally
anvil --fork-url https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY3. Deploy the contract
forge create FlashLoanBot \
--constructor-args 0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e \
--rpc-url http://localhost:8545 \
--private-key YOUR_PRIVATE_KEY4. Trigger a test Flash Loan
cast send YOUR_CONTRACT \
"solicitarPrestamo(address,uint256)" \
0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 \
1000000000000000000 \
--rpc-url http://localhost:8545 \
--private-key YOUR_PRIVATE_KEY| Setup & Build | Test PASS |
|---|---|
![]() |
![]() |
- Base Flash Loan receiver (Aave V3)
- Uniswap V3 interface integration
- Automatic repayment approval with fee
- Real arbitrage logic (WETH → DAI → WETH)
- Automated Foundry tests on fork
- Profitability check before triggering
- Off-chain integration with
RealPriceBrainradar
This project is for educational and DeFi research purposes only. The authors are not responsible for financial losses, regulatory violations, or any damages from using this software.
Héctor Oviedo — Backend Developer & DeFi Researcher

