-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathACOFactory.sol
669 lines (602 loc) · 28.5 KB
/
ACOFactory.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
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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
pragma solidity ^0.6.6;
import "../libs/Address.sol";
import "../interfaces/IACOToken.sol";
/**
* @title ACOFactory
* @dev The contract is the implementation for the ACOProxy.
*/
contract ACOFactory {
/**
* @dev Emitted when the factory admin address has been changed.
* @param previousFactoryAdmin Address of the previous factory admin.
* @param newFactoryAdmin Address of the new factory admin.
*/
event SetFactoryAdmin(address indexed previousFactoryAdmin, address indexed newFactoryAdmin);
/**
* @dev Emitted when the ACO token implementation has been changed.
* @param previousAcoTokenImplementation Address of the previous ACO token implementation.
* @param newAcoTokenImplementation Address of the new ACO token implementation.
*/
event SetAcoTokenImplementation(address indexed previousAcoTokenImplementation, address indexed newAcoTokenImplementation);
/**
* @dev Emitted when the ACO fee has been changed.
* @param previousAcoFee Value of the previous ACO fee.
* @param newAcoFee Value of the new ACO fee.
*/
event SetAcoFee(uint256 indexed previousAcoFee, uint256 indexed newAcoFee);
/**
* @dev Emitted when the ACO fee destination address has been changed.
* @param previousAcoFeeDestination Address of the previous ACO fee destination.
* @param newAcoFeeDestination Address of the new ACO fee destination.
*/
event SetAcoFeeDestination(address indexed previousAcoFeeDestination, address indexed newAcoFeeDestination);
/**
* @dev Emitted when a new ACO token has been created.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall True if the type is CALL, false for PUT.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @param acoToken Address of the new ACO token created.
* @param acoTokenImplementation Address of the ACO token implementation used on creation.
*/
event NewAcoToken(address indexed underlying, address indexed strikeAsset, bool indexed isCall, uint256 strikePrice, uint256 expiryTime, address acoToken, address acoTokenImplementation);
/**
* @dev The ACO fee value.
* It is a percentage value (100000 is 100%).
*/
uint256 public acoFee;
/**
* @dev The factory admin address.
*/
address public factoryAdmin;
/**
* @dev The ACO token implementation address.
*/
address public acoTokenImplementation;
/**
* @dev The ACO fee destination address.
*/
address public acoFeeDestination;
/**
* @dev Modifier to check if the `msg.sender` is the factory admin.
* Only factory admin address can execute.
*/
modifier onlyFactoryAdmin() {
require(msg.sender == factoryAdmin, "ACOFactory::onlyFactoryAdmin");
_;
}
/**
* @dev Function to initialize the contract.
* It should be called through the `data` argument when creating the proxy.
* It must be called only once. The `assert` is to guarantee that behavior.
* @param _factoryAdmin Address of the factory admin.
* @param _acoTokenImplementation Address of the ACO token implementation.
* @param _acoFee Value of the ACO fee.
* @param _acoFeeDestination Address of the ACO fee destination.
*/
function init(address _factoryAdmin, address _acoTokenImplementation, uint256 _acoFee, address _acoFeeDestination) public {
require(factoryAdmin == address(0) && acoTokenImplementation == address(0), "ACOFactory::init: Contract already initialized.");
_setFactoryAdmin(_factoryAdmin);
_setAcoTokenImplementation(_acoTokenImplementation);
_setAcoFee(_acoFee);
_setAcoFeeDestination(_acoFeeDestination);
}
/**
* @dev Function to guarantee that the contract will not receive ether.
*/
receive() external payable virtual {
revert();
}
/**
* @dev Function to create a new ACO token.
* It deploys a minimal proxy for the ACO token implementation address.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @param maxExercisedAccounts The maximum number of accounts that can be exercised by transaction.
*/
function createAcoToken(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime,
uint256 maxExercisedAccounts
) onlyFactoryAdmin external virtual returns(address) {
address acoToken = _deployAcoToken(underlying, strikeAsset, isCall, strikePrice, expiryTime, maxExercisedAccounts);
emit NewAcoToken(underlying, strikeAsset, isCall, strikePrice, expiryTime, acoToken, acoTokenImplementation);
return acoToken;
}
/**
* @dev Function to set the factory admin address.
* Only can be called by the factory admin.
* @param newFactoryAdmin Address of the new factory admin.
*/
function setFactoryAdmin(address newFactoryAdmin) onlyFactoryAdmin external virtual {
_setFactoryAdmin(newFactoryAdmin);
}
/**
* @dev Function to set the ACO token implementation address.
* Only can be called by the factory admin.
* @param newAcoTokenImplementation Address of the new ACO token implementation.
*/
function setAcoTokenImplementation(address newAcoTokenImplementation) onlyFactoryAdmin external virtual {
_setAcoTokenImplementation(newAcoTokenImplementation);
}
/**
* @dev Function to set the ACO fee.
* Only can be called by the factory admin.
* @param newAcoFee Value of the new ACO fee. It is a percentage value (100000 is 100%).
*/
function setAcoFee(uint256 newAcoFee) onlyFactoryAdmin external virtual {
_setAcoFee(newAcoFee);
}
/**
* @dev Function to set the ACO destination address.
* Only can be called by the factory admin.
* @param newAcoFeeDestination Address of the new ACO destination.
*/
function setAcoFeeDestination(address newAcoFeeDestination) onlyFactoryAdmin external virtual {
_setAcoFeeDestination(newAcoFeeDestination);
}
/**
* @dev Internal function to set the factory admin address.
* @param newFactoryAdmin Address of the new factory admin.
*/
function _setFactoryAdmin(address newFactoryAdmin) internal virtual {
require(newFactoryAdmin != address(0), "ACOFactory::_setFactoryAdmin: Invalid factory admin");
emit SetFactoryAdmin(factoryAdmin, newFactoryAdmin);
factoryAdmin = newFactoryAdmin;
}
/**
* @dev Internal function to set the ACO token implementation address.
* @param newAcoTokenImplementation Address of the new ACO token implementation.
*/
function _setAcoTokenImplementation(address newAcoTokenImplementation) internal virtual {
require(Address.isContract(newAcoTokenImplementation), "ACOFactory::_setAcoTokenImplementation: Invalid ACO token implementation");
emit SetAcoTokenImplementation(acoTokenImplementation, newAcoTokenImplementation);
acoTokenImplementation = newAcoTokenImplementation;
}
/**
* @dev Internal function to set the ACO fee.
* @param newAcoFee Value of the new ACO fee. It is a percentage value (100000 is 100%).
*/
function _setAcoFee(uint256 newAcoFee) internal virtual {
emit SetAcoFee(acoFee, newAcoFee);
acoFee = newAcoFee;
}
/**
* @dev Internal function to set the ACO destination address.
* @param newAcoFeeDestination Address of the new ACO destination.
*/
function _setAcoFeeDestination(address newAcoFeeDestination) internal virtual {
require(newAcoFeeDestination != address(0), "ACOFactory::_setAcoFeeDestination: Invalid ACO fee destination");
emit SetAcoFeeDestination(acoFeeDestination, newAcoFeeDestination);
acoFeeDestination = newAcoFeeDestination;
}
/**
* @dev Internal function to deploy a minimal proxy using ACO token implementation.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall True if the type is CALL, false for PUT.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @param maxExercisedAccounts The maximum number of accounts that can be exercised by transaction.
* @return Address of the new minimal proxy deployed for the ACO token.
*/
function _deployAcoToken(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime,
uint256 maxExercisedAccounts
) internal virtual returns(address) {
bytes20 implentationBytes = bytes20(acoTokenImplementation);
address proxy;
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone, 0x14), implentationBytes)
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
proxy := create(0, clone, 0x37)
}
IACOToken(proxy).init(underlying, strikeAsset, isCall, strikePrice, expiryTime, acoFee, payable(acoFeeDestination), maxExercisedAccounts);
return proxy;
}
}
contract ACOFactoryV2 is ACOFactory {
/**
* @dev Struct to store the ACO Token basic data.
*/
struct ACOTokenData {
/**
* @dev Address of the underlying asset (0x0 for Ethereum).
*/
address underlying;
/**
* @dev Address of the strike asset (0x0 for Ethereum).
*/
address strikeAsset;
/**
* @dev True if the type is CALL, false for PUT.
*/
bool isCall;
/**
* @dev The strike price with the strike asset precision.
*/
uint256 strikePrice;
/**
* @dev The UNIX time for the ACO token expiration.
*/
uint256 expiryTime;
}
/**
* @dev The ACO token basic data.
*/
mapping(address => ACOTokenData) public acoTokenData;
/**
* @dev Function to create a new ACO token.
* It deploys a minimal proxy for the ACO token implementation address.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @param maxExercisedAccounts The maximum number of accounts that can be exercised by transaction.
* @return The created ACO token address.
*/
function createAcoToken(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime,
uint256 maxExercisedAccounts
) onlyFactoryAdmin external override virtual returns(address) {
address acoToken = _deployAcoToken(underlying, strikeAsset, isCall, strikePrice, expiryTime, maxExercisedAccounts);
acoTokenData[acoToken] = ACOTokenData(underlying, strikeAsset, isCall, strikePrice, expiryTime);
emit NewAcoToken(underlying, strikeAsset, isCall, strikePrice, expiryTime, acoToken, acoTokenImplementation);
return acoToken;
}
}
contract ACOFactoryV3 is ACOFactoryV2 {
/**
* @dev Emitted when the operator address permission has been changed.
* @param operator Address of the operator.
* @param previousPermission Whether the operator was authorized.
* @param newPermission Whether the operator will be authorized.
*/
event SetOperator(address indexed operator, bool indexed previousPermission, bool indexed newPermission);
/**
* @dev Emitted when a new ACO token has been created.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall True if the type is CALL, false for PUT.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @param acoToken Address of the new ACO token created.
* @param acoTokenImplementation Address of the ACO token implementation used on creation.
* @param creator Address of the ACO creator.
*/
event NewAcoTokenData(address indexed underlying, address indexed strikeAsset, bool indexed isCall, uint256 strikePrice, uint256 expiryTime, address acoToken, address acoTokenImplementation, address creator);
/**
* @dev A map to register the ACO Factory operators permissions.
*/
mapping(address => bool) public operators;
/**
* @dev A map to register the ACO creator.
*/
mapping(address => address) public creators;
/**
* @dev Function to set the operator permission.
* @param operator Address of the operator.
* @param newPermission Whether the operator will be authorized.
*/
function setOperator(address operator, bool newPermission) onlyFactoryAdmin external virtual {
_setOperator(operator, newPermission);
}
/**
* @dev Function to create a new ACO token.
* It deploys a minimal proxy for the ACO token implementation address.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @param maxExercisedAccounts The maximum number of accounts that can be exercised by transaction.
* @return The created ACO token address.
*/
function createAcoToken(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime,
uint256 maxExercisedAccounts
) external override virtual returns(address) {
require(operators[msg.sender], "ACOFactory::createAcoToken: Only authorized operators");
address acoToken = _deployAcoToken(underlying, strikeAsset, isCall, strikePrice, expiryTime, maxExercisedAccounts);
acoTokenData[acoToken] = ACOTokenData(underlying, strikeAsset, isCall, strikePrice, expiryTime);
creators[acoToken] = msg.sender;
emit NewAcoTokenData(underlying, strikeAsset, isCall, strikePrice, expiryTime, acoToken, acoTokenImplementation, msg.sender);
return acoToken;
}
/**
* @dev Internal function to set the operator permission.
* @param operator Address of the operator.
* @param newPermission Whether the operator will be authorized.
*/
function _setOperator(address operator, bool newPermission) internal virtual {
emit SetOperator(operator, operators[operator], newPermission);
operators[operator] = newPermission;
}
}
contract ACOFactoryV4 is ACOFactoryV3 {
uint256 public constant MAX_EXPIRATION = 157852800;
uint256 public constant DEFAULT_MAX_EXERCISED_ACCOUNTS = 100;
uint256 public constant DEFAULT_MAX_SIGNIFICANT_DIGITS = 3;
struct AssetData {
/*
* The maximum significant digits on the strike to ACO creation.
*/
uint256 maxSignificantDigits;
/*
* The maximum number of accounts that can be exercised by transaction.
*/
uint256 maxExercisedAccounts;
}
/**
* @dev Emitted when the strike asset address permission has been changed.
* @param strikeAsset Address of the strike asset.
* @param previousPermission Whether the strike asset was authorized.
* @param newPermission Whether the strike asset will be authorized.
*/
event SetStrikeAssetPermission(address indexed strikeAsset, bool indexed previousPermission, bool indexed newPermission);
/**
* @dev Emitted when the asset specific data has been changed.
* @param asset Address of the asset.
* @param previousMaxSignificantDigits Previous value of the maximum significant digits on the strike to ACO creation.
* @param previousMaxExercisedAccounts Previous value of the maximum number of accounts that can be exercised by transaction.
* @param newMaxSignificantDigits New value of the maximum significant digits on the strike to ACO creation.
* @param newMaxExercisedAccounts New value of the maximum number of accounts that can be exercised by transaction.
*/
event SetAssetSpecificData(address indexed asset, uint256 previousMaxSignificantDigits, uint256 previousMaxExercisedAccounts, uint256 newMaxSignificantDigits, uint256 newMaxExercisedAccounts);
/**
* @dev A map to register the strike assets permissions.
*/
mapping(address => bool) public strikeAssets;
/**
* @dev A map to register the ACOs by their hash.
*/
mapping(bytes32 => address) public acoHashes;
/**
* @dev A map to register the assets specific data.
*/
mapping(address => AssetData) public assetsSpecificData;
/**
* @dev Function to set the strike asset permission.
* @param strikeAsset Address of the strike asset.
* @param newPermission Whether the strike asset will be authorized.
*/
function setStrikeAssetPermission(address strikeAsset, bool newPermission) onlyFactoryAdmin external virtual {
_setStrikeAssetPermission(strikeAsset, newPermission);
}
/**
* @dev Function to set the asset specific data.
* @param asset Address of the asset.
* @param maxSignificantDigits The maximum significant digits on the strike to create an ACO.
* @param maxExercisedAccounts The maximum number of accounts that can be exercised by transaction.
*/
function setAssetSpecificData(
address asset,
uint256 maxSignificantDigits,
uint256 maxExercisedAccounts
) onlyFactoryAdmin external virtual {
_setAssetSpecificData(asset, maxSignificantDigits, maxExercisedAccounts);
}
/**
* @dev Function to get the ACO token by the parameters.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @return The ACO address or 0x0 if it does not exist.
*/
function getAcoToken(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime
) external virtual view returns(address) {
bytes32 acoHash = _getAcoHash(underlying, strikeAsset, isCall, strikePrice, expiryTime);
return acoHashes[acoHash];
}
/**
* @dev Function to create a new ACO token.
* It deploys a minimal proxy for the ACO token implementation address.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @param maxExercisedAccounts The maximum number of accounts that can be exercised by transaction.
* @return The created ACO token address.
*/
function createAcoToken(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime,
uint256 maxExercisedAccounts
) external override virtual returns(address) {
require(operators[msg.sender], "ACOFactory::createAcoToken: Only authorized operators");
return _createAcoToken(underlying, strikeAsset, isCall, strikePrice, expiryTime, maxExercisedAccounts);
}
/**
* @dev Function to create a new ACO token.
* It deploys a minimal proxy for the ACO token implementation address.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @return The created ACO token address.
*/
function newAcoToken(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime
) external virtual returns(address) {
require(strikeAssets[strikeAsset], "ACOFactory::newAcoToken: Invalid strike asset");
require(_isValidTime(expiryTime), "ACOFactory::newAcoToken: Invalid expiry time");
AssetData storage strikeAssetData = assetsSpecificData[strikeAsset];
uint256 maxSignificantDigits = _getMaxSignificantDigits(strikeAssetData);
require(_isValidStrikePrice(strikePrice, maxSignificantDigits), "ACOFactory::newAcoToken: Invalid strike price");
uint256 maxExercisedAccounts = _getMaxExercisedAccounts(underlying, isCall, strikeAssetData);
return _createAcoToken(underlying, strikeAsset, isCall, strikePrice, expiryTime, maxExercisedAccounts);
}
/**
* @dev Internal function to create a new ACO token.
* It deploys a minimal proxy for the ACO token implementation address.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @param maxExercisedAccounts The maximum number of accounts that can be exercised by transaction.
* @return The created ACO token address.
*/
function _createAcoToken(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime,
uint256 maxExercisedAccounts
) internal virtual returns(address) {
require(expiryTime <= (block.timestamp + MAX_EXPIRATION), "ACOFactory::_createAcoToken: Invalid expiry time");
bytes32 acoHash = _getAcoHash(underlying, strikeAsset, isCall, strikePrice, expiryTime);
require(acoHashes[acoHash] == address(0), "ACOFactory::_createAcoToken: ACO already exists");
address acoToken = _deployAcoToken(underlying, strikeAsset, isCall, strikePrice, expiryTime, maxExercisedAccounts);
acoTokenData[acoToken] = ACOTokenData(underlying, strikeAsset, isCall, strikePrice, expiryTime);
creators[acoToken] = msg.sender;
acoHashes[acoHash] = acoToken;
emit NewAcoTokenData(underlying, strikeAsset, isCall, strikePrice, expiryTime, acoToken, acoTokenImplementation, msg.sender);
return acoToken;
}
/**
* @dev Internal function to get the ACO hash.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param strikeAsset Address of the strike asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikePrice The strike price with the strike asset precision.
* @param expiryTime The UNIX time for the ACO token expiration.
* @return The ACO hash.
*/
function _getAcoHash(
address underlying,
address strikeAsset,
bool isCall,
uint256 strikePrice,
uint256 expiryTime
) internal pure virtual returns(bytes32) {
return keccak256(abi.encodePacked(underlying, strikeAsset, isCall, strikePrice, expiryTime));
}
/**
* @dev Internal function to get the maximum number of accounts that can be exercised by transaction for an ACO creation.
* @param underlying Address of the underlying asset (0x0 for Ethereum).
* @param isCall Whether the ACO token is the Call type.
* @param strikeAssetData The strike asset specific data.
* @return The maximum number of accounts that can be exercised by transaction for an ACO creation.
*/
function _getMaxExercisedAccounts(
address underlying,
bool isCall,
AssetData storage strikeAssetData
) internal view virtual returns(uint256) {
if (isCall) {
AssetData storage underlyingData = assetsSpecificData[underlying];
if (underlyingData.maxExercisedAccounts > 0) {
return underlyingData.maxExercisedAccounts;
}
} else if (strikeAssetData.maxExercisedAccounts > 0) {
return strikeAssetData.maxExercisedAccounts;
}
return DEFAULT_MAX_EXERCISED_ACCOUNTS;
}
/**
* @dev Internal function to get the maximum significant digits on the strike to ACO creation.
* @param strikeAssetData The strike asset specific data.
* @return The maximum significant digits on the strike to ACO creation.
*/
function _getMaxSignificantDigits(AssetData storage strikeAssetData) internal view virtual returns(uint256) {
if (strikeAssetData.maxSignificantDigits > 0) {
return strikeAssetData.maxSignificantDigits;
}
return DEFAULT_MAX_SIGNIFICANT_DIGITS;
}
/**
* @dev Internal function to check if the expiry time is 8:00 AM.
* @param expiryTime The UNIX time for the ACO token expiration.
* @return TRUE if it is 8:00 AM else FALSE.
*/
function _isValidTime(uint256 expiryTime) internal pure virtual returns(bool) {
return ((expiryTime % 60) == 0 && ((expiryTime % 3600) / 60) == 0 && ((expiryTime % 86400) / 3600) == 8);
}
/**
* @dev Internal function to check if the strike price respect the maximum significant digits allowed.
* @param strikePrice The strike price with the strike asset precision.
* @param maxSignificantDigits The maximum significant digits on the strike to ACO creation.
* @return TRUE if it is valid else FALSE.
*/
function _isValidStrikePrice(uint256 strikePrice, uint256 maxSignificantDigits) internal pure virtual returns(bool) {
uint256 i = strikePrice;
uint256 len;
while (i != 0) {
len++;
i /= 10;
}
if (len <= maxSignificantDigits) {
return true;
}
uint256 diff = len - maxSignificantDigits;
if (diff < 78) {
uint256 nonSignificant = 10 ** diff;
return ((strikePrice / nonSignificant) * nonSignificant) == strikePrice;
}
return false;
}
/**
* @dev Internal function to set the strike asset permission.
* @param strikeAsset Address of the strike asset.
* @param newPermission Whether the strike asset will be authorized.
*/
function _setStrikeAssetPermission(address strikeAsset, bool newPermission) internal virtual {
emit SetStrikeAssetPermission(strikeAsset, strikeAssets[strikeAsset], newPermission);
strikeAssets[strikeAsset] = newPermission;
}
/**
* @dev Internal function to set the asset specific data.
* @param asset Address of the asset.
* @param maxSignificantDigits The maximum significant digits on the strike to ACO creation.
* @param maxExercisedAccounts The maximum number of accounts that can be exercised by transaction.
*/
function _setAssetSpecificData(
address asset,
uint256 maxSignificantDigits,
uint256 maxExercisedAccounts
) internal virtual {
AssetData storage previousData = assetsSpecificData[asset];
emit SetAssetSpecificData(asset, previousData.maxSignificantDigits, previousData.maxExercisedAccounts, maxSignificantDigits, maxExercisedAccounts);
assetsSpecificData[asset] = AssetData(maxSignificantDigits, maxExercisedAccounts);
}
}