fix: math error in tick updates during swaps #60
+5
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello,
It appears there's a bug in the tick section of handleSwap that result in ticks (and tickDayDatas) not getting updated when they should, causing a mismatch between subgraph and blockchain data, see [1].
Using small numbers to describe the issue, let's assume a pool's state in which the current tick is 19 and a swap occurs that pushes the current tick to 21, and let's also assume a tick spacing of 10 for this pool.
handleSwap in that example would compute
modulo = newTick.mod(tickSpacing)
== 1 and firstInitialized would thus be defined asfirstInitialized = oldTick.plus(tickSpacing.minus(modulo))
== 28. The correct value for firstInitialized should of course be 20 instead.I'm syncing a subgraph with the changes here, but it still has a long way to go.
[1] I've noticed bogus subgraph data while rendering charts of daily fees for positions, but I believe issue 44 is a good example. I wrote a test to compare subgraph data with blockchain data at the specified block, uploaded here. Use brownie to run it:
brownie run tickstest.py --network mainnet
.Results: