-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathLiquidityEngine.sol
More file actions
222 lines (187 loc) · 7.51 KB
/
LiquidityEngine.sol
File metadata and controls
222 lines (187 loc) · 7.51 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
// import "../../interfaces/IVault.sol";
import "../Recoverable.sol";
import "../../interfaces/ILendingStrategy.sol";
import "../../interfaces/ILiquidityEngine.sol";
import "../../libraries/NTransferUtilV2.sol";
/**
* @title Liquidity Engine contract
* @dev The liquidity engine contract enables liquidity manager(s)
* to add, disable, remove, or manage lending or other income strategies.
*
*/
contract LiquidityEngine is ILiquidityEngine, Recoverable {
using NTransferUtilV2 for IERC20;
using ProtoUtilV1 for IStore;
using RegistryLibV1 for IStore;
using StoreKeyUtil for IStore;
using StrategyLibV1 for IStore;
using ValidationLibV1 for IStore;
constructor(IStore s) Recoverable(s) {} // solhint-disable-line
/**
* @dev Adds an array of strategies to the liquidity engine.
* @param strategies Enter one or more strategies.
*/
function addStrategies(address[] calldata strategies) external override nonReentrant {
require(strategies.length > 0, "No strategy specified");
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);
s.addStrategiesInternal(strategies);
}
/**
* @dev The liquidity state update interval allows the protocol
* to perform various activies such as NPM token price update,
* deposits or withdrawals to lending strategies, and more.
*
* @param value Specify the update interval value
*
*/
function setLiquidityStateUpdateInterval(uint256 value) external override nonReentrant {
require(value > 0, "Invalid value");
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);
s.setUintByKey(ProtoUtilV1.NS_LIQUIDITY_STATE_UPDATE_INTERVAL, value);
emit LiquidityStateUpdateIntervalSet(value);
}
/**
* @dev Disables a strategy by address.
* When a strategy is disabled, it immediately withdraws and cannot lend any further.
*
* @custom:suppress-address-trust-issue The address `strategy` can be trusted because of the ACL requirement.
*
* @param strategy Enter the strategy contract address to disable
*/
function disableStrategy(address strategy) external override nonReentrant {
// because this function can only be invoked by a liquidity manager.
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);
s.disableStrategyInternal(strategy);
emit StrategyDisabled(strategy);
}
/**
* @dev Permanently deletes a disabled strategy by address.
*
* @custom:suppress-address-trust-issue This instance of strategy can be trusted because of the ACL requirement.
*
* @param strategy Enter the strategy contract address to delete
*/
function deleteStrategy(address strategy) external override nonReentrant {
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);
s.deleteStrategyInternal(strategy);
emit StrategyDeleted(strategy);
}
/**
* @dev In order to pool risks collectively, liquidity providers
* may lend their stablecoins to a cover pool of their choosing during "lending periods"
* and withdraw them during "withdrawal windows." These periods are known as risk pooling periods.
*
* <br /> <br />
*
* The default lending period is six months, and the withdrawal window is seven days.
* Specify a cover key if you want to configure or override these periods for a cover.
* If no cover key is specified, the values entered will be set as global parameters.
*
* @param coverKey Enter a cover key to set the periods. Enter `0x` if you want to set the values globally.
* @param lendingPeriod Enter the lending duration. Example: 180 days.
* @param withdrawalWindow Enter the withdrawal duration. Example: 7 days.
*
*/
function setRiskPoolingPeriods(
bytes32 coverKey,
uint256 lendingPeriod,
uint256 withdrawalWindow
) external override nonReentrant {
require(lendingPeriod > 0, "Please specify lending period");
require(withdrawalWindow > 0, "Please specify withdrawal window");
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);
s.setRiskPoolingPeriodsInternal(coverKey, lendingPeriod, withdrawalWindow);
// event emitted in the above function
}
/**
* @dev Specify the maximum lending ratio a strategy can utilize, not to exceed 100 percent.
*
* @param ratio. Enter the ratio as a percentage value. Use `ProtoUtilV1.MULTIPLIER` as your divisor.
*
*/
function setMaxLendingRatio(uint256 ratio) external override nonReentrant {
require(ratio > 0, "Please specify lending ratio");
require(ratio <= ProtoUtilV1.MULTIPLIER, "Invalid lending ratio");
s.mustNotBePaused();
AccessControlLibV1.mustBeLiquidityManager(s);
s.setMaxLendingRatioInternal(ratio);
}
/**
* @dev Gets the maximum lending ratio a strategy can utilize.
*/
function getMaxLendingRatio() external view override returns (uint256 ratio) {
return s.getMaxLendingRatioInternal();
}
/**
* @dev Returns the risk pooling periods of a given cover key.
* Global values are returned if the risk pooling period for the given cover key was not defined.
* If global values are also undefined, fallback value of 180-day lending period
* and 7-day withdrawal window are returned.
*
* Warning: this function does not validate the cover key supplied.
*
* @param coverKey Enter the coverkey to retrieve the lending period of.
* Warning: this function doesn't check if the supplied cover key is a valid.
*
*/
function getRiskPoolingPeriods(bytes32 coverKey) external view override returns (uint256 lendingPeriod, uint256 withdrawalWindow) {
return s.getRiskPoolingPeriodsInternal(coverKey);
}
/**
* @dev Returns a list of disabled strategies.
*/
function getDisabledStrategies() external view override returns (address[] memory strategies) {
return s.getDisabledStrategiesInternal();
}
/**
* @dev Returns a list of actively lending strategies.
*/
function getActiveStrategies() external view override returns (address[] memory strategies) {
return s.getActiveStrategiesInternal();
}
function addBulkLiquidity(IVault.AddLiquidityArgs[] calldata args) external override {
IERC20 stablecoin = IERC20(s.getStablecoinAddressInternal());
IERC20 npm = s.getNpmTokenInstanceInternal();
uint256 totalAmount;
uint256 totalNpm;
for (uint256 i = 0; i < args.length; i++) {
totalAmount += args[i].amount;
totalNpm += args[i].npmStakeToAdd;
}
stablecoin.ensureTransferFrom(msg.sender, address(this), totalAmount);
npm.ensureTransferFrom(msg.sender, address(this), totalNpm);
for (uint256 i = 0; i < args.length; i++) {
IVault vault = s.getVault(args[i].coverKey);
uint256 balance = vault.balanceOf(address(this));
if (balance > 0) {
IERC20(vault).ensureTransfer(s.getTreasuryAddressInternal(), balance);
}
stablecoin.approve(address(vault), args[i].amount);
npm.approve(address(vault), args[i].npmStakeToAdd);
vault.addLiquidity(args[i]);
balance = vault.balanceOf(address(this));
require(balance > 0, "Fatal, no PODs minted");
IERC20(vault).ensureTransfer(msg.sender, vault.balanceOf(address(this)));
}
}
/**
* @dev Version number of this contract
*/
function version() external pure override returns (bytes32) {
return "v0.1";
}
/**
* @dev Name of this contract
*/
function getName() external pure override returns (bytes32) {
return ProtoUtilV1.CNAME_LIQUIDITY_ENGINE;
}
}