⚡Flash Loans
Ruler Protocol allows for flash loans to be executed! The fee for a flashloans is 0.085%.

How to Flash Loan?

Ruler implemented the ERC3156 flash lender standard interface. You will need to deploy a contract that implements the ERC3156 flash borrower standard interface. You can read more here about the EIP-3156 flash loan standard.

Flash Borrower Example

1
// SPDX-License-Identifier: No License
2
3
pragma solidity ^0.8.0;
4
5
import "./interfaces/IERC3156FlashBorrower.sol";
6
import "./interfaces/IERC3156FlashLender.sol";
7
import "./ERC20/IERC20.sol";
8
import "./ERC20/SafeERC20.sol";
9
10
contract RulerFlashBorrower {
11
using SafeERC20 for IERC20;
12
13
IERC3156FlashLender public flashLender;
14
constructor (IERC3156FlashLender _flashLender) {
15
flashLender = _flashLender;
16
}
17
18
function onFlashLoan(address initiator, address token, uint256 amount, uint256 fee, bytes calldata data) external returns (bytes32) {
19
address caller = abi.decode(data, (address));
20
require(msg.sender == address(flashLender), "RulerFlashBorrower: Untrusted lender");
21
require(initiator == address(this), "RulerFlashBorrower: Untrusted loan initiator");
22
23
// your logic
24
25
uint256 amountOwed = amount + fee;
26
// fees are adopting pulling strategy, Ruler contract will transfer fees
27
IERC20(token).approve(address(flashLender), amountOwed);
28
29
return keccak256("ERC3156FlashBorrower.onFlashLoan");
30
}
31
32
function flashBorrow(address _token, uint256 _amount) external {
33
require(_amount <= flashLender.maxFlashLoan(_token), "RulerFlashBorrower: Insufficient lender reserves");
34
flashLender.flashLoan(IERC3156FlashBorrower(address(this)), _token, _amount, abi.encode(msg.sender));
35
}
36
}
37
Copied!
Last modified 8mo ago