forked from Uniswap/v3-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtickLibrary.ts
61 lines (53 loc) · 1.92 KB
/
tickLibrary.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
import JSBI from 'jsbi'
import { ZERO } from '../internalConstants'
interface FeeGrowthOutside {
feeGrowthOutside0X128: JSBI
feeGrowthOutside1X128: JSBI
}
const Q256 = JSBI.exponentiate(JSBI.BigInt(2), JSBI.BigInt(256))
export function subIn256(x: JSBI, y: JSBI): JSBI {
const difference = JSBI.subtract(x, y)
if (JSBI.lessThan(difference, ZERO)) {
return JSBI.add(Q256, difference)
} else {
return difference
}
}
export abstract class TickLibrary {
/**
* Cannot be constructed.
*/
private constructor() {}
public static getFeeGrowthInside(
feeGrowthOutsideLower: FeeGrowthOutside,
feeGrowthOutsideUpper: FeeGrowthOutside,
tickLower: number,
tickUpper: number,
tickCurrent: number,
feeGrowthGlobal0X128: JSBI,
feeGrowthGlobal1X128: JSBI
) {
let feeGrowthBelow0X128: JSBI
let feeGrowthBelow1X128: JSBI
if (tickCurrent >= tickLower) {
feeGrowthBelow0X128 = feeGrowthOutsideLower.feeGrowthOutside0X128
feeGrowthBelow1X128 = feeGrowthOutsideLower.feeGrowthOutside1X128
} else {
feeGrowthBelow0X128 = subIn256(feeGrowthGlobal0X128, feeGrowthOutsideLower.feeGrowthOutside0X128)
feeGrowthBelow1X128 = subIn256(feeGrowthGlobal1X128, feeGrowthOutsideLower.feeGrowthOutside1X128)
}
let feeGrowthAbove0X128: JSBI
let feeGrowthAbove1X128: JSBI
if (tickCurrent < tickUpper) {
feeGrowthAbove0X128 = feeGrowthOutsideUpper.feeGrowthOutside0X128
feeGrowthAbove1X128 = feeGrowthOutsideUpper.feeGrowthOutside1X128
} else {
feeGrowthAbove0X128 = subIn256(feeGrowthGlobal0X128, feeGrowthOutsideUpper.feeGrowthOutside0X128)
feeGrowthAbove1X128 = subIn256(feeGrowthGlobal1X128, feeGrowthOutsideUpper.feeGrowthOutside1X128)
}
return [
subIn256(subIn256(feeGrowthGlobal0X128, feeGrowthBelow0X128), feeGrowthAbove0X128),
subIn256(subIn256(feeGrowthGlobal1X128, feeGrowthBelow1X128), feeGrowthAbove1X128)
]
}
}