Skip to content
raviwijerathnaFPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

19 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

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

  1. Option A — Remix (fastest for experimentation)
  • Open SimpleFlashLoan.sol in Remix.
  • Compile with Solidity 0.8.10.
  • Deploy the contract with constructor argument _addressProvider set 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).
  1. 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
      
  • Add SimpleFlashLoan.sol to your contracts folder and write a deployment script that passes the correct PoolAddressesProvider address.
  • Compile:
    • Hardhat: npx hardhat compile
    • Foundry: forge build
  • Deploy to a testnet (configure private key and RPC URL in env):
    • Example (Hardhat): npx hardhat run scripts/deploy.js --network goerli
  • Execute fn_RequestFlashLoan through a script or tests.

Note: This repository does not include Hardhat/Foundry config or scripts — add them per your project preferences.

Using Remix (quick demo)

  1. Set the environment to the network you want (e.g., Injected Web3 to use MetaMask).
  2. Ensure the address you use for _addressProvider is the Aave v3 provider for that network (consult Aave docs).
  3. Deploy the contract, then call fn_RequestFlashLoan with:
  • _token: token address to borrow (e.g., WETH address)
  • _amount: borrow amount (in token minimal units)
  1. Watch executeOperation be called by Aave. Implement your profit logic in executeOperation before approving repayment.

Notes & security considerations

  • This is an educational minimal example — it deliberately leaves executeOperation logic empty (comment: //Logic goes here). You must implement any actual arbitrage or trade logic there.
  • Always ensure the contract repays amount + premium before executeOperation returns; 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.

Testing & development suggestions

  • 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 --fork configuration or set hardhat network to fork an RPC URL.
  • Add scripts:
  • A deployment script that accepts POOL_ADDRESSES_PROVIDER from env.
  • A script to trigger fn_RequestFlashLoan and assert expected state after executeOperation.

License

The Solidity source has SPDX license identifier MIT. Consider adding an explicit LICENSE file to the repository.

Contributing

Contributions, corrections, and improvements are welcome. Suggested first steps:

  • Add development tooling (Hardhat/Foundry config)
  • Add tests and example scripts
  • Provide example PoolAddressesProvider addresses and step-by-step demo for a specific testnet

Author

Repository: raviwijerathna1/Flash-Loan-Smart-Contract

***********not wo

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages