@@ -25,20 +25,19 @@ import { createTick, feeTierToTickSpacing } from '../utils/tick'
25
25
export function handleInitialize ( event : Initialize ) : void {
26
26
// update pool sqrt price and tick
27
27
let pool = Pool . load ( event . address . toHexString ( ) )
28
- if ( ! pool ) return
28
+ if ( ! pool ) return
29
29
30
30
pool . sqrtPrice = event . params . sqrtPriceX96
31
31
pool . tick = BigInt . fromI32 ( event . params . tick )
32
32
pool . save ( )
33
-
33
+
34
34
// update token prices
35
35
let token0 = Token . load ( pool . token0 )
36
36
let token1 = Token . load ( pool . token1 )
37
37
38
-
39
38
// update ETH price now that prices could have changed
40
39
let bundle = Bundle . load ( '1' )
41
- if ( ! token0 || ! token1 || ! bundle ) return
40
+ if ( ! token0 || ! token1 || ! bundle ) return
42
41
43
42
bundle . ethPriceUSD = getEthPriceInUSD ( )
44
43
bundle . save ( )
@@ -53,7 +52,6 @@ export function handleInitialize(event: Initialize): void {
53
52
token1 . save ( )
54
53
}
55
54
56
-
57
55
function updateTickFeeVarsAndSave ( tick : Tick , event : ethereum . Event ) : void {
58
56
let poolAddress = event . address
59
57
// not all ticks are initialized so obtaining null is expected behavior
@@ -66,7 +64,6 @@ function updateTickFeeVarsAndSave(tick: Tick, event: ethereum.Event): void {
66
64
updateTickDayData ( tick , event )
67
65
}
68
66
69
-
70
67
export function handleMint ( event : MintEvent ) : void {
71
68
let bundle = Bundle . load ( '1' )
72
69
let poolAddress = event . address . toHexString ( )
@@ -76,7 +73,7 @@ export function handleMint(event: MintEvent): void {
76
73
77
74
let token0 = Token . load ( pool . token0 )
78
75
let token1 = Token . load ( pool . token1 )
79
- if ( ! token0 || ! token1 ) return
76
+ if ( ! token0 || ! token1 ) return
80
77
81
78
let amount0 = convertTokenToDecimal ( event . params . amount0 , token0 . decimals )
82
79
let amount1 = convertTokenToDecimal ( event . params . amount1 , token1 . decimals )
@@ -185,8 +182,8 @@ export function handleMint(event: MintEvent): void {
185
182
mint . save ( )
186
183
187
184
// Update inner tick vars and save the ticks
188
- updateTickFeeVarsAndSave ( lowerTick ! , event )
189
- updateTickFeeVarsAndSave ( upperTick ! , event )
185
+ updateTickFeeVarsAndSave ( lowerTick , event )
186
+ updateTickFeeVarsAndSave ( upperTick , event )
190
187
}
191
188
192
189
export function handleBurn ( event : BurnEvent ) : void {
@@ -199,7 +196,7 @@ export function handleBurn(event: BurnEvent): void {
199
196
let token0 = Token . load ( pool . token0 )
200
197
let token1 = Token . load ( pool . token1 )
201
198
202
- if ( ! token0 || ! token1 ) return
199
+ if ( ! token0 || ! token1 ) return
203
200
204
201
let amount0 = convertTokenToDecimal ( event . params . amount0 , token0 . decimals )
205
202
let amount1 = convertTokenToDecimal ( event . params . amount1 , token1 . decimals )
@@ -270,7 +267,7 @@ export function handleBurn(event: BurnEvent): void {
270
267
let upperTickId = poolAddress + '#' + BigInt . fromI32 ( event . params . tickUpper ) . toString ( )
271
268
let lowerTick = Tick . load ( lowerTickId )
272
269
let upperTick = Tick . load ( upperTickId )
273
- if ( ! lowerTick || ! upperTick ) return
270
+ if ( ! lowerTick || ! upperTick ) return
274
271
275
272
let amount = event . params . amount
276
273
lowerTick . liquidityGross = lowerTick . liquidityGross . minus ( amount )
@@ -285,8 +282,8 @@ export function handleBurn(event: BurnEvent): void {
285
282
updateTokenDayData ( token1 as Token , event )
286
283
updateTokenHourData ( token0 as Token , event )
287
284
updateTokenHourData ( token1 as Token , event )
288
- updateTickFeeVarsAndSave ( lowerTick ! , event )
289
- updateTickFeeVarsAndSave ( upperTick ! , event )
285
+ updateTickFeeVarsAndSave ( lowerTick , event )
286
+ updateTickFeeVarsAndSave ( upperTick , event )
290
287
291
288
token0 . save ( )
292
289
token1 . save ( )
@@ -304,16 +301,15 @@ function loadTickUpdateFeeVarsAndSave(tickId: i32, event: ethereum.Event): void
304
301
. concat ( tickId . toString ( ) )
305
302
)
306
303
if ( tick !== null ) {
307
- updateTickFeeVarsAndSave ( tick ! , event )
304
+ updateTickFeeVarsAndSave ( tick , event )
308
305
}
309
306
}
310
307
311
-
312
308
export function handleSwap ( event : SwapEvent ) : void {
313
309
let bundle = Bundle . load ( '1' )
314
310
let factory = Factory . load ( FACTORY_ADDRESS )
315
311
let pool = Pool . load ( event . address . toHexString ( ) )
316
- if ( ! factory || ! pool || ! bundle ) return
312
+ if ( ! factory || ! pool || ! bundle ) return
317
313
318
314
// hot fix for bad pricing
319
315
if ( pool . id == '0x9663f2ca0454accad3e094448ea6f77443880454' ) {
@@ -322,7 +318,7 @@ export function handleSwap(event: SwapEvent): void {
322
318
323
319
let token0 = Token . load ( pool . token0 )
324
320
let token1 = Token . load ( pool . token1 )
325
- if ( ! token0 || ! token1 ) return
321
+ if ( ! token0 || ! token1 ) return
326
322
327
323
let oldTick = pool . tick
328
324
@@ -509,42 +505,44 @@ export function handleSwap(event: SwapEvent): void {
509
505
// Update inner vars of current or crossed ticks
510
506
let newTick = pool . tick
511
507
let tickSpacing = feeTierToTickSpacing ( pool . feeTier )
512
- let modulo = newTick . mod ( tickSpacing )
513
- if ( modulo . equals ( ZERO_BI ) ) {
514
- // Current tick is initialized and needs to be updated
515
- loadTickUpdateFeeVarsAndSave ( newTick . toI32 ( ) , event )
516
- }
517
-
518
- if ( ! oldTick ) return
519
-
520
- let numIters = oldTick
521
- . minus ( newTick ! )
522
- . abs ( )
523
- . div ( tickSpacing )
524
-
525
- if ( numIters . gt ( BigInt . fromI32 ( 100 ) ) ) {
526
- // In case more than 100 ticks need to be updated ignore the update in
527
- // order to avoid timeouts. From testing this behavior occurs only upon
528
- // pool initialization. This should not be a big issue as the ticks get
529
- // updated later. For early users this error also disappears when calling
530
- // collect
531
- } else if ( newTick . gt ( oldTick ! ) ) {
532
- let firstInitialized = oldTick . plus ( tickSpacing . minus ( modulo ) )
533
- for ( let i = firstInitialized ; i . le ( newTick ! ) ; i = i . plus ( tickSpacing ) ) {
534
- loadTickUpdateFeeVarsAndSave ( i . toI32 ( ) , event )
508
+ if ( newTick ) {
509
+ let modulo = newTick . mod ( tickSpacing )
510
+ if ( modulo . equals ( ZERO_BI ) ) {
511
+ // Current tick is initialized and needs to be updated
512
+ loadTickUpdateFeeVarsAndSave ( newTick . toI32 ( ) , event )
535
513
}
536
- } else if ( newTick . lt ( oldTick ! ) ) {
537
- let firstInitialized = oldTick . minus ( modulo )
538
- for ( let i = firstInitialized ; i . ge ( newTick ! ) ; i = i . minus ( tickSpacing ) ) {
539
- loadTickUpdateFeeVarsAndSave ( i . toI32 ( ) , event )
514
+
515
+ if ( oldTick ) {
516
+ let numIters = oldTick
517
+ . minus ( newTick )
518
+ . abs ( )
519
+ . div ( tickSpacing )
520
+
521
+ if ( numIters . gt ( BigInt . fromI32 ( 100 ) ) ) {
522
+ // In case more than 100 ticks need to be updated ignore the update in
523
+ // order to avoid timeouts. From testing this behavior occurs only upon
524
+ // pool initialization. This should not be a big issue as the ticks get
525
+ // updated later. For early users this error also disappears when calling
526
+ // collect
527
+ } else if ( newTick . gt ( oldTick ) ) {
528
+ let firstInitialized = oldTick . plus ( tickSpacing . minus ( modulo ) )
529
+ for ( let i = firstInitialized ; i . le ( newTick ) ; i = i . plus ( tickSpacing ) ) {
530
+ loadTickUpdateFeeVarsAndSave ( i . toI32 ( ) , event )
531
+ }
532
+ } else if ( newTick . lt ( oldTick ) ) {
533
+ let firstInitialized = oldTick . minus ( modulo )
534
+ for ( let i = firstInitialized ; i . ge ( newTick ) ; i = i . minus ( tickSpacing ) ) {
535
+ loadTickUpdateFeeVarsAndSave ( i . toI32 ( ) , event )
536
+ }
537
+ }
540
538
}
541
539
}
542
540
}
543
541
544
542
export function handleFlash ( event : FlashEvent ) : void {
545
543
// update fee growth
546
544
let pool = Pool . load ( event . address . toHexString ( ) )
547
- if ( ! pool ) return
545
+ if ( ! pool ) return
548
546
549
547
let poolContract = PoolABI . bind ( event . address )
550
548
let feeGrowthGlobal0X128 = poolContract . feeGrowthGlobal0X128 ( )
0 commit comments