Skip to content

Latest commit

 

History

History
349 lines (303 loc) · 10.9 KB

VaultDelegateWithFlashLoan.md

File metadata and controls

349 lines (303 loc) · 10.9 KB

With Flash Loan Delegate Contract (VaultDelegateWithFlashLoan.sol)

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

↗ Extends: VaultDelegateBase ↘ Derived Contracts: VaultDelegate

VaultDelegateWithFlashLoan

This contract implements EIP-3156 Flash Loan.

Functions

getFlashFee

The fee to be charged for a given loan. Warning: this function does not validate the cover key supplied.

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. Warning: this function does not validate the cover key supplied.

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

This hook runs before flashLoan implementation on vault(s)

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 Enter the cover key
coverKey bytes32 Enter the cover key
IERC3156FlashBorrower coverKey Enter the cover key
token address Enter the token you want to borrow
amount uint256 Enter the flash loan amount to receive
bytes coverKey Enter the cover key
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.mustEnsureAllProductsAreNormal(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

This hook runs after flashLoan implementation on vault(s)

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

Arguments

Name Type Description
address coverKey Enter the cover key
coverKey bytes32 Enter the cover key
IERC3156FlashBorrower coverKey Enter the cover key
address coverKey Enter the cover key
uint256 coverKey Enter the cover key
bytes coverKey Enter the cover key
Source Code
function postFlashLoan(
    address, /*caller*/
    bytes32 coverKey,
    IERC3156FlashBorrower, /*receiver*/
    address, /*token*/
    uint256, /*amount*/
    bytes calldata /*data*/
  ) external override {
    // @suppress-zero-value-check The `amount` value isn't used and therefore not checked
    s.mustNotBePaused();
    s.senderMustBeVaultContract(coverKey);
    s.mustEnsureAllProductsAreNormal(coverKey);

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

Contracts