Aave Flash Loan Truffle Box¶
This Truffle box comes with everything you need to start developing on flash loans
Installation and Setup¶
- Install Truffle globally, if not already installed. Note: there is an issue with some older Truffle versions, e.g. v.5.1.25. This truffle box is confirmed working with the latest version (Truffle v5.1.32)
npm install -g truffle@latest
- Download the box.
truffle unbox aave/flashloan-box
- Rename the
env
file to.env
and edit the following values in the file:- Sign up for Infura (or a similar provider) and replace
YOUR_INFURA_KEY
with an API key for your project (this is called Project ID in the Infra dashboard). - Replace
YOUR_ACCOUNT_KEY_FOR_DEPLOYMENT
with the private key of the ethereum account you will be using to deploy the contracts. This account will become theowner
of the contract.
- Sign up for Infura (or a similar provider) and replace
- Ensure your ethereum account has some ETH to deploy the contract.
- In your terminal, navigate to your repo directory and install the dependencies (if not already done):
npm install
- In the same terminal, replace
NAME_OF_YOUR_NETWORK
with eitherkovan
,ropsten
, ormainnet
(depending on where you want to deploy the contract):truffle console --network NAME_OF_YOUR_NETWORK
- You are now connected to the network you chose. In the same terminal window:
migrate --reset
- After a few minutes, your contract will be deployed on your chosen network.
- If you have not added any profitable logic to
Flashloan.sol
line 23, then you will need to fund your contract with the desired asset. - See our documentation for token address and faucets.
- If you have not added any profitable logic to
- Call your contract's flashloan function within the truffle console, replacing
RESERVE_ADDRESS
with the reserve address found in our documentation:let f = await Flashloan.deployed() await f.flashloan(RESERVE_ADDRESS)
- if the above operation takes an unreasonably long time or timesout, try
CTRL+C
to exit the Truffle console, repeat step 5, then try this step agin. You may need to wait a few blocks before your node can 'see' the deployed contract.
- if the above operation takes an unreasonably long time or timesout, try
- If you've successfully followed the above steps, then congratulations, you've just made a flash loan.
- For reference, here is an example transaction that followed the above steps on
Ropsten
using Dai. - For reference, here is an example transaction that followed the above steps on
Ropsten
using ETH.
- For reference, here is an example transaction that followed the above steps on
Setup for cross protocol flash lending¶
If you are working across protocols, such as using the flash loaned amount on another #DeFi protocol, sometimes it is easier to fork mainnet and use each protocol's production contracts and production ERC20 tokens.
- Follow the steps 0 --> step 4 from above.
- (Install and) Run Ganache, preferably the CLI version
- In
truffle-config.js
, ensure the details for thedevelopment
network match up with your running Ganache instance. - To minimise set up steps with Aave's lending pools, use Ganache's fork feature. This will 'fork' mainnet into your Ganache instance. Open terminal, replace
YOUR_INFURA_KEY
(this is called Project ID in the Infra dashboard) in the following and run:ganache-cli --fork https://mainnet.infura.io/v3/YOUR_INFURA_KEY -i 1
- In a new terminal window in your repo directory, run:
truffle console
- Migrate your Flashloan contract to your instance of Ganache with:
migrate --reset
- After a few minutes, your contract will be deployed.
- If you have not added any profitable logic to
Flashloan.sol
line 23, then you will need to fund your contract with the desired asset. - See our documentation for token address and faucets.
- If you have not added any profitable logic to
-
Your contract is now deployed on your local Ganache, which is mirroring mainnet. Call your contract's flashloan function within the truffle console, replacing
RESERVE_ADDRESS
with the reserve address found in our documentation:Be patient as your ganache instance works its magic.let f = await Flashloan.deployed() await f.flashloan(RESERVE_ADDRESS)
-
If your implementation is correct, then the transaction will succeed. If it fails/reverts, a reason will be given.
Known issues¶
No access to archive state errors¶
If you are using Ganache to fork a network, then you may have issues with the blockchain archive state every 30 minutes. This is due to your node provider (i.e. Infura) only allowing free users access to 30 minutes of archive state. To solve this, upgrade to a paid plan, or simply restart your ganache instance and redploy your contracts.
Unable to debug executeOperation() with mainnet ganache fork¶
The Truffle debugger does not work too well with proxy / complex calls. You may find that the Truffle debugger returns an error such as:
TypeError: Cannot read property 'version' of undefined
at ...
executeOperation()
function directly, instead of having Aave's LendingPool
contract invoke the function. This will allow you to debug the function directly, however you will need to supply the relevant parameters (e.g. _amount
, _fee
, _reserve
, etc). - Alternatively, see the 'Troubleshooting' link. Troubleshooting¶
See our Troubleshooting Errors documentation.