... with Truffle
Truffle is the most popular development environment for Ethereum developers, part of the Truffle suite and supported by Consensys.
If you are new to Truffle, we'd recommend you have a look at one of the many tutorials on the basics of Truffle. The official tutorials are here, although some of them may be outdated.
Flash Loan Truffle Box
Get up and running in a 'flash'⚡️by using our official Truffle box (also listed in Truffle's official directory), which contains everything you need to perform your first flash loan, with Solidity ^0.6.6.
If you're already familiar with Truffle, then we'd recommend you start with our Flash Loan starter template.
If you prefer to work with the older Solidity ^0.5.15, see Flash loan starter template.
Installation and Setup
Install Truffle globally, if not already installed.
Download the box.
Rename the
env
file 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.
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):
In the same terminal, replace
NAME_OF_YOUR_NETWORK
with eitherkovan
,ropsten
, ormainnet
(depending on where you want to deploy the contract):You are now connected to the network you chose. In the same terminal window:
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.
Call your contract's flashloan function within the truffle console, replacing
RESERVE_ADDRESS
with the reserve address found in our documentation:if the above operation takes an unreasonably long time or times-out, 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 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.
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
in the following and run:In a new terminal window in your repo directory, run:
Migrate your Flashloan contract to your instance of Ganache with:
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.
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.
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:
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.
Troubleshooting
See our Troubleshooting Errors documentation.
Flash loan starter template
To implement a Flash Loan with Truffle, using Solidity ^0.5.15, see the starter template found here.
The README.MD
contains all the necessary information you need to set up and run your Truffle project.
If you need development support, join the #developers channel on our Aave community Discord server.
Last updated