-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathProtocolSettings.sol
976 lines (843 loc) · 33.8 KB
/
ProtocolSettings.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
/**
* Copyright 2017-2020, bZeroX, LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
*/
pragma solidity 0.5.17;
pragma experimental ABIEncoderV2;
import "../core/State.sol";
import "../events/ProtocolSettingsEvents.sol";
import "../openzeppelin/SafeERC20.sol";
import "../mixins/ProtocolTokenUser.sol";
import "../modules/interfaces/ProtocolSwapExternalInterface.sol";
import "../mixins/ModuleCommonFunctionalities.sol";
import "../governance/IFeeSharingCollector.sol";
import "../feeds/IPriceFeeds.sol";
/**
* @title Protocol Settings contract.
*
* @notice This contract code comes from bZx. bZx is a protocol for tokenized
* margin trading and lending https://bzx.network similar to the dYdX protocol.
*
* This contract contains functions to customize protocol settings.
* */
contract ProtocolSettings is
State,
ProtocolTokenUser,
ProtocolSettingsEvents,
ModuleCommonFunctionalities
{
using SafeERC20 for IERC20;
using SafeMath for uint256;
/**
* @notice Empty public constructor.
* */
constructor() public {}
/**
* @notice Fallback function is to react to receiving value (rBTC).
* */
function() external {
revert("fallback not allowed");
}
/**
* @notice Set function selectors on target contract.
*
* @param target The address of the target contract.
* */
function initialize(address target) external onlyAdminOrOwner {
address prevModuleContractAddress = logicTargets[this.setPriceFeedContract.selector];
_setTarget(this.setPriceFeedContract.selector, target);
_setTarget(this.setSwapsImplContract.selector, target);
_setTarget(this.setLoanPool.selector, target);
_setTarget(this.setSupportedTokens.selector, target);
_setTarget(this.setLendingFeePercent.selector, target);
_setTarget(this.setTradingFeePercent.selector, target);
_setTarget(this.setBorrowingFeePercent.selector, target);
_setTarget(this.setSwapExternalFeePercent.selector, target);
_setTarget(this.setAffiliateFeePercent.selector, target);
_setTarget(this.setAffiliateTradingTokenFeePercent.selector, target);
_setTarget(this.setLiquidationIncentivePercent.selector, target);
_setTarget(this.setMaxDisagreement.selector, target);
_setTarget(this.setSourceBuffer.selector, target);
_setTarget(this.setMaxSwapSize.selector, target);
_setTarget(this.setFeesController.selector, target);
_setTarget(this.withdrawFees.selector, target);
_setTarget(this.withdrawLendingFees.selector, target);
_setTarget(this.withdrawTradingFees.selector, target);
_setTarget(this.withdrawBorrowingFees.selector, target);
_setTarget(this.withdrawProtocolToken.selector, target);
_setTarget(this.depositProtocolToken.selector, target);
_setTarget(this.getLoanPoolsList.selector, target);
_setTarget(this.isLoanPool.selector, target);
_setTarget(this.setSovrynSwapContractRegistryAddress.selector, target);
_setTarget(this.setWrbtcToken.selector, target);
_setTarget(this.setProtocolTokenAddress.selector, target);
_setTarget(this.setRolloverBaseReward.selector, target);
_setTarget(this.setRebatePercent.selector, target);
_setTarget(this.setSpecialRebates.selector, target);
_setTarget(this.setSovrynProtocolAddress.selector, target);
_setTarget(this.setSOVTokenAddress.selector, target);
_setTarget(this.setLockedSOVAddress.selector, target);
_setTarget(this.setMinReferralsToPayoutAffiliates.selector, target);
_setTarget(this.getSpecialRebates.selector, target);
_setTarget(this.getProtocolAddress.selector, target);
_setTarget(this.getSovTokenAddress.selector, target);
_setTarget(this.getLockedSOVAddress.selector, target);
_setTarget(this.getFeeRebatePercent.selector, target);
_setTarget(this.togglePaused.selector, target);
_setTarget(this.isProtocolPaused.selector, target);
_setTarget(this.getSwapExternalFeePercent.selector, target);
_setTarget(this.setTradingRebateRewardsBasisPoint.selector, target);
_setTarget(this.getTradingRebateRewardsBasisPoint.selector, target);
_setTarget(this.getDedicatedSOVRebate.selector, target);
_setTarget(this.setRolloverFlexFeePercent.selector, target);
_setTarget(this.getDefaultPathConversion.selector, target);
_setTarget(this.setDefaultPathConversion.selector, target);
_setTarget(this.removeDefaultPathConversion.selector, target);
_setTarget(this.setAdmin.selector, target);
_setTarget(this.getAdmin.selector, target);
_setTarget(this.setPauser.selector, target);
_setTarget(this.getPauser.selector, target);
emit ProtocolModuleContractReplaced(prevModuleContractAddress, target, "ProtocolSettings");
}
/**
* setting wrong address will break inter module functions calling
* should be set once
*/
function setSovrynProtocolAddress(
address newProtocolAddress
) external onlyAdminOrOwner whenNotPaused {
address oldProtocolAddress = protocolAddress;
protocolAddress = newProtocolAddress;
emit SetProtocolAddress(msg.sender, oldProtocolAddress, newProtocolAddress);
}
function setSOVTokenAddress(
address newSovTokenAddress
) external onlyAdminOrOwner whenNotPaused {
require(Address.isContract(newSovTokenAddress), "newSovTokenAddress not a contract");
address oldTokenAddress = sovTokenAddress;
sovTokenAddress = newSovTokenAddress;
emit SetSOVTokenAddress(msg.sender, oldTokenAddress, newSovTokenAddress);
}
function setLockedSOVAddress(
address newLockedSOVAddress
) external onlyAdminOrOwner whenNotPaused {
require(Address.isContract(newLockedSOVAddress), "newLockSOVAddress not a contract");
address oldLockedSOVAddress = lockedSOVAddress;
lockedSOVAddress = newLockedSOVAddress;
emit SetLockedSOVAddress(msg.sender, oldLockedSOVAddress, newLockedSOVAddress);
}
/**
* @notice Set the basis point of trading rebate rewards (SOV), max value is 9999 (99.99% liquid, 0.01% vested).
*
* @param newBasisPoint Basis point value.
*/
function setTradingRebateRewardsBasisPoint(
uint256 newBasisPoint
) external onlyAdminOrOwner whenNotPaused {
require(newBasisPoint <= 9999, "value too high");
uint256 oldBasisPoint = tradingRebateRewardsBasisPoint;
tradingRebateRewardsBasisPoint = newBasisPoint;
emit SetTradingRebateRewardsBasisPoint(msg.sender, oldBasisPoint, newBasisPoint);
}
/**
* @notice Update the minimum number of referrals to get affiliates rewards.
*
* @param newMinReferrals The new minimum number of referrals.
* */
function setMinReferralsToPayoutAffiliates(
uint256 newMinReferrals
) external onlyAdminOrOwner whenNotPaused {
uint256 oldMinReferrals = minReferralsToPayout;
minReferralsToPayout = newMinReferrals;
emit SetMinReferralsToPayoutAffiliates(msg.sender, oldMinReferrals, newMinReferrals);
}
/**
* @notice Set the address of the Price Feed instance.
*
* @param newContract The address of the Price Feed new instance.
* */
function setPriceFeedContract(address newContract) external onlyAdminOrOwner whenNotPaused {
address oldContract = priceFeeds;
priceFeeds = newContract;
emit SetPriceFeedContract(msg.sender, oldContract, newContract);
}
/**
* @notice Set the address of the asset swapper instance.
*
* @param newContract The address of the asset swapper new instance.
* */
function setSwapsImplContract(address newContract) external onlyAdminOrOwner whenNotPaused {
address oldContract = swapsImpl;
swapsImpl = newContract;
emit SetSwapsImplContract(msg.sender, oldContract, newContract);
}
/**
* @notice Set a list of loan pools and its tokens.
*
* @param pools The array of addresses of new loan pool instances.
* @param assets The array of addresses of the corresponding underlying tokens.
* */
function setLoanPool(
address[] calldata pools,
address[] calldata assets
) external onlyAdminOrOwner whenNotPaused {
require(pools.length == assets.length, "count mismatch");
for (uint256 i = 0; i < pools.length; i++) {
require(pools[i] != assets[i], "pool == asset");
require(pools[i] != address(0), "pool == 0");
require(
assets[i] != address(0) || loanPoolToUnderlying[pools[i]] != address(0),
"pool not exists"
);
if (assets[i] == address(0)) {
underlyingToLoanPool[loanPoolToUnderlying[pools[i]]] = address(0);
loanPoolToUnderlying[pools[i]] = address(0);
loanPoolsSet.removeAddress(pools[i]);
} else {
loanPoolToUnderlying[pools[i]] = assets[i];
underlyingToLoanPool[assets[i]] = pools[i];
loanPoolsSet.addAddress(pools[i]);
}
emit SetLoanPool(msg.sender, pools[i], assets[i]);
}
}
/**
* @notice Set a list of supported tokens by populating the
* storage supportedTokens mapping.
*
* @param addrs The array of addresses of the tokens.
* @param toggles The array of flags indicating whether
* the corresponding token is supported or not.
* */
function setSupportedTokens(
address[] calldata addrs,
bool[] calldata toggles
) external onlyAdminOrOwner whenNotPaused {
require(addrs.length == toggles.length, "count mismatch");
for (uint256 i = 0; i < addrs.length; i++) {
supportedTokens[addrs[i]] = toggles[i];
emit SetSupportedTokens(msg.sender, addrs[i], toggles[i]);
}
}
/**
* @notice Set the value of lendingFeePercent storage variable.
*
* @param newValue The new value for lendingFeePercent.
* */
function setLendingFeePercent(uint256 newValue) external onlyAdminOrOwner whenNotPaused {
require(newValue <= 10 ** 20, "value too high");
uint256 oldValue = lendingFeePercent;
lendingFeePercent = newValue;
emit SetLendingFeePercent(msg.sender, oldValue, newValue);
}
/**
* @notice Set the value of tradingFeePercent storage variable.
*
* @param newValue The new value for tradingFeePercent.
* */
function setTradingFeePercent(uint256 newValue) external onlyAdminOrOwner whenNotPaused {
require(newValue <= 10 ** 20, "value too high");
uint256 oldValue = tradingFeePercent;
tradingFeePercent = newValue;
emit SetTradingFeePercent(msg.sender, oldValue, newValue);
}
/**
* @notice Set the value of borrowingFeePercent storage variable.
*
* @param newValue The new value for borrowingFeePercent.
* */
function setBorrowingFeePercent(uint256 newValue) external onlyAdminOrOwner whenNotPaused {
require(newValue <= 10 ** 20, "value too high");
uint256 oldValue = borrowingFeePercent;
borrowingFeePercent = newValue;
emit SetBorrowingFeePercent(msg.sender, oldValue, newValue);
}
/**
* @notice Set the value of swapExtrernalFeePercent storage variable
*
* @param newValue the new value for swapExternalFeePercent
*/
function setSwapExternalFeePercent(uint256 newValue) external onlyAdminOrOwner whenNotPaused {
require(newValue <= 10 ** 20, "value too high");
uint256 oldValue = swapExtrernalFeePercent;
swapExtrernalFeePercent = newValue;
emit SetSwapExternalFeePercent(msg.sender, oldValue, newValue);
}
/**
* @notice Set the value of affiliateFeePercent storage variable.
*
* @param newValue The new value for affiliateFeePercent.
* */
function setAffiliateFeePercent(uint256 newValue) external onlyAdminOrOwner whenNotPaused {
require(newValue <= 10 ** 20, "value too high");
uint256 oldValue = affiliateFeePercent;
affiliateFeePercent = newValue;
emit SetAffiliateFeePercent(msg.sender, oldValue, newValue);
}
/**
* @notice Set the value of affiliateTradingTokenFeePercent storage variable.
*
* @param newValue The new value for affiliateTradingTokenFeePercent.
* */
function setAffiliateTradingTokenFeePercent(
uint256 newValue
) external onlyAdminOrOwner whenNotPaused {
require(newValue <= 10 ** 20, "value too high");
uint256 oldValue = affiliateTradingTokenFeePercent;
affiliateTradingTokenFeePercent = newValue;
emit SetAffiliateTradingTokenFeePercent(msg.sender, oldValue, newValue);
}
/**
* @notice Set the value of liquidationIncentivePercent storage variable.
*
* @param newValue The new value for liquidationIncentivePercent.
* */
function setLiquidationIncentivePercent(
uint256 newValue
) external onlyAdminOrOwner whenNotPaused {
require(newValue <= 10 ** 20, "value too high");
uint256 oldValue = liquidationIncentivePercent;
liquidationIncentivePercent = newValue;
emit SetLiquidationIncentivePercent(msg.sender, oldValue, newValue);
}
/**
* @notice Set the value of the maximum swap spread.
*
* @param newValue The new value for maxDisagreement.
* */
function setMaxDisagreement(uint256 newValue) external onlyAdminOrOwner whenNotPaused {
maxDisagreement = newValue;
}
/**
* @notice Set the value of the maximum source buffer.
*
* @dev To avoid rounding issues on the swap rate a small buffer is implemented.
*
* @param newValue The new value for the maximum source buffer.
* */
function setSourceBuffer(uint256 newValue) external onlyAdminOrOwner whenNotPaused {
sourceBuffer = newValue;
}
/**
* @notice Set the value of the swap size limit.
*
* @param newValue The new value for the maximum swap size.
* */
function setMaxSwapSize(uint256 newValue) external onlyAdminOrOwner whenNotPaused {
uint256 oldValue = maxSwapSize;
maxSwapSize = newValue;
emit SetMaxSwapSize(msg.sender, oldValue, newValue);
}
/**
* @notice Set the address of the feesController instance.
*
* @dev The fee sharing proxy must be the feesController of the
* protocol contract. This allows the fee sharing proxy
* to withdraw the fees.
*
* @param newController The new address of the feesController.
* */
function setFeesController(address newController) external onlyAdminOrOwner whenNotPaused {
address oldController = feesController;
feesController = newController;
emit SetFeesController(msg.sender, oldController, newController);
}
/**
* @notice Set the pauser address of sovryn protocol.
*
* only pauser or owner can perform this action.
*
* @param newPauser The new address of the pauser.
* */
function setPauser(address newPauser) external onlyOwner {
address oldPauser = pauser;
pauser = newPauser;
emit SetPauser(msg.sender, oldPauser, newPauser);
}
/**
* @dev Get pauser address.
*
*
* @return pauser address.
*/
function getPauser() external view returns (address) {
return pauser;
}
/*
* @notice Set the admin address of sovryn protocol.
*
* only owner can perform this action.
*
* @param newAdmin The new address of the admin.
* */
function setAdmin(address newAdmin) external onlyOwner {
emit SetAdmin(msg.sender, admin, newAdmin);
admin = newAdmin;
}
/**
* @dev Get admin address.
*
*
* @return admin address.
*/
function getAdmin() external view returns (address) {
return admin;
}
/**
* @notice The feesController calls this function to withdraw fees
* from three sources: lending, trading and borrowing.
* The fees (except SOV) will be converted to wRBTC.
* For SOV, it will be deposited directly to feeSharingCollector from the protocol.
*
* @param tokens The array of address of the token instance.
* @param receiver The address of the withdrawal recipient.
*
* @return The withdrawn total amount in wRBTC
* */
function withdrawFees(
address[] calldata tokens,
address receiver
) external whenNotPaused returns (uint256 totalWRBTCWithdrawn) {
require(msg.sender == feesController, "unauthorized");
for (uint256 i = 0; i < tokens.length; i++) {
uint256 lendingBalance = lendingFeeTokensHeld[tokens[i]];
if (lendingBalance > 0) {
lendingFeeTokensHeld[tokens[i]] = 0;
lendingFeeTokensPaid[tokens[i]] = lendingFeeTokensPaid[tokens[i]].add(
lendingBalance
);
}
uint256 tradingBalance = tradingFeeTokensHeld[tokens[i]];
if (tradingBalance > 0) {
tradingFeeTokensHeld[tokens[i]] = 0;
tradingFeeTokensPaid[tokens[i]] = tradingFeeTokensPaid[tokens[i]].add(
tradingBalance
);
}
uint256 borrowingBalance = borrowingFeeTokensHeld[tokens[i]];
if (borrowingBalance > 0) {
borrowingFeeTokensHeld[tokens[i]] = 0;
borrowingFeeTokensPaid[tokens[i]] = borrowingFeeTokensPaid[tokens[i]].add(
borrowingBalance
);
}
uint256 tempAmount = lendingBalance.add(tradingBalance).add(borrowingBalance);
if (tempAmount == 0) {
continue;
}
uint256 amountConvertedToWRBTC;
if (tokens[i] == address(sovTokenAddress)) {
IERC20(tokens[i]).approve(feesController, tempAmount);
IFeeSharingCollector(feesController).transferTokens(
address(sovTokenAddress),
uint96(tempAmount)
);
amountConvertedToWRBTC = 0;
} else {
if (tokens[i] == address(wrbtcToken)) {
amountConvertedToWRBTC = tempAmount;
IERC20(address(wrbtcToken)).safeTransfer(receiver, amountConvertedToWRBTC);
} else {
IERC20(tokens[i]).approve(protocolAddress, tempAmount);
(amountConvertedToWRBTC, ) = ProtocolSwapExternalInterface(protocolAddress)
.swapExternal(
tokens[i], // source token address
address(wrbtcToken), // dest token address
feesController, // set feeSharingCollector as receiver
protocolAddress, // protocol as the sender
tempAmount, // source token amount
0, // reqDestToken
0, // minReturn
"" // loan data bytes
);
/// Will revert if disagreement found.
IPriceFeeds(priceFeeds).checkPriceDisagreement(
tokens[i],
address(wrbtcToken),
tempAmount,
amountConvertedToWRBTC,
maxDisagreement
);
}
totalWRBTCWithdrawn = totalWRBTCWithdrawn.add(amountConvertedToWRBTC);
}
emit WithdrawFees(
msg.sender,
tokens[i],
receiver,
lendingBalance,
tradingBalance,
borrowingBalance,
amountConvertedToWRBTC
);
}
return totalWRBTCWithdrawn;
}
/**
* @notice The feesController calls this function to withdraw fees
* accrued from lending operations.
*
* @param token The address of the token instance.
* @param receiver The address of the withdrawal recipient.
* @param amount The amount of fees to get, ignored if greater than balance.
*
* @return Whether withdrawal was successful.
* */
function withdrawLendingFees(
address token,
address receiver,
uint256 amount
) external whenNotPaused returns (bool) {
require(msg.sender == feesController, "unauthorized");
uint256 withdrawAmount = amount;
uint256 balance = lendingFeeTokensHeld[token];
if (withdrawAmount > balance) {
withdrawAmount = balance;
}
if (withdrawAmount == 0) {
return false;
}
lendingFeeTokensHeld[token] = balance.sub(withdrawAmount);
lendingFeeTokensPaid[token] = lendingFeeTokensPaid[token].add(withdrawAmount);
IERC20(token).safeTransfer(receiver, withdrawAmount);
emit WithdrawLendingFees(msg.sender, token, receiver, withdrawAmount);
return true;
}
/**
* @notice The feesController calls this function to withdraw fees
* accrued from trading operations.
*
* @param token The address of the token instance.
* @param receiver The address of the withdrawal recipient.
* @param amount The amount of fees to get, ignored if greater than balance.
*
* @return Whether withdrawal was successful.
* */
function withdrawTradingFees(
address token,
address receiver,
uint256 amount
) external whenNotPaused returns (bool) {
require(msg.sender == feesController, "unauthorized");
uint256 withdrawAmount = amount;
uint256 balance = tradingFeeTokensHeld[token];
if (withdrawAmount > balance) {
withdrawAmount = balance;
}
if (withdrawAmount == 0) {
return false;
}
tradingFeeTokensHeld[token] = balance.sub(withdrawAmount);
tradingFeeTokensPaid[token] = tradingFeeTokensPaid[token].add(withdrawAmount);
IERC20(token).safeTransfer(receiver, withdrawAmount);
emit WithdrawTradingFees(msg.sender, token, receiver, withdrawAmount);
return true;
}
/**
* @notice The feesController calls this function to withdraw fees
* accrued from borrowing operations.
*
* @param token The address of the token instance.
* @param receiver The address of the withdrawal recipient.
* @param amount The amount of fees to get, ignored if greater than balance.
*
* @return Whether withdrawal was successful.
* */
function withdrawBorrowingFees(
address token,
address receiver,
uint256 amount
) external whenNotPaused returns (bool) {
require(msg.sender == feesController, "unauthorized");
uint256 withdrawAmount = amount;
uint256 balance = borrowingFeeTokensHeld[token];
if (withdrawAmount > balance) {
withdrawAmount = balance;
}
if (withdrawAmount == 0) {
return false;
}
borrowingFeeTokensHeld[token] = balance.sub(withdrawAmount);
borrowingFeeTokensPaid[token] = borrowingFeeTokensPaid[token].add(withdrawAmount);
IERC20(token).safeTransfer(receiver, withdrawAmount);
emit WithdrawBorrowingFees(msg.sender, token, receiver, withdrawAmount);
return true;
}
/**
* @notice The owner calls this function to withdraw protocol tokens.
*
* @dev Wrapper for ProtocolTokenUser::_withdrawProtocolToken internal function.
*
* @param receiver The address of the withdrawal recipient.
* @param amount The amount of tokens to get.
*
* @return The protocol token address.
* @return Withdrawal success (true/false).
* */
function withdrawProtocolToken(
address receiver,
uint256 amount
) external onlyAdminOrOwner whenNotPaused returns (address, bool) {
return _withdrawProtocolToken(receiver, amount);
}
/**
* @notice The owner calls this function to deposit protocol tokens.
*
* @param amount The tokens of fees to send.
* */
function depositProtocolToken(uint256 amount) external onlyAdminOrOwner whenNotPaused {
/// @dev Update local balance
protocolTokenHeld = protocolTokenHeld.add(amount);
/// @dev Send the tokens
IERC20(protocolTokenAddress).safeTransferFrom(msg.sender, address(this), amount);
}
/**
* @notice Get a list of loan pools.
*
* @param start The offset.
* @param count The limit.
*
* @return The array of loan pools.
* */
function getLoanPoolsList(
uint256 start,
uint256 count
) external view returns (bytes32[] memory) {
return loanPoolsSet.enumerate(start, count);
}
/**
* @notice Check whether a token is a pool token.
*
* @dev By querying its underlying token.
*
* @param loanPool The token address to check.
* */
function isLoanPool(address loanPool) external view returns (bool) {
return loanPoolToUnderlying[loanPool] != address(0);
}
/**
* @notice Set the contract registry address of the SovrynSwap network.
*
* @param registryAddress the address of the registry contract.
* */
function setSovrynSwapContractRegistryAddress(
address registryAddress
) external onlyAdminOrOwner whenNotPaused {
require(Address.isContract(registryAddress), "registryAddress not a contract");
address oldSovrynSwapContractRegistryAddress = sovrynSwapContractRegistryAddress;
sovrynSwapContractRegistryAddress = registryAddress;
emit SetSovrynSwapContractRegistryAddress(
msg.sender,
oldSovrynSwapContractRegistryAddress,
sovrynSwapContractRegistryAddress
);
}
/**
* @notice Set the wrBTC contract address.
*
* @param wrbtcTokenAddress The address of the wrBTC contract.
* */
function setWrbtcToken(address wrbtcTokenAddress) external onlyAdminOrOwner whenNotPaused {
require(Address.isContract(wrbtcTokenAddress), "wrbtcTokenAddress not a contract");
address oldwrbtcToken = address(wrbtcToken);
wrbtcToken = IWrbtcERC20(wrbtcTokenAddress);
emit SetWrbtcToken(msg.sender, oldwrbtcToken, wrbtcTokenAddress);
}
/**
* @notice Set the protocol token contract address.
*
* @param _protocolTokenAddress The address of the protocol token contract.
* */
function setProtocolTokenAddress(
address _protocolTokenAddress
) external onlyAdminOrOwner whenNotPaused {
require(Address.isContract(_protocolTokenAddress), "_protocolTokenAddress not a contract");
address oldProtocolTokenAddress = protocolTokenAddress;
protocolTokenAddress = _protocolTokenAddress;
emit SetProtocolTokenAddress(msg.sender, oldProtocolTokenAddress, _protocolTokenAddress);
}
/**
* @notice Set rollover base reward. It should be denominated in wrBTC.
*
* @param baseRewardValue The base reward.
* */
function setRolloverBaseReward(
uint256 baseRewardValue
) external onlyAdminOrOwner whenNotPaused {
require(baseRewardValue > 0, "Base reward is zero");
uint256 oldValue = rolloverBaseReward;
rolloverBaseReward = baseRewardValue;
emit SetRolloverBaseReward(msg.sender, oldValue, rolloverBaseReward);
}
/**
* @notice Set the fee rebate percent.
*
* @param rebatePercent The fee rebate percent.
* */
function setRebatePercent(uint256 rebatePercent) external onlyAdminOrOwner whenNotPaused {
require(rebatePercent <= 10 ** 20, "Fee rebate is too high");
uint256 oldRebatePercent = feeRebatePercent;
feeRebatePercent = rebatePercent;
emit SetRebatePercent(msg.sender, oldRebatePercent, rebatePercent);
}
/**
* @notice Set the special fee rebate percent for specific pair
*
* @param specialRebatesPercent The new special fee rebate percent.
* */
function setSpecialRebates(
address sourceToken,
address destToken,
uint256 specialRebatesPercent
) external onlyAdminOrOwner whenNotPaused {
// Set max special rebates to 1000%
require(specialRebatesPercent <= 1000e18, "Special fee rebate is too high");
uint256 oldSpecialRebatesPercent = specialRebates[sourceToken][destToken];
specialRebates[sourceToken][destToken] = specialRebatesPercent;
emit SetSpecialRebates(
msg.sender,
sourceToken,
destToken,
oldSpecialRebatesPercent,
specialRebatesPercent
);
}
/**
* @notice Get a rebate percent of specific pairs.
*
* @param sourceTokenAddress The source of pairs.
* @param destTokenAddress The dest of pairs.
*
* @return The percent rebates of the pairs.
* */
function getSpecialRebates(
address sourceTokenAddress,
address destTokenAddress
) external view returns (uint256 specialRebatesPercent) {
return specialRebates[sourceTokenAddress][destTokenAddress];
}
function getProtocolAddress() external view returns (address) {
return protocolAddress;
}
function getSovTokenAddress() external view returns (address) {
return sovTokenAddress;
}
function getLockedSOVAddress() external view returns (address) {
return lockedSOVAddress;
}
function getFeeRebatePercent() external view returns (uint256) {
return feeRebatePercent;
}
function togglePaused(bool paused) external onlyPauserOrOwner {
require(paused != pause, "Can't toggle");
pause = paused;
emit TogglePaused(msg.sender, !paused, paused);
}
function isProtocolPaused() external view returns (bool) {
return pause;
}
function getSwapExternalFeePercent() external view returns (uint256) {
return swapExtrernalFeePercent;
}
/**
* @notice Get the basis point of trading rebate rewards.
*
* @return The basis point value.
*/
function getTradingRebateRewardsBasisPoint() external view returns (uint256) {
return tradingRebateRewardsBasisPoint;
}
/**
* @dev Get how much SOV that is dedicated to pay the trading rebate rewards.
* @notice If SOV balance is less than the fees held, it will return 0.
*
* @return total dedicated SOV.
*/
function getDedicatedSOVRebate() public view returns (uint256) {
uint256 sovProtocolBalance = IERC20(sovTokenAddress).balanceOf(address(this));
uint256 sovFees = lendingFeeTokensHeld[sovTokenAddress]
.add(tradingFeeTokensHeld[sovTokenAddress])
.add(borrowingFeeTokensHeld[sovTokenAddress]);
return sovProtocolBalance >= sovFees ? sovProtocolBalance.sub(sovFees) : 0;
}
/**
* @notice Set rolloverFlexFeePercent (max value is 1%)
*
* @param newRolloverFlexFeePercent uint256 value of new rollover flex fee percentage (0.1 ether = 0.1%)
*/
function setRolloverFlexFeePercent(
uint256 newRolloverFlexFeePercent
) external onlyAdminOrOwner whenNotPaused {
require(newRolloverFlexFeePercent <= 1e18, "value too high");
uint256 oldRolloverFlexFeePercent = rolloverFlexFeePercent;
rolloverFlexFeePercent = newRolloverFlexFeePercent;
emit SetRolloverFlexFeePercent(
msg.sender,
oldRolloverFlexFeePercent,
newRolloverFlexFeePercent
);
}
/**
* @dev Get default path conversion for pairs.
*
* @param sourceTokenAddress source token address.
* @param destTokenAddress destination token address.
*
* @return default path of the conversion.
*/
function getDefaultPathConversion(
address sourceTokenAddress,
address destTokenAddress
) external view returns (IERC20[] memory) {
return defaultPathConversion[sourceTokenAddress][destTokenAddress];
}
/**
* @dev Set default path conversion for pairs.
*
* @param defaultPath array of addresses for the default path.
*
*/
function setDefaultPathConversion(
IERC20[] calldata defaultPath
) external onlyAdminOrOwner whenNotPaused {
address sourceTokenAddress = address(defaultPath[0]);
address destTokenAddress = address(defaultPath[defaultPath.length - 1]);
uint256 defaultPathLength = defaultPath.length;
require(defaultPathLength >= 3, "ERR_PATH_LENGTH");
for (uint256 i = 0; i < defaultPathLength; i++) {
require(Address.isContract(address(defaultPath[i])), "ERR_PATH_NON_CONTRACT_ADDR");
}
defaultPathConversion[sourceTokenAddress][destTokenAddress] = defaultPath;
emit SetDefaultPathConversion(
msg.sender,
sourceTokenAddress,
destTokenAddress,
defaultPath
);
}
/**
* @dev Remove the default path conversion for pairs
*
* @param sourceTokenAddress source token address.
* @param destTokenAddress destination token address
*/
function removeDefaultPathConversion(
address sourceTokenAddress,
address destTokenAddress
) external onlyAdminOrOwner whenNotPaused {
require(
defaultPathConversion[sourceTokenAddress][destTokenAddress].length > 0,
"DEFAULT_PATH_EMPTY"
);
IERC20[] memory defaultPathValue = defaultPathConversion[sourceTokenAddress][
destTokenAddress
];
delete defaultPathConversion[sourceTokenAddress][destTokenAddress];
emit RemoveDefaultPathConversion(
msg.sender,
sourceTokenAddress,
destTokenAddress,
defaultPathValue
);
}
}