-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAlgebraPool.sol
972 lines (850 loc) · 34.8 KB
/
AlgebraPool.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.7.6;
import './interfaces/IAlgebraPool.sol';
import './interfaces/IDataStorageOperator.sol';
import './interfaces/IAlgebraVirtualPool.sol';
import './base/PoolState.sol';
import './base/PoolImmutables.sol';
import './libraries/TokenDeltaMath.sol';
import './libraries/PriceMovementMath.sol';
import './libraries/TickManager.sol';
import './libraries/TickTable.sol';
import './libraries/LowGasSafeMath.sol';
import './libraries/SafeCast.sol';
import './libraries/FullMath.sol';
import './libraries/Constants.sol';
import './libraries/TransferHelper.sol';
import './libraries/TickMath.sol';
import './libraries/LiquidityMath.sol';
import './interfaces/IAlgebraPoolDeployer.sol';
import './interfaces/IAlgebraFactory.sol';
import './interfaces/IERC20Minimal.sol';
import './interfaces/callback/IAlgebraMintCallback.sol';
import './interfaces/callback/IAlgebraSwapCallback.sol';
import './interfaces/callback/IAlgebraFlashCallback.sol';
contract AlgebraPool is PoolState, PoolImmutables, IAlgebraPool {
using LowGasSafeMath for uint256;
using LowGasSafeMath for int256;
using LowGasSafeMath for uint128;
using SafeCast for uint256;
using SafeCast for int256;
using TickTable for mapping(int16 => uint256);
using TickManager for mapping(int24 => TickManager.Tick);
struct Position {
uint128 liquidity; // The amount of liquidity concentrated in the range
uint32 lastLiquidityAddTimestamp; // Timestamp of last adding of liquidity
uint256 innerFeeGrowth0Token; // The last updated fee growth per unit of liquidity
uint256 innerFeeGrowth1Token;
uint128 fees0; // The amount of token0 owed to a LP
uint128 fees1; // The amount of token1 owed to a LP
}
/// @inheritdoc IAlgebraPoolState
mapping(bytes32 => Position) public override positions;
/// @dev Restricts everyone calling a function except factory owner
modifier onlyFactoryOwner() {
require(msg.sender == IAlgebraFactory(factory).owner());
_;
}
modifier onlyValidTicks(int24 bottomTick, int24 topTick) {
require(topTick < TickMath.MAX_TICK + 1, 'TUM');
require(topTick > bottomTick, 'TLU');
require(bottomTick > TickMath.MIN_TICK - 1, 'TLM');
_;
}
constructor() PoolImmutables(msg.sender) {
globalState.fee = Constants.BASE_FEE;
}
function balanceToken0() private view returns (uint256) {
return IERC20Minimal(token0).balanceOf(address(this));
}
function balanceToken1() private view returns (uint256) {
return IERC20Minimal(token1).balanceOf(address(this));
}
/// @inheritdoc IAlgebraPoolState
function timepoints(uint256 index)
external
view
override
returns (
bool initialized,
uint32 blockTimestamp,
int56 tickCumulative,
uint160 secondsPerLiquidityCumulative,
uint88 volatilityCumulative,
int24 averageTick,
uint144 volumePerLiquidityCumulative
)
{
return IDataStorageOperator(dataStorageOperator).timepoints(index);
}
struct Cumulatives {
int56 tickCumulative;
uint160 outerSecondPerLiquidity;
uint32 outerSecondsSpent;
}
/// @inheritdoc IAlgebraPoolDerivedState
function getInnerCumulatives(int24 bottomTick, int24 topTick)
external
view
override
onlyValidTicks(bottomTick, topTick)
returns (
int56 innerTickCumulative,
uint160 innerSecondsSpentPerLiquidity,
uint32 innerSecondsSpent
)
{
Cumulatives memory lower;
{
TickManager.Tick storage _lower = ticks[bottomTick];
(lower.tickCumulative, lower.outerSecondPerLiquidity, lower.outerSecondsSpent) = (
_lower.outerTickCumulative,
_lower.outerSecondsPerLiquidity,
_lower.outerSecondsSpent
);
require(_lower.initialized);
}
Cumulatives memory upper;
{
TickManager.Tick storage _upper = ticks[topTick];
(upper.tickCumulative, upper.outerSecondPerLiquidity, upper.outerSecondsSpent) = (
_upper.outerTickCumulative,
_upper.outerSecondsPerLiquidity,
_upper.outerSecondsSpent
);
require(_upper.initialized);
}
(int24 currentTick, uint16 currentTimepointIndex) = (globalState.tick, globalState.timepointIndex);
if (currentTick < bottomTick) {
return (
lower.tickCumulative - upper.tickCumulative,
lower.outerSecondPerLiquidity - upper.outerSecondPerLiquidity,
lower.outerSecondsSpent - upper.outerSecondsSpent
);
}
if (currentTick < topTick) {
uint32 globalTime = _blockTimestamp();
(int56 globalTickCumulative, uint160 globalSecondsPerLiquidityCumulative, , ) = _getSingleTimepoint(
globalTime,
0,
currentTick,
currentTimepointIndex,
liquidity
);
return (
globalTickCumulative - lower.tickCumulative - upper.tickCumulative,
globalSecondsPerLiquidityCumulative - lower.outerSecondPerLiquidity - upper.outerSecondPerLiquidity,
globalTime - lower.outerSecondsSpent - upper.outerSecondsSpent
);
}
return (
upper.tickCumulative - lower.tickCumulative,
upper.outerSecondPerLiquidity - lower.outerSecondPerLiquidity,
upper.outerSecondsSpent - lower.outerSecondsSpent
);
}
/// @inheritdoc IAlgebraPoolDerivedState
function getTimepoints(uint32[] calldata secondsAgos)
external
view
override
returns (
int56[] memory tickCumulatives,
uint160[] memory secondsPerLiquidityCumulatives,
uint112[] memory volatilityCumulatives,
uint256[] memory volumePerAvgLiquiditys
)
{
return
IDataStorageOperator(dataStorageOperator).getTimepoints(
_blockTimestamp(),
secondsAgos,
globalState.tick,
globalState.timepointIndex,
liquidity
);
}
/// @inheritdoc IAlgebraPoolActions
function initialize(uint160 initialPrice) external override {
require(globalState.price == 0, 'AI');
// getTickAtSqrtRatio checks validity of initialPrice inside
int24 tick = TickMath.getTickAtSqrtRatio(initialPrice);
uint32 timestamp = _blockTimestamp();
IDataStorageOperator(dataStorageOperator).initialize(timestamp, tick);
globalState.price = initialPrice;
globalState.unlocked = true;
globalState.tick = tick;
emit Initialize(initialPrice, tick);
}
/**
* @notice Increases amounts of tokens owed to owner of the position
* @param _position The position object to operate with
* @param liquidityDelta The amount on which to increase\decrease the liquidity
* @param innerFeeGrowth0Token Total fee token0 fee growth per 1/liquidity between position's lower and upper ticks
* @param innerFeeGrowth1Token Total fee token1 fee growth per 1/liquidity between position's lower and upper ticks
*/
function _recalculatePosition(
Position storage _position,
int128 liquidityDelta,
uint256 innerFeeGrowth0Token,
uint256 innerFeeGrowth1Token
) internal {
(uint128 currentLiquidity, uint32 lastLiquidityAddTimestamp) = (_position.liquidity, _position.lastLiquidityAddTimestamp);
if (liquidityDelta == 0) {
require(currentLiquidity > 0, 'NP'); // Do not recalculate the empty ranges
} else {
if (liquidityDelta < 0) {
uint32 _liquidityCooldown = liquidityCooldown;
if (_liquidityCooldown > 0) {
require((_blockTimestamp() - lastLiquidityAddTimestamp) >= _liquidityCooldown);
}
}
// change position liquidity
uint128 liquidityNext = LiquidityMath.addDelta(currentLiquidity, liquidityDelta);
(_position.liquidity, _position.lastLiquidityAddTimestamp) = (
liquidityNext,
liquidityNext > 0 ? (liquidityDelta > 0 ? _blockTimestamp() : lastLiquidityAddTimestamp) : 0
);
}
// update the position
uint256 _innerFeeGrowth0Token = _position.innerFeeGrowth0Token;
uint256 _innerFeeGrowth1Token = _position.innerFeeGrowth1Token;
uint128 fees0;
if (innerFeeGrowth0Token != _innerFeeGrowth0Token) {
_position.innerFeeGrowth0Token = innerFeeGrowth0Token;
fees0 = uint128(FullMath.mulDiv(innerFeeGrowth0Token - _innerFeeGrowth0Token, currentLiquidity, Constants.Q128));
}
uint128 fees1;
if (innerFeeGrowth1Token != _innerFeeGrowth1Token) {
_position.innerFeeGrowth1Token = innerFeeGrowth1Token;
fees1 = uint128(FullMath.mulDiv(innerFeeGrowth1Token - _innerFeeGrowth1Token, currentLiquidity, Constants.Q128));
}
// To avoid overflow owner has to collect fee before it
if (fees0 | fees1 != 0) {
_position.fees0 += fees0;
_position.fees1 += fees1;
}
}
struct UpdatePositionCache {
uint160 price; // The square root of the current price in Q64.96 format
int24 tick; // The current tick
uint16 timepointIndex; // The index of the last written timepoint
}
/**
* @dev Updates position's ticks and its fees
* @return position The Position object to operate with
* @return amount0 The amount of token0 the caller needs to send, negative if the pool needs to send it
* @return amount1 The amount of token1 the caller needs to send, negative if the pool needs to send it
*/
function _updatePositionTicksAndFees(
address owner,
int24 bottomTick,
int24 topTick,
int128 liquidityDelta
)
private
returns (
Position storage position,
int256 amount0,
int256 amount1
)
{
UpdatePositionCache memory cache = UpdatePositionCache(globalState.price, globalState.tick, globalState.timepointIndex);
position = getOrCreatePosition(owner, bottomTick, topTick);
(uint256 _totalFeeGrowth0Token, uint256 _totalFeeGrowth1Token) = (totalFeeGrowth0Token, totalFeeGrowth1Token);
bool toggledBottom;
bool toggledTop;
if (liquidityDelta != 0) {
uint32 time = _blockTimestamp();
(int56 tickCumulative, uint160 secondsPerLiquidityCumulative, , ) = _getSingleTimepoint(time, 0, cache.tick, cache.timepointIndex, liquidity);
if (
ticks.update(
bottomTick,
cache.tick,
liquidityDelta,
_totalFeeGrowth0Token,
_totalFeeGrowth1Token,
secondsPerLiquidityCumulative,
tickCumulative,
time,
false // isTopTick
)
) {
toggledBottom = true;
tickTable.toggleTick(bottomTick);
}
if (
ticks.update(
topTick,
cache.tick,
liquidityDelta,
_totalFeeGrowth0Token,
_totalFeeGrowth1Token,
secondsPerLiquidityCumulative,
tickCumulative,
time,
true // isTopTick
)
) {
toggledTop = true;
tickTable.toggleTick(topTick);
}
}
(uint256 feeGrowthInside0X128, uint256 feeGrowthInside1X128) = ticks.getInnerFeeGrowth(
bottomTick,
topTick,
cache.tick,
_totalFeeGrowth0Token,
_totalFeeGrowth1Token
);
_recalculatePosition(position, liquidityDelta, feeGrowthInside0X128, feeGrowthInside1X128);
if (liquidityDelta != 0) {
// if liquidityDelta is negative and the tick was toggled, it means that it should not be initialized anymore, so we delete it
if (liquidityDelta < 0) {
if (toggledBottom) delete ticks[bottomTick];
if (toggledTop) delete ticks[topTick];
}
int128 globalLiquidityDelta;
(amount0, amount1, globalLiquidityDelta) = _getAmountsForLiquidity(bottomTick, topTick, liquidityDelta, cache.tick, cache.price);
if (globalLiquidityDelta != 0) {
uint128 liquidityBefore = liquidity;
uint16 newTimepointIndex = _writeTimepoint(cache.timepointIndex, _blockTimestamp(), cache.tick, liquidityBefore, volumePerLiquidityInBlock);
if (cache.timepointIndex != newTimepointIndex) {
globalState.fee = _getNewFee(_blockTimestamp(), cache.tick, newTimepointIndex, liquidityBefore);
globalState.timepointIndex = newTimepointIndex;
volumePerLiquidityInBlock = 0;
}
liquidity = LiquidityMath.addDelta(liquidityBefore, liquidityDelta);
}
}
}
function _getAmountsForLiquidity(
int24 bottomTick,
int24 topTick,
int128 liquidityDelta,
int24 currentTick,
uint160 currentPrice
)
private
pure
returns (
int256 amount0,
int256 amount1,
int128 globalLiquidityDelta
)
{
// If current tick is less than the provided bottom one then only the token0 has to be provided
if (currentTick < bottomTick) {
amount0 = TokenDeltaMath.getToken0Delta(TickMath.getSqrtRatioAtTick(bottomTick), TickMath.getSqrtRatioAtTick(topTick), liquidityDelta);
} else if (currentTick < topTick) {
amount0 = TokenDeltaMath.getToken0Delta(currentPrice, TickMath.getSqrtRatioAtTick(topTick), liquidityDelta);
amount1 = TokenDeltaMath.getToken1Delta(TickMath.getSqrtRatioAtTick(bottomTick), currentPrice, liquidityDelta);
globalLiquidityDelta = liquidityDelta;
}
// If current tick is greater than the provided top one then only the token1 has to be provided
else {
amount1 = TokenDeltaMath.getToken1Delta(TickMath.getSqrtRatioAtTick(bottomTick), TickMath.getSqrtRatioAtTick(topTick), liquidityDelta);
}
}
/**
* @notice This function fetches certain position object
* @param owner The address owing the position
* @param bottomTick The position's bottom tick
* @param topTick The position's top tick
* @return position The Position object
*/
function getOrCreatePosition(
address owner,
int24 bottomTick,
int24 topTick
) private view returns (Position storage) {
bytes32 key;
assembly {
key := or(shl(24, or(shl(24, owner), and(bottomTick, 0xFFFFFF))), and(topTick, 0xFFFFFF))
}
return positions[key];
}
/// @inheritdoc IAlgebraPoolActions
function mint(
address sender,
address recipient,
int24 bottomTick,
int24 topTick,
uint128 liquidityDesired,
bytes calldata data
)
external
override
lock
onlyValidTicks(bottomTick, topTick)
returns (
uint256 amount0,
uint256 amount1,
uint128 liquidityActual
)
{
require(liquidityDesired > 0, 'IL');
{
(int256 amount0Int, int256 amount1Int, ) = _getAmountsForLiquidity(
bottomTick,
topTick,
int256(liquidityDesired).toInt128(),
globalState.tick,
globalState.price
);
amount0 = uint256(amount0Int);
amount1 = uint256(amount1Int);
}
uint256 receivedAmount0;
uint256 receivedAmount1;
{
if (amount0 > 0) receivedAmount0 = balanceToken0();
if (amount1 > 0) receivedAmount1 = balanceToken1();
IAlgebraMintCallback(msg.sender).algebraMintCallback(amount0, amount1, data);
if (amount0 > 0) require((receivedAmount0 = balanceToken0() - receivedAmount0) > 0, 'IIAM');
if (amount1 > 0) require((receivedAmount1 = balanceToken1() - receivedAmount1) > 0, 'IIAM');
}
liquidityActual = liquidityDesired;
if (receivedAmount0 < amount0) {
liquidityActual = uint128(FullMath.mulDiv(uint256(liquidityActual), receivedAmount0, amount0));
}
if (receivedAmount1 < amount1) {
uint128 liquidityForRA1 = uint128(FullMath.mulDiv(uint256(liquidityActual), receivedAmount1, amount1));
if (liquidityForRA1 < liquidityActual) {
liquidityActual = liquidityForRA1;
}
}
require(liquidityActual > 0, 'IIL2');
{
(, int256 amount0Int, int256 amount1Int) = _updatePositionTicksAndFees(recipient, bottomTick, topTick, int256(liquidityActual).toInt128());
require((amount0 = uint256(amount0Int)) <= receivedAmount0, 'IIAM2');
require((amount1 = uint256(amount1Int)) <= receivedAmount1, 'IIAM2');
}
if (receivedAmount0 > amount0) {
TransferHelper.safeTransfer(token0, sender, receivedAmount0 - amount0);
}
if (receivedAmount1 > amount1) {
TransferHelper.safeTransfer(token1, sender, receivedAmount1 - amount1);
}
emit Mint(msg.sender, recipient, bottomTick, topTick, liquidityActual, amount0, amount1);
}
/// @inheritdoc IAlgebraPoolActions
function collect(
address recipient,
int24 bottomTick,
int24 topTick,
uint128 amount0Requested,
uint128 amount1Requested
) external override lock returns (uint128 amount0, uint128 amount1) {
Position storage position = getOrCreatePosition(msg.sender, bottomTick, topTick);
(uint128 positionFees0, uint128 positionFees1) = (position.fees0, position.fees1);
amount0 = amount0Requested > positionFees0 ? positionFees0 : amount0Requested;
amount1 = amount1Requested > positionFees1 ? positionFees1 : amount1Requested;
if (amount0 | amount1 != 0) {
position.fees0 = positionFees0 - amount0;
position.fees1 = positionFees1 - amount1;
if (amount0 > 0) TransferHelper.safeTransfer(token0, recipient, amount0);
if (amount1 > 0) TransferHelper.safeTransfer(token1, recipient, amount1);
}
emit Collect(msg.sender, recipient, bottomTick, topTick, amount0, amount1);
}
/// @inheritdoc IAlgebraPoolActions
function burn(
int24 bottomTick,
int24 topTick,
uint128 amount
) external override lock onlyValidTicks(bottomTick, topTick) returns (uint256 amount0, uint256 amount1) {
(Position storage position, int256 amount0Int, int256 amount1Int) = _updatePositionTicksAndFees(
msg.sender,
bottomTick,
topTick,
-int256(amount).toInt128()
);
amount0 = uint256(-amount0Int);
amount1 = uint256(-amount1Int);
if (amount0 | amount1 != 0) {
(position.fees0, position.fees1) = (position.fees0.add128(uint128(amount0)), position.fees1.add128(uint128(amount1)));
}
emit Burn(msg.sender, bottomTick, topTick, amount, amount0, amount1);
}
/// @dev Returns new fee according combination of sigmoids
function _getNewFee(
uint32 _time,
int24 _tick,
uint16 _index,
uint128 _liquidity
) private returns (uint16 newFee) {
newFee = IDataStorageOperator(dataStorageOperator).getFee(_time, _tick, _index, _liquidity);
emit Fee(newFee);
}
function _payCommunityFee(address token, uint256 amount) private {
address vault = IAlgebraFactory(factory).vaultAddress();
TransferHelper.safeTransfer(token, vault, amount);
}
function _writeTimepoint(
uint16 timepointIndex,
uint32 blockTimestamp,
int24 tick,
uint128 liquidity,
uint128 volumePerLiquidityInBlock
) private returns (uint16 newTimepointIndex) {
return IDataStorageOperator(dataStorageOperator).write(timepointIndex, blockTimestamp, tick, liquidity, volumePerLiquidityInBlock);
}
function _getSingleTimepoint(
uint32 blockTimestamp,
uint32 secondsAgo,
int24 startTick,
uint16 timepointIndex,
uint128 liquidityStart
)
private
view
returns (
int56 tickCumulative,
uint160 secondsPerLiquidityCumulative,
uint112 volatilityCumulative,
uint256 volumePerAvgLiquidity
)
{
return IDataStorageOperator(dataStorageOperator).getSingleTimepoint(blockTimestamp, secondsAgo, startTick, timepointIndex, liquidityStart);
}
function _swapCallback(
int256 amount0,
int256 amount1,
bytes calldata data
) private {
IAlgebraSwapCallback(msg.sender).algebraSwapCallback(amount0, amount1, data);
}
/// @inheritdoc IAlgebraPoolActions
function swap(
address recipient,
bool zeroToOne,
int256 amountRequired,
uint160 limitSqrtPrice,
bytes calldata data
) external override returns (int256 amount0, int256 amount1) {
uint160 currentPrice;
int24 currentTick;
uint128 currentLiquidity;
uint256 communityFee;
// function _calculateSwapAndLock locks globalState.unlocked and does not release
(amount0, amount1, currentPrice, currentTick, currentLiquidity, communityFee) = _calculateSwapAndLock(zeroToOne, amountRequired, limitSqrtPrice);
if (zeroToOne) {
if (amount1 < 0) TransferHelper.safeTransfer(token1, recipient, uint256(-amount1)); // transfer to recipient
uint256 balance0Before = balanceToken0();
_swapCallback(amount0, amount1, data); // callback to get tokens from the caller
require(balance0Before.add(uint256(amount0)) <= balanceToken0(), 'IIA');
} else {
if (amount0 < 0) TransferHelper.safeTransfer(token0, recipient, uint256(-amount0)); // transfer to recipient
uint256 balance1Before = balanceToken1();
_swapCallback(amount0, amount1, data); // callback to get tokens from the caller
require(balance1Before.add(uint256(amount1)) <= balanceToken1(), 'IIA');
}
if (communityFee > 0) {
_payCommunityFee(zeroToOne ? token0 : token1, communityFee);
}
emit Swap(msg.sender, recipient, amount0, amount1, currentPrice, currentLiquidity, currentTick);
globalState.unlocked = true; // release after lock in _calculateSwapAndLock
}
/// @inheritdoc IAlgebraPoolActions
function swapSupportingFeeOnInputTokens(
address sender,
address recipient,
bool zeroToOne,
int256 amountRequired,
uint160 limitSqrtPrice,
bytes calldata data
) external override returns (int256 amount0, int256 amount1) {
// Since the pool can get less tokens then sent, firstly we are getting tokens from the
// original caller of the transaction. And change the _amountRequired_
require(globalState.unlocked, 'LOK');
globalState.unlocked = false;
if (zeroToOne) {
uint256 balance0Before = balanceToken0();
_swapCallback(amountRequired, 0, data);
require((amountRequired = int256(balanceToken0().sub(balance0Before))) > 0, 'IIA');
} else {
uint256 balance1Before = balanceToken1();
_swapCallback(0, amountRequired, data);
require((amountRequired = int256(balanceToken1().sub(balance1Before))) > 0, 'IIA');
}
globalState.unlocked = true;
uint160 currentPrice;
int24 currentTick;
uint128 currentLiquidity;
uint256 communityFee;
// function _calculateSwapAndLock locks 'globalState.unlocked' and does not release
(amount0, amount1, currentPrice, currentTick, currentLiquidity, communityFee) = _calculateSwapAndLock(zeroToOne, amountRequired, limitSqrtPrice);
// only transfer to the recipient
if (zeroToOne) {
if (amount1 < 0) TransferHelper.safeTransfer(token1, recipient, uint256(-amount1));
// return the leftovers
if (amount0 < amountRequired) TransferHelper.safeTransfer(token0, sender, uint256(amountRequired.sub(amount0)));
} else {
if (amount0 < 0) TransferHelper.safeTransfer(token0, recipient, uint256(-amount0));
// return the leftovers
if (amount1 < amountRequired) TransferHelper.safeTransfer(token1, sender, uint256(amountRequired.sub(amount1)));
}
if (communityFee > 0) {
_payCommunityFee(zeroToOne ? token0 : token1, communityFee);
}
emit Swap(msg.sender, recipient, amount0, amount1, currentPrice, currentLiquidity, currentTick);
globalState.unlocked = true; // release after lock in _calculateSwapAndLock
}
struct SwapCalculationCache {
uint256 communityFee; // The community fee of the selling token, uint256 to minimize casts
uint128 volumePerLiquidityInBlock;
int56 tickCumulative; // The global tickCumulative at the moment
uint160 secondsPerLiquidityCumulative; // The global secondPerLiquidity at the moment
bool computedLatestTimepoint; // if we have already fetched _tickCumulative_ and _secondPerLiquidity_ from the DataOperator
int256 amountRequiredInitial; // The initial value of the exact input\output amount
int256 amountCalculated; // The additive amount of total output\input calculated trough the swap
uint256 totalFeeGrowth; // The initial totalFeeGrowth + the fee growth during a swap
uint256 totalFeeGrowthB;
IAlgebraVirtualPool.Status incentiveStatus; // If there is an active incentive at the moment
bool exactInput; // Whether the exact input or output is specified
uint16 fee; // The current dynamic fee
int24 startTick; // The tick at the start of a swap
uint16 timepointIndex; // The index of last written timepoint
}
struct PriceMovementCache {
uint160 stepSqrtPrice; // The Q64.96 sqrt of the price at the start of the step
int24 nextTick; // The tick till the current step goes
bool initialized; // True if the _nextTick is initialized
uint160 nextTickPrice; // The Q64.96 sqrt of the price calculated from the _nextTick
uint256 input; // The additive amount of tokens that have been provided
uint256 output; // The additive amount of token that have been withdrawn
uint256 feeAmount; // The total amount of fee earned within a current step
}
/// @notice For gas optimization, locks 'globalState.unlocked' and does not release.
function _calculateSwapAndLock(
bool zeroToOne,
int256 amountRequired,
uint160 limitSqrtPrice
)
private
returns (
int256 amount0,
int256 amount1,
uint160 currentPrice,
int24 currentTick,
uint128 currentLiquidity,
uint256 communityFeeAmount
)
{
uint32 blockTimestamp;
SwapCalculationCache memory cache;
{
// load from one storage slot
currentPrice = globalState.price;
currentTick = globalState.tick;
cache.fee = globalState.fee;
cache.timepointIndex = globalState.timepointIndex;
uint256 _communityFeeToken0 = globalState.communityFeeToken0;
uint256 _communityFeeToken1 = globalState.communityFeeToken1;
bool unlocked = globalState.unlocked;
globalState.unlocked = false; // lock will not be released in this function
require(unlocked, 'LOK');
require(amountRequired != 0, 'AS');
(cache.amountRequiredInitial, cache.exactInput) = (amountRequired, amountRequired > 0);
(currentLiquidity, cache.volumePerLiquidityInBlock) = (liquidity, volumePerLiquidityInBlock);
if (zeroToOne) {
require(limitSqrtPrice < currentPrice && limitSqrtPrice > TickMath.MIN_SQRT_RATIO, 'SPL');
cache.totalFeeGrowth = totalFeeGrowth0Token;
cache.communityFee = _communityFeeToken0;
} else {
require(limitSqrtPrice > currentPrice && limitSqrtPrice < TickMath.MAX_SQRT_RATIO, 'SPL');
cache.totalFeeGrowth = totalFeeGrowth1Token;
cache.communityFee = _communityFeeToken1;
}
cache.startTick = currentTick;
blockTimestamp = _blockTimestamp();
if (activeIncentive != address(0)) {
IAlgebraVirtualPool.Status _status = IAlgebraVirtualPool(activeIncentive).increaseCumulative(blockTimestamp);
if (_status == IAlgebraVirtualPool.Status.NOT_EXIST) {
activeIncentive = address(0);
} else if (_status == IAlgebraVirtualPool.Status.ACTIVE) {
cache.incentiveStatus = IAlgebraVirtualPool.Status.ACTIVE;
} else if (_status == IAlgebraVirtualPool.Status.NOT_STARTED) {
cache.incentiveStatus = IAlgebraVirtualPool.Status.NOT_STARTED;
}
}
uint16 newTimepointIndex = _writeTimepoint(
cache.timepointIndex,
blockTimestamp,
cache.startTick,
currentLiquidity,
cache.volumePerLiquidityInBlock
);
// new timepoint appears only for first swap in block
if (newTimepointIndex != cache.timepointIndex) {
cache.timepointIndex = newTimepointIndex;
cache.volumePerLiquidityInBlock = 0;
cache.fee = _getNewFee(blockTimestamp, currentTick, newTimepointIndex, currentLiquidity);
}
}
PriceMovementCache memory step;
// swap until there is remaining input or output tokens or we reach the price limit
while (true) {
step.stepSqrtPrice = currentPrice;
(step.nextTick, step.initialized) = tickTable.nextTickInTheSameRow(currentTick, zeroToOne);
step.nextTickPrice = TickMath.getSqrtRatioAtTick(step.nextTick);
// calculate the amounts needed to move the price to the next target if it is possible or as much as possible
(currentPrice, step.input, step.output, step.feeAmount) = PriceMovementMath.movePriceTowardsTarget(
zeroToOne,
currentPrice,
(zeroToOne == (step.nextTickPrice < limitSqrtPrice)) // move the price to the target or to the limit
? limitSqrtPrice
: step.nextTickPrice,
currentLiquidity,
amountRequired,
cache.fee
);
if (cache.exactInput) {
amountRequired -= (step.input + step.feeAmount).toInt256(); // decrease remaining input amount
cache.amountCalculated = cache.amountCalculated.sub(step.output.toInt256()); // decrease calculated output amount
} else {
amountRequired += step.output.toInt256(); // increase remaining output amount (since its negative)
cache.amountCalculated = cache.amountCalculated.add((step.input + step.feeAmount).toInt256()); // increase calculated input amount
}
if (cache.communityFee > 0) {
uint256 delta = (step.feeAmount.mul(cache.communityFee)) / Constants.COMMUNITY_FEE_DENOMINATOR;
step.feeAmount -= delta;
communityFeeAmount += delta;
}
if (currentLiquidity > 0) cache.totalFeeGrowth += FullMath.mulDiv(step.feeAmount, Constants.Q128, currentLiquidity);
if (currentPrice == step.nextTickPrice) {
// if the reached tick is initialized then we need to cross it
if (step.initialized) {
// once at a swap we have to get the last timepoint of the observation
if (!cache.computedLatestTimepoint) {
(cache.tickCumulative, cache.secondsPerLiquidityCumulative, , ) = _getSingleTimepoint(
blockTimestamp,
0,
cache.startTick,
cache.timepointIndex,
currentLiquidity // currentLiquidity can be changed only after computedLatestTimepoint
);
cache.computedLatestTimepoint = true;
cache.totalFeeGrowthB = zeroToOne ? totalFeeGrowth1Token : totalFeeGrowth0Token;
}
// every tick cross is needed to be duplicated in a virtual pool
if (cache.incentiveStatus != IAlgebraVirtualPool.Status.NOT_EXIST) {
IAlgebraVirtualPool(activeIncentive).cross(step.nextTick, zeroToOne);
}
int128 liquidityDelta;
if (zeroToOne) {
liquidityDelta = -ticks.cross(
step.nextTick,
cache.totalFeeGrowth, // A == 0
cache.totalFeeGrowthB, // B == 1
cache.secondsPerLiquidityCumulative,
cache.tickCumulative,
blockTimestamp
);
} else {
liquidityDelta = ticks.cross(
step.nextTick,
cache.totalFeeGrowthB, // B == 0
cache.totalFeeGrowth, // A == 1
cache.secondsPerLiquidityCumulative,
cache.tickCumulative,
blockTimestamp
);
}
currentLiquidity = LiquidityMath.addDelta(currentLiquidity, liquidityDelta);
}
currentTick = zeroToOne ? step.nextTick - 1 : step.nextTick;
} else if (currentPrice != step.stepSqrtPrice) {
// if the price has changed but hasn't reached the target
currentTick = TickMath.getTickAtSqrtRatio(currentPrice);
break; // since the price hasn't reached the target, amountRequired should be 0
}
// check stop condition
if (amountRequired == 0 || currentPrice == limitSqrtPrice) {
break;
}
}
(amount0, amount1) = zeroToOne == cache.exactInput // the amount to provide could be less then initially specified (e.g. reached limit)
? (cache.amountRequiredInitial - amountRequired, cache.amountCalculated) // the amount to get could be less then initially specified (e.g. reached limit)
: (cache.amountCalculated, cache.amountRequiredInitial - amountRequired);
(globalState.price, globalState.tick, globalState.fee, globalState.timepointIndex) = (currentPrice, currentTick, cache.fee, cache.timepointIndex);
(liquidity, volumePerLiquidityInBlock) = (
currentLiquidity,
cache.volumePerLiquidityInBlock + IDataStorageOperator(dataStorageOperator).calculateVolumePerLiquidity(currentLiquidity, amount0, amount1)
);
if (zeroToOne) {
totalFeeGrowth0Token = cache.totalFeeGrowth;
} else {
totalFeeGrowth1Token = cache.totalFeeGrowth;
}
}
/// @inheritdoc IAlgebraPoolActions
function flash(
address recipient,
uint256 amount0,
uint256 amount1,
bytes calldata data
) external override lock {
uint128 _liquidity = liquidity;
require(_liquidity > 0, 'L');
uint16 _fee = globalState.fee;
uint256 fee0;
uint256 balance0Before = balanceToken0();
if (amount0 > 0) {
fee0 = FullMath.mulDivRoundingUp(amount0, _fee, 1e6);
TransferHelper.safeTransfer(token0, recipient, amount0);
}
uint256 fee1;
uint256 balance1Before = balanceToken1();
if (amount1 > 0) {
fee1 = FullMath.mulDivRoundingUp(amount1, _fee, 1e6);
TransferHelper.safeTransfer(token1, recipient, amount1);
}
IAlgebraFlashCallback(msg.sender).algebraFlashCallback(fee0, fee1, data);
address vault = IAlgebraFactory(factory).vaultAddress();
uint256 paid0 = balanceToken0();
require(balance0Before.add(fee0) <= paid0, 'F0');
paid0 -= balance0Before;
if (paid0 > 0) {
uint8 _communityFeeToken0 = globalState.communityFeeToken0;
uint256 fees0;
if (_communityFeeToken0 > 0) {
fees0 = (paid0 * _communityFeeToken0) / Constants.COMMUNITY_FEE_DENOMINATOR;
TransferHelper.safeTransfer(token0, vault, fees0);
}
totalFeeGrowth0Token += FullMath.mulDiv(paid0 - fees0, Constants.Q128, _liquidity);
}
uint256 paid1 = balanceToken1();
require(balance1Before.add(fee1) <= paid1, 'F1');
paid1 -= balance1Before;
if (paid1 > 0) {
uint8 _communityFeeToken1 = globalState.communityFeeToken1;
uint256 fees1;
if (_communityFeeToken1 > 0) {
fees1 = (paid1 * _communityFeeToken1) / Constants.COMMUNITY_FEE_DENOMINATOR;
TransferHelper.safeTransfer(token1, vault, fees1);
}
totalFeeGrowth1Token += FullMath.mulDiv(paid1 - fees1, Constants.Q128, _liquidity);
}
emit Flash(msg.sender, recipient, amount0, amount1, paid0, paid1);
}
/// @inheritdoc IAlgebraPoolPermissionedActions
function setCommunityFee(uint8 communityFee0, uint8 communityFee1) external override lock onlyFactoryOwner {
require((communityFee0 <= Constants.MAX_COMMUNITY_FEE) && (communityFee1 <= Constants.MAX_COMMUNITY_FEE));
(globalState.communityFeeToken0, globalState.communityFeeToken1) = (communityFee0, communityFee1);
emit CommunityFee(communityFee0, communityFee1);
}
/// @inheritdoc IAlgebraPoolPermissionedActions
function setIncentive(address virtualPoolAddress) external override {
require(msg.sender == IAlgebraFactory(factory).farmingAddress());
activeIncentive = virtualPoolAddress;
emit Incentive(virtualPoolAddress);
}
/// @inheritdoc IAlgebraPoolPermissionedActions
function setLiquidityCooldown(uint32 newLiquidityCooldown) external override onlyFactoryOwner {
require(newLiquidityCooldown <= Constants.MAX_LIQUIDITY_COOLDOWN && liquidityCooldown != newLiquidityCooldown);
liquidityCooldown = newLiquidityCooldown;
emit LiquidityCooldown(newLiquidityCooldown);
}
}