-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwapMathEchidnaTest.sol
52 lines (44 loc) · 1.71 KB
/
SwapMathEchidnaTest.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.7.6;
import '../libraries/SwapMath.sol';
contract SwapMathEchidnaTest {
function checkComputeSwapStepInvariants(
uint160 sqrtPriceRaw,
uint160 sqrtPriceTargetRaw,
uint128 liquidity,
int256 amountRemaining,
uint24 feePips
) external pure {
require(sqrtPriceRaw > 0);
require(sqrtPriceTargetRaw > 0);
require(feePips > 0);
require(feePips < 1e6);
(uint160 sqrtQ, uint256 amountIn, uint256 amountOut, uint256 feeAmount) =
SwapMath.computeSwapStep(sqrtPriceRaw, sqrtPriceTargetRaw, liquidity, amountRemaining, feePips);
assert(amountIn <= type(uint256).max - feeAmount);
if (amountRemaining < 0) {
assert(amountOut <= uint256(-amountRemaining));
} else {
assert(amountIn + feeAmount <= uint256(amountRemaining));
}
if (sqrtPriceRaw == sqrtPriceTargetRaw) {
assert(amountIn == 0);
assert(amountOut == 0);
assert(feeAmount == 0);
assert(sqrtQ == sqrtPriceTargetRaw);
}
// didn't reach price target, entire amount must be consumed
if (sqrtQ != sqrtPriceTargetRaw) {
if (amountRemaining < 0) assert(amountOut == uint256(-amountRemaining));
else assert(amountIn + feeAmount == uint256(amountRemaining));
}
// next price is between price and price target
if (sqrtPriceTargetRaw <= sqrtPriceRaw) {
assert(sqrtQ <= sqrtPriceRaw);
assert(sqrtQ >= sqrtPriceTargetRaw);
} else {
assert(sqrtQ >= sqrtPriceRaw);
assert(sqrtQ <= sqrtPriceTargetRaw);
}
}
}