FlashLoaner is a smartcontract to make flashloans with AAVE. This mechanism allows you to make a swap with Uniswap if you only have enough money just to pay the fee. This can be used in an arbitrage bot
This contract takes addresses of the UniswapRouter during deploy, and provides method startFlashLoan to make falshloans
constructor(address addressesProvider, address router) public;
You can use your own Router and AddressesProvider or get actual addresses for default networks below:
- Kovan:
- LendingPoolAddressesProvider: 0x88757f2f99175387ab4c6a4b3067c77a695b0349
- UniswapV2Router02: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
- Mainnet:
- LendingPoolAddressesProvider: 0xB53C1a33016B2DC2fF3653530bfF1848a515c8c5
- UniswapV2Router02: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
List of deployed AAVE contracts addresses
function startFlashLoan(uint amount, address[] memory path) external;
This method borrows the first token of path
from AAVE pool, makes swap with Router, returns loans back and sends balance after deduction to the msg.sender
.
amount
โ amount of tokens that will be borrowed.path
โ an array of token addresses between which the swap will be performed in turn. First and last addresses must be the same to make a looped swap. Contract saves it to a private attribute_path
to read it in the methoduniswapV2Call
that will be called next. You can pass it usingparams
argument instead of an attribute, if you need it.params
is abytes
type therefore if you want to pass addresses as an argument you need a library to convert list of addresses to bytes and vice versa. You can read about this here.
Install dependencies of the package.json:
npm i -D
Use .env as a local config to set private options manually:
Don't commit it to git! (keep it secret)
cp .env.template .env
You should also register at Infura and create new project there.
After that set your Infura Project ID (from project settings) to .env
To run any script enter:
npx hardhat run path/to/script.ts --network network_name
Use the script to generate a new wallet with it's own private key and address:
npx hardhat run scripts/0_create_new_wallet.ts
Copy generated private key (or your own private key) to .env config on the corresponding line.
Add some ETH to address of this wallet. For tests you can use any faucet for your network. For example faucet.kovan.network
Also you should get DAI in kovan, because AAVE allows to borrow only tokens that were added to its list. Use the AAVE Faucet to get DAI (0xff795577d9ac8bd7d90ee22b6c1703490b6512fd)
A script to deploy the FlashLoaner contract and to get it's address:
npx hardhat run scripts/1_deploy_flashloaner.ts --network kovan
Hardhat allows you to execute tests in it's own network, but due to the fact that you need to interact with Uniswap, you need to make a fork of a network where the Uniswap is. For example - Kovan.
You can use AlchemyAPI, then you will can specify the block from which the fork will be made. If you're going specify block, specify block after you get DAI tokens in kovan network!. To use AlchemyAPI, create an app in its dashboard and set it's URL to .env. If the AlchemyAPI URL not setted Infura API will be used.
Important: Infura API takes a last block during run test. This can lead to different results for the same tests
npx hardhat test test/FlashLoaner.ts
npx hardhat test test/FlashLoaner.ts --network kovan