This Truffle box comes with everything you need to start developing on flash loans
- Install Truffle globally, if not already installed.
npm install -g truffle
- 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. - 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
Kovan
using Dai. - For reference, here is an example transaction that followed the above steps on
Kovan
using ETH.
- For reference, here is an example transaction that followed the above steps on
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
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:let f = await Flashloan.deployed() await f.flashloan(RESERVE_ADDRESS)
Be patient as your ganache instance works its magic.
-
If your implementation is correct, then the transaction will succeed. If it fails/reverts, a reason will be given.
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.
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 ...
- In this case you can try calling your
executeOperation()
function directly, instead of having Aave'sLendingPool
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.
See our Troubleshooting Errors documentation.