-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJasmineBasePool.sol
More file actions
594 lines (516 loc) · 23 KB
/
JasmineBasePool.sol
File metadata and controls
594 lines (516 loc) · 23 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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
// ───────────────────────────────── Imports ───────────────────────────────── \\
// Inheritted Contracts
import {ERC20} from "./implementations/ERC20.sol";
import {ERC20Permit} from "./implementations/ERC20Permit.sol";
import {EATManager} from "./implementations/EATManager.sol";
import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.sol";
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
// Implemented Interfaces
import {IJasminePool} from "../../interfaces/IJasminePool.sol";
import {IJasmineEATBackedPool as IEATBackedPool} from "../../interfaces/pool/IEATBackedPool.sol";
import {IJasmineQualifiedPool as IQualifiedPool} from "../../interfaces/pool/IQualifiedPool.sol";
import {IJasmineRetireablePool as IRetireablePool} from "../../interfaces/pool/IRetireablePool.sol";
import {JasmineErrors} from "../../interfaces/errors/JasmineErrors.sol";
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {IERC1046} from "../../interfaces/ERC/IERC1046.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
// External Contracts
import {IJasmineEAT} from "../../interfaces/core/IJasmineEAT.sol";
import {JasmineRetirementService} from "../../JasmineRetirementService.sol";
import {JasminePoolFactory} from "../../JasminePoolFactory.sol";
// Utility Libraries
import {PoolPolicy} from "../../libraries/PoolPolicy.sol";
import {Calldata} from "../../libraries/Calldata.sol";
import {ArrayUtils} from "../../libraries/ArrayUtils.sol";
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
/**
* @title Jasmine Base Pool
* @author Kai Aldag<kai.aldag@jasmine.energy>
* @notice Jasmine's Base Pool contract which other pools extend as needed
* @custom:security-contact dev@jasmine.energy
*/
abstract contract JasmineBasePool is
IJasminePool,
JasmineErrors,
ERC20Permit,
EATManager,
IERC1046,
Initializable,
ReentrancyGuard
{
// ──────────────────────────────────────────────────────────────────────────────
// Libraries
// ──────────────────────────────────────────────────────────────────────────────
using ArrayUtils for uint256[];
// ──────────────────────────────────────────────────────────────────────────────
// Fields
// ──────────────────────────────────────────────────────────────────────────────
// ──────────────────────────────── Addresses ──────────────────────────────── \\
address public immutable retirementService;
address public immutable poolFactory;
// ───────────────────────────── Token Metadata ────────────────────────────── \\
/// @notice Token Display name - per ERC-20
string private _name;
/// @notice Token Symbol - per ERC-20
string private _symbol;
// ──────────────────────────────────────────────────────────────────────────────
// Setup
// ──────────────────────────────────────────────────────────────────────────────
/**
* @param _eat Address of the Jasmine Energy Attribution Token (EAT) contract
* @param _poolFactory Address of the Jasmine Pool Factory contract
* @param _retirementService Address of the Jasmine retirement service contract
* @param _contractName Name of the pool contract per EIP-712 and ERC-20
* NOTE: as pools are intended to be deployed via proxy, constructor name is not public facing
*/
constructor(
address _eat,
address _poolFactory,
address _retirementService,
string memory _contractName
) ERC20(_contractName, "JLT") ERC20Permit(_contractName) EATManager(_eat) {
if (
_eat == address(0x0) ||
_poolFactory == address(0x0) ||
_retirementService == address(0x0)
) revert JasmineErrors.ValidationFailed();
retirementService = _retirementService;
poolFactory = _poolFactory;
}
/**
* @dev Initializer function to set name and symbol
*
* @param name_ JLT token name
* @param symbol_ JLT token symbol
*/
function initialize(
string calldata name_,
string calldata symbol_
) internal onlyInitializing {
_name = name_;
_symbol = symbol_;
}
// ──────────────────────────────────────────────────────────────────────────────
// User Functionality
// ──────────────────────────────────────────────────────────────────────────────
// ─────────────────────────── Deposit Functions ───────────────────────────── \\
/// @inheritdoc IEATBackedPool
function deposit(
uint256 tokenId,
uint256 amount
) external virtual returns (uint256 jltQuantity) {
return _deposit(_msgSender(), tokenId, amount);
}
/// @inheritdoc IEATBackedPool
function depositFrom(
address from,
uint256 tokenId,
uint256 amount
) external virtual returns (uint256 jltQuantity) {
return _deposit(from, tokenId, amount);
}
/// @inheritdoc IEATBackedPool
function depositBatch(
address from,
uint256[] calldata tokenIds,
uint256[] calldata amounts
) external virtual nonReentrant returns (uint256 jltQuantity) {
IJasmineEAT(eat).safeBatchTransferFrom(
from,
address(this),
tokenIds,
amounts,
""
);
return _standardizeDecimal(amounts.sum());
}
/**
* @dev Internal utility function to deposit EATs to pool
*
* @param from Address from which EATs will be transfered
* @param tokenId ID of EAT to deposit into pool
* @param amount Number of EATs to deposit
*
* @return jltQuantity Number of JLTs issued
*/
function _deposit(
address from,
uint256 tokenId,
uint256 amount
) internal virtual nonReentrant returns (uint256 jltQuantity) {
IJasmineEAT(eat).safeTransferFrom(
from,
address(this),
tokenId,
amount,
""
);
return _standardizeDecimal(amount);
}
// ────────────────────────── Withdrawal Functions ─────────────────────────── \\
/// @inheritdoc IEATBackedPool
function withdraw(
address recipient,
uint256 amount,
bytes calldata data
)
external
virtual
returns (uint256[] memory tokenIds, uint256[] memory amounts)
{
return _withdraw(_msgSender(), recipient, amount, data);
}
/// @inheritdoc IEATBackedPool
function withdrawFrom(
address from,
address recipient,
uint256 amount,
bytes calldata data
)
external
virtual
returns (uint256[] memory tokenIds, uint256[] memory amounts)
{
return _withdraw(from, recipient, amount, data);
}
/// @inheritdoc IEATBackedPool
function withdrawSpecific(
address from,
address recipient,
uint256[] calldata tokenIds,
uint256[] calldata amounts,
bytes calldata data
) external virtual {
_withdraw(from, recipient, tokenIds, amounts, data);
}
/**
* @dev Internal utility function for withdrawing EATs where
* the pool selects the EATs to withdraw
*
* @param from JLT holder from which token will be burned
* @param recipient Address to receive EATs
* @param tokenIds EAT token IDs to withdraw
* @param amounts EAT token amounts to withdraw
* @param data Calldata relayed during EAT transfer
*/
function _withdraw(
address from,
address recipient,
uint256 amount,
bytes memory data
)
internal
virtual
withdrawal
nonReentrant
returns (uint256[] memory tokenIds, uint256[] memory amounts)
{
// 1. Ensure spender has sufficient JLTs and lengths match
uint256 cost = JasmineBasePool.withdrawalCost(amount);
// 2. Burn Tokens
_spendJLT(from, cost);
// 3. Transfer Select Tokens
(tokenIds, amounts) = _transferQueuedDeposits(recipient, amount, data);
// 4. Emit Withdrawal event
emit Withdraw(from, recipient, amount);
}
/**
* @dev Internal utility function for withdrawing EATs from pool
* in exchange for JLTs
*
* @param from JLT holder from which token will be burned
* @param recipient Address to receive EATs
* @param tokenIds EAT token IDs to withdraw
* @param amounts EAT token amounts to withdraw
* @param data Calldata relayed during EAT transfer
*/
function _withdraw(
address from,
address recipient,
uint256[] memory tokenIds,
uint256[] memory amounts,
bytes memory data
) internal virtual withdrawal nonReentrant {
// 1. Ensure spender has sufficient JLTs and lengths match
uint256 cost = JasmineBasePool.withdrawalCost(tokenIds, amounts);
// 2. Burn Tokens
_spendJLT(from, cost);
// 3. Transfer Select Tokens
_transferDeposits(recipient, tokenIds, amounts, data);
// 4. Emit Withdrawal event
emit Withdraw(from, recipient, amounts.sum());
}
// ────────────────────────── Retirement Functions ─────────────────────────── \\
/// @inheritdoc IRetireablePool
function retire(
address from,
address beneficiary,
uint256 amount,
bytes calldata data
) external virtual {
_retire(from, beneficiary, amount, data);
}
/**
* @dev Internal function to execute retirements
*
* @param from Address from which to burn JLT
* @param beneficiary Address to receive retirement accredidation
* @param amount Number of JLT to return
* @param data Additional data to encode in retirement
*/
function _retire(
address from,
address beneficiary,
uint256 amount,
bytes calldata data
) internal virtual withdrawal nonReentrant {
// 1. Burn JLTs from owner
uint256 cost = JasmineBasePool.retirementCost(amount);
_spendJLT(from, cost);
// 2. Select quantity of EATs to retire
uint256 eatQuantity = _totalDeposits -
Math.ceilDiv(totalSupply(), 10 ** decimals());
// 3. Encode transfer data
bool hasFractional = eatQuantity > (amount / (10 ** decimals()));
bytes memory retirementData;
if (eatQuantity == 0) {
emit Retirement(from, beneficiary, amount);
return;
} else if (hasFractional && eatQuantity == 1) {
retirementData = Calldata.encodeFractionalRetirementData();
} else {
retirementData = Calldata.encodeRetirementData(
beneficiary,
hasFractional
);
}
if (data.length != 0) {
retirementData = abi.encode(retirementData, data);
}
// 4. Send to retirement service and emit retirement event
_transferQueuedDeposits(retirementService, eatQuantity, retirementData);
emit Retirement(from, beneficiary, amount);
}
// ──────────────────────────────────────────────────────────────────────────────
// Jasmine Qualified Pool Implementations
// ──────────────────────────────────────────────────────────────────────────────
// ──────────────────────────── Policy Functions ───────────────────────────── \\
/// @inheritdoc IQualifiedPool
function meetsPolicy(
uint256 tokenId
) public view virtual returns (bool isEligible) {
return
IJasmineEAT(eat).exists(tokenId) &&
!IJasmineEAT(eat).frozen(tokenId);
}
/// @inheritdoc IQualifiedPool
function policyForVersion(
uint8 metadataVersion
) external view virtual returns (bytes memory policy) {
if (metadataVersion != 1)
revert JasmineErrors.UnsupportedMetadataVersion(metadataVersion);
return
abi.encode(
IJasmineEAT(eat).exists.selector,
IJasmineEAT(eat).frozen.selector
);
}
// ──────────────────────────────────────────────────────────────────────────────
// Costing Functionality
// ──────────────────────────────────────────────────────────────────────────────
/// @inheritdoc IEATBackedPool
function withdrawalCost(
uint256[] memory tokenIds,
uint256[] memory amounts
) public view virtual returns (uint256 cost) {
if (tokenIds.length != amounts.length) {
revert JasmineErrors.InvalidInput();
}
return _standardizeDecimal(amounts.sum());
}
/// @inheritdoc IEATBackedPool
function withdrawalCost(
uint256 amount
) public view virtual returns (uint256 cost) {
return _standardizeDecimal(amount);
}
/// @inheritdoc IRetireablePool
function retirementCost(
uint256 amount
) public view virtual returns (uint256 cost) {
return amount;
}
// ──────────────────────────────────────────────────────────────────────────────
// Overrides
// ──────────────────────────────────────────────────────────────────────────────
// ─────────────────────── ERC-20 Metadata Conformance ─────────────────────── \\
/**
* @inheritdoc IERC20Metadata
* @dev See {IERC20Metadata-name}
*/
function name()
public
view
override(ERC20, IERC20Metadata)
returns (string memory)
{
return _name;
}
/**
* @inheritdoc IERC20Metadata
* @dev See {IERC20Metadata-symbol}
*/
function symbol()
public
view
override(ERC20, IERC20Metadata)
returns (string memory)
{
return _symbol;
}
function decimals()
public
view
virtual
override(ERC20, IERC20Metadata)
returns (uint8)
{
return 6;
}
// ────────────────────────── ERC-1046 Conformance ─────────────────────────── \\
/**
* @inheritdoc IERC1046
* @dev Appends token symbol to end of base URI
*/
function tokenURI() external view virtual returns (string memory) {
return
string(
abi.encodePacked(
JasminePoolFactory(poolFactory).poolsBaseURI(),
_symbol
)
);
}
// ─────────────────────────── ERC-165 Conformance ─────────────────────────── \\
/**
* @inheritdoc IERC165
* @dev See {IERC165-supportsInterface}
*/
function supportsInterface(
bytes4 interfaceId
) public view virtual override(EATManager, ERC20Permit) returns (bool) {
return
interfaceId == type(IJasminePool).interfaceId ||
interfaceId == type(IERC1046).interfaceId ||
super.supportsInterface(interfaceId);
}
// ─────────────────────────────────────────────────────────────────────────────
// Token Transfer Functions
// ─────────────────────────────────────────────────────────────────────────────
// ─────────────────────── EAT Manager Deposit Hooks ───────────────────────── \\
/**
* @dev Enforce EAT eligibility before deposits
*
* @param tokenIds ERC-1155 token IDs received
*/
function _beforeDeposit(
address,
uint256[] memory tokenIds,
uint256[] memory
) internal view override {
_enforceEligibility(tokenIds);
}
/**
* @dev Mint JLTs to depositor following EAT deposit
*
* @param operator Address which initiated the deposit
* @param from Address from which ERC-1155 tokens were transferred
* @param quantity Number of ERC-1155 tokens received
*
* Emits a {Withdraw} event.
*/
function _afterDeposit(
address operator,
address from,
uint256 quantity
) internal override {
_mint(from, _standardizeDecimal(quantity));
emit Deposit(operator, from, quantity);
}
// ────────────────────── Deposit Flagging Functions ───────────────────────── \\
/**
* @dev Checks if an EAT depositted into the pool is frozen and validates internal
* balance for token. If frozen, it is internally removed from the pool's
* list of withdrawable tokens. If internal count does not match balance,
* caller will have their JLT burned to rectify the inbalance.
*
* @param tokenId EAT token ID to check
*/
function validateDepositValidity(
uint256 tokenId
) external nonReentrant returns (bool isValid) {
if (!_isTokenInRecords(tokenId)) {
revert JasmineErrors.InvalidInput();
}
uint256 preTotalDeposits = _totalDeposits;
bool isFrozen = IJasmineEAT(eat).frozen(tokenId);
bool wasUpdated = _updateTokenStatus(tokenId, !isFrozen) ||
_validateInternalBalance(tokenId);
if (wasUpdated) {
uint256 changeInDeposits = Math.max(
_totalDeposits,
preTotalDeposits
) - Math.min(_totalDeposits, preTotalDeposits);
isValid = changeInDeposits == 0;
if (isValid) return true;
if (preTotalDeposits < _totalDeposits) {
_burn(_msgSender(), _standardizeDecimal(changeInDeposits));
} else {
_mint(_msgSender(), _standardizeDecimal(changeInDeposits));
}
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Internal
// ─────────────────────────────────────────────────────────────────────────────
/**
* @dev Standardizes an integers input to the pool's ERC-20 decimal storage value
*
* @param input Integer value to standardize
*
* @return value Decimal value of input per pool's decimal specificity
*/
function _standardizeDecimal(
uint256 input
) private view returns (uint256 value) {
return input * (10 ** decimals());
}
/**
* @dev Private function for burning JLT and decreasing allowance
*/
function _spendJLT(address from, uint256 amount) private {
if (amount == 0) revert JasmineErrors.InvalidInput();
else if (from != _msgSender()) {
_spendAllowance(from, _msgSender(), amount);
}
_burn(from, amount);
}
// ──────────────────────────────── Modifiers ──────────────────────────────── \\
/**
* @dev Utility function to enforce eligibility of many EATs
*
* @dev Throws Unqualified(uint256 tokenId) on failure
*
* @param tokenIds EAT token IDs to check eligibility
*/
function _enforceEligibility(uint256[] memory tokenIds) private view {
for (uint256 i = 0; i < tokenIds.length; ) {
if (!meetsPolicy(tokenIds[i])) revert Unqualified(tokenIds[i]);
unchecked {
i++;
}
}
}
}