forked from Uniswap/v3-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqrtPriceMathEchidnaTest.sol
232 lines (198 loc) Β· 7.43 KB
/
SqrtPriceMathEchidnaTest.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.7.6;
import '../libraries/FullMath.sol';
import '../libraries/SqrtPriceMath.sol';
import '../libraries/FixedPoint96.sol';
contract SqrtPriceMathEchidnaTest {
function mulDivRoundingUpInvariants(
uint256 x,
uint256 y,
uint256 z
) external pure {
require(z > 0);
uint256 notRoundedUp = FullMath.mulDiv(x, y, z);
uint256 roundedUp = FullMath.mulDivRoundingUp(x, y, z);
assert(roundedUp >= notRoundedUp);
assert(roundedUp - notRoundedUp < 2);
if (roundedUp - notRoundedUp == 1) {
assert(mulmod(x, y, z) > 0);
} else {
assert(mulmod(x, y, z) == 0);
}
}
function getNextSqrtPriceFromInputInvariants(
uint160 sqrtP,
uint128 liquidity,
uint256 amountIn,
bool zeroForOne
) external pure {
uint160 sqrtQ = SqrtPriceMath.getNextSqrtPriceFromInput(sqrtP, liquidity, amountIn, zeroForOne);
if (zeroForOne) {
assert(sqrtQ <= sqrtP);
assert(amountIn >= SqrtPriceMath.getAmount0Delta(sqrtQ, sqrtP, liquidity, true));
} else {
assert(sqrtQ >= sqrtP);
assert(amountIn >= SqrtPriceMath.getAmount1Delta(sqrtP, sqrtQ, liquidity, true));
}
}
function getNextSqrtPriceFromOutputInvariants(
uint160 sqrtP,
uint128 liquidity,
uint256 amountOut,
bool zeroForOne
) external pure {
uint160 sqrtQ = SqrtPriceMath.getNextSqrtPriceFromOutput(sqrtP, liquidity, amountOut, zeroForOne);
if (zeroForOne) {
assert(sqrtQ <= sqrtP);
assert(amountOut <= SqrtPriceMath.getAmount1Delta(sqrtQ, sqrtP, liquidity, false));
} else {
assert(sqrtQ > 0); // this has to be true, otherwise we need another require
assert(sqrtQ >= sqrtP);
assert(amountOut <= SqrtPriceMath.getAmount0Delta(sqrtP, sqrtQ, liquidity, false));
}
}
function getNextSqrtPriceFromAmount0RoundingUpInvariants(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) external pure {
require(sqrtPX96 > 0);
require(liquidity > 0);
uint160 sqrtQX96 = SqrtPriceMath.getNextSqrtPriceFromAmount0RoundingUp(sqrtPX96, liquidity, amount, add);
if (add) {
assert(sqrtQX96 <= sqrtPX96);
} else {
assert(sqrtQX96 >= sqrtPX96);
}
if (amount == 0) {
assert(sqrtPX96 == sqrtQX96);
}
}
function getNextSqrtPriceFromAmount1RoundingDownInvariants(
uint160 sqrtPX96,
uint128 liquidity,
uint256 amount,
bool add
) external pure {
require(sqrtPX96 > 0);
require(liquidity > 0);
uint160 sqrtQX96 = SqrtPriceMath.getNextSqrtPriceFromAmount1RoundingDown(sqrtPX96, liquidity, amount, add);
if (add) {
assert(sqrtQX96 >= sqrtPX96);
} else {
assert(sqrtQX96 <= sqrtPX96);
}
if (amount == 0) {
assert(sqrtPX96 == sqrtQX96);
}
}
function getAmount0DeltaInvariants(
uint160 sqrtP,
uint160 sqrtQ,
uint128 liquidity
) external pure {
require(sqrtP > 0 && sqrtQ > 0);
uint256 amount0Down = SqrtPriceMath.getAmount0Delta(sqrtQ, sqrtP, liquidity, false);
assert(amount0Down == SqrtPriceMath.getAmount0Delta(sqrtP, sqrtQ, liquidity, false));
uint256 amount0Up = SqrtPriceMath.getAmount0Delta(sqrtQ, sqrtP, liquidity, true);
assert(amount0Up == SqrtPriceMath.getAmount0Delta(sqrtP, sqrtQ, liquidity, true));
assert(amount0Down <= amount0Up);
// diff is 0 or 1
assert(amount0Up - amount0Down < 2);
}
// ensure that chained division is always equal to the full-precision case for
// liquidity * (sqrt(P) - sqrt(Q)) / (sqrt(P) * sqrt(Q))
function getAmount0DeltaEquivalency(
uint160 sqrtP,
uint160 sqrtQ,
uint128 liquidity,
bool roundUp
) external pure {
require(sqrtP >= sqrtQ);
require(sqrtP > 0 && sqrtQ > 0);
require((sqrtP * sqrtQ) / sqrtP == sqrtQ);
uint256 numerator1 = uint256(liquidity) << FixedPoint96.RESOLUTION;
uint256 numerator2 = sqrtP - sqrtQ;
uint256 denominator = uint256(sqrtP) * sqrtQ;
uint256 safeResult =
roundUp
? FullMath.mulDivRoundingUp(numerator1, numerator2, denominator)
: FullMath.mulDiv(numerator1, numerator2, denominator);
uint256 fullResult = SqrtPriceMath.getAmount0Delta(sqrtQ, sqrtP, liquidity, roundUp);
assert(safeResult == fullResult);
}
function getAmount1DeltaInvariants(
uint160 sqrtP,
uint160 sqrtQ,
uint128 liquidity
) external pure {
require(sqrtP > 0 && sqrtQ > 0);
uint256 amount1Down = SqrtPriceMath.getAmount1Delta(sqrtP, sqrtQ, liquidity, false);
assert(amount1Down == SqrtPriceMath.getAmount1Delta(sqrtQ, sqrtP, liquidity, false));
uint256 amount1Up = SqrtPriceMath.getAmount1Delta(sqrtP, sqrtQ, liquidity, true);
assert(amount1Up == SqrtPriceMath.getAmount1Delta(sqrtQ, sqrtP, liquidity, true));
assert(amount1Down <= amount1Up);
// diff is 0 or 1
assert(amount1Up - amount1Down < 2);
}
function getAmount0DeltaSignedInvariants(
uint160 sqrtP,
uint160 sqrtQ,
int128 liquidity
) external pure {
require(sqrtP > 0 && sqrtQ > 0);
int256 amount0 = SqrtPriceMath.getAmount0Delta(sqrtQ, sqrtP, liquidity);
if (liquidity < 0) assert(amount0 <= 0);
if (liquidity > 0) {
if (sqrtP == sqrtQ) assert(amount0 == 0);
else assert(amount0 > 0);
}
if (liquidity == 0) assert(amount0 == 0);
}
function getAmount1DeltaSignedInvariants(
uint160 sqrtP,
uint160 sqrtQ,
int128 liquidity
) external pure {
require(sqrtP > 0 && sqrtQ > 0);
int256 amount1 = SqrtPriceMath.getAmount1Delta(sqrtP, sqrtQ, liquidity);
if (liquidity < 0) assert(amount1 <= 0);
if (liquidity > 0) {
if (sqrtP == sqrtQ) assert(amount1 == 0);
else assert(amount1 > 0);
}
if (liquidity == 0) assert(amount1 == 0);
}
function getOutOfRangeMintInvariants(
uint160 sqrtA,
uint160 sqrtB,
int128 liquidity
) external pure {
require(sqrtA > 0 && sqrtB > 0);
require(liquidity > 0);
int256 amount0 = SqrtPriceMath.getAmount0Delta(sqrtA, sqrtB, liquidity);
int256 amount1 = SqrtPriceMath.getAmount1Delta(sqrtA, sqrtB, liquidity);
if (sqrtA == sqrtB) {
assert(amount0 == 0);
assert(amount1 == 0);
} else {
assert(amount0 > 0);
assert(amount1 > 0);
}
}
function getInRangeMintInvariants(
uint160 sqrtLower,
uint160 sqrtCurrent,
uint160 sqrtUpper,
int128 liquidity
) external pure {
require(sqrtLower > 0);
require(sqrtLower < sqrtUpper);
require(sqrtLower <= sqrtCurrent && sqrtCurrent <= sqrtUpper);
require(liquidity > 0);
int256 amount0 = SqrtPriceMath.getAmount0Delta(sqrtCurrent, sqrtUpper, liquidity);
int256 amount1 = SqrtPriceMath.getAmount1Delta(sqrtLower, sqrtCurrent, liquidity);
assert(amount0 > 0 || amount1 > 0);
}
}