forked from Uniswap/v3-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtickList.test.ts
153 lines (132 loc) · 5.76 KB
/
tickList.test.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
import { Tick } from '../entities/tick'
import { TickList } from './tickList'
import { TickMath } from './tickMath'
describe('TickList', () => {
let highTick: Tick
let lowTick: Tick
let midTick: Tick
beforeEach(() => {
lowTick = new Tick({
index: TickMath.MIN_TICK + 1,
liquidityNet: 10,
liquidityGross: 10
})
midTick = new Tick({
index: 0,
liquidityNet: -5,
liquidityGross: 5
})
highTick = new Tick({
index: TickMath.MAX_TICK - 1,
liquidityNet: -5,
liquidityGross: 5
})
})
describe('#validate', () => {
it('errors for incomplete lists', () => {
expect(() => {
TickList.validateList([lowTick], 1)
}).toThrow('ZERO_NET')
})
it('errors for unsorted lists', () => {
expect(() => {
TickList.validateList([highTick, lowTick, midTick], 1)
}).toThrow('SORTED')
})
it('errors if ticks are not on multiples of tick spacing', () => {
expect(() => {
TickList.validateList([highTick, lowTick, midTick], 1337)
}).toThrow('TICK_SPACING')
})
})
it('#isBelowSmallest', () => {
const ticks = [lowTick, midTick, highTick]
expect(TickList.isBelowSmallest(ticks, TickMath.MIN_TICK)).toBe(true)
expect(TickList.isBelowSmallest(ticks, TickMath.MIN_TICK + 1)).toBe(false)
})
it('#isAtOrAboveLargest', () => {
const result = [lowTick, midTick, highTick]
expect(TickList.isAtOrAboveLargest(result, TickMath.MAX_TICK - 2)).toBe(false)
expect(TickList.isAtOrAboveLargest(result, TickMath.MAX_TICK - 1)).toBe(true)
})
describe('#nextInitializedTick', () => {
let ticks: Tick[]
beforeEach(() => {
ticks = [lowTick, midTick, highTick]
})
it('low - lte = true', () => {
expect(() => {
TickList.nextInitializedTick(ticks, TickMath.MIN_TICK, true)
}).toThrow('BELOW_SMALLEST')
expect(TickList.nextInitializedTick(ticks, TickMath.MIN_TICK + 1, true)).toEqual(lowTick)
expect(TickList.nextInitializedTick(ticks, TickMath.MIN_TICK + 2, true)).toEqual(lowTick)
})
it('low - lte = false', () => {
expect(TickList.nextInitializedTick(ticks, TickMath.MIN_TICK, false)).toEqual(lowTick)
expect(TickList.nextInitializedTick(ticks, TickMath.MIN_TICK + 1, false)).toEqual(midTick)
})
it('mid - lte = true', () => {
expect(TickList.nextInitializedTick(ticks, 0, true)).toEqual(midTick)
expect(TickList.nextInitializedTick(ticks, 1, true)).toEqual(midTick)
})
it('mid - lte = false', () => {
expect(TickList.nextInitializedTick(ticks, -1, false)).toEqual(midTick)
expect(TickList.nextInitializedTick(ticks, 0 + 1, false)).toEqual(highTick)
})
it('high - lte = true', () => {
expect(TickList.nextInitializedTick(ticks, TickMath.MAX_TICK - 1, true)).toEqual(highTick)
expect(TickList.nextInitializedTick(ticks, TickMath.MAX_TICK, true)).toEqual(highTick)
})
it('high - lte = false', () => {
expect(() => {
TickList.nextInitializedTick(ticks, TickMath.MAX_TICK - 1, false)
}).toThrow('AT_OR_ABOVE_LARGEST')
expect(TickList.nextInitializedTick(ticks, TickMath.MAX_TICK - 2, false)).toEqual(highTick)
expect(TickList.nextInitializedTick(ticks, TickMath.MAX_TICK - 3, false)).toEqual(highTick)
})
})
describe('#nextInitializedTickWithinOneWord', () => {
let ticks: Tick[]
beforeEach(() => {
ticks = [lowTick, midTick, highTick]
})
it('words around 0, lte = true', () => {
expect(TickList.nextInitializedTickWithinOneWord(ticks, -257, true, 1)).toEqual([-512, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, -256, true, 1)).toEqual([-256, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, -1, true, 1)).toEqual([-256, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 0, true, 1)).toEqual([0, true])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 1, true, 1)).toEqual([0, true])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 255, true, 1)).toEqual([0, true])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 256, true, 1)).toEqual([256, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 257, true, 1)).toEqual([256, false])
})
it('words around 0, lte = false', () => {
expect(TickList.nextInitializedTickWithinOneWord(ticks, -258, false, 1)).toEqual([-257, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, -257, false, 1)).toEqual([-1, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, -256, false, 1)).toEqual([-1, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, -2, false, 1)).toEqual([-1, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, -1, false, 1)).toEqual([0, true])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 0, false, 1)).toEqual([255, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 1, false, 1)).toEqual([255, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 254, false, 1)).toEqual([255, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 255, false, 1)).toEqual([511, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 256, false, 1)).toEqual([511, false])
})
it('performs correctly with tickSpacing > 1', () => {
ticks = [
new Tick({
index: 0,
liquidityNet: 0,
liquidityGross: 0
}),
new Tick({
index: 511,
liquidityNet: 0,
liquidityGross: 0
})
]
expect(TickList.nextInitializedTickWithinOneWord(ticks, 0, false, 1)).toEqual([255, false])
expect(TickList.nextInitializedTickWithinOneWord(ticks, 0, false, 2)).toEqual([510, false])
})
})
})