forked from Uniswap/v3-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwapMath.spec.ts
324 lines (296 loc) Β· 10.7 KB
/
SwapMath.spec.ts
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
import { BigNumber } from 'ethers'
import { ethers } from 'hardhat'
import { SwapMathTest } from '../typechain/SwapMathTest'
import { expect } from './shared/expect'
import snapshotGasCost from './shared/snapshotGasCost'
import { encodePriceSqrt, expandTo18Decimals } from './shared/utilities'
import { SqrtPriceMathTest } from '../typechain/SqrtPriceMathTest'
describe('SwapMath', () => {
let swapMath: SwapMathTest
let sqrtPriceMath: SqrtPriceMathTest
before(async () => {
const swapMathTestFactory = await ethers.getContractFactory('SwapMathTest')
const sqrtPriceMathTestFactory = await ethers.getContractFactory('SqrtPriceMathTest')
swapMath = (await swapMathTestFactory.deploy()) as SwapMathTest
sqrtPriceMath = (await sqrtPriceMathTestFactory.deploy()) as SqrtPriceMathTest
})
describe('#computeSwapStep', () => {
it('exact amount in that gets capped at price target in one for zero', async () => {
const price = encodePriceSqrt(1, 1)
const priceTarget = encodePriceSqrt(101, 100)
const liquidity = expandTo18Decimals(2)
const amount = expandTo18Decimals(1)
const fee = 600
const zeroForOne = false
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
price,
priceTarget,
liquidity,
amount,
fee
)
expect(amountIn).to.eq('9975124224178055')
expect(feeAmount).to.eq('5988667735148')
expect(amountOut).to.eq('9925619580021728')
expect(amountIn.add(feeAmount), 'entire amount is not used').to.lt(amount)
const priceAfterWholeInputAmount = await sqrtPriceMath.getNextSqrtPriceFromInput(
price,
liquidity,
amount,
zeroForOne
)
expect(sqrtQ, 'price is capped at price target').to.eq(priceTarget)
expect(sqrtQ, 'price is less than price after whole input amount').to.lt(priceAfterWholeInputAmount)
})
it('exact amount out that gets capped at price target in one for zero', async () => {
const price = encodePriceSqrt(1, 1)
const priceTarget = encodePriceSqrt(101, 100)
const liquidity = expandTo18Decimals(2)
const amount = expandTo18Decimals(1).mul(-1)
const fee = 600
const zeroForOne = false
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
price,
priceTarget,
liquidity,
amount,
fee
)
expect(amountIn).to.eq('9975124224178055')
expect(feeAmount).to.eq('5988667735148')
expect(amountOut).to.eq('9925619580021728')
expect(amountOut, 'entire amount out is not returned').to.lt(amount.mul(-1))
const priceAfterWholeOutputAmount = await sqrtPriceMath.getNextSqrtPriceFromOutput(
price,
liquidity,
amount.mul(-1),
zeroForOne
)
expect(sqrtQ, 'price is capped at price target').to.eq(priceTarget)
expect(sqrtQ, 'price is less than price after whole output amount').to.lt(priceAfterWholeOutputAmount)
})
it('exact amount in that is fully spent in one for zero', async () => {
const price = encodePriceSqrt(1, 1)
const priceTarget = encodePriceSqrt(1000, 100)
const liquidity = expandTo18Decimals(2)
const amount = expandTo18Decimals(1)
const fee = 600
const zeroForOne = false
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
price,
priceTarget,
liquidity,
amount,
fee
)
expect(amountIn).to.eq('999400000000000000')
expect(feeAmount).to.eq('600000000000000')
expect(amountOut).to.eq('666399946655997866')
expect(amountIn.add(feeAmount), 'entire amount is used').to.eq(amount)
const priceAfterWholeInputAmountLessFee = await sqrtPriceMath.getNextSqrtPriceFromInput(
price,
liquidity,
amount.sub(feeAmount),
zeroForOne
)
expect(sqrtQ, 'price does not reach price target').to.be.lt(priceTarget)
expect(sqrtQ, 'price is equal to price after whole input amount').to.eq(priceAfterWholeInputAmountLessFee)
})
it('exact amount out that is fully received in one for zero', async () => {
const price = encodePriceSqrt(1, 1)
const priceTarget = encodePriceSqrt(10000, 100)
const liquidity = expandTo18Decimals(2)
const amount = expandTo18Decimals(1).mul(-1)
const fee = 600
const zeroForOne = false
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
price,
priceTarget,
liquidity,
amount,
fee
)
expect(amountIn).to.eq('2000000000000000000')
expect(feeAmount).to.eq('1200720432259356')
expect(amountOut).to.eq(amount.mul(-1))
const priceAfterWholeOutputAmount = await sqrtPriceMath.getNextSqrtPriceFromOutput(
price,
liquidity,
amount.mul(-1),
zeroForOne
)
expect(sqrtQ, 'price does not reach price target').to.be.lt(priceTarget)
expect(sqrtQ, 'price is less than price after whole output amount').to.eq(priceAfterWholeOutputAmount)
})
it('amount out is capped at the desired amount out', async () => {
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
BigNumber.from('417332158212080721273783715441582'),
BigNumber.from('1452870262520218020823638996'),
'159344665391607089467575320103',
'-1',
1
)
expect(amountIn).to.eq('1')
expect(feeAmount).to.eq('1')
expect(amountOut).to.eq('1') // would be 2 if not capped
expect(sqrtQ).to.eq('417332158212080721273783715441581')
})
it('target price of 1 uses partial input amount', async () => {
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
BigNumber.from('2'),
BigNumber.from('1'),
'1',
'3915081100057732413702495386755767',
1
)
expect(amountIn).to.eq('39614081257132168796771975168')
expect(feeAmount).to.eq('39614120871253040049813')
expect(amountIn.add(feeAmount)).to.be.lte('3915081100057732413702495386755767')
expect(amountOut).to.eq('0')
expect(sqrtQ).to.eq('1')
})
it('entire input amount taken as fee', async () => {
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
'2413',
'79887613182836312',
'1985041575832132834610021537970',
'10',
1872
)
expect(amountIn).to.eq('0')
expect(feeAmount).to.eq('10')
expect(amountOut).to.eq('0')
expect(sqrtQ).to.eq('2413')
})
it('handles intermediate insufficient liquidity in zero for one exact output case', async () => {
const sqrtP = BigNumber.from('20282409603651670423947251286016')
const sqrtPTarget = sqrtP.mul(11).div(10)
const liquidity = 1024
// virtual reserves of one are only 4
// https://www.wolframalpha.com/input/?i=1024+%2F+%2820282409603651670423947251286016+%2F+2**96%29
const amountRemaining = -4
const feePips = 3000
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
sqrtP,
sqrtPTarget,
liquidity,
amountRemaining,
feePips
)
expect(amountOut).to.eq(0)
expect(sqrtQ).to.eq(sqrtPTarget)
expect(amountIn).to.eq(26215)
expect(feeAmount).to.eq(79)
})
it('handles intermediate insufficient liquidity in one for zero exact output case', async () => {
const sqrtP = BigNumber.from('20282409603651670423947251286016')
const sqrtPTarget = sqrtP.mul(9).div(10)
const liquidity = 1024
// virtual reserves of zero are only 262144
// https://www.wolframalpha.com/input/?i=1024+*+%2820282409603651670423947251286016+%2F+2**96%29
const amountRemaining = -263000
const feePips = 3000
const { amountIn, amountOut, sqrtQ, feeAmount } = await swapMath.computeSwapStep(
sqrtP,
sqrtPTarget,
liquidity,
amountRemaining,
feePips
)
expect(amountOut).to.eq(26214)
expect(sqrtQ).to.eq(sqrtPTarget)
expect(amountIn).to.eq(1)
expect(feeAmount).to.eq(1)
})
describe('gas', () => {
it('swap one for zero exact in capped', async () => {
await snapshotGasCost(
swapMath.getGasCostOfComputeSwapStep(
encodePriceSqrt(1, 1),
encodePriceSqrt(101, 100),
expandTo18Decimals(2),
expandTo18Decimals(1),
600
)
)
})
it('swap zero for one exact in capped', async () => {
await snapshotGasCost(
swapMath.getGasCostOfComputeSwapStep(
encodePriceSqrt(1, 1),
encodePriceSqrt(99, 100),
expandTo18Decimals(2),
expandTo18Decimals(1),
600
)
)
})
it('swap one for zero exact out capped', async () => {
await snapshotGasCost(
swapMath.getGasCostOfComputeSwapStep(
encodePriceSqrt(1, 1),
encodePriceSqrt(101, 100),
expandTo18Decimals(2),
expandTo18Decimals(1).mul(-1),
600
)
)
})
it('swap zero for one exact out capped', async () => {
await snapshotGasCost(
swapMath.getGasCostOfComputeSwapStep(
encodePriceSqrt(1, 1),
encodePriceSqrt(99, 100),
expandTo18Decimals(2),
expandTo18Decimals(1).mul(-1),
600
)
)
})
it('swap one for zero exact in partial', async () => {
await snapshotGasCost(
swapMath.getGasCostOfComputeSwapStep(
encodePriceSqrt(1, 1),
encodePriceSqrt(1010, 100),
expandTo18Decimals(2),
1000,
600
)
)
})
it('swap zero for one exact in partial', async () => {
await snapshotGasCost(
swapMath.getGasCostOfComputeSwapStep(
encodePriceSqrt(1, 1),
encodePriceSqrt(99, 1000),
expandTo18Decimals(2),
1000,
600
)
)
})
it('swap one for zero exact out partial', async () => {
await snapshotGasCost(
swapMath.getGasCostOfComputeSwapStep(
encodePriceSqrt(1, 1),
encodePriceSqrt(1010, 100),
expandTo18Decimals(2),
1000,
600
)
)
})
it('swap zero for one exact out partial', async () => {
await snapshotGasCost(
swapMath.getGasCostOfComputeSwapStep(
encodePriceSqrt(1, 1),
encodePriceSqrt(99, 1000),
expandTo18Decimals(2),
1000,
600
)
)
})
})
})
})