forked from superrare/v3-periphery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoolTicksCounter.spec.ts
280 lines (255 loc) · 10.2 KB
/
PoolTicksCounter.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
import { waffle, ethers, artifacts } from 'hardhat'
import { expect } from './shared/expect'
import { PoolTicksCounterTest } from '../typechain'
import { deployMockContract, Fixture, MockContract } from 'ethereum-waffle'
import { Artifact } from 'hardhat/types'
describe('PoolTicksCounter', () => {
const TICK_SPACINGS = [200, 60, 10]
TICK_SPACINGS.forEach((TICK_SPACING) => {
let PoolTicksCounter: PoolTicksCounterTest
let pool: MockContract
let PoolAbi: Artifact
// Bit index to tick
const bitIdxToTick = (idx: number, page = 0) => {
return idx * TICK_SPACING + page * 256 * TICK_SPACING
}
before(async () => {
const wallets = await (ethers as any).getSigners()
PoolAbi = await artifacts.readArtifact('IUniswapV3Pool')
const poolTicksHelperFactory = await ethers.getContractFactory('PoolTicksCounterTest')
PoolTicksCounter = (await poolTicksHelperFactory.deploy()) as PoolTicksCounterTest
pool = await deployMockContract(wallets[0], PoolAbi.abi)
await pool.mock.tickSpacing.returns(TICK_SPACING)
})
describe(`[Tick Spacing: ${TICK_SPACING}]: tick after is bigger`, async () => {
it('same tick initialized', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1100) // 1100
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(2),
bitIdxToTick(2)
)
expect(result).to.be.eq(1)
})
it('same tick not-initialized', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1100) // 1100
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(1),
bitIdxToTick(1)
)
expect(result).to.be.eq(0)
})
it('same page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1100) // 1100
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(0),
bitIdxToTick(255)
)
expect(result).to.be.eq(2)
})
it('multiple pages', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1100) // 1100
await pool.mock.tickBitmap.withArgs(1).returns(0b1101) // 1101
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(0),
bitIdxToTick(255, 1)
)
expect(result).to.be.eq(5)
})
it('counts all ticks in a page except ending tick', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(ethers.constants.MaxUint256)
await pool.mock.tickBitmap.withArgs(1).returns(0x0)
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(0),
bitIdxToTick(255, 1)
)
expect(result).to.be.eq(255)
})
it('counts ticks to left of start and right of end on same page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1111000100001111)
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(8),
bitIdxToTick(255)
)
expect(result).to.be.eq(4)
})
it('counts ticks to left of start and right of end across on multiple pages', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1111000100001111)
await pool.mock.tickBitmap.withArgs(1).returns(0b1111000100001111)
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(8),
bitIdxToTick(8, 1)
)
expect(result).to.be.eq(9)
})
it('counts ticks when before and after are initialized on same page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b11111100)
const startingTickInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(2),
bitIdxToTick(255)
)
expect(startingTickInit).to.be.eq(5)
const endingTickInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(0),
bitIdxToTick(3)
)
expect(endingTickInit).to.be.eq(2)
const bothInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(2),
bitIdxToTick(5)
)
expect(bothInit).to.be.eq(3)
})
it('counts ticks when before and after are initialized on multiple page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b11111100)
await pool.mock.tickBitmap.withArgs(1).returns(0b11111100)
const startingTickInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(2),
bitIdxToTick(255)
)
expect(startingTickInit).to.be.eq(5)
const endingTickInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(0),
bitIdxToTick(3, 1)
)
expect(endingTickInit).to.be.eq(8)
const bothInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(2),
bitIdxToTick(5, 1)
)
expect(bothInit).to.be.eq(9)
})
it('counts ticks with lots of pages', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b11111100)
await pool.mock.tickBitmap.withArgs(1).returns(0b11111111)
await pool.mock.tickBitmap.withArgs(2).returns(0x0)
await pool.mock.tickBitmap.withArgs(3).returns(0x0)
await pool.mock.tickBitmap.withArgs(4).returns(0b11111100)
const bothInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(4),
bitIdxToTick(5, 4)
)
expect(bothInit).to.be.eq(15)
})
})
describe(`[Tick Spacing: ${TICK_SPACING}]: tick after is smaller`, async () => {
it('same page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1100)
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(255),
bitIdxToTick(0)
)
expect(result).to.be.eq(2)
})
it('multiple pages', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1100)
await pool.mock.tickBitmap.withArgs(-1).returns(0b1100)
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(255),
bitIdxToTick(0, -1)
)
expect(result).to.be.eq(4)
})
it('counts all ticks in a page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(ethers.constants.MaxUint256)
await pool.mock.tickBitmap.withArgs(-1).returns(0x0)
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(255),
bitIdxToTick(0, -1)
)
expect(result).to.be.eq(256)
})
it('counts ticks to right of start and left of end on same page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1111000100001111)
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(15),
bitIdxToTick(2)
)
expect(result).to.be.eq(6)
})
it('counts ticks to right of start and left of end on multiple pages', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b1111000100001111)
await pool.mock.tickBitmap.withArgs(-1).returns(0b1111000100001111)
const result = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(8),
bitIdxToTick(8, -1)
)
expect(result).to.be.eq(9)
})
it('counts ticks when before and after are initialized on same page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b11111100)
const startingTickInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(3),
bitIdxToTick(0)
)
expect(startingTickInit).to.be.eq(2)
const endingTickInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(255),
bitIdxToTick(2)
)
expect(endingTickInit).to.be.eq(5)
const bothInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(5),
bitIdxToTick(2)
)
expect(bothInit).to.be.eq(3)
})
it('counts ticks when before and after are initialized on multiple page', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b11111100)
await pool.mock.tickBitmap.withArgs(-1).returns(0b11111100)
const startingTickInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(2),
bitIdxToTick(3, -1)
)
expect(startingTickInit).to.be.eq(5)
const endingTickInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(5),
bitIdxToTick(255, -1)
)
expect(endingTickInit).to.be.eq(4)
const bothInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(2),
bitIdxToTick(5, -1)
)
expect(bothInit).to.be.eq(3)
})
it('counts ticks with lots of pages', async () => {
await pool.mock.tickBitmap.withArgs(0).returns(0b11111100)
await pool.mock.tickBitmap.withArgs(-1).returns(0xff)
await pool.mock.tickBitmap.withArgs(-2).returns(0x0)
await pool.mock.tickBitmap.withArgs(-3).returns(0x0)
await pool.mock.tickBitmap.withArgs(-4).returns(0b11111100)
const bothInit = await PoolTicksCounter.countInitializedTicksCrossed(
pool.address,
bitIdxToTick(3),
bitIdxToTick(6, -4)
)
expect(bothInit).to.be.eq(11)
})
})
})
})