-
Notifications
You must be signed in to change notification settings - Fork 2
/
VaderPoolV2.sol
449 lines (385 loc) · 13.7 KB
/
VaderPoolV2.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
// SPDX-License-Identifier: Unlicense
pragma solidity =0.8.9;
import "@openzeppelin/contracts/access/Ownable.sol";
import "./BasePoolV2.sol";
import "../../interfaces/shared/IERC20Extended.sol";
import "../../interfaces/dex-v2/pool/IVaderPoolV2.sol";
import "../../interfaces/dex-v2/wrapper/ILPWrapper.sol";
import "../../interfaces/dex-v2/synth/ISynthFactory.sol";
/*
* @dev Implementation of {VaderPoolV2} contract.
*
* The contract VaderPool inherits from {BasePoolV2} contract and implements
* queue system.
*
* Extends on the liquidity redeeming function by introducing the `burn` function
* that internally calls the namesake on `BasePoolV2` contract and computes the
* loss covered by the position being redeemed and returns it along with amounts
* of native and foreign assets sent.
**/
contract VaderPoolV2 is IVaderPoolV2, BasePoolV2, Ownable {
/* ========== LIBRARIES ========== */
// Used for safe token transfers
using SafeERC20 for IERC20;
/* ========== STATE VARIABLES ========== */
// The LP wrapper contract
ILPWrapper public wrapper;
// The Synth Factory
ISynthFactory public synthFactory;
// Denotes whether the queue system is active
bool public queueActive;
/* ========== CONSTRUCTOR ========== */
/*
* @dev Initialised the contract state by passing the native asset's address
* to the inherited {BasePoolV2} contract's constructor and setting queue status
* to the {queueActive} state variable.
**/
constructor(bool _queueActive, IERC20 _nativeAsset)
BasePoolV2(_nativeAsset)
{
queueActive = _queueActive;
}
/* ========== VIEWS ========== */
/*
* @dev Returns cumulative prices and the timestamp the were last updated
* for both native and foreign assets against the pair specified by
* parameter {foreignAsset}.
**/
function cumulativePrices(IERC20 foreignAsset)
public
view
returns (
uint256 price0CumulativeLast,
uint256 price1CumulativeLast,
uint32 blockTimestampLast
)
{
PriceCumulative memory priceCumulative = pairInfo[foreignAsset]
.priceCumulative;
price0CumulativeLast = priceCumulative.nativeLast;
price1CumulativeLast = priceCumulative.foreignLast;
blockTimestampLast = pairInfo[foreignAsset].blockTimestampLast;
}
/* ========== MUTATIVE FUNCTIONS ========== */
/*
* @dev Initializes contract's state with LP wrapper, synth factory
* and router addresses.
*
* Requirements:
* - None of the parameters are zero addresses.
* - The parameters are not already set.
* - Only callable by contract owner.
**/
function initialize(
ILPWrapper _wrapper,
ISynthFactory _synthFactory,
address _router
) external onlyOwner {
require(
wrapper == ILPWrapper(_ZERO_ADDRESS),
"VaderPoolV2::initialize: Already initialized"
);
require(
_wrapper != ILPWrapper(_ZERO_ADDRESS),
"VaderPoolV2::initialize: Incorrect Wrapper Specified"
);
require(
_synthFactory != ISynthFactory(_ZERO_ADDRESS),
"VaderPoolV2::initialize: Incorrect SynthFactory Specified"
);
require(
_router != _ZERO_ADDRESS,
"VaderPoolV2::initialize: Incorrect Router Specified"
);
wrapper = _wrapper;
synthFactory = _synthFactory;
router = _router;
}
/*
* @dev Allows minting of synthetic assets corresponding to the {foreignAsset} based
* on the native asset amount deposited and returns the minted synth asset amount.
*
* Creates the synthetic asset against {foreignAsset} if it does not already exist.
*
* Updates the cumulative prices for native and foreign assets.
*
* Requirements:
* - {foreignAsset} must be a supported token.
**/
function mintSynth(
IERC20 foreignAsset,
uint256 nativeDeposit,
address from,
address to
)
external
override
nonReentrant
supportedToken(foreignAsset)
returns (uint256 amountSynth)
{
nativeAsset.safeTransferFrom(from, address(this), nativeDeposit);
ISynth synth = synthFactory.synths(foreignAsset);
if (synth == ISynth(_ZERO_ADDRESS))
synth = synthFactory.createSynth(
IERC20Extended(address(foreignAsset))
);
(uint112 reserveNative, uint112 reserveForeign, ) = getReserves(
foreignAsset
); // gas savings
amountSynth = VaderMath.calculateSwap(
nativeDeposit,
reserveNative,
reserveForeign
);
// TODO: Clarify
_update(
foreignAsset,
reserveNative + nativeDeposit,
reserveForeign,
reserveNative,
reserveForeign
);
synth.mint(to, amountSynth);
}
/*
* @dev Allows burning of synthetic assets corresponding to the {foreignAsset}
* and returns the redeemed amount of native asset.
*
* Updates the cumulative prices for native and foreign assets.
*
* Requirements:
* - {foreignAsset} must have a valid synthetic asset against it.
* - {synthAmount} must be greater than zero.
**/
function burnSynth(
IERC20 foreignAsset,
uint256 synthAmount,
address to
) external override nonReentrant returns (uint256 amountNative) {
ISynth synth = synthFactory.synths(foreignAsset);
require(
synth != ISynth(_ZERO_ADDRESS),
"VaderPoolV2::burnSynth: Inexistent Synth"
);
require(
synthAmount > 0,
"VaderPoolV2::burnSynth: Insufficient Synth Amount"
);
IERC20(synth).safeTransferFrom(msg.sender, address(this), synthAmount);
synth.burn(synthAmount);
(uint112 reserveNative, uint112 reserveForeign, ) = getReserves(
foreignAsset
); // gas savings
amountNative = VaderMath.calculateSwap(
synthAmount,
reserveForeign,
reserveNative
);
// TODO: Clarify
_update(
foreignAsset,
reserveNative - amountNative,
reserveForeign,
reserveNative,
reserveForeign
);
nativeAsset.safeTransfer(to, amountNative);
}
/*
* @dev Allows burning of NFT represented by param {id} for liquidity redeeming.
*
* Deletes the position in {positions} mapping against the burned NFT token.
*
* Internally calls `_burn` function on {BasePoolV2} contract.
*
* Calculates the impermanent loss incurred by the position.
*
* Returns the amounts for native and foreign assets sent to the {to} address
* along with the covered loss.
*
* Requirements:
* - Can only be called by the Router.
**/
// NOTE: IL is only covered via router!
function burn(uint256 id, address to)
external
override
onlyRouter
returns (
uint256 amountNative,
uint256 amountForeign,
uint256 coveredLoss
)
{
(amountNative, amountForeign) = _burn(id, to);
Position storage position = positions[id];
uint256 creation = position.creation;
uint256 originalNative = position.originalNative;
uint256 originalForeign = position.originalForeign;
delete positions[id];
// NOTE: Validate it behaves as expected for non-18 decimal tokens
uint256 loss = VaderMath.calculateLoss(
originalNative,
originalForeign,
amountNative,
amountForeign
);
// TODO: Original Implementation Applied 100 Days
coveredLoss =
(loss * _min(block.timestamp - creation, _ONE_YEAR)) /
_ONE_YEAR;
}
/*
* @dev Allows minting of liquidity in fungible tokens. The fungible token
* is a wrapped LP token against a particular pair. The liquidity issued is also
* tracked within this contract along with liquidity issued against non-fungible
* token.
*
* Updates the cumulative prices for native and foreign assets.
*
* Calls 'mint' on the LP wrapper token contract.
*
* Requirements:
* - LP wrapper token must exist against {foreignAsset}.
**/
function mintFungible(
IERC20 foreignAsset,
uint256 nativeDeposit,
uint256 foreignDeposit,
address from,
address to
) external override nonReentrant returns (uint256 liquidity) {
IERC20Extended lp = wrapper.tokens(foreignAsset);
require(
lp != IERC20Extended(_ZERO_ADDRESS),
"VaderPoolV2::mintFungible: Unsupported Token"
);
(uint112 reserveNative, uint112 reserveForeign, ) = getReserves(
foreignAsset
); // gas savings
nativeAsset.safeTransferFrom(from, address(this), nativeDeposit);
foreignAsset.safeTransferFrom(from, address(this), foreignDeposit);
PairInfo storage pair = pairInfo[foreignAsset];
uint256 totalLiquidityUnits = pair.totalSupply;
if (totalLiquidityUnits == 0) liquidity = nativeDeposit;
else
liquidity = VaderMath.calculateLiquidityUnits(
nativeDeposit,
reserveNative,
foreignDeposit,
reserveForeign,
totalLiquidityUnits
);
require(
liquidity > 0,
"VaderPoolV2::mintFungible: Insufficient Liquidity Provided"
);
pair.totalSupply = totalLiquidityUnits + liquidity;
_update(
foreignAsset,
reserveNative + nativeDeposit,
reserveForeign + foreignDeposit,
reserveNative,
reserveForeign
);
lp.mint(to, liquidity);
emit Mint(from, to, nativeDeposit, foreignDeposit);
}
/*
* @dev Allows burning of liquidity issued in fungible tokens.
*
* Updates the cumulative prices for native and foreign assets.
*
* Calls 'burn' on the LP wrapper token contract.
*
* Requirements:
* - LP wrapper token must exist against {foreignAsset}.
* - {amountNative} and {amountForeign} redeemed, both must be greater than zero.,
**/
function burnFungible(
IERC20 foreignAsset,
uint256 liquidity,
address to
)
external
override
nonReentrant
returns (uint256 amountNative, uint256 amountForeign)
{
IERC20Extended lp = wrapper.tokens(foreignAsset);
require(
lp != IERC20Extended(_ZERO_ADDRESS),
"VaderPoolV2::burnFungible: Unsupported Token"
);
IERC20(lp).safeTransferFrom(msg.sender, address(this), liquidity);
lp.burn(liquidity);
(uint112 reserveNative, uint112 reserveForeign, ) = getReserves(
foreignAsset
); // gas savings
PairInfo storage pair = pairInfo[foreignAsset];
uint256 _totalSupply = pair.totalSupply;
amountNative = (liquidity * reserveNative) / _totalSupply;
amountForeign = (liquidity * reserveForeign) / _totalSupply;
require(
amountNative > 0 && amountForeign > 0,
"VaderPoolV2::burnFungible: Insufficient Liquidity Burned"
);
pair.totalSupply = _totalSupply - liquidity;
nativeAsset.safeTransfer(to, amountNative);
foreignAsset.safeTransfer(to, amountForeign);
_update(
foreignAsset,
reserveNative - amountNative,
reserveForeign - amountForeign,
reserveNative,
reserveForeign
);
emit Burn(msg.sender, amountNative, amountForeign, to);
}
/* ========== RESTRICTED FUNCTIONS ========== */
// TODO: Investigate Necessity
function toggleQueue() external override onlyOwner {
bool _queueActive = !queueActive;
queueActive = _queueActive;
emit QueueActive(_queueActive);
}
/*
* @dev Sets the supported state of the token represented by param {foreignAsset}.
*
* Requirements:
* - The param {foreignAsset} is not already a supported token.
**/
function setTokenSupport(IERC20 foreignAsset, bool support)
external
override
onlyOwner
{
require(
supported[foreignAsset] != support,
"VaderPoolV2::supportToken: Already At Desired State"
);
supported[foreignAsset] = support;
}
/*
* @dev Sets the supported state of the token represented by param {foreignAsset}.
*
* Requirements:
* - The param {foreignAsset} is not already a supported token.
**/
function setFungibleTokenSupport(IERC20 foreignAsset)
external
override
onlyOwner
{
wrapper.createWrapper(foreignAsset);
}
/* ========== INTERNAL FUNCTIONS ========== */
/* ========== PRIVATE FUNCTIONS ========== */
/**
* @dev Calculates the minimum of the two values
*/
function _min(uint256 a, uint256 b) private pure returns (uint256) {
return a < b ? a : b;
}
}