-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathUlyssesPool.sol
1222 lines (1035 loc) · 48.2 KB
/
UlyssesPool.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Ownable} from "solady/auth/Ownable.sol";
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {SafeCastLib} from "solady/utils/SafeCastLib.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {UlyssesERC4626} from "@ERC4626/UlyssesERC4626.sol";
import {UlyssesFactory} from "./factories/UlyssesFactory.sol";
import {IUlyssesPool} from "./interfaces/IUlyssesPool.sol";
/// @title Ulysses Pool - Single Sided Stableswap LP
/// @author Maia DAO (https://github.com/Maia-DAO)
contract UlyssesPool is UlyssesERC4626, Ownable, IUlyssesPool {
using SafeTransferLib for address;
using FixedPointMathLib for uint256;
using SafeCastLib for uint256;
/// @notice ulysses factory associated with the Ulysses LP
UlyssesFactory public immutable factory;
/// @notice ID of this Ulysses LP
uint256 public immutable id;
/// @notice List of all added LPs
BandwidthState[] public bandwidthStateList;
/// @notice destinations[destinationId] => bandwidthStateList index
mapping(uint256 => uint256) public destinations;
/// @notice destinationIds[address] => destinationId
mapping(address => uint256) public destinationIds;
/// @notice Sum of all weights
uint256 public totalWeights;
/// @notice The minimum amount that can be swapped
uint256 private constant MIN_SWAP_AMOUNT = 1e4;
/// @notice The maximum sum of all weights
uint256 private constant MAX_TOTAL_WEIGHT = 256;
/// @notice The maximum destinations that can be added
uint256 private constant MAX_DESTINATIONS = 15;
/// @notice The maximum protocol fee that can be set (1%)
uint256 private constant MAX_PROTOCOL_FEE = 1e16;
/// @notice The maximum lambda1 that can be set (10%)
uint256 private constant MAX_LAMBDA1 = 1e17;
/// @notice The minimum sigma2 that can be set (1%)
uint256 private constant MIN_SIGMA2 = 1e16;
/*//////////////////////////////////////////////////////////////
FEE PARAMETERS
//////////////////////////////////////////////////////////////*/
/// @notice The divisioner for fee calculations
uint256 private constant DIVISIONER = 1 ether;
uint256 public protocolFee = 1e14;
/// @notice The current rebalancing fees
Fees public fees = Fees({lambda1: 20e14, lambda2: 4980e14, sigma1: 6000e14, sigma2: 500e14});
/**
* @param _id the Ulysses LP ID
* @param _asset the underlying asset
* @param _name the name of the LP
* @param _symbol the symbol of the LP
* @param _owner the owner of this contract
* @param _factory the Ulysses factory
*/
constructor(
uint256 _id,
address _asset,
string memory _name,
string memory _symbol,
address _owner,
address _factory
) UlyssesERC4626(_asset, _name, _symbol) {
require(_owner != address(0));
factory = UlyssesFactory(_factory);
_initializeOwner(_owner);
require(_id != 0);
id = _id;
bandwidthStateList.push(BandwidthState({bandwidth: 0, destination: UlyssesPool(address(0)), weight: 0}));
}
/*//////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/
// @inheritdoc UlyssesERC4626
function totalAssets() public view override returns (uint256) {
return asset.balanceOf(address(this)) - getProtocolFees();
}
// @inheritdoc UlyssesERC4626
function maxRedeem(address owner) public view override returns (uint256) {
return balanceOf[owner].min(asset.balanceOf(address(this)));
}
/// @inheritdoc IUlyssesPool
function getBandwidth(uint256 destinationId) external view returns (uint256) {
/**
* @dev bandwidthStateList first element has always 0 bandwidth
* so this line will never fail and return 0 instead
*/
return bandwidthStateList[destinations[destinationId]].bandwidth;
}
/// @inheritdoc IUlyssesPool
function getBandwidthStateList() external view returns (BandwidthState[] memory) {
return bandwidthStateList;
}
/// @inheritdoc IUlyssesPool
function getProtocolFees() public view returns (uint256) {
uint256 balance = asset.balanceOf(address(this));
uint256 assets;
for (uint256 i = 1; i < bandwidthStateList.length; i++) {
uint256 targetBandwidth = totalSupply.mulDiv(bandwidthStateList[i].weight, totalWeights);
assets += _calculateRebalancingFee(bandwidthStateList[i].bandwidth, targetBandwidth, false);
assets += bandwidthStateList[i].bandwidth;
}
if (balance > assets) {
return balance - assets;
} else {
return 0;
}
}
/*//////////////////////////////////////////////////////////////
ADMIN LOGIC
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IUlyssesPool
function claimProtocolFees() external nonReentrant returns (uint256 claimed) {
claimed = getProtocolFees();
if (claimed > 0) {
asset.safeTransfer(factory.owner(), claimed);
}
}
/// @inheritdoc IUlyssesPool
function addNewBandwidth(uint256 poolId, uint8 weight) external nonReentrant onlyOwner returns (uint256 index) {
if (weight == 0) revert InvalidWeight();
UlyssesPool destination = factory.pools(poolId);
uint256 destinationId = destination.id();
if (destinationIds[address(destination)] != 0 || destinationId == id) revert InvalidPool();
if (destinationId == 0) revert NotUlyssesLP();
index = bandwidthStateList.length;
if (index > MAX_DESTINATIONS) revert TooManyDestinations();
uint256 oldRebalancingFee;
for (uint256 i = 1; i < index; i++) {
uint256 targetBandwidth = totalSupply.mulDiv(bandwidthStateList[i].weight, totalWeights);
oldRebalancingFee += _calculateRebalancingFee(bandwidthStateList[i].bandwidth, targetBandwidth, false);
}
uint256 oldTotalWeights = totalWeights;
uint256 newTotalWeights = oldTotalWeights + weight;
totalWeights = newTotalWeights;
if (newTotalWeights > MAX_TOTAL_WEIGHT) revert InvalidWeight();
uint256 newBandwidth;
for (uint256 i = 1; i < index;) {
uint256 oldBandwidth = bandwidthStateList[i].bandwidth;
if (oldBandwidth > 0) {
bandwidthStateList[i].bandwidth = oldBandwidth.mulDivUp(oldTotalWeights, newTotalWeights).toUint248();
newBandwidth += oldBandwidth - bandwidthStateList[i].bandwidth;
}
unchecked {
++i;
}
}
bandwidthStateList.push(
BandwidthState({bandwidth: newBandwidth.toUint248(), destination: destination, weight: weight})
);
destinations[destinationId] = index;
destinationIds[address(destination)] = index;
uint256 newRebalancingFee;
for (uint256 i = 1; i <= index; i++) {
uint256 targetBandwidth = totalSupply.mulDiv(bandwidthStateList[i].weight, totalWeights);
newRebalancingFee += _calculateRebalancingFee(bandwidthStateList[i].bandwidth, targetBandwidth, false);
}
if (oldRebalancingFee < newRebalancingFee) {
asset.safeTransferFrom(msg.sender, address(this), newRebalancingFee - oldRebalancingFee);
}
}
/// @inheritdoc IUlyssesPool
function setWeight(uint256 poolId, uint8 weight) external nonReentrant onlyOwner {
if (weight == 0) revert InvalidWeight();
uint256 poolIndex = destinations[poolId];
if (poolIndex == 0) revert NotUlyssesLP();
uint256 oldRebalancingFee;
for (uint256 i = 1; i < bandwidthStateList.length; i++) {
uint256 targetBandwidth = totalSupply.mulDiv(bandwidthStateList[i].weight, totalWeights);
oldRebalancingFee += _calculateRebalancingFee(bandwidthStateList[i].bandwidth, targetBandwidth, false);
}
uint256 oldTotalWeights = totalWeights;
uint256 weightsWithoutPool = oldTotalWeights - bandwidthStateList[poolIndex].weight;
uint256 newTotalWeights = weightsWithoutPool + weight;
totalWeights = newTotalWeights;
if (totalWeights > MAX_TOTAL_WEIGHT || oldTotalWeights == newTotalWeights) {
revert InvalidWeight();
}
uint256 leftOverBandwidth;
BandwidthState storage poolState = bandwidthStateList[poolIndex];
poolState.weight = weight;
if (oldTotalWeights > newTotalWeights) {
for (uint256 i = 1; i < bandwidthStateList.length;) {
if (i != poolIndex) {
uint256 oldBandwidth = bandwidthStateList[i].bandwidth;
if (oldBandwidth > 0) {
bandwidthStateList[i].bandwidth =
oldBandwidth.mulDivUp(oldTotalWeights, newTotalWeights).toUint248();
leftOverBandwidth += oldBandwidth - bandwidthStateList[i].bandwidth;
}
}
unchecked {
++i;
}
}
poolState.bandwidth += leftOverBandwidth.toUint248();
} else {
uint256 oldBandwidth = poolState.bandwidth;
if (oldBandwidth > 0) {
poolState.bandwidth = oldBandwidth.mulDivUp(oldTotalWeights, newTotalWeights).toUint248();
leftOverBandwidth += oldBandwidth - poolState.bandwidth;
}
for (uint256 i = 1; i < bandwidthStateList.length;) {
if (i != poolIndex) {
if (i == bandwidthStateList.length - 1) {
bandwidthStateList[i].bandwidth += leftOverBandwidth.toUint248();
} else if (leftOverBandwidth > 0) {
bandwidthStateList[i].bandwidth +=
leftOverBandwidth.mulDiv(bandwidthStateList[i].weight, weightsWithoutPool).toUint248();
}
}
unchecked {
++i;
}
}
}
uint256 newRebalancingFee;
for (uint256 i = 1; i < bandwidthStateList.length; i++) {
uint256 targetBandwidth = totalSupply.mulDiv(bandwidthStateList[i].weight, totalWeights);
newRebalancingFee += _calculateRebalancingFee(bandwidthStateList[i].bandwidth, targetBandwidth, false);
}
if (oldRebalancingFee < newRebalancingFee) {
asset.safeTransferFrom(msg.sender, address(this), newRebalancingFee - oldRebalancingFee);
}
}
/// @inheritdoc IUlyssesPool
function setFees(Fees calldata _fees) external nonReentrant onlyOwner {
// Lower fee must be lower than 1%
if (_fees.lambda1 > MAX_LAMBDA1) revert InvalidFee();
// Sum of both fees must be 50%
if (_fees.lambda1 + _fees.lambda2 != DIVISIONER / 2) revert InvalidFee();
// Upper bound must be lower than 100%
if (_fees.sigma1 > DIVISIONER) revert InvalidFee();
// Lower bound must be lower than Upper bound and higher than 1%
if (_fees.sigma1 <= _fees.sigma2 || _fees.sigma2 < MIN_SIGMA2) revert InvalidFee();
fees = _fees;
}
/// @inheritdoc IUlyssesPool
function setProtocolFee(uint256 _protocolFee) external nonReentrant {
if (msg.sender != factory.owner()) revert Unauthorized();
// Revert if the protocol fee is larger than 1%
if (_protocolFee > MAX_PROTOCOL_FEE) revert InvalidFee();
protocolFee = _protocolFee;
}
/*//////////////////////////////////////////////////////////////
ULYSSES LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @notice Calculates the bandwidth increase/decrease amount.
* Is called when a user is doing a swap or adding/removing liquidity.
* @param roundUp Whether to round up or down
* @param positiveTransfer Whether the transfer is positive or negative
* @param amount The amount to transfer
* @param _totalWeights The total weights
* @param _totalSupply The total supply
*/
function getBandwidthUpdateAmounts(
bool roundUp,
bool positiveTransfer,
uint256 amount,
uint256 _totalWeights,
uint256 _totalSupply
) private view returns (uint256[] memory bandwidthUpdateAmounts, uint256 length) {
// Get the bandwidth state list length
length = bandwidthStateList.length;
/// @solidity memory-safe-assembly
assembly {
// Revert if the list is empty
if eq(length, 1) {
// Store the function selector of `NotInitialized()`.
mstore(0x00, 0x87138d5c)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Revert if the amount is too small
if lt(amount, MIN_SWAP_AMOUNT) {
// Store the function selector of `AmountTooSmall()`.
mstore(0x00, 0xc2f5625a)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
}
// Initialize bandwidth update amounts
bandwidthUpdateAmounts = new uint256[](length);
// Initialize bandwidth differences from target bandwidth
uint256[] memory diffs = new uint256[](length);
/// @solidity memory-safe-assembly
assembly {
// Store bandwidth state slot in memory
mstore(0x00, bandwidthStateList.slot)
// Hash the bandwidth state slot to get the bandwidth state list start
let bandwidthStateListStart := keccak256(0x00, 0x20)
// Total difference from target bandwidth of all bandwidth states
let totalDiff
// Total difference from target bandwidth of all bandwidth states
let transfered
// Total amount to be distributed according to each bandwidth weights
let transferedChange
for { let i := 1 } lt(i, length) { i := add(i, 1) } {
// Load bandwidth and weight from storage
// Each bandwidth state occupies two storage slots
let slot := sload(add(bandwidthStateListStart, mul(i, 2)))
// Bandwidth is the first 248 bits of the slot
let bandwidth := and(slot, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
// Weight is the last 8 bits of the slot
let weight := shr(248, slot)
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(weight, gt(_totalSupply, div(not(0), weight))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Calculate the target bandwidth
let targetBandwidth := div(mul(_totalSupply, weight), _totalWeights)
// Calculate the difference from the target bandwidth
switch positiveTransfer
// If the transfer is positive, calculate deficit from target bandwidth
case true {
// If there is a deficit, store the difference
if gt(targetBandwidth, bandwidth) {
// Calculate the difference
let diff := sub(targetBandwidth, bandwidth)
// Add the difference to the total difference
totalDiff := add(totalDiff, diff)
// Store the difference in the diffs array
mstore(add(diffs, add(mul(i, 0x20), 0x20)), diff)
}
}
// If the transfer is negative, calculate surplus from target bandwidth
default {
// If there is a surplus, store the difference
if gt(bandwidth, targetBandwidth) {
// Calculate the difference
let diff := sub(bandwidth, targetBandwidth)
// Add the difference to the total difference
totalDiff := add(totalDiff, diff)
// Store the difference in the diffs array
mstore(add(diffs, add(mul(i, 0x20), 0x20)), diff)
}
}
}
// Calculate the amount to be distributed according deficit/surplus
// and/or the amount to be distributed according to each bandwidth weights
switch gt(amount, totalDiff)
// If the amount is greater than the total deficit/surplus
case true {
// Total deficit/surplus is distributed
transfered := totalDiff
// Set rest to be distributed according to each bandwidth weights
transferedChange := sub(amount, totalDiff)
}
// If the amount is less than the total deficit/surplus
default {
// Amount will be distributed according to deficit/surplus
transfered := amount
}
for { let i := 1 } lt(i, length) { i := add(i, 1) } {
// Increase/decrease amount of bandwidth for each bandwidth state
let bandwidthUpdate
// If there is a deficit/surplus, calculate the amount to be distributed
if gt(transfered, 0) {
// Load the difference from the diffs array
let diff := mload(add(diffs, add(mul(i, 0x20), 0x20)))
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(diff, gt(transfered, div(not(0), diff))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Calculate the amount to be distributed according to deficit/surplus
switch roundUp
// If round up then do mulDivUp(transfered, diff, totalDiff)
case true {
bandwidthUpdate :=
add(
iszero(iszero(mod(mul(transfered, diff), totalDiff))), div(mul(transfered, diff), totalDiff)
)
}
// If round down then do mulDiv(transfered, diff, totalDiff)
default { bandwidthUpdate := div(mul(transfered, diff), totalDiff) }
}
// If there is a rest, calculate the amount to be distributed according to each bandwidth weights
if gt(transferedChange, 0) {
// Load weight from storage
let weight := shr(248, sload(add(bandwidthStateListStart, mul(i, 2))))
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(weight, gt(transferedChange, div(not(0), weight))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Calculate the amount to be distributed according to each bandwidth weights
switch roundUp
// If round up then do mulDivUp(transferedChange, weight, _totalWeights)
case true {
bandwidthUpdate :=
add(
bandwidthUpdate,
add(
iszero(iszero(mod(mul(transferedChange, weight), _totalWeights))),
div(mul(transferedChange, weight), _totalWeights)
)
)
}
// If round down then do mulDiv(transferedChange, weight, _totalWeights)
default {
bandwidthUpdate := add(bandwidthUpdate, div(mul(transferedChange, weight), _totalWeights))
}
}
// If there is an update in bandwidth
if gt(bandwidthUpdate, 0) {
// Store the amount to be updated in the bandwidthUpdateAmounts array
mstore(add(bandwidthUpdateAmounts, add(mul(i, 0x20), 0x20)), bandwidthUpdate)
}
}
}
}
/**
* @notice Updates the bandwidth of the destination Ulysses LP
* @param depositFees Whether to deposit fees or not
* @param positiveTransfer Whether the transfer is positive or negative
* @param destinationState The state of the destination Ulysses LP
* @param difference The difference between the old and new total supply
* @param _totalWeights The total weights of all Ulysses LPs
* @param _totalSupply The total supply of the Ulysses LP
* @param _newTotalSupply The new total supply of the Ulysses LP
* @return positivefee The positive fee
*/
function updateBandwidth(
bool depositFees,
bool positiveTransfer,
BandwidthState storage destinationState,
uint256 difference,
uint256 _totalWeights,
uint256 _totalSupply,
uint256 _newTotalSupply
) private returns (uint256 positivefee, uint256 negativeFee) {
uint256 bandwidth;
uint256 targetBandwidth;
uint256 weight;
/// @solidity memory-safe-assembly
assembly {
// Load bandwidth and weight from storage
let slot := sload(destinationState.slot)
// Bandwidth is the first 248 bits of the slot
bandwidth := and(slot, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff)
// Weight is the last 8 bits of the slot
weight := shr(248, slot)
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(weight, gt(_totalSupply, div(not(0), weight))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Get the target bandwidth
targetBandwidth := div(mul(_totalSupply, weight), _totalWeights)
}
// get the rebalancing fee prior to updating the bandwidth
uint256 oldRebalancingFee = _calculateRebalancingFee(
bandwidth,
targetBandwidth,
positiveTransfer // Rounds down if positive, up if negative
);
/// @solidity memory-safe-assembly
assembly {
switch positiveTransfer
// If the transfer is positive
case true {
// Add the difference to the bandwidth
bandwidth := add(bandwidth, difference)
// Revert if bandwidth overflows
if lt(bandwidth, difference) {
// Store the function selector of `Overflow()`.
mstore(0x00, 0x35278d12)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
}
// If the transfer is negative
default {
// Revert if bandwidth underflows
if gt(difference, bandwidth) {
// Store the function selector of `Underflow()`.
mstore(0x00, 0xcaccb6d9)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Subtract the difference from the bandwidth
bandwidth := sub(bandwidth, difference)
}
// True on deposit, mint and redeem
if gt(_newTotalSupply, 0) {
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(weight, gt(_newTotalSupply, div(not(0), weight))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Get the new target bandwidth after total supply change
targetBandwidth := div(mul(_newTotalSupply, weight), _totalWeights)
}
}
// get the rebalancing fee after updating the bandwidth
uint256 newRebalancingFee = _calculateRebalancingFee(
bandwidth,
targetBandwidth,
positiveTransfer // Rounds down if positive, up if negative
);
/// @solidity memory-safe-assembly
assembly {
switch lt(newRebalancingFee, oldRebalancingFee)
// If new fee is lower than old fee
case true {
// Calculate the positive fee
positivefee := sub(oldRebalancingFee, newRebalancingFee)
// If depositFees is true, add the positive fee to the bandwidth
if depositFees {
bandwidth := add(bandwidth, positivefee)
// Revert if bandwidth overflows
if lt(bandwidth, positivefee) {
// Store the function selector of `Overflow()`.
mstore(0x00, 0x35278d12)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
}
}
default {
// If new fee is higher than old fee
if gt(newRebalancingFee, oldRebalancingFee) {
// Calculate the negative fee
negativeFee := sub(newRebalancingFee, oldRebalancingFee)
}
}
if gt(bandwidth, 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff) {
// Store the function selector of `Overflow()`.
mstore(0x00, 0x35278d12)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Update storage with the new bandwidth
sstore(destinationState.slot, or(bandwidth, shl(248, weight)))
}
}
/**
* @notice Calculates the positive or negative rebalancing fee for a bandwidth change
* @param bandwidth The new bandwidth, after decreasing or increasing the current bandwidth
* @param targetBandwidth The ideal bandwidth according to weight and totalSupply
* @param roundDown Whether to round down or up
* @return fee The rebalancing fee for this action
*/
function _calculateRebalancingFee(uint256 bandwidth, uint256 targetBandwidth, bool roundDown)
internal
view
returns (uint256 fee)
{
// If the bandwidth is larger or equal to the target bandwidth, return 0
if (bandwidth >= targetBandwidth) return 0;
// Upper bound of the first fee interval
uint256 upperBound1;
// Upper bound of the second fee interval
uint256 upperBound2;
// Fee tier 1 (fee % divided by 2)
uint256 lambda1;
// Fee tier 2 (fee % divided by 2)
uint256 lambda2;
// @solidity memory-safe-assembly
assembly {
// Load the rebalancing fee slot to get the fee parameters
let feeSlot := sload(fees.slot)
// Get sigma2 from the first 8 bytes of the fee slot
let sigma2 := shr(192, feeSlot)
// Get sigma1 from the next 8 bytes of the fee slot
let sigma1 := and(shr(128, feeSlot), 0xffffffffffffffff)
// Get lambda2 from the next 8 bytes of the fee slot
lambda2 := and(shr(64, feeSlot), 0xffffffffffffffff)
// Get lambda1 from the last 8 bytes of the fee slot
lambda1 := and(feeSlot, 0xffffffffffffffff)
// Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y))
if mul(sigma1, gt(targetBandwidth, div(not(0), sigma1))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Calculate the upper bound for the first fee
upperBound1 := div(mul(targetBandwidth, sigma1), DIVISIONER)
// Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y))
if mul(sigma2, gt(targetBandwidth, div(not(0), sigma2))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Calculate the upper bound for the second fee
upperBound2 := div(mul(targetBandwidth, sigma2), DIVISIONER)
}
if (bandwidth >= upperBound1) return 0;
uint256 maxWidth;
/// @solidity memory-safe-assembly
assembly {
// Calculate the maximum width of the trapezium
maxWidth := sub(upperBound1, upperBound2)
}
// If the bandwidth is smaller than upperBound2
if (bandwidth >= upperBound2) {
// Calculate the fee for the first interval
fee = calcFee(lambda1, maxWidth, upperBound1, bandwidth, 0, roundDown);
} else {
// Calculate the fee for the first interval
fee = calcFee(lambda1, maxWidth, upperBound1, upperBound2, 0, roundDown);
/// @solidity memory-safe-assembly
assembly {
// offset = lambda1 * 2
lambda1 := shl(1, lambda1)
}
// Calculate the fee for the second interval
uint256 fee2 = calcFee(lambda2, upperBound2, upperBound2, bandwidth, lambda1, roundDown);
/// @solidity memory-safe-assembly
assembly {
// Add the two fees together
fee := add(fee, fee2)
}
}
}
/**
* @notice Calculates outstanding rebalancing fees for a specific bandwidth
* @dev The fee is calculated as a trapezium with a base of width and a height of height
* The formula for the area of a trapezium is (a + b) * h / 2
* ___________
* fee1 + fee2 -->| /|
* | / |
* |________/ |
* fee1 + fee2 * amount-->| /| |
* ------------- | / | |
* max width | / | |
* |____/ | |
* fee1 -->| | | |
* | | | |
* |___|____|__|_____
* | | |
* upper bound 2 | 0
* |
* bandwidth
*
* max width = upper bound 2
* amount = upper bound 2 - bandwidth
*
* h = amount
* a = fee1 + (fee2 * amount / max width)
* b = fee1
*
* fee = (a + b) * h / 2
* = (fee1 + fee1 + (fee2 * amount / max width)) * amount / 2
* = ((fee1 * 2) + (fee2 * amount / max width)) * amount / 2
*
* Because lambda1 = fee1 / 2 and lambda2 = fee2 / 2
*
* fee = ((fee1 * 2) + (fee2 * amount / max width)) * amount / 2
* = (lambda1 * 2 * amount) + (lambda2 * amount * amount) / max width
* = amount * ((lambda1 * 2) + (lambda2 * amount / max width))
*
*
* When offset (b) is 0, the trapezium is equivalent to a triangle:
* ___________
* fee1 -->| /|
* | / |
* |________/ |
* fee1 * amount -->| /| |
* ------------- | / | |
* max width | / | |
* | / | |
* |___/____|__|_____
* | | |
* upper bound 1 | upper bound 2
* |
* bandwidth
*
* max width = upper bound 1 - upper bound 2
* amount = upper bound 1 - bandwidth
*
* h = amount
* a = fee1 * amount / max width
* b = 0
*
* fee = (a + b) * h / 2
* = fee1 * amount * amount / (2 * max width)
*
* Because lambda1 = fee1 / 2
*
* fee = fee1 * amount * amount / (2 * max width)
* = lambda2 * amount * amount / max width
*
* @param feeTier The fee tier of the bandwidth
* @param maxWidth The maximum width of the bandwidth
* @param upperBound The upper bound of the bandwidth
* @param bandwidth The bandwidth of the bandwidth
* @param offset The offset of the bandwidth
* @param roundDown Whether to round down or up
*/
function calcFee(
uint256 feeTier,
uint256 maxWidth,
uint256 upperBound,
uint256 bandwidth,
uint256 offset,
bool roundDown
) private pure returns (uint256 fee) {
/// @solidity memory-safe-assembly
assembly {
// Calculate the height of the trapezium
// The height is calculated as `upperBound - bandwidth`
let height := sub(upperBound, bandwidth)
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(feeTier, gt(height, div(not(0), feeTier))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Calculate the width of the trapezium, rounded up
// The width is calculated as `feeTier * height / maxWidth + offset`
let width :=
add(add(iszero(iszero(mod(mul(height, feeTier), maxWidth))), div(mul(height, feeTier), maxWidth)), offset)
// Equivalent to require(denominator != 0 && (y == 0 || x <= type(uint256).max / y))
if mul(height, gt(width, div(not(0), height))) {
// Store the function selector of `MulDivFailed()`.
mstore(0x00, 0xad251c27)
// Revert with (offset, size).
revert(0x1c, 0x04)
}
// Calculate the fee for this tier
switch roundDown
// If round down then do mulDiv(transfered, diff, totalDiff)
case true { fee := div(mul(width, height), DIVISIONER) }
// If round up then do mulDivUp(transfered, diff, totalDiff)
default {
fee := add(iszero(iszero(mod(mul(width, height), DIVISIONER))), div(mul(width, height), DIVISIONER))
}
}
}
/**
* @notice Adds assets to bandwidths and returns the assets to be swapped to a destination pool
* @param assets The assets to be distributed between all bandwidths
* @return output The assets of assets to be swapped to a destination pool
*/
function ulyssesSwap(uint256 assets) private returns (uint256 output) {
uint256 _totalWeights = totalWeights;
uint256 _totalSupply = totalSupply;
// Get the bandwidth update amounts and chainStateList length
(uint256[] memory bandwidthUpdateAmounts, uint256 length) = getBandwidthUpdateAmounts(
false, // round down bandwidths
true, // is positive transfer
assets,
_totalWeights,
_totalSupply
);
for (uint256 i = 1; i < length;) {
// Get the update amount for this bandwidth
uint256 updateAmount = bandwidthUpdateAmounts[i];
// if updateAmount > 0
if (updateAmount > 0) {
/// @solidity memory-safe-assembly
assembly {
// Add updateAmount to output
output := add(output, updateAmount)
}
// Update bandwidth with the update amount and get the positive fee
// Negative fee is always 0 because totalSupply does not increase
(uint256 positiveFee,) =
updateBandwidth(true, true, bandwidthStateList[i], updateAmount, _totalWeights, _totalSupply, 0);
/// @solidity memory-safe-assembly
assembly {
// if positiveFee > 0 then add positiveFee to output
if gt(positiveFee, 0) { output := add(output, positiveFee) }
}
}
unchecked {
++i;
}
}
}
/**
* @notice Adds amount to bandwidths and returns assets to deposit or shares to mint
* @param amount The amount to be distributed between all bandwidths
* @param depositFees True when called from deposit, false when called from mint
* @return output The output amount to be minted or deposited
*/
function ulyssesAddLP(uint256 amount, bool depositFees) private returns (uint256 output) {
uint256 _totalWeights = totalWeights;
uint256 _totalSupply = totalSupply;
uint256 _newTotalSupply;
/// @solidity memory-safe-assembly
assembly {
// Get the new total supply by adding amount to totalSupply
_newTotalSupply := add(_totalSupply, amount)
}
// Get the bandwidth update amounts and chainStateList length
// newTotalSupply is used to avoid negative rebalancing fees
// Rounds up when depositFees is false
(uint256[] memory bandwidthUpdateAmounts, uint256 length) =
getBandwidthUpdateAmounts(!depositFees, true, amount, _totalWeights, _newTotalSupply);
// Discount in assets in `mint` or negative fee in `deposit`
uint256 negativeFee;
for (uint256 i = 1; i < length;) {
// Get the update amount for this bandwidth
uint256 updateAmount = bandwidthUpdateAmounts[i];
/// @solidity memory-safe-assembly
assembly {
// if updateAmount > 0 then add updateAmount to output
if gt(updateAmount, 0) { output := add(output, updateAmount) }
}
// Update bandwidth with the update amount and get the positive fee and negative fee
(uint256 _positiveFee, uint256 _negativeFee) = updateBandwidth(
depositFees, true, bandwidthStateList[i], updateAmount, _totalWeights, _totalSupply, _newTotalSupply
);
/// @solidity memory-safe-assembly
assembly {
switch depositFees
// if depositFees is true, `deposit` was called
case true {
switch gt(_positiveFee, 0)
// if _positiveFee > 0 then add _positiveFee to output
// Adding shares to mint
case true { output := add(output, _positiveFee) }
// if _positiveFee <= 0 then add _negativeFee to negativeFee
// Subtracting shares to mint
default { negativeFee := add(negativeFee, _negativeFee) }
}
// if depositFees is false, `mint` was called
default {
switch gt(_positiveFee, 0)
// if _positiveFee > 0 then add _positiveFee to output
// Subtracting assets to deposit
case true { negativeFee := add(negativeFee, _positiveFee) }
// if _positiveFee <= 0 then add _negativeFee to output
// Adding assets to deposit
default { output := add(output, _negativeFee) }
}
}