Skip to content

Latest commit

 

History

History
284 lines (248 loc) · 9.53 KB

WithFlashLoan.md

File metadata and controls

284 lines (248 loc) · 9.53 KB

WithFlashLoan.sol

View Source: contracts/core/liquidity/WithFlashLoan.sol

↗ Extends: VaultStrategy, IERC3156FlashLender ↘ Derived Contracts: Vault

WithFlashLoan

Functions

flashLoan

function flashLoan(IERC3156FlashBorrower receiver, address token, uint256 amount, bytes data) external nonpayable nonReentrant 
returns(bool)

Arguments

Name Type Description
receiver IERC3156FlashBorrower Specify the contract that receives the flash loan.
token address Specify the token you want to borrow.
amount uint256 Enter the amount you would like to borrow.
data bytes
Source Code
function flashLoan(
    IERC3156FlashBorrower receiver,
    address token,
    uint256 amount,
    bytes calldata data
  ) external override nonReentrant returns (bool) {
    require(amount > 0, "Please specify amount");

    /******************************************************************************************
      PRE
     ******************************************************************************************/
    (IERC20 stablecoin, uint256 fee, uint256 protocolFee) = delgate().preFlashLoan(msg.sender, key, receiver, token, amount, data);

    /******************************************************************************************
      BODY
     ******************************************************************************************/
    uint256 previousBalance = stablecoin.balanceOf(address(this));
    // require(previousBalance >= amount, "Balance insufficient"); <-- already checked in `preFlashLoan` --> `getFlashFeesInternal`

    stablecoin.ensureTransfer(address(receiver), amount);
    require(receiver.onFlashLoan(msg.sender, token, amount, fee, data) == keccak256("ERC3156FlashBorrower.onFlashLoan"), "IERC3156: Callback failed");
    stablecoin.ensureTransferFrom(address(receiver), address(this), amount + fee);

    uint256 finalBalance = stablecoin.balanceOf(address(this));
    require(finalBalance >= previousBalance + fee, "Access is denied");

    // Transfer protocol fee to the treasury
    stablecoin.ensureTransfer(s.getTreasury(), protocolFee);

    /******************************************************************************************
      POST
     ******************************************************************************************/

    delgate().postFlashLoan(msg.sender, key, receiver, token, amount, data);

    emit FlashLoanBorrowed(address(this), address(receiver), token, amount, fee);

    return true;
  }

flashFee

Gets the fee required to borrow the spefied token and given amount of the loan.

function flashFee(address token, uint256 amount) external view
returns(uint256)

Arguments

Name Type Description
token address
amount uint256
Source Code
function flashFee(address token, uint256 amount) external view override returns (uint256) {
    return delgate().getFlashFee(msg.sender, key, token, amount);
  }

maxFlashLoan

Gets maximum amount in the specified token units that can be borrowed.

function maxFlashLoan(address token) external view
returns(uint256)

Arguments

Name Type Description
token address
Source Code
function maxFlashLoan(address token) external view override returns (uint256) {
    return delgate().getMaxFlashLoan(msg.sender, key, token);
  }

Contracts