⚡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

// SPDX-License-Identifier: No License

pragma solidity ^0.8.0;

import "./interfaces/IERC3156FlashBorrower.sol";
import "./interfaces/IERC3156FlashLender.sol";
import "./ERC20/IERC20.sol";
import "./ERC20/SafeERC20.sol";

contract RulerFlashBorrower {
    using SafeERC20 for IERC20;

    IERC3156FlashLender public flashLender;
    constructor (IERC3156FlashLender _flashLender) {
        flashLender = _flashLender;
    }
    
    function onFlashLoan(address initiator, address token, uint256 amount, uint256 fee, bytes calldata data) external returns (bytes32) {
        address caller = abi.decode(data, (address));
        require(msg.sender == address(flashLender), "RulerFlashBorrower: Untrusted lender");
        require(initiator == address(this), "RulerFlashBorrower: Untrusted loan initiator");
        
        // your logic
        
        uint256 amountOwed = amount + fee;
        // fees are adopting pulling strategy, Ruler contract will transfer fees
        IERC20(token).approve(address(flashLender), amountOwed);

        return keccak256("ERC3156FlashBorrower.onFlashLoan");
    }

    function flashBorrow(address _token, uint256 _amount) external {
        require(_amount <= flashLender.maxFlashLoan(_token), "RulerFlashBorrower: Insufficient lender reserves");
        flashLender.flashLoan(IERC3156FlashBorrower(address(this)), _token, _amount, abi.encode(msg.sender));
    }
}

Last updated

Was this helpful?