forked from flashburst/protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVaultDelegateWithFlashLoan.sol
More file actions
136 lines (124 loc) · 3.93 KB
/
VaultDelegateWithFlashLoan.sol
File metadata and controls
136 lines (124 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "./VaultDelegateBase.sol";
/**
* Important: This contract is not intended to be accessed
* by anyone/anything except individual vault contracts.
*
* @title With Flash Loan Delegate Contract
*
* @dev This contract implements [EIP-3156 Flash Loan](https://eips.ethereum.org/EIPS/eip-3156).
*
*/
abstract contract VaultDelegateWithFlashLoan is VaultDelegateBase {
using ProtoUtilV1 for IStore;
using StoreKeyUtil for IStore;
using ValidationLibV1 for IStore;
using VaultLibV1 for IStore;
using RoutineInvokerLibV1 for IStore;
/**
* @dev The fee to be charged for a given loan.
*
* Warning: this function does not validate the cover key supplied.
*
* @param token The loan currency.
* @param amount The amount of tokens lent.
* @return The amount of `token` to be charged for the loan, on top of the returned principal.
*
*/
function getFlashFee(
address, /*caller*/
bytes32 coverKey,
address token,
uint256 amount
) external view override returns (uint256) {
s.senderMustBeVaultContract(coverKey);
return s.getFlashFeeInternal(coverKey, token, amount);
}
/**
* @dev The amount of currency available to be lent.
*
* Warning: this function does not validate the cover key supplied.
*
* @param token The loan currency.
* @return The amount of `token` that can be borrowed.
*
*/
function getMaxFlashLoan(
address, /*caller*/
bytes32 coverKey,
address token
) external view override returns (uint256) {
s.senderMustBeVaultContract(coverKey);
return s.getMaxFlashLoanInternal(coverKey, token);
}
/**
* @dev This hook runs before `flashLoan` implementation on vault(s)
*
* @custom:suppress-acl This function is only accessible to the vault contract
* @custom:note Please note the following:
*
* - msg.sender must be the correct vault contract
* - Cover status should be normal
*
* @param coverKey Enter the cover key
* @param token Enter the token you want to borrow
* @param amount Enter the flash loan amount to receive
*
*/
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");
}
/**
* @dev This hook runs after `flashLoan` implementation on vault(s)
*
* @custom:suppress-acl This function is only accessible to the vault contract
* @custom:note Please note the following:
*
* - msg.sender must be the correct vault contract
* - Cover status should be normal
*
* @param coverKey Enter the cover key
*
*/
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);
}
}