View Source: contracts/core/liquidity/WithFlashLoan.sol
↗ Extends: VaultStrategy, IERC3156FlashLender ↘ Derived Contracts: Vault
WithFlashLoan
- flashLoan(IERC3156FlashBorrower receiver, address token, uint256 amount, bytes data)
- flashFee(address token, uint256 amount)
- maxFlashLoan(address token)
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;
}
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);
}
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);
}
- AaveStrategy
- AccessControl
- AccessControlLibV1
- Address
- BaseLibV1
- BokkyPooBahsDateTimeLibrary
- BondPool
- BondPoolBase
- BondPoolLibV1
- CompoundStrategy
- Context
- Cover
- CoverBase
- CoverLibV1
- CoverReassurance
- CoverStake
- CoverUtilV1
- cxToken
- cxTokenFactory
- cxTokenFactoryLibV1
- Delayable
- Destroyable
- ERC165
- ERC20
- FakeAaveLendingPool
- FakeCompoundDaiDelegator
- FakePriceOracle
- FakeRecoverable
- FakeStore
- FakeToken
- FakeUniswapPair
- FakeUniswapV2FactoryLike
- FakeUniswapV2PairLike
- FakeUniswapV2RouterLike
- FaultyAaveLendingPool
- FaultyCompoundDaiDelegator
- Finalization
- ForceEther
- Governance
- GovernanceUtilV1
- IAaveV2LendingPoolLike
- IAccessControl
- IBondPool
- IClaimsProcessor
- ICompoundERC20DelegatorLike
- ICover
- ICoverReassurance
- ICoverStake
- ICxToken
- ICxTokenFactory
- IERC165
- IERC20
- IERC20Detailed
- IERC20Metadata
- IERC3156FlashBorrower
- IERC3156FlashLender
- IFinalization
- IGovernance
- ILendingStrategy
- ILiquidityEngine
- IMember
- INeptuneRouterV1
- InvalidStrategy
- IPausable
- IPolicy
- IPolicyAdmin
- IPriceOracle
- IProtocol
- IRecoverable
- IReporter
- IResolution
- IResolvable
- IStakingPools
- IStore
- IStoreLike
- IUniswapV2FactoryLike
- IUniswapV2PairLike
- IUniswapV2RouterLike
- IUnstakable
- IVault
- IVaultDelegate
- IVaultFactory
- IWitness
- LiquidityEngine
- MaliciousToken
- MockAccessControlUser
- MockCoverUtilUser
- MockCxToken
- MockCxTokenPolicy
- MockCxTokenStore
- MockFlashBorrower
- MockLiquidityEngineUser
- MockProcessorStore
- MockProcessorStoreLib
- MockProtocol
- MockRegistryClient
- MockStore
- MockStoreKeyUtilUser
- MockValidationLibUser
- MockVault
- MockVaultLibUser
- NeptuneRouterV1
- NPM
- NpmDistributor
- NTransferUtilV2
- NTransferUtilV2Intermediate
- Ownable
- Pausable
- Policy
- PolicyAdmin
- PolicyHelperV1
- PoorMansERC20
- POT
- PriceLibV1
- Processor
- ProtoBase
- Protocol
- ProtoUtilV1
- Recoverable
- ReentrancyGuard
- RegistryLibV1
- Reporter
- Resolution
- Resolvable
- RoutineInvokerLibV1
- SafeERC20
- StakingPoolBase
- StakingPoolCoreLibV1
- StakingPoolInfo
- StakingPoolLibV1
- StakingPoolReward
- StakingPools
- Store
- StoreBase
- StoreKeyUtil
- StrategyLibV1
- Strings
- TimelockController
- Unstakable
- ValidationLibV1
- Vault
- VaultBase
- VaultDelegate
- VaultDelegateBase
- VaultDelegateWithFlashLoan
- VaultFactory
- VaultFactoryLibV1
- VaultLibV1
- VaultLiquidity
- VaultStrategy
- WithFlashLoan
- WithPausability
- WithRecovery
- Witness