-
Notifications
You must be signed in to change notification settings - Fork 22
/
MultiFlowPump.sol
622 lines (563 loc) · 25.4 KB
/
MultiFlowPump.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {IPump} from "src/interfaces/pumps/IPump.sol";
import {IMultiFlowPumpErrors} from "src/interfaces/pumps/IMultiFlowPumpErrors.sol";
import {IWell, Call} from "src/interfaces/IWell.sol";
import {IInstantaneousPump} from "src/interfaces/pumps/IInstantaneousPump.sol";
import {IMultiFlowPumpWellFunction} from "src/interfaces/IMultiFlowPumpWellFunction.sol";
import {ICumulativePump} from "src/interfaces/pumps/ICumulativePump.sol";
import {ABDKMathQuad} from "src/libraries/ABDKMathQuad.sol";
import {LibBytes16} from "src/libraries/LibBytes16.sol";
import {LibLastReserveBytes} from "src/libraries/LibLastReserveBytes.sol";
import {Math} from "oz/utils/math/Math.sol";
import {SafeCast} from "oz/utils/math/SafeCast.sol";
import {LibMath} from "src/libraries/LibMath.sol";
/**
* @title MultiFlowPump
* @author Brendan
* @notice Stores a geometric EMA and cumulative geometric SMA for each reserve.
* @dev A Pump designed for use in Beanstalk with 2 tokens.
*
* This Pump has 3 main features:
* 1. Multi-block MEV resistence reserves
* 2. MEV-resistant Geometric EMA intended for instantaneous reserve queries
* 3. MEV-resistant Cumulative Geometric intended for SMA reserve queries
*
* Note: If an `update` call is made with a reserve of 0, the Geometric mean oracles will be set to 0.
* Each Well is responsible for ensuring that an `update` call cannot be made with a reserve of 0.
*/
contract MultiFlowPump is IPump, IMultiFlowPumpErrors, IInstantaneousPump, ICumulativePump {
using LibLastReserveBytes for bytes32;
using LibBytes16 for bytes32;
using ABDKMathQuad for bytes16;
using ABDKMathQuad for uint256;
using SafeCast for int256;
using Math for uint256;
using LibMath for uint256;
uint256 constant CAP_PRECISION = 1e18;
uint256 constant CAP_PRECISION2 = 2 ** 128;
bytes16 constant MAX_CONVERT_TO_128x128 = 0x407dffffffffffffffffffffffffffff;
uint256 constant MAX_UINT256_SQRT = 340_282_366_920_938_463_463_374_607_431_768_211_455;
struct PumpState {
uint40 lastTimestamp;
uint256[] lastReserves;
bytes16[] emaReserves;
bytes16[] cumulativeReserves;
}
struct CapReservesParameters {
bytes16[][] maxRateChanges;
bytes16 maxLpSupplyIncrease;
bytes16 maxLpSupplyDecrease;
}
struct CapRatesVariables {
uint256 r;
uint256 rLast;
uint256 rLimit;
uint256[] ratios;
}
//////////////////// PUMP ////////////////////
/**
* @dev Update the Pump's manipulation resistant reserve balances for a given `well` with `reserves`.
*/
function update(uint256[] calldata reserves, bytes calldata data) external {
// Require two token well
if (reserves.length != 2) {
revert TooManyTokens();
}
(bytes16 alpha, uint256 capInterval, CapReservesParameters memory crp) =
abi.decode(data, (bytes16, uint256, CapReservesParameters));
uint256 numberOfReserves = reserves.length;
PumpState memory pumpState;
// All reserves are stored starting at the msg.sender address slot in storage.
bytes32 slot = _getSlotForAddress(msg.sender);
// Read: Last Timestamp & Last Reserves
(, pumpState.lastTimestamp, pumpState.lastReserves) = slot.readLastReserves();
// If the last timestamp is 0, then the pump has never been used before.
if (pumpState.lastTimestamp == 0) {
_init(slot, uint40(block.timestamp), reserves);
return;
}
if (reserves[0] == 0 && reserves[1] == 0) {
slot.resetLastReserves(numberOfReserves);
return;
}
bytes16 alphaN;
bytes16 deltaTimestampBytes;
uint256 capExponent;
// Isolate in brackets to prevent stack too deep errors
{
uint256 deltaTimestamp = _getDeltaTimestamp(pumpState.lastTimestamp);
// If no time has passed, don't update the pump reserves.
if (deltaTimestamp == 0) return;
alphaN = alpha.powu(deltaTimestamp);
deltaTimestampBytes = deltaTimestamp.fromUInt();
// Round up in case capInterval > block time to guarantee capExponent > 0 if time has passed since the last update.
capExponent = calcCapExponent(deltaTimestamp, capInterval);
}
pumpState.lastReserves = _capReserves(msg.sender, pumpState.lastReserves, reserves, capExponent, crp);
// If the last reserves have been manipulated to 0, reset the pump.
if (pumpState.lastReserves[0] == 0 && pumpState.lastReserves[1] == 0) {
slot.resetLastReserves(numberOfReserves);
return;
}
// Read: Cumulative & EMA Reserves
// Start at the slot after `pumpState.lastReserves`
uint256 numSlots = _getSlotsOffset(numberOfReserves);
assembly {
slot := add(slot, numSlots)
}
pumpState.emaReserves = slot.readBytes16(numberOfReserves);
assembly {
slot := add(slot, numSlots)
}
pumpState.cumulativeReserves = slot.readBytes16(numberOfReserves);
bytes16 lastReserve;
for (uint256 i; i < numberOfReserves; ++i) {
lastReserve = pumpState.lastReserves[i].fromUIntToLog2();
pumpState.emaReserves[i] =
lastReserve.mul((ABDKMathQuad.ONE.sub(alphaN))).add(pumpState.emaReserves[i].mul(alphaN));
pumpState.cumulativeReserves[i] = pumpState.cumulativeReserves[i].add(lastReserve.mul(deltaTimestampBytes));
}
// Write: Cumulative & EMA Reserves
// Order matters: work backwards to avoid using a new memory var to count up
slot.storeBytes16(pumpState.cumulativeReserves);
assembly {
slot := sub(slot, numSlots)
}
slot.storeBytes16(pumpState.emaReserves);
assembly {
slot := sub(slot, numSlots)
}
// Write: Last Timestamp & Last Reserves
slot.storeLastReserves(uint40(block.timestamp), pumpState.lastReserves);
}
/**
* @dev On first update for a particular Well, initialize oracle with
* reserves data.
*/
function _init(bytes32 slot, uint40 lastTimestamp, uint256[] memory reserves) internal {
uint256 numberOfReserves = reserves.length;
bytes16[] memory byteReserves = new bytes16[](numberOfReserves);
// Skip {_capReserve} since we have no prior reference
for (uint256 i; i < numberOfReserves; ++i) {
uint256 _reserve = reserves[i];
if (_reserve == 0) return;
byteReserves[i] = _reserve.fromUIntToLog2();
}
// Write: Last Timestamp & Last Reserves
slot.storeLastReserves(lastTimestamp, reserves);
// Write: EMA Reserves
// Start at the slot after `byteReserves`
uint256 numSlots = _getSlotsOffset(numberOfReserves);
assembly {
slot := add(slot, numSlots)
}
slot.storeBytes16(byteReserves); // EMA Reserves
}
//////////////////// LAST RESERVES ////////////////////
/**
* @dev Reads the last capped reserves from the Pump from storage.
*/
function readLastCappedReserves(
address well,
bytes memory
) public view returns (uint256[] memory lastCappedReserves) {
uint8 numberOfReserves;
(numberOfReserves,, lastCappedReserves) = _getSlotForAddress(well).readLastReserves();
if (numberOfReserves == 0) {
revert NotInitialized();
}
}
/**
* @dev Reads the capped reserves from the Pump updated to the current block using the current reserves of `well`.
*/
function readCappedReserves(
address well,
bytes calldata data
) external view returns (uint256[] memory cappedReserves) {
(, uint256 capInterval, CapReservesParameters memory crp) =
abi.decode(data, (bytes16, uint256, CapReservesParameters));
bytes32 slot = _getSlotForAddress(well);
uint256[] memory currentReserves = IWell(well).getReserves();
uint8 numberOfReserves;
uint40 lastTimestamp;
(numberOfReserves, lastTimestamp, cappedReserves) = slot.readLastReserves();
if (numberOfReserves == 0) {
revert NotInitialized();
}
uint256 deltaTimestamp = _getDeltaTimestamp(lastTimestamp);
if (deltaTimestamp == 0) {
return cappedReserves;
}
uint256 capExponent = calcCapExponent(deltaTimestamp, capInterval);
cappedReserves = _capReserves(well, cappedReserves, currentReserves, capExponent, crp);
}
/**
* @notice Cap `reserves` to have at most a maximum % increase/decrease in rate and a maximum % increase/decrease in total liquidity
* in relation to `lastReserves` based on the parameters defined in `crp` and the time passed since the last update, which is used
* to calculate `capExponent`.
* @param well The address of the Well
* @param lastReserves The last capped reserves.
* @param reserves The current reserves being capped.
* @param capExponent The exponent to raise the all % changes to.
* @param crp The parameters for capping reserves. See {CapReservesParameters}.
* @return cappedReserves The current reserves capped to the maximum % changes defined by `crp`.
*/
function _capReserves(
address well,
uint256[] memory lastReserves,
uint256[] memory reserves,
uint256 capExponent,
CapReservesParameters memory crp
) internal view returns (uint256[] memory cappedReserves) {
Call memory wf = IWell(well).wellFunction();
IMultiFlowPumpWellFunction mfpWf = IMultiFlowPumpWellFunction(wf.target);
// The order that the LP token supply and the rates are capped are dependent upon the values of the reserves to maximize precision.
cappedReserves = _capLpTokenSupply(lastReserves, reserves, capExponent, crp, mfpWf, wf.data, true);
// If `_capLpTokenSupply` returns an empty array, then the rates should be capped first.
if (cappedReserves.length == 0) {
cappedReserves = _capRates(lastReserves, reserves, capExponent, crp, mfpWf, wf.data);
cappedReserves = _capLpTokenSupply(lastReserves, cappedReserves, capExponent, crp, mfpWf, wf.data, false);
} else {
cappedReserves = _capRates(lastReserves, cappedReserves, capExponent, crp, mfpWf, wf.data);
}
}
/**
* @dev Cap the change in ratio of `reserves` to a maximum % change from `lastReserves`.
*/
function _capRates(
uint256[] memory lastReserves,
uint256[] memory reserves,
uint256 capExponent,
CapReservesParameters memory crp,
IMultiFlowPumpWellFunction mfpWf,
bytes memory data
) internal view returns (uint256[] memory cappedReserves) {
cappedReserves = reserves;
// Part 1: Cap Rates
// Use the larger reserve as the numerator for the ratio to maximize precision
(uint256 i, uint256 j) = lastReserves[0] > lastReserves[1] ? (0, 1) : (1, 0);
CapRatesVariables memory crv;
crv.rLast = tryCalcRate(mfpWf, lastReserves, i, j, data);
crv.r = tryCalcRate(mfpWf, cappedReserves, i, j, data);
// The ratio can only be infinite due to unintended behavior.
// Therefore, to get the reseves back to a normal state, return the current reserves.
if (crv.rLast == type(uint256).max) {
return reserves;
}
// If the ratio increased, check that it didn't increase above the max.
if (crv.r > crv.rLast) {
bytes16 tempExp = ABDKMathQuad.ONE.add(crp.maxRateChanges[i][j]).powu(capExponent);
crv.rLimit = tempExp.cmp(MAX_CONVERT_TO_128x128) != -1
? crv.rLimit = type(uint256).max
: crv.rLast.mulDivOrMax(tempExp.to128x128().toUint256(), CAP_PRECISION2);
if (crv.r > crv.rLimit) {
calcReservesAtRatioSwap(mfpWf, crv.rLimit, cappedReserves, i, j, data);
}
// If the ratio decreased, check that it didn't overflow during calculation
} else if (crv.r < crv.rLast) {
bytes16 tempExp = ABDKMathQuad.ONE.div(ABDKMathQuad.ONE.add(crp.maxRateChanges[j][i])).powu(capExponent);
// Check for overflow before converting to 128x128
if (tempExp.cmp(MAX_CONVERT_TO_128x128) != -1) {
crv.rLimit = 0; // Set limit to 0 in case of overflow
} else {
crv.rLimit = crv.rLast.mulDiv(tempExp.to128x128().toUint256(), CAP_PRECISION2);
}
if (crv.r < crv.rLimit) {
calcReservesAtRatioSwap(mfpWf, crv.rLimit, cappedReserves, i, j, data);
}
}
}
/**
* @dev Cap the change in LP Token Supply of `reserves` to a maximum % change from `lastReserves`.
*/
function _capLpTokenSupply(
uint256[] memory lastReserves,
uint256[] memory reserves,
uint256 capExponent,
CapReservesParameters memory crp,
IMultiFlowPumpWellFunction mfpWf,
bytes memory data,
bool returnIfBelowMin
) internal view returns (uint256[] memory cappedReserves) {
cappedReserves = reserves;
// Part 2: Cap LP Token Supply Change
uint256 lastLpTokenSupply = tryCalcLpTokenSupply(mfpWf, lastReserves, data);
uint256 lpTokenSupply = tryCalcLpTokenSupply(mfpWf, cappedReserves, data);
// If LP Token Supply increased, check that it didn't increase above the max.
if (lpTokenSupply > lastLpTokenSupply) {
bytes16 tempExp = ABDKMathQuad.ONE.add(crp.maxLpSupplyIncrease).powu(capExponent);
uint256 maxLpTokenSupply = tempExp.cmp(MAX_CONVERT_TO_128x128) != -1
? type(uint256).max
: lastLpTokenSupply.mulDiv(tempExp.to128x128().toUint256(), CAP_PRECISION2);
if (lpTokenSupply > maxLpTokenSupply) {
// If `_capLpTokenSupply` decreases the reserves, cap the ratio first, to maximize precision.
if (returnIfBelowMin) return new uint256[](0);
cappedReserves = tryCalcLPTokenUnderlying(mfpWf, maxLpTokenSupply, cappedReserves, lpTokenSupply, data);
}
// If LP Token Supply decreased, check that it didn't decrease below the min.
} else if (lpTokenSupply < lastLpTokenSupply) {
uint256 minLpTokenSupply = lastLpTokenSupply
* (ABDKMathQuad.ONE.sub(crp.maxLpSupplyDecrease)).powu(capExponent).to128x128().toUint256() / CAP_PRECISION2;
if (lpTokenSupply < minLpTokenSupply) {
cappedReserves = tryCalcLPTokenUnderlying(mfpWf, minLpTokenSupply, cappedReserves, lpTokenSupply, data);
}
}
}
//////////////////// EMA RESERVES ////////////////////
function readLastInstantaneousReserves(
address well,
bytes memory
) external view returns (uint256[] memory emaReserves) {
bytes32 slot = _getSlotForAddress(well);
uint8 numberOfReserves = slot.readNumberOfReserves();
if (numberOfReserves == 0) {
revert NotInitialized();
}
uint256 offset = _getSlotsOffset(numberOfReserves);
assembly {
slot := add(slot, offset)
}
bytes16[] memory byteReserves = slot.readBytes16(numberOfReserves);
emaReserves = new uint256[](numberOfReserves);
for (uint256 i; i < numberOfReserves; ++i) {
emaReserves[i] = byteReserves[i].pow_2ToUInt();
}
}
function readInstantaneousReserves(
address well,
bytes memory data
) external view returns (uint256[] memory emaReserves) {
(bytes16 alpha, uint256 capInterval, CapReservesParameters memory crp) =
abi.decode(data, (bytes16, uint256, CapReservesParameters));
bytes32 slot = _getSlotForAddress(well);
uint256[] memory reserves = IWell(well).getReserves();
(uint8 numberOfReserves, uint40 lastTimestamp, uint256[] memory lastReserves) = slot.readLastReserves();
if (numberOfReserves == 0) {
revert NotInitialized();
}
uint256 offset = _getSlotsOffset(numberOfReserves);
assembly {
slot := add(slot, offset)
}
bytes16[] memory lastEmaReserves = slot.readBytes16(numberOfReserves);
emaReserves = new uint256[](numberOfReserves);
uint256 deltaTimestamp = _getDeltaTimestamp(lastTimestamp);
// If no time has passed, return last EMA reserves.
if (deltaTimestamp == 0) {
for (uint256 i; i < numberOfReserves; ++i) {
emaReserves[i] = lastEmaReserves[i].pow_2ToUInt();
}
return emaReserves;
}
uint256 capExponent = calcCapExponent(deltaTimestamp, capInterval);
lastReserves = _capReserves(well, lastReserves, reserves, capExponent, crp);
bytes16 alphaN = alpha.powu(deltaTimestamp);
for (uint256 i; i < numberOfReserves; ++i) {
emaReserves[i] = lastReserves[i].fromUIntToLog2().mul((ABDKMathQuad.ONE.sub(alphaN))).add(
lastEmaReserves[i].mul(alphaN)
).pow_2ToUInt();
}
}
//////////////////// CUMULATIVE RESERVES ////////////////////
/**
* @notice Read the latest cumulative reserves of `well`.
*/
function readLastCumulativeReserves(
address well,
bytes memory
) external view returns (bytes16[] memory cumulativeReserves) {
bytes32 slot = _getSlotForAddress(well);
uint8 numberOfReserves = slot.readNumberOfReserves();
if (numberOfReserves == 0) {
revert NotInitialized();
}
uint256 offset = _getSlotsOffset(numberOfReserves) << 1;
assembly {
slot := add(slot, offset)
}
cumulativeReserves = slot.readBytes16(numberOfReserves);
}
function readCumulativeReserves(
address well,
bytes memory data
) external view returns (bytes memory cumulativeReserves) {
bytes16[] memory byteCumulativeReserves = _readCumulativeReserves(well, data);
cumulativeReserves = abi.encode(byteCumulativeReserves);
}
function _readCumulativeReserves(
address well,
bytes memory data
) internal view returns (bytes16[] memory cumulativeReserves) {
(, uint256 capInterval, CapReservesParameters memory crp) =
abi.decode(data, (bytes16, uint256, CapReservesParameters));
bytes32 slot = _getSlotForAddress(well);
uint256[] memory reserves = IWell(well).getReserves();
(uint8 numberOfReserves, uint40 lastTimestamp, uint256[] memory lastReserves) = slot.readLastReserves();
if (numberOfReserves == 0) {
revert NotInitialized();
}
uint256 offset = _getSlotsOffset(numberOfReserves) << 1;
assembly {
slot := add(slot, offset)
}
cumulativeReserves = slot.readBytes16(numberOfReserves);
uint256 deltaTimestamp = _getDeltaTimestamp(lastTimestamp);
// If no time has passed, return last cumulative reserves.
if (deltaTimestamp == 0) {
return cumulativeReserves;
}
bytes16 deltaTimestampBytes = deltaTimestamp.fromUInt();
uint256 capExponent = calcCapExponent(deltaTimestamp, capInterval);
lastReserves = _capReserves(well, lastReserves, reserves, capExponent, crp);
// Currently, there is so support for overflow.
for (uint256 i; i < cumulativeReserves.length; ++i) {
cumulativeReserves[i] = cumulativeReserves[i].add(lastReserves[i].fromUIntToLog2().mul(deltaTimestampBytes));
}
}
function readTwaReserves(
address well,
bytes calldata startCumulativeReserves,
uint256 startTimestamp,
bytes memory data
) public view returns (uint256[] memory twaReserves, bytes memory cumulativeReserves) {
bytes16[] memory byteCumulativeReserves = _readCumulativeReserves(well, data);
bytes16[] memory byteStartCumulativeReserves = abi.decode(startCumulativeReserves, (bytes16[]));
twaReserves = new uint256[](byteCumulativeReserves.length);
// Overflow is desired on `startTimestamp`, so SafeCast is not used.
bytes16 deltaTimestamp = _getDeltaTimestamp(uint40(startTimestamp)).fromUInt();
if (deltaTimestamp == bytes16(0)) {
revert NoTimePassed();
}
for (uint256 i; i < byteCumulativeReserves.length; ++i) {
// Currently, there is no support for overflow.
twaReserves[i] =
(byteCumulativeReserves[i].sub(byteStartCumulativeReserves[i])).div(deltaTimestamp).pow_2ToUInt();
}
cumulativeReserves = abi.encode(byteCumulativeReserves);
}
//////////////////// HELPERS ////////////////////
/**
* @dev Calculate the cap exponent for a given `deltaTimestamp` and `capInterval`.
*/
function calcCapExponent(uint256 deltaTimestamp, uint256 capInterval) private pure returns (uint256 capExponent) {
capExponent = ((deltaTimestamp - 1) / capInterval + 1);
}
/**
* @dev Calculates the capped reserves given a rate limit.
*/
function calcReservesAtRatioSwap(
IMultiFlowPumpWellFunction mfpWf,
uint256 rLimit,
uint256[] memory reserves,
uint256 i,
uint256 j,
bytes memory data
) private view returns (uint256[] memory) {
uint256[] memory ratios = new uint256[](2);
ratios[i] = rLimit;
ratios[j] = mfpWf.ratioPrecision(j, data);
// Use a minimum of 1 for reserve. Geometric means will be set to 0 if a reserve is 0.
uint256 cappedReserveI = Math.max(tryCalcReserveAtRatioSwap(mfpWf, reserves, i, ratios, data), 1);
reserves[j] = Math.max(tryCalcReserveAtRatioSwap(mfpWf, reserves, j, ratios, data), 1);
reserves[i] = cappedReserveI;
return reserves;
}
/**
* @dev Convert an `address` into a `bytes32` by zero padding the right 12 bytes.
*/
function _getSlotForAddress(
address addressValue
) internal pure returns (bytes32 _slot) {
_slot = bytes32(bytes20(addressValue)); // Because right padded, no collision on adjacent
}
/**
* @dev Get the slot number that contains the `n`th element of an array.
* slots are seperated by 32 bytes to allow for future expansion of the Pump (i.e supporting Well with more than 3 tokens).
*/
function _getSlotsOffset(
uint256 numberOfReserves
) internal pure returns (uint256 _slotsOffset) {
_slotsOffset = ((numberOfReserves - 1) / 2 + 1) << 5;
}
/**
* @dev Get the delta between the current and provided timestamp as a `uint256`.
*/
function _getDeltaTimestamp(
uint40 lastTimestamp
) internal view returns (uint256 _deltaTimestamp) {
return uint256(uint40(block.timestamp) - lastTimestamp);
}
/**
* @dev Assumes that if `calcReserveAtRatioSwap` fails, it fails because of overflow.
* If the call fails, returns the maximum possible return value for `calcReserveAtRatioSwap`.
*/
function tryCalcReserveAtRatioSwap(
IMultiFlowPumpWellFunction wf,
uint256[] memory reserves,
uint256 i,
uint256[] memory ratios,
bytes memory data
) internal view returns (uint256 reserve) {
try wf.calcReserveAtRatioSwap(reserves, i, ratios, data) returns (uint256 _reserve) {
reserve = _reserve;
} catch {
reserve = type(uint128).max;
}
}
/**
* @dev Assumes that if `calcLpTokenSupply` fails, it fails because of overflow.
* If it fails, returns the maximum possible return value for `calcLpTokenSupply`.
*/
function tryCalcLpTokenSupply(
IMultiFlowPumpWellFunction wf,
uint256[] memory reserves,
bytes memory data
) internal view returns (uint256 lpTokenSupply) {
try wf.calcLpTokenSupply(reserves, data) returns (uint256 _lpTokenSupply) {
lpTokenSupply = _lpTokenSupply;
} catch {
lpTokenSupply = MAX_UINT256_SQRT;
}
}
/**
* @dev Assumes that if `CalcRate` fails, it fails because of overflow.
* If it fails, returns the maximum possible return value for `CalcRate`.
*/
function tryCalcRate(
IMultiFlowPumpWellFunction wf,
uint256[] memory reserves,
uint256 i,
uint256 j,
bytes memory data
) internal view returns (uint256 rate) {
try wf.calcRate(reserves, i, j, data) returns (uint256 _rate) {
rate = _rate;
} catch {
rate = type(uint256).max;
}
}
/**
* @dev Assumes that if `calcLPTokenUnderlying` fails, it fails because of overflow.
* If the call fails, returns the maximum possible return value for `calcLPTokenUnderlying`.
* Also, enforces a minimum of 1 for each reserve.
*/
function tryCalcLPTokenUnderlying(
IMultiFlowPumpWellFunction wf,
uint256 lpTokenAmount,
uint256[] memory reserves,
uint256 lpTokenSupply,
bytes memory data
) internal view returns (uint256[] memory underlyingAmounts) {
try wf.calcLPTokenUnderlying(lpTokenAmount, reserves, lpTokenSupply, data) returns (
uint256[] memory _underlyingAmounts
) {
underlyingAmounts = _underlyingAmounts;
} catch {
underlyingAmounts = new uint256[](reserves.length);
for (uint256 i; i < reserves.length; ++i) {
underlyingAmounts[i] = 0;
}
}
}
}