Categories
blog DeFi erc20 javascript solidity

Making a flash loan with Solidity (Aave, Dy/Dx, Kollateral)

Flash Loans are loans that allow the borrowing of an asset, as long as the borrowed amount (and a fee) is returned before the end of the transaction. Those types of loans enable you to perform actions without using your funds for a really small percentage fee (0.09% at the time of writing on Aave and 0% on Dy/Dx). Flash loans can be used for arbitrage across DEXes, liquidation of positions on protocols like Dy/Dx, and migration of CDPs for example. In this tutorial we’ll different ways you can make a flash loan in a Solidity smart contract. If you’d like to learn more about what can flash loans be used for we recommend reading a few articles like this one, this paper or this one.

Another nice way to get ideas on what to build with flash loans is checking some transactions that are using flash loans and check what they are doing on a block explorer.

Currently available flash loans protocols only offer to borrow one asset per transaction, but if you have ETH it is easy for example easy to get DAI by minting them or even use 1inch aggregator to swap to any other token.

Here are the different protocols you can use to perform a flash loan on the Ethereum blockchain:

Flash loans with Aave

Aave is an Open Source and Non-Custodial protocol to earn interest on deposits & borrow assets. As their documentation is really well made and complete we directly invite you to see their documentation.

Support EthereumDev.io

We hope you enjoy our free tutorials about ethereum programming and would love to get some help to keep new articles coming and cover server costs.

Support free content

👍 Advantages:

  • Short and convenient code
  • Lot of assets available (ETH, USDC, DAI, MAKER, REP, BAT, TUSD, USDT..)
  • ETH is directly provided as Ether
  • Awesome documentation and community support

👎 Disadvantages:

  • A 0.09% fee

Flash loans with Dy/Dx

DyDx does offer natively flashloans. But you you can still achieve a similar behavior by executing a series of operations on the SoloMargin contract. In order to mimic an Aave flashloan on DyDx, you would need to:

  • Borrow the amount of tokens
  • Call a function where you use the borrowed funds
  • Deposit back the amount of tokens (+2 wei)

You can find an implementation example on the Money Legos website. This implementation is based on Kollateral’s source code that is listed above.

👍 Advantages:

  • No fee (Only 2 wei)

👎 Disadvantages:

  • ETH is provided as wrapped Ether (WETH)
  • Less readable code
  • Few tokens available (ETH/USDC/DAI)

Flash loans with Kollateral

Kollateral is a smart contract that aggregates liquidity from the Aave and Dy/Dx platforms and surfaces it to developers with a simple interface.

👍 Advantages:

  • Clean code
  • Use assets from several protocols

👎 Disadvantages:

  • Not clear what are the fees
  • A dependency for your project

The code of a flash loan with Kollateral looks like this:

import "@kollateral/contracts/invoke/KollateralInvokable.sol";
contract MyContract is KollateralInvokable {
  constructor () public { }
  function execute(bytes calldata data) external payable {
    // liquidate
    // swap
    // refinance
    // etc...
    repay();
  }
}

And can be called form javaScript in a really simple way:

import { Kollateral, Token } from '@kollateral/kollateral'
const kollateral = await Kollateral.init(ethereum);
kollateral.invoke({
    contract: myContractAddress
}, {
  token: Token.DAI,
  amount: web3.utils.toWei(1000)
}).then(() => console.log("success!");

This article is part of a series to help people building on top of DeFi using Solidity and Javascript. You can find our other articles like:

Leave a Reply