This Truffle box comes with everything you need to start developing on flash loans
npm install -g truffle@latest
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)
truffle unbox aave/flashloan-box
env
file to .env
and edit the following values in the file:
YOUR_INFURA_KEY
with an API key for your project (this is called Project ID in the Infra dashboard).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 the owner
of the contract.npm install
NAME_OF_YOUR_NETWORK
with either kovan
, ropsten
, or mainnet
(depending on where you want to deploy the contract):
truffle console --network NAME_OF_YOUR_NETWORK
migrate --reset
Flashloan.sol
line 23, then you will need to fund your contract with the desired asset.RESERVE_ADDRESS
with the reserve address found in our documentation:
let f = await Flashloan.deployed()
await f.flashloan(RESERVE_ADDRESS)
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.Ropsten
using Dai.Ropsten
using ETH.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 the development
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.
Flashloan.sol
line 23, then you will need to fund your contract with the desired asset.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 ...
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).See our Troubleshooting Errors documentation.