This is a working Truffle project you can build upon which draws on flash liquidity from multiple flash lenders within a single transaction. This template uses the Kollateral protocol which handles the aggregation of liquidity from multiple flash-loan-enabled pools such as DyDx and Aave.
Please note this template assumes prior experience with truffle development/deployment, dotenv usage, infura endpoints...etc If you're not familiar I highly recommend undertaking a corresponding tutorial beforehand, as most of the errors you encounter can be resolved with prior study.
- Clone the repo.
git clone https://github.com/fifikobayashi/Aggregated-Flashloan
cd Aggregated-Flashloan
- Install the Kollateral solidity library.
npm install @kollateral/contracts
- Install dotenv and hdwallet provider so you can hide your private key and Infura IDs within your own .env file.
npm install dotenv
npm install --save truffle-hdwallet-provider
- Create a .env file in the root of your project folder.
touch .env
Then edit the .env file and add the following two lines, along with your Infura ID and private key (no quotes).
IMPORTANT Make sure you learn how to use .gitignore to exclude this .env file when publishing your code to github. There have been many incidents, as recently as last week where someone accidentally uploaded their env file containing their private keys onto Github and got their accounts emptied within minutes.
ROPSTEN_PROVIDER=https://ropsten.infura.io/v3/YOUR_INFURA_ID
ROPSTEN_PRTK=YOUR_PRIVATE_KEY
- Compile the project
truffle compile
- Deploy the project. (note: if you're on Node14 please see troubleshooting section below re: callback error)
Initial deployment:
truffle migrate --network ropsten
Subsequent deployments thereafter:
truffle migrate --network ropsten --reset
- When deployment completes, take note of the contract address and send some Ether to it so it can cover the aggregation fee plus the native flash loan fees. See Fees below for more info.
- Pop into the ropsten console
truffle console --network ropsten
- Execute the aggregated flash loan function by calling the invoke() function while in console mode:
AggregatedFlashloans.deployed().then(function(instance){return instance.invoke()});
- A successful execution of this code looks like this in console and like this on Etherscan.
- Once you're done playing with this, while still in console mode, you can withdraw the ether from the contract by calling:
AggregatedFlashloans.deployed().then(function(instance){return instance.WithdrawBalance()});
Kollateral currently charges 6bps on the flash liquidity that is sourced. In addition, you also need to factor in the native fees from each pool you aggregate from, such as Aave's 9bps flash fee and DyDx's 3 wei. If there is not enough funds on the contract to repay the loan + fees then the TX will be reverted.
- If you keep getting a Callback was already called error when deploying contracts via Truffle/Infura/Node14, it's because ganache-cli's internal core is not yet node v14 compatible, so just use 'truffle migrate --network ropsten --skipDryRun'.
- Failed Tx with ExternalCaller: insufficient ether balance - this means you forgot to send some ether to this contract to cover the aggregation and flash loan fees.
- Failed Tx with Invoker: not enough liquidity - this means you're asking for too much liquidity than the aggregate pools can handle at this point in time, particularly prevalent in testnets with limited liquidity. Reduce your flash amount.
If you found this useful and would like to send me some gas money:
0xef03254aBC88C81Cb822b5E4DCDf22D55645bCe6
Thanks, @fifikobayashi.