-
Notifications
You must be signed in to change notification settings - Fork 4
/
CnStakingContract.sol
1250 lines (1157 loc) · 39.1 KB
/
CnStakingContract.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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
//
// Changes by Swapscanner:
// - Modified to support higher solidity version.
pragma solidity >=0.8.0 <0.9.0;
import "./SafeMath.sol";
contract CnStakingContract {
using SafeMath for uint256;
/*
* Events
*/
event DeployContract(
string contractType,
address contractValidator,
address nodeId,
address rewardAddress,
address[] cnAdminList,
uint256 requirement,
uint256[] unlockTime,
uint256[] unlockAmount
);
event ReviewInitialConditions(address indexed from);
event CompleteReviewInitialConditions();
event DepositLockupStakingAndInit(address from, uint256 value);
event SubmitRequest(
uint256 indexed id,
address indexed from,
Functions functionId,
bytes32 firstArg,
bytes32 secondArg,
bytes32 thirdArg
);
event ConfirmRequest(
uint256 indexed id,
address indexed from,
Functions functionId,
bytes32 firstArg,
bytes32 secondArg,
bytes32 thirdArg,
address[] confirmers
);
event RevokeConfirmation(
uint256 indexed id,
address indexed from,
Functions functionId,
bytes32 firstArg,
bytes32 secondArg,
bytes32 thirdArg,
address[] confirmers
);
event CancelRequest(
uint256 indexed id,
address indexed from,
Functions functionId,
bytes32 firstArg,
bytes32 secondArg,
bytes32 thirdArg
);
event ExecuteRequestSuccess(
uint256 indexed id,
address indexed from,
Functions functionId,
bytes32 firstArg,
bytes32 secondArg,
bytes32 thirdArg
);
event ExecuteRequestFailure(
uint256 indexed id,
address indexed from,
Functions functionId,
bytes32 firstArg,
bytes32 secondArg,
bytes32 thirdArg
);
event ClearRequest();
event AddAdmin(address indexed admin);
event DeleteAdmin(address indexed admin);
event UpdateRequirement(uint256 requirement);
event WithdrawLockupStaking(address indexed to, uint256 value);
event ApproveStakingWithdrawal(
uint256 approvedWithdrawalId,
address to,
uint256 value,
uint256 withdrawableFrom
);
event CancelApprovedStakingWithdrawal(
uint256 approvedWithdrawalId,
address to,
uint256 value
);
event UpdateRewardAddress(address rewardAddress);
event StakeKlay(address from, uint256 value);
event WithdrawApprovedStaking(
uint256 approvedWithdrawalId,
address to,
uint256 value
);
//AddressBook event
event ReviseRewardAddress(
address cnNodeId,
address prevRewardAddress,
address curRewardAddress
);
/*
* Constants
*/
uint256 public constant MAX_ADMIN = 50;
string public constant CONTRACT_TYPE = "CnStakingContract";
uint256 public constant VERSION = 1;
address public constant ADDRESS_BOOK_ADDRESS =
0x0000000000000000000000000000000000000400;
uint256 public constant ONE_WEEK = 1 weeks;
/*
* Enums
*/
enum RequestState {
Unknown,
NotConfirmed,
Executed,
ExecutionFailed,
Canceled
}
enum Functions {
Unknown,
AddAdmin,
DeleteAdmin,
UpdateRequirement,
ClearRequest,
WithdrawLockupStaking,
ApproveStakingWithdrawal,
CancelApprovedStakingWithdrawal,
UpdateRewardAddress
}
enum WithdrawalStakingState {
Unknown,
Transferred,
Canceled
}
/*
* Storage
*/
address[] private adminList;
uint256 public requirement;
mapping(address => bool) private isAdmin;
uint256 public lastClearedId;
uint256 public requestCount;
mapping(uint256 => Request) private requestMap;
struct Request {
Functions functionId;
bytes32 firstArg;
bytes32 secondArg;
bytes32 thirdArg;
address requestProposer;
address[] confirmers;
RequestState state;
}
address public contractValidator;
bool public isInitialized;
struct LockupConditions {
uint256[] unlockTime;
uint256[] unlockAmount;
bool allReviewed;
uint256 reviewedCount;
mapping(address => bool) reviewedAdmin;
}
LockupConditions public lockupConditions;
uint256 public initialLockupStaking;
uint256 public remainingLockupStaking;
address public nodeId;
address public rewardAddress;
uint256 public staking;
uint256 public withdrawalRequestCount;
mapping(uint256 => WithdrawalRequest) private withdrawalRequestMap;
struct WithdrawalRequest {
address payable to;
uint256 value;
uint256 withdrawableFrom;
WithdrawalStakingState state;
}
/*
* Modifiers
*/
modifier onlyMultisigTx() {
require(msg.sender == address(this), "Not a multisig-transaction.");
_;
}
modifier onlyAdmin(address _admin) {
require(isAdmin[_admin], "Address is not admin.");
_;
}
modifier adminDoesNotExist(address _admin) {
require(!isAdmin[_admin], "Admin already exists.");
_;
}
modifier notNull(address _address) {
require(_address != address(0), "Address is null");
_;
}
modifier notConfirmedRequest(uint256 _id) {
require(
requestMap[_id].state == RequestState.NotConfirmed,
"Must be at not-confirmed state."
);
_;
}
modifier validRequirement(uint256 _adminCount, uint256 _requirement) {
require(
_adminCount <= MAX_ADMIN &&
_requirement <= _adminCount &&
_requirement != 0 &&
_adminCount != 0,
"Invalid requirement."
);
_;
}
modifier beforeInit() {
require(isInitialized == false, "Contract has been initialized.");
_;
}
modifier afterInit() {
require(isInitialized == true, "Contract is not initialized.");
_;
}
/*
* Constructor
*/
constructor(
address _contractValidator,
address _nodeId,
address _rewardAddress,
address[] memory _cnAdminlist,
uint256 _requirement,
uint256[] memory _unlockTime,
uint256[] memory _unlockAmount
)
public
validRequirement(_cnAdminlist.length, _requirement)
notNull(_nodeId)
notNull(_rewardAddress)
{
require(_contractValidator != address(0), "Validator is null.");
isAdmin[_contractValidator] = true;
for (uint256 i = 0; i < _cnAdminlist.length; i++) {
require(
!isAdmin[_cnAdminlist[i]] && _cnAdminlist[i] != address(0),
"Address is null or not unique."
);
isAdmin[_cnAdminlist[i]] = true;
}
require(
_unlockTime.length != 0 &&
_unlockAmount.length != 0 &&
_unlockTime.length == _unlockAmount.length,
"Invalid unlock time and amount."
);
uint256 unlockTime = block.timestamp;
for (uint256 i = 0; i < _unlockAmount.length; i++) {
require(
unlockTime < _unlockTime[i],
"Unlock time is not in ascending order."
);
require(_unlockAmount[i] > 0, "Amount is not positive number.");
unlockTime = _unlockTime[i];
}
contractValidator = _contractValidator;
nodeId = _nodeId;
rewardAddress = _rewardAddress;
adminList = _cnAdminlist;
requirement = _requirement;
lockupConditions.unlockTime = _unlockTime;
lockupConditions.unlockAmount = _unlockAmount;
isInitialized = false;
emit DeployContract(
CONTRACT_TYPE,
_contractValidator,
_nodeId,
_rewardAddress,
_cnAdminlist,
_requirement,
_unlockTime,
_unlockAmount
);
}
function reviewInitialConditions()
external
onlyAdmin(msg.sender)
beforeInit
{
require(
lockupConditions.reviewedAdmin[msg.sender] == false,
"Msg.sender already reviewed."
);
lockupConditions.reviewedAdmin[msg.sender] = true;
lockupConditions.reviewedCount = lockupConditions.reviewedCount.add(1);
emit ReviewInitialConditions(msg.sender);
if (lockupConditions.reviewedCount == adminList.length + 1) {
lockupConditions.allReviewed = true;
emit CompleteReviewInitialConditions();
}
}
function depositLockupStakingAndInit() external payable beforeInit {
uint256 requiredStakingAmount;
uint256 cnt = lockupConditions.unlockAmount.length;
for (uint256 i = 0; i < cnt; i++) {
requiredStakingAmount = requiredStakingAmount.add(
lockupConditions.unlockAmount[i]
);
}
require(
lockupConditions.allReviewed == true,
"Reviewing is not finished."
);
require(msg.value == requiredStakingAmount, "Value does not match.");
isAdmin[contractValidator] = false;
delete contractValidator;
initialLockupStaking = requiredStakingAmount;
remainingLockupStaking = requiredStakingAmount;
isInitialized = true;
emit DepositLockupStakingAndInit(msg.sender, msg.value);
}
/*
* Submit multisig function request
*/
/// @notice submit a request to add new admin at consensus node (multi-sig operation)
/// @param _admin new admin address to be added
function submitAddAdmin(
address _admin
)
external
afterInit
adminDoesNotExist(_admin)
notNull(_admin)
onlyAdmin(msg.sender)
validRequirement(adminList.length.add(1), requirement)
{
uint256 id = requestCount;
submitRequest(
id,
Functions.AddAdmin,
bytes32(uint256(uint160(_admin)) << 96),
0,
0
);
confirmRequest(
id,
requestMap[id].functionId,
requestMap[id].firstArg,
requestMap[id].secondArg,
requestMap[id].thirdArg
);
}
/// @notice submit a request to delete an admin at consensus node (multi-sig operation)
/// @param _admin address of the admin to be deleted
function submitDeleteAdmin(
address _admin
)
external
afterInit
onlyAdmin(_admin)
notNull(_admin)
onlyAdmin(msg.sender)
validRequirement(adminList.length.sub(1), requirement)
{
uint256 id = requestCount;
submitRequest(
id,
Functions.DeleteAdmin,
bytes32(uint256(uint160(_admin)) << 96),
0,
0
);
confirmRequest(
id,
requestMap[id].functionId,
requestMap[id].firstArg,
requestMap[id].secondArg,
requestMap[id].thirdArg
);
}
/// @notice submit a request to update the confirmation threshold (multi-sig operation)
/// @param _requirement new confirmation threshold
function submitUpdateRequirement(
uint256 _requirement
)
external
afterInit
onlyAdmin(msg.sender)
validRequirement(adminList.length, _requirement)
{
require(_requirement != requirement, "Invalid value");
uint256 id = requestCount;
submitRequest(
id,
Functions.UpdateRequirement,
bytes32(_requirement),
0,
0
);
confirmRequest(
id,
requestMap[id].functionId,
requestMap[id].firstArg,
requestMap[id].secondArg,
requestMap[id].thirdArg
);
}
/// @notice submit a request to clear all unfinalized request (multi-sig operation)
function submitClearRequest() external afterInit onlyAdmin(msg.sender) {
uint256 id = requestCount;
submitRequest(id, Functions.ClearRequest, 0, 0, 0);
confirmRequest(
id,
requestMap[id].functionId,
requestMap[id].firstArg,
requestMap[id].secondArg,
requestMap[id].thirdArg
);
}
function submitWithdrawLockupStaking(
address _to,
uint256 _value
) external afterInit notNull(_to) onlyAdmin(msg.sender) {
uint256 withdrawableStakingAmount;
(, , , , withdrawableStakingAmount) = getLockupStakingInfo();
require(
_value > 0 && _value <= withdrawableStakingAmount,
"Invalid value."
);
uint256 id = requestCount;
submitRequest(
id,
Functions.WithdrawLockupStaking,
bytes32(uint256(uint160(_to)) << 96),
bytes32(_value),
0
);
confirmRequest(
id,
requestMap[id].functionId,
requestMap[id].firstArg,
requestMap[id].secondArg,
requestMap[id].thirdArg
);
}
/// @notice submit a request to withdraw staked KLAY (multi-sig operation).
/// 7 days after "approveStakingWithdrawal" has been requested,
/// admins can call this function for actual withdrawal for another 7-day period.
/// If the admin doesn't withraw KLAY for that 7-day period, it expires.
/// @param _to target address to receive KLAY
/// @param _value withdrawl amount of KLAY
function submitApproveStakingWithdrawal(
address _to,
uint256 _value
) external afterInit notNull(_to) onlyAdmin(msg.sender) {
require(_value > 0 && _value <= staking, "Invalid value.");
uint256 id = requestCount;
submitRequest(
id,
Functions.ApproveStakingWithdrawal,
bytes32(uint256(uint160(_to)) << 96),
bytes32(_value),
0
);
confirmRequest(
id,
requestMap[id].functionId,
requestMap[id].firstArg,
requestMap[id].secondArg,
requestMap[id].thirdArg
);
}
/// @notice submit a request to cancel KLAY withdrawl request (multi-sig operation).
/// @param _approvedWithdrawalId the withdrawal ID to cancel. The ID is acquired at the event log of ApproveStakingWithdrawal
function submitCancelApprovedStakingWithdrawal(
uint256 _approvedWithdrawalId
) external afterInit onlyAdmin(msg.sender) {
require(
withdrawalRequestMap[_approvedWithdrawalId].to != address(0),
"Withdrawal request does not exist."
);
require(
withdrawalRequestMap[_approvedWithdrawalId].state ==
WithdrawalStakingState.Unknown,
"Invalid state."
);
uint256 id = requestCount;
submitRequest(
id,
Functions.CancelApprovedStakingWithdrawal,
bytes32(_approvedWithdrawalId),
0,
0
);
confirmRequest(
id,
requestMap[id].functionId,
requestMap[id].firstArg,
requestMap[id].secondArg,
requestMap[id].thirdArg
);
}
/// @notice submit a request to update the reward address of consensus node (multi-sig operation).
/// @param _rewardAddress new reward address
function submitUpdateRewardAddress(
address _rewardAddress
) external afterInit notNull(_rewardAddress) onlyAdmin(msg.sender) {
uint256 id = requestCount;
submitRequest(
id,
Functions.UpdateRewardAddress,
bytes32(uint256(uint160(_rewardAddress)) << 96),
0,
0
);
confirmRequest(
id,
requestMap[id].functionId,
requestMap[id].firstArg,
requestMap[id].secondArg,
requestMap[id].thirdArg
);
}
/*
* Confirm and revoke confirmation
*/
/// @notice after requesting of proposer, other admin confirm the request by confirmRequest
/// @param _id the request ID. It can be obtained by submitRequest event
/// @param _functionId the function ID of the request. It can be obtained by submitRequest event or getRequestInfo getter function
/// @param _firstArg the first argument of the request. It can be obtained by submitRequest event or getRequestInfo getter function
/// @param _secondArg the second argument of the request. If there is no second argument, then it should be 0. It can be obtained by submitRequest event or getRequestInfo getter function
/// @param _thirdArg the third argument of the request. If there is no second argument, then it should be 0. It can be obtained by submitRequest event or getRequestInfo getter function
function confirmRequest(
uint256 _id,
Functions _functionId,
bytes32 _firstArg,
bytes32 _secondArg,
bytes32 _thirdArg
) public notConfirmedRequest(_id) onlyAdmin(msg.sender) {
bool hasConfirmed = false;
uint256 confirmersCnt = requestMap[_id].confirmers.length;
for (uint256 i = 0; i < confirmersCnt; i++) {
if (msg.sender == requestMap[_id].confirmers[i]) {
hasConfirmed = true;
break;
}
}
require(!hasConfirmed, "Msg.sender already confirmed.");
require(
requestMap[_id].functionId == _functionId &&
requestMap[_id].firstArg == _firstArg &&
requestMap[_id].secondArg == _secondArg &&
requestMap[_id].thirdArg == _thirdArg,
"Function id and arguments do not match."
);
requestMap[_id].confirmers.push(msg.sender);
address[] memory confirmers = requestMap[_id].confirmers;
emit ConfirmRequest(
_id,
msg.sender,
_functionId,
_firstArg,
_secondArg,
_thirdArg,
confirmers
);
if (checkQuorum(_id)) {
executeRequest(_id);
}
}
/// @notice revoking confirmation of each admin. If the admin is proposer, then the request will be cancled regardless of confirmation of other admins.
/// @param _id the request ID. It can be obtained by submitRequest event
/// @param _functionId the function ID of the request. It can be obtained by submitRequest event or getRequestInfo getter function
/// @param _firstArg the first argument of the request. It can be obtained by submitRequest event or getRequestInfo getter function
/// @param _secondArg the second argument of the request. If there is no second argument, then it should be 0. It can be obtained by submitRequest event or getRequestInfo getter function
/// @param _thirdArg the third argument of the request. If there is no second argument, then it should be 0. It can be obtained by submitRequest event or getRequestInfo getter function
function revokeConfirmation(
uint256 _id,
Functions _functionId,
bytes32 _firstArg,
bytes32 _secondArg,
bytes32 _thirdArg
) external notConfirmedRequest(_id) onlyAdmin(msg.sender) {
bool hasConfirmed = false;
uint256 confirmersCnt = requestMap[_id].confirmers.length;
for (uint256 i = 0; i < confirmersCnt; i++) {
if (msg.sender == requestMap[_id].confirmers[i]) {
hasConfirmed = true;
break;
}
}
require(hasConfirmed, "Msg.sender has not confirmed.");
require(
requestMap[_id].functionId == _functionId &&
requestMap[_id].firstArg == _firstArg &&
requestMap[_id].secondArg == _secondArg &&
requestMap[_id].thirdArg == _thirdArg,
"Function id and arguments do not match."
);
revokeHandler(_id);
}
// this function separated from revokeConfirmation function because of 'stack too deep' issue of solidity
function revokeHandler(uint256 _id) private {
if (requestMap[_id].requestProposer == msg.sender) {
requestMap[_id].state = RequestState.Canceled;
emit CancelRequest(
_id,
msg.sender,
requestMap[_id].functionId,
requestMap[_id].firstArg,
requestMap[_id].secondArg,
requestMap[_id].thirdArg
);
} else {
deleteFromConfirmerList(_id, msg.sender);
emit RevokeConfirmation(
_id,
msg.sender,
requestMap[_id].functionId,
requestMap[_id].firstArg,
requestMap[_id].secondArg,
requestMap[_id].thirdArg,
requestMap[_id].confirmers
);
}
}
/*
* Multisig functions
*/
function addAdmin(
address _admin
)
external
onlyMultisigTx
adminDoesNotExist(_admin)
validRequirement(adminList.length.add(1), requirement)
{
isAdmin[_admin] = true;
adminList.push(_admin);
clearRequest();
emit AddAdmin(_admin);
}
function deleteAdmin(
address _admin
)
external
onlyMultisigTx
onlyAdmin(_admin)
validRequirement(adminList.length.sub(1), requirement)
{
isAdmin[_admin] = false;
uint256 adminCnt = adminList.length;
for (uint256 i = 0; i < adminCnt - 1; i++) {
if (adminList[i] == _admin) {
adminList[i] = adminList[adminCnt - 1];
break;
}
}
adminList.pop();
clearRequest();
emit DeleteAdmin(_admin);
}
function updateRequirement(
uint256 _requirement
) external onlyMultisigTx validRequirement(adminList.length, _requirement) {
requirement = _requirement;
clearRequest();
emit UpdateRequirement(_requirement);
}
function clearRequest() public onlyMultisigTx {
for (uint256 i = lastClearedId; i < requestCount; i++) {
if (requestMap[i].state == RequestState.NotConfirmed) {
requestMap[i].state = RequestState.Canceled;
}
}
lastClearedId = requestCount;
emit ClearRequest();
}
function withdrawLockupStaking(
address payable _to,
uint256 _value
) external onlyMultisigTx {
uint256 withdrawableStakingAmount;
(, , , , withdrawableStakingAmount) = getLockupStakingInfo();
require(
withdrawableStakingAmount >= _value,
"Value is not withdrawable."
);
remainingLockupStaking = remainingLockupStaking.sub(_value);
_to.transfer(_value);
emit WithdrawLockupStaking(_to, _value);
}
function approveStakingWithdrawal(
address payable _to,
uint256 _value
) external onlyMultisigTx {
require(_value <= staking, "Value is not withdrawable.");
uint256 approvedWithdrawalId = withdrawalRequestCount;
withdrawalRequestMap[approvedWithdrawalId] = WithdrawalRequest({
to: _to,
value: _value,
withdrawableFrom: block.timestamp + ONE_WEEK,
state: WithdrawalStakingState.Unknown
});
withdrawalRequestCount = withdrawalRequestCount.add(1);
emit ApproveStakingWithdrawal(
approvedWithdrawalId,
_to,
_value,
block.timestamp + ONE_WEEK
);
}
function cancelApprovedStakingWithdrawal(
uint256 _approvedWithdrawalId
) external onlyMultisigTx {
require(
withdrawalRequestMap[_approvedWithdrawalId].to != address(0),
"Withdrawal request does not exist."
);
require(
withdrawalRequestMap[_approvedWithdrawalId].state ==
WithdrawalStakingState.Unknown,
"Invalid state."
);
withdrawalRequestMap[_approvedWithdrawalId]
.state = WithdrawalStakingState.Canceled;
emit CancelApprovedStakingWithdrawal(
_approvedWithdrawalId,
withdrawalRequestMap[_approvedWithdrawalId].to,
withdrawalRequestMap[_approvedWithdrawalId].value
);
}
function updateRewardAddress(
address _rewardAddress
) external onlyMultisigTx {
rewardAddress = _rewardAddress;
AddressBookInterface(ADDRESS_BOOK_ADDRESS).reviseRewardAddress(
_rewardAddress
);
emit UpdateRewardAddress(rewardAddress);
}
/*
* Public functions
*/
/// @notice stake KLAY
function stakeKlay() external payable afterInit {
require(msg.value > 0, "Invalid amount.");
staking = staking.add(msg.value);
emit StakeKlay(msg.sender, msg.value);
}
/// @notice stake KLAY fallback function
receive() external payable afterInit {
require(msg.value > 0, "Invalid amount.");
staking = staking.add(msg.value);
emit StakeKlay(msg.sender, msg.value);
}
/// @notice 7 days after "approveStakingWithdrawal" has been requested, admins can call this function for actual withdrawal. However, it's only available for 7 days
/// @param _approvedWithdrawalId the withdrawal ID to excute. The ID is acquired at the event log of ApproveStakingWithdrawal
function withdrawApprovedStaking(
uint256 _approvedWithdrawalId
) external onlyAdmin(msg.sender) {
require(
withdrawalRequestMap[_approvedWithdrawalId].to != address(0),
"Withdrawal request does not exist."
);
require(
withdrawalRequestMap[_approvedWithdrawalId].state ==
WithdrawalStakingState.Unknown,
"Invalid state."
);
require(
withdrawalRequestMap[_approvedWithdrawalId].value <= staking,
"Value is not withdrawable."
);
require(
block.timestamp >=
withdrawalRequestMap[_approvedWithdrawalId].withdrawableFrom,
"Not withdrawable yet."
);
if (
block.timestamp >=
withdrawalRequestMap[_approvedWithdrawalId].withdrawableFrom +
ONE_WEEK
) {
withdrawalRequestMap[_approvedWithdrawalId]
.state = WithdrawalStakingState.Canceled;
emit CancelApprovedStakingWithdrawal(
_approvedWithdrawalId,
withdrawalRequestMap[_approvedWithdrawalId].to,
withdrawalRequestMap[_approvedWithdrawalId].value
);
} else {
staking = staking.sub(
withdrawalRequestMap[_approvedWithdrawalId].value
);
withdrawalRequestMap[_approvedWithdrawalId]
.state = WithdrawalStakingState.Transferred;
withdrawalRequestMap[_approvedWithdrawalId].to.transfer(
withdrawalRequestMap[_approvedWithdrawalId].value
);
emit WithdrawApprovedStaking(
_approvedWithdrawalId,
withdrawalRequestMap[_approvedWithdrawalId].to,
withdrawalRequestMap[_approvedWithdrawalId].value
);
}
}
/*
* Private functions
*/
/// @dev
function deleteFromConfirmerList(uint256 _id, address _admin) private {
uint256 confirmersCnt = requestMap[_id].confirmers.length;
for (uint256 i = 0; i < confirmersCnt; i++) {
if (_admin == requestMap[_id].confirmers[i]) {
if (i != confirmersCnt - 1) {
requestMap[_id].confirmers[i] = requestMap[_id].confirmers[
confirmersCnt - 1
];
}
requestMap[_id].confirmers.pop();
break;
}
}
}
function submitRequest(
uint256 _id,
Functions _functionId,
bytes32 _firstArg,
bytes32 _secondArg,
bytes32 _thirdArg
) private {
requestMap[_id] = Request({
functionId: _functionId,
firstArg: _firstArg,
secondArg: _secondArg,
thirdArg: _thirdArg,
requestProposer: msg.sender,
confirmers: new address[](0),
state: RequestState.NotConfirmed
});
emit SubmitRequest(
_id,
msg.sender,
_functionId,
_firstArg,
_secondArg,
_thirdArg
);
requestCount = requestCount.add(1);
}
function executeRequest(uint256 _id) private {
bool executed = false;
if (requestMap[_id].functionId == Functions.AddAdmin) {
//bytes4(keccak256("addAdmin(address)")) => 0x70480275
(executed, ) = address(this).call(
abi.encode(
0x70480275,
address(uint160(uint256(requestMap[_id].firstArg)))
)
);
}
if (requestMap[_id].functionId == Functions.DeleteAdmin) {
//bytes4(keccak256("deleteAdmin(address)")) => 0x27e1f7df
(executed, ) = address(this).call(
abi.encode(
0x27e1f7df,
address(uint160(uint256(requestMap[_id].firstArg)))
)
);
}
if (requestMap[_id].functionId == Functions.UpdateRequirement) {
//bytes4(keccak256("updateRequirement(uint256)")) => 0xc47afb3a
(executed, ) = address(this).call(
abi.encode(
0xc47afb3a,
address(uint160(uint256(requestMap[_id].firstArg)))
)
);
}
if (requestMap[_id].functionId == Functions.ClearRequest) {
//bytes4(keccak256("clearRequest()")) => 0x4f97638f
(executed, ) = address(this).call(abi.encode(0x4f97638f));
}
if (requestMap[_id].functionId == Functions.WithdrawLockupStaking) {
//bytes4(keccak256("withdrawLockupStaking(address,uint256)")) => 0x505ebed4
(executed, ) = address(this).call(
abi.encode(
0x505ebed4,
address(uint160(uint256(requestMap[_id].firstArg))),
uint256(requestMap[_id].secondArg)
)
);
}
if (requestMap[_id].functionId == Functions.ApproveStakingWithdrawal) {
//bytes4(keccak256("approveStakingWithdrawal(address,uint256)")) => 0x5df8b09a
(executed, ) = address(this).call(
abi.encode(
0x5df8b09a,
address(uint160(uint256(requestMap[_id].firstArg))),
uint256(requestMap[_id].secondArg)
)
);
}
if (
requestMap[_id].functionId ==
Functions.CancelApprovedStakingWithdrawal
) {
//bytes4(keccak256("cancelApprovedStakingWithdrawal(uint256)")) => 0xc804b115
(executed, ) = address(this).call(
abi.encode(