Skip to content

Commit

Permalink
refactor(flash-loan): remove erc3156
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubilmax committed Jul 25, 2023
1 parent e54f7b0 commit 762b88d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 87 deletions.
28 changes: 6 additions & 22 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pragma solidity 0.8.20;

import {IIrm} from "src/interfaces/IIrm.sol";
import {IERC20} from "src/interfaces/IERC20.sol";
import {IERC3156FlashLender} from "src/interfaces/IERC3156FlashLender.sol";
import {IERC3156FlashBorrower, FLASH_BORROWER_SUCCESS_HASH} from "src/interfaces/IERC3156FlashBorrower.sol";
import {IFlashLender} from "src/interfaces/IFlashLender.sol";
import {IFlashBorrower} from "src/interfaces/IFlashBorrower.sol";

import {Errors} from "./libraries/Errors.sol";
import {SharesMath} from "src/libraries/SharesMath.sol";
Expand All @@ -16,7 +16,7 @@ uint256 constant WAD = 1e18;
uint256 constant MAX_FEE = 0.2e18;
uint256 constant ALPHA = 0.5e18;

contract Blue is IERC3156FlashLender {
contract Blue is IFlashLender {
using SharesMath for uint256;
using FixedPointMathLib for uint256;
using SafeTransferLib for IERC20;
Expand Down Expand Up @@ -249,29 +249,13 @@ contract Blue is IERC3156FlashLender {

// Flash Loans.

/// @inheritdoc IERC3156FlashLender
function maxFlashLoan(address token) external view returns (uint256) {
return IERC20(token).balanceOf(address(this));
}

/// @inheritdoc IERC3156FlashLender
function flashFee(address, uint256) external pure returns (uint256) {
return 0;
}

/// @inheritdoc IERC3156FlashLender
function flashLoan(IERC3156FlashBorrower receiver, address token, uint256 amount, bytes calldata data)
external
returns (bool)
{
/// @inheritdoc IFlashLender
function flashLoan(IFlashBorrower receiver, address token, uint256 amount, bytes calldata data) external {
IERC20(token).safeTransfer(address(receiver), amount);

bytes32 successHash = receiver.onFlashLoan(msg.sender, token, amount, 0, data);
require(successHash == FLASH_BORROWER_SUCCESS_HASH, Errors.INVALID_SUCCESS_HASH);
receiver.onFlashLoan(msg.sender, token, amount, data);

IERC20(token).safeTransferFrom(address(receiver), address(this), amount);

return true;
}

// Position management.
Expand Down
20 changes: 0 additions & 20 deletions src/interfaces/IERC3156FlashBorrower.sol

This file was deleted.

32 changes: 0 additions & 32 deletions src/interfaces/IERC3156FlashLender.sol

This file was deleted.

13 changes: 13 additions & 0 deletions src/interfaces/IFlashBorrower.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.5.0;

interface IFlashBorrower {
/**
* @dev Receives a flash loan.
* @param initiator The initiator of the loan.
* @param token The token lent.
* @param amount The amount of tokens lent.
* @param data Arbitrary data structure, intended to contain user-defined parameters.
*/
function onFlashLoan(address initiator, address token, uint256 amount, bytes calldata data) external;
}
15 changes: 15 additions & 0 deletions src/interfaces/IFlashLender.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.5.0;

import {IFlashBorrower} from "src/interfaces/IFlashBorrower.sol";

interface IFlashLender {
/**
* @dev Initiate a flash loan.
* @param receiver The receiver of the tokens in the loan, and the receiver of the callback.
* @param token The token lent.
* @param amount The amount of tokens lent.
* @param data Arbitrary data structure, intended to contain user-defined parameters.
*/
function flashLoan(IFlashBorrower receiver, address token, uint256 amount, bytes calldata data) external;
}
21 changes: 8 additions & 13 deletions src/mocks/FlashBorrowerMock.sol
Original file line number Diff line number Diff line change
@@ -1,31 +1,26 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import {IERC3156FlashLender} from "src/interfaces/IERC3156FlashLender.sol";
import {IERC3156FlashBorrower, FLASH_BORROWER_SUCCESS_HASH} from "src/interfaces/IERC3156FlashBorrower.sol";
import {IFlashLender} from "src/interfaces/IFlashLender.sol";
import {IFlashBorrower} from "src/interfaces/IFlashBorrower.sol";

import {ERC20, SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";

contract FlashBorrowerMock is IERC3156FlashBorrower {
contract FlashBorrowerMock is IFlashBorrower {
using SafeTransferLib for ERC20;

IERC3156FlashLender private immutable _LENDER;
IFlashLender private immutable _LENDER;

constructor(IERC3156FlashLender lender) {
constructor(IFlashLender lender) {
_LENDER = lender;
}

/* EXTERNAL */

/// @inheritdoc IERC3156FlashBorrower
function onFlashLoan(address, address token, uint256 amount, uint256 fee, bytes calldata)
external
returns (bytes32)
{
/// @inheritdoc IFlashBorrower
function onFlashLoan(address, address token, uint256 amount, bytes calldata) external {
require(msg.sender == address(_LENDER), "invalid lender");

ERC20(token).safeApprove(address(_LENDER), amount + fee);

return FLASH_BORROWER_SUCCESS_HASH;
ERC20(token).safeApprove(address(_LENDER), amount);
}
}
2 changes: 2 additions & 0 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@ contract BlueTest is Test {
blue.supply(market, amount, address(this));

blue.flashLoan(flashBorrower, address(borrowableAsset), amount, bytes(""));

assertEq(borrowableAsset.balanceOf(address(blue)), amount, "balanceOf");
}
}

Expand Down

0 comments on commit 762b88d

Please sign in to comment.