Flash Loans
The latest implementation of the IdleToken for the Best Yield strategy. has support for flash loans, which are uncollateralized loans that allow users to borrow the underlying token (e.g DAI) of an IdleToken (e.g IdleDAI).

Overview

All IdleToken contracts can lend their undelrying tokens to a borrower smart contract as long as the principal amount + a fee are returned within the same transaction.
The IdleToken flash loans implementation follows the EIP-3156 which defines a standard interface that the lender and borrower smart contracts must implement.

Fee

The current fee is set to 0.02%, available in the uint256 public flashLoanFee; variable. You can also call the flashFee method to ask for the final fee based on the _amount to be borrowed:
function flashFee(address _token, uint256 _amount) public view returns (uint256);

How it works

Step 1
An EOA (external owned account) or a smart contract calls the flashLoan method of an IdleToken.
1
function flashLoan(
2
IERC3156FlashBorrower _receiver,
3
address _token,
4
uint256 _amount,
5
bytes calldata _params
6
) external returns (bool);
Copied!
  • _receiver: the smart contract that will receive the loan.
  • _token: it must be the underlying token (e.g. DAI) of the IdleToken.
  • _amount: the amount to borrow.
  • _params: parameters to forward to the borrower smart contract.
Step 2
The IdleToken contract transfers _amount token to the borrower contract and calls its onFlashLoan method:
1
function onFlashLoan(
2
address _initiator,
3
address _token,
4
uint256 _amount,
5
uint256 _fee,
6
bytes calldata _params
7
) external returns (bytes32);
Copied!
  • _initiator: the EOA or contracts that initiated the flash loan.
  • _token: the token borrowed.
  • _amount: the amount borrowed.
  • _fee: the fee to be paid back on top of the principal.
  • _params: the params forwarded by the IdleToken.
At this point the borrower contracts can use the borrowed tokens in any way.
Step 3
After using the borrowed tokens, the borrower contracts must:
  1. 1.
    approve the IdleToken contract to get backprincipal + fee of the borrowed token
  2. 2.
    return keccak256("ERC3156FlashBorrower.onFlashLoan")
Step 4
The IdleToken checks that the returned value is the one expected (keccak256("ERC3156FlashBorrower.onFlashLoan")) and transfers amount + fee tokens from the borrower to itself.
If the returned value is wrong, or if the transfers fails (i.e. the borrower didn't approve or approved less than amount + fee), the transaction is reverted.

Example

Flash loan borrower contract:
1
interface IERC3156FlashBorrower {
2
function onFlashLoan(
3
address initiator,
4
address token,
5
uint256 amount,
6
uint256 fee,
7
bytes calldata data
8
) external returns (bytes32);
9
}
10
​
11
contract FlashLoanBorrower is IERC3156FlashBorrower {
12
using SafeMath for uint256;
13
using SafeERC20 for IERC20;
14
​
15
function onFlashLoan(
16
address _initiator,
17
address _token,
18
uint256 _amount,
19
uint256 _fee,
20
bytes calldata _params
21
) external returns (bytes32) {
22
// custom logic where the contract uses the borrowed tokens
23
// NOTE: Remember to verify that _initiator address
24
​
25
// approve principal + fee to be received back by the sender
26
IERC20(_token).safeApprove(msg.sender, _amount.add(_fee));
27
​
28
// return value specified in the ERC3156
29
return keccak256("ERC3156FlashBorrower.onFlashLoan");
30
}
31
}
Copied!
Executing the flash loan:
1
const idleToken = await IdleTokenGovernance.at(addresses.idleDAIV4);
2
const underlying = await IERC20.at(await idleToken.token);
3
​
4
await deployer.deploy(FlashLoanBorrower);
5
const borrower = await FlashLoanBorrower.deployed();
6
​
7
await idleToken.flashLoan(
8
borrower.address,
9
underlying.address,
10
"1000000000000000000", // 1 DAI
11
web3.eth.abi.encodeParameters(["uint256"], [123]),
12
{ from: accounts[0] }
13
);
Copied!
Last modified 1mo ago