-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathVaultManagerV2.sol
308 lines (277 loc) · 8.2 KB
/
VaultManagerV2.sol
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// SPDX-License-Identifier: MIT
pragma solidity =0.8.17;
import {DNft} from "./DNft.sol";
import {Dyad} from "./Dyad.sol";
import {Licenser} from "./Licenser.sol";
import {Vault} from "./Vault.sol";
import {IVaultManager} from "../interfaces/IVaultManager.sol";
import {KerosineManager} from "../../src/core/KerosineManager.sol";
import {FixedPointMathLib} from "@solmate/src/utils/FixedPointMathLib.sol";
import {ERC20} from "@solmate/src/tokens/ERC20.sol";
import {SafeTransferLib} from "@solmate/src/utils/SafeTransferLib.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
contract VaultManagerV2 is IVaultManager, Initializable {
using EnumerableSet for EnumerableSet.AddressSet;
using FixedPointMathLib for uint;
using SafeTransferLib for ERC20;
uint public constant MAX_VAULTS = 5;
uint public constant MAX_VAULTS_KEROSENE = 5;
uint public constant MIN_COLLATERIZATION_RATIO = 1.5e18; // 150%
uint public constant LIQUIDATION_REWARD = 0.2e18; // 20%
DNft public immutable dNft;
Dyad public immutable dyad;
Licenser public immutable vaultLicenser;
KerosineManager public keroseneManager;
mapping (uint => EnumerableSet.AddressSet) internal vaults;
mapping (uint => EnumerableSet.AddressSet) internal vaultsKerosene;
mapping (uint => uint) public idToBlockOfLastDeposit;
modifier isDNftOwner(uint id) {
if (dNft.ownerOf(id) != msg.sender) revert NotOwner(); _;
}
modifier isValidDNft(uint id) {
if (dNft.ownerOf(id) == address(0)) revert InvalidDNft(); _;
}
modifier isLicensed(address vault) {
if (!vaultLicenser.isLicensed(vault)) revert NotLicensed(); _;
}
constructor(
DNft _dNft,
Dyad _dyad,
Licenser _licenser
) {
dNft = _dNft;
dyad = _dyad;
vaultLicenser = _licenser;
}
function setKeroseneManager(KerosineManager _keroseneManager)
external
initializer
{
keroseneManager = _keroseneManager;
}
/// @inheritdoc IVaultManager
function add(
uint id,
address vault
)
external
isDNftOwner(id)
{
if (vaults[id].length() >= MAX_VAULTS) revert TooManyVaults();
if (!vaultLicenser.isLicensed(vault)) revert VaultNotLicensed();
if (!vaults[id].add(vault)) revert VaultAlreadyAdded();
emit Added(id, vault);
}
function addKerosene(
uint id,
address vault
)
external
isDNftOwner(id)
{
if (vaultsKerosene[id].length() >= MAX_VAULTS_KEROSENE) revert TooManyVaults();
if (!keroseneManager.isLicensed(vault)) revert VaultNotLicensed();
if (!vaultsKerosene[id].add(vault)) revert VaultAlreadyAdded();
emit Added(id, vault);
}
/// @inheritdoc IVaultManager
function remove(
uint id,
address vault
)
external
isDNftOwner(id)
{
if (Vault(vault).id2asset(id) > 0) revert VaultHasAssets();
if (!vaults[id].remove(vault)) revert VaultNotAdded();
emit Removed(id, vault);
}
function removeKerosene(
uint id,
address vault
)
external
isDNftOwner(id)
{
if (Vault(vault).id2asset(id) > 0) revert VaultHasAssets();
if (!vaultsKerosene[id].remove(vault)) revert VaultNotAdded();
emit Removed(id, vault);
}
/// @inheritdoc IVaultManager
function deposit(
uint id,
address vault,
uint amount
)
external
isValidDNft(id)
{
idToBlockOfLastDeposit[id] = block.number;
Vault _vault = Vault(vault);
_vault.asset().safeTransferFrom(msg.sender, address(vault), amount);
_vault.deposit(id, amount);
}
/// @inheritdoc IVaultManager
function withdraw(
uint id,
address vault,
uint amount,
address to
)
public
isDNftOwner(id)
{
if (idToBlockOfLastDeposit[id] == block.number) revert DepositedInSameBlock();
uint dyadMinted = dyad.mintedDyad(address(this), id);
Vault _vault = Vault(vault);
uint value = amount * _vault.assetPrice()
* 1e18
/ 10**_vault.oracle().decimals()
/ 10**_vault.asset().decimals();
if (getNonKeroseneValue(id) - value < dyadMinted) revert NotEnoughExoCollat();
_vault.withdraw(id, to, amount);
if (collatRatio(id) < MIN_COLLATERIZATION_RATIO) revert CrTooLow();
}
/// @inheritdoc IVaultManager
function mintDyad(
uint id,
uint amount,
address to
)
external
isDNftOwner(id)
{
uint newDyadMinted = dyad.mintedDyad(address(this), id) + amount;
if (getNonKeroseneValue(id) < newDyadMinted) revert NotEnoughExoCollat();
dyad.mint(id, to, amount);
if (collatRatio(id) < MIN_COLLATERIZATION_RATIO) revert CrTooLow();
emit MintDyad(id, amount, to);
}
/// @inheritdoc IVaultManager
function burnDyad(
uint id,
uint amount
)
external
isValidDNft(id)
{
dyad.burn(id, msg.sender, amount);
emit BurnDyad(id, amount, msg.sender);
}
/// @inheritdoc IVaultManager
function redeemDyad(
uint id,
address vault,
uint amount,
address to
)
external
isDNftOwner(id)
returns (uint) {
dyad.burn(id, msg.sender, amount);
Vault _vault = Vault(vault);
uint asset = amount
* (10**(_vault.oracle().decimals() + _vault.asset().decimals()))
/ _vault.assetPrice()
/ 1e18;
withdraw(id, vault, asset, to);
emit RedeemDyad(id, vault, amount, to);
return asset;
}
/// @inheritdoc IVaultManager
function liquidate(
uint id,
uint to
)
external
isValidDNft(id)
isValidDNft(to)
{
uint cr = collatRatio(id);
if (cr >= MIN_COLLATERIZATION_RATIO) revert CrTooHigh();
dyad.burn(id, msg.sender, dyad.mintedDyad(address(this), id));
uint cappedCr = cr < 1e18 ? 1e18 : cr;
uint liquidationEquityShare = (cappedCr - 1e18).mulWadDown(LIQUIDATION_REWARD);
uint liquidationAssetShare = (liquidationEquityShare + 1e18).divWadDown(cappedCr);
uint numberOfVaults = vaults[id].length();
for (uint i = 0; i < numberOfVaults; i++) {
Vault vault = Vault(vaults[id].at(i));
uint collateral = vault.id2asset(id).mulWadUp(liquidationAssetShare);
vault.move(id, to, collateral);
}
emit Liquidate(id, msg.sender, to);
}
function collatRatio(
uint id
)
public
view
returns (uint) {
uint _dyad = dyad.mintedDyad(address(this), id);
if (_dyad == 0) return type(uint).max;
return getTotalUsdValue(id).divWadDown(_dyad);
}
function getTotalUsdValue(
uint id
)
public
view
returns (uint) {
return getNonKeroseneValue(id) + getKeroseneValue(id);
}
function getNonKeroseneValue(
uint id
)
public
view
returns (uint) {
uint totalUsdValue;
uint numberOfVaults = vaults[id].length();
for (uint i = 0; i < numberOfVaults; i++) {
Vault vault = Vault(vaults[id].at(i));
uint usdValue;
if (vaultLicenser.isLicensed(address(vault))) {
usdValue = vault.getUsdValue(id);
}
totalUsdValue += usdValue;
}
return totalUsdValue;
}
function getKeroseneValue(
uint id
)
public
view
returns (uint) {
uint totalUsdValue;
uint numberOfVaults = vaultsKerosene[id].length();
for (uint i = 0; i < numberOfVaults; i++) {
Vault vault = Vault(vaultsKerosene[id].at(i));
uint usdValue;
if (keroseneManager.isLicensed(address(vault))) {
usdValue = vault.getUsdValue(id);
}
totalUsdValue += usdValue;
}
return totalUsdValue;
}
// ----------------- MISC ----------------- //
function getVaults(
uint id
)
external
view
returns (address[] memory) {
return vaults[id].values();
}
function hasVault(
uint id,
address vault
)
external
view
returns (bool) {
return vaults[id].contains(vault);
}
}