A barebones solidity template for Aave's flashloans
- Basic knowledge of Solidity
- Basic knowledge of Truffle
- Basic knowledge of Javascript
- An Infura account and API key
- In
Flashloan.sol
, code your logic into theexecuteOperation()
function - When ready, call
flashloan()
on your contract - Remember that your contract needs to have enough funds of whatever asset you are borrowing to payback the flashloan fee.
- If not deploying on mainnet, then change the
addressProvider
in./aave/FlashLoanReceiverBase.sol
to the relevant address
-
Clone this repo and:
npm install
-
(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 and run:
ganache-cli --fork https://mainnet.infura.io/v3/YOUR_INFURA_KEY
-
In a new terminal window in your repo directory, run:
truffle console
-
Migrate your Flashloan contract to your instance of Ganache with:
migrate --reset
-
Your contract is now deployed on your local Ganache, which is mirroring mainnet. Call your contract's flashloan function within the truffle console with:
let f = await Flashloan.deployed() await f.flashloan()
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 didn't make any changes to this template and just deployed it, then the call to
f.flashloan()
will fail as the contract is not funded with any DAI, so cannot make the flashloan repayment (which includes the amount borrowed + a fee). Solve this by using an address already funded with DAI, or send yourself some DAI to begin with.
- if you didn't make any changes to this template and just deployed it, then the call to
My recommendations would be:
- If doing arbitrage and taking profits, hardcode your profit taking address
- Rename
env
file to.env
and include your infura key and deployment wallet private key (don't commit this file to Git!) - In
truffle-config.js
, uncomment themainnet
section under networks. - Deploy to mainnet with
truffle migrate --network mainnet
-
Aave's official documentation: This template is a bit different to their example as everything happens within this one contract, including the calling of
flashloan()
-
All imported contracts will have a link to the 'official' contract at the top.