Skip to content

Latest commit

 

History

History
344 lines (300 loc) · 10.4 KB

VaultDelegateWithFlashLoan.md

File metadata and controls

344 lines (300 loc) · 10.4 KB

With Flash Loan Contract (VaultDelegateWithFlashLoan.sol)

View Source: contracts/core/delegates/VaultDelegateWithFlashLoan.sol

↗ Extends: VaultDelegateBase ↘ Derived Contracts: VaultDelegate

VaultDelegateWithFlashLoan

WithFlashLoan contract implements EIP-3156 Flash Loan. Using flash loans, you can borrow up to the total available amount of the stablecoin liquidity available in this cover liquidity pool. You need to return back the borrowed amount + fee in the same transaction. The function flashFee enables you to check, in advance, fee that you need to pay to take out the loan.

Functions

getFlashFee

The fee to be charged for a given loan.

function getFlashFee(address , bytes32 coverKey, address token, uint256 amount) external view
returns(uint256)

Arguments

Name Type Description
address token The loan currency.
coverKey bytes32
token address The loan currency.
amount uint256 The amount of tokens lent.

Returns

The amount of token to be charged for the loan, on top of the returned principal.

Source Code
function getFlashFee(
    address, /*caller*/
    bytes32 coverKey,
    address token,
    uint256 amount
  ) external view override returns (uint256) {
    s.senderMustBeVaultContract(coverKey);
    return s.getFlashFeeInternal(coverKey, token, amount);
  }

getMaxFlashLoan

The amount of currency available to be lent.

function getMaxFlashLoan(address , bytes32 coverKey, address token) external view
returns(uint256)

Arguments

Name Type Description
address token The loan currency.
coverKey bytes32
token address The loan currency.

Returns

The amount of token that can be borrowed.

Source Code
function getMaxFlashLoan(
    address, /*caller*/
    bytes32 coverKey,
    address token
  ) external view override returns (uint256) {
    s.senderMustBeVaultContract(coverKey);
    return s.getMaxFlashLoanInternal(coverKey, token);
  }

preFlashLoan

function preFlashLoan(address , bytes32 coverKey, IERC3156FlashBorrower , address token, uint256 amount, bytes ) external nonpayable
returns(stablecoin contract IERC20, fee uint256, protocolFee uint256)

Arguments

Name Type Description
address
coverKey bytes32
IERC3156FlashBorrower
token address
amount uint256
bytes
Source Code
function preFlashLoan(
    address, /*caller*/
    bytes32 coverKey,
    IERC3156FlashBorrower, /*receiver*/
    address token,
    uint256 amount,
    bytes calldata /*data*/
  )
    external
    override
    returns (
      IERC20 stablecoin,
      uint256 fee,
      uint256 protocolFee
    )
  {
    s.mustNotBePaused();
    s.mustHaveNormalCoverStatus(coverKey);
    s.senderMustBeVaultContract(coverKey);

    stablecoin = IERC20(s.getStablecoin());

    // require(address(stablecoin) == token, "Unknown token"); <-- already checked in `getFlashFeesInternal`
    // require(amount > 0, "Loan too small"); <-- already checked in `getFlashFeesInternal`

    s.setBoolByKeys(ProtoUtilV1.NS_COVER_HAS_FLASH_LOAN, coverKey, true);

    (fee, protocolFee) = s.getFlashFeesInternal(coverKey, token, amount);

    require(fee > 0, "Loan too small");
    require(protocolFee > 0, "Loan too small");
  }

postFlashLoan

function postFlashLoan(address , bytes32 coverKey, IERC3156FlashBorrower , address , uint256 , bytes ) external nonpayable

Arguments

Name Type Description
address
coverKey bytes32
IERC3156FlashBorrower
address
uint256
bytes
Source Code
function postFlashLoan(
    address, /*caller*/
    bytes32 coverKey,
    IERC3156FlashBorrower, /*receiver*/
    address, /*token*/
    uint256, /*amount*/
    bytes calldata /*data*/
  ) external override {
    s.senderMustBeVaultContract(coverKey);

    s.setBoolByKeys(ProtoUtilV1.NS_COVER_HAS_FLASH_LOAN, coverKey, false);
    s.updateStateAndLiquidity(coverKey);
  }

Contracts