Flash-Loan-Smart-Contract A minimal example Solidity contract demonstrating a simple flash loan receiver using Aave v3's FlashLoanSimpleReceiverBase. This project shows how to request a flash loan and implement the repayment flow in executeOperation.
Table of contents What this is Files Requirements How it works Quick start Using Remix (quick demo) Notes & security considerations Testing & development suggestions License Contributing Author What this is A compact example showing how to request and handle a flash loan from Aave v3. It's intended for learning and experimentation (not production use).
Files SimpleFlashLoan.sol — single contract implementing a basic flash loan receiver using FlashLoanSimpleReceiverBase from Aave v3. Requirements Solidity 0.8.10 (contract pragma) An Aave v3 PoolAddressesProvider address for the network you will use Node.js + npm (for Hardhat/Foundry usage) or Remix for quickly experimenting How it works Constructor: SimpleFlashLoan(address _addressProvider) — passes the PoolAddressesProvider address to the inherited base. Request loan: fn_RequestFlashLoan(address _token, uint256 _amount) — calls POOL.flashLoanSimple(...) to request a flash loan of the given token and amount. Callback: executeOperation(address asset, uint256 amount, uint256 premium, address initiator, bytes calldata params) — called by the Aave pool after funds are transferred. This is where you implement your arbitrage/logic. The contract must approve the pool to pull the repayment: IERC20(asset).approve(address(POOL), amount + premium). Fallback: receive() allows receiving ETH. Important detail: the contract approves the pool for amount + premium before returning true in executeOperation, ensuring repayment.
Quick start Clone the repo
- Option A — Remix (fastest for experimentation)
- Open
SimpleFlashLoan.solin Remix. - Compile with Solidity 0.8.10.
- Deploy the contract with constructor argument
_addressProviderset to the Aave v3 PoolAddressesProvider address for your chosen network (get the correct provider address from Aave v3 docs for mainnet/testnet). - Call
fn_RequestFlashLoan(tokenAddress, amount)to request a loan (amount in token's smallest unit, e.g., wei for ETH).
- Option B — Hardhat / Foundry (recommended for development)
- Initialize a project (if you don't have one) and install dependencies:
- Hardhat:
npm init -y npm install --save-dev hardhat @nomiclabs/hardhat-ethers ethers npx hardhat - Foundry:
curl -L https://foundry.paradigm.xyz | bash foundryup
- Hardhat:
- Add
SimpleFlashLoan.solto your contracts folder and write a deployment script that passes the correctPoolAddressesProvideraddress. - Compile:
- Hardhat:
npx hardhat compile - Foundry:
forge build
- Hardhat:
- Deploy to a testnet (configure private key and RPC URL in env):
- Example (Hardhat):
npx hardhat run scripts/deploy.js --network goerli
- Example (Hardhat):
- Execute
fn_RequestFlashLoanthrough a script or tests.
Note: This repository does not include Hardhat/Foundry config or scripts — add them per your project preferences.
- Set the environment to the network you want (e.g., Injected Web3 to use MetaMask).
- Ensure the address you use for
_addressProvideris the Aave v3 provider for that network (consult Aave docs). - Deploy the contract, then call
fn_RequestFlashLoanwith:
_token: token address to borrow (e.g., WETH address)_amount: borrow amount (in token minimal units)
- Watch
executeOperationbe called by Aave. Implement your profit logic inexecuteOperationbefore approving repayment.
- This is an educational minimal example — it deliberately leaves
executeOperationlogic empty (comment://Logic goes here). You must implement any actual arbitrage or trade logic there. - Always ensure the contract repays
amount + premiumbeforeexecuteOperationreturns; otherwise the transaction will revert. - Approving repayment to the pool as shown is necessary for automatic pull of funds by Aave.
- Be cautious with:
- Unchecked token behavior (non-standard ERC-20s)
- Reentrancy and external calls inside
executeOperation - Handling slippage, front-running, and failed swaps during your flash loan logic
- Test thoroughly on testnets before any mainnet activity.
- Add unit/integration tests that simulate the flash loan flow (Foundry or Hardhat + a forked mainnet or a local Aave deployment).
- Use a forked mainnet in Hardhat to test interactions with Aave v3 in a deterministic environment:
- Hardhat: run tests against
--forkconfiguration or sethardhatnetwork to fork an RPC URL. - Add scripts:
- A deployment script that accepts
POOL_ADDRESSES_PROVIDERfrom env. - A script to trigger
fn_RequestFlashLoanand assert expected state afterexecuteOperation.
The Solidity source has SPDX license identifier MIT. Consider adding an explicit LICENSE file to the repository.
Contributions, corrections, and improvements are welcome. Suggested first steps:
- Add development tooling (Hardhat/Foundry config)
- Add tests and example scripts
- Provide example
PoolAddressesProvideraddresses and step-by-step demo for a specific testnet
Repository: raviwijerathna1/Flash-Loan-Smart-Contract
***********not wo