-
Notifications
You must be signed in to change notification settings - Fork 12
refactor(pool): optimize swap hook checks and cache block timestamp #1267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
41328cf
3b7ba04
bfeac62
14d95ad
294d158
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -169,15 +169,11 @@ func (i *poolV1) Swap( | |||||||||||||||||||||||||||
| slot0Start.SetUnlocked(false) | ||||||||||||||||||||||||||||
| pool.SetSlot0(slot0Start) | ||||||||||||||||||||||||||||
| startTick := pool.Slot0Tick() | ||||||||||||||||||||||||||||
| blockTimestamp := time.Now().Unix() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Call swap start hook if set | ||||||||||||||||||||||||||||
| if i.store.HasSwapStartHook() { | ||||||||||||||||||||||||||||
| swapStartHook := i.store.GetSwapStartHook() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if swapStartHook != nil { | ||||||||||||||||||||||||||||
| currentTime := time.Now().Unix() | ||||||||||||||||||||||||||||
| swapStartHook(cross, pool.PoolPath(), currentTime) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| i.store.GetSwapStartHook()(cross, pool.PoolPath(), blockTimestamp) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| defer func() { | ||||||||||||||||||||||||||||
|
|
@@ -187,13 +183,8 @@ func (i *poolV1) Swap( | |||||||||||||||||||||||||||
| pool.SetSlot0(slot0End) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if i.store.HasSwapEndHook() { | ||||||||||||||||||||||||||||
| swapEndHook := i.store.GetSwapEndHook() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if swapEndHook != nil { | ||||||||||||||||||||||||||||
| err := swapEndHook(cross, pool.PoolPath()) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| panic(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| if err := i.store.GetSwapEndHook()(cross, pool.PoolPath()); err != nil { | ||||||||||||||||||||||||||||
| panic(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||
|
|
@@ -204,7 +195,7 @@ func (i *poolV1) Swap( | |||||||||||||||||||||||||||
| amounts := i256.MustFromDecimal(amountSpecified) | ||||||||||||||||||||||||||||
| feeGrowthGlobalX128 := getFeeGrowthGlobal(pool, zeroForOne) | ||||||||||||||||||||||||||||
| feeProtocol := getFeeProtocol(slot0Start, zeroForOne) | ||||||||||||||||||||||||||||
| cache := newSwapCache(feeProtocol, pool.Liquidity().Clone()) | ||||||||||||||||||||||||||||
| cache := newSwapCache(feeProtocol, pool.Liquidity().Clone(), blockTimestamp) | ||||||||||||||||||||||||||||
| state := newSwapState(amounts, feeGrowthGlobalX128, cache.liquidityStart.Clone(), slot0Start) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| comp := SwapComputation{ | ||||||||||||||||||||||||||||
|
|
@@ -223,9 +214,7 @@ func (i *poolV1) Swap( | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Update oracle BEFORE applying swap result (using pre-swap state) | ||||||||||||||||||||||||||||
| if result.NewTick != pool.Slot0Tick() { | ||||||||||||||||||||||||||||
| currentTime := time.Now().Unix() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| err := writeObservationByPool(pool, currentTime, pool.Slot0Tick(), pool.Liquidity()) | ||||||||||||||||||||||||||||
| err := writeObservationByPool(pool, cache.blockTimestamp, pool.Slot0Tick(), pool.Liquidity()) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| panic(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
@@ -320,7 +309,7 @@ func (i *poolV1) DrySwap( | |||||||||||||||||||||||||||
| amounts := i256.MustFromDecimal(amountSpecified) | ||||||||||||||||||||||||||||
| feeGrowthGlobalX128 := getFeeGrowthGlobal(poolSnapshot, zeroForOne) | ||||||||||||||||||||||||||||
| feeProtocol := getFeeProtocol(slot0Start, zeroForOne) | ||||||||||||||||||||||||||||
| cache := newSwapCache(feeProtocol, poolSnapshot.Liquidity().Clone()) | ||||||||||||||||||||||||||||
| cache := newSwapCache(feeProtocol, poolSnapshot.Liquidity().Clone(), time.Now().Unix()) | ||||||||||||||||||||||||||||
| state := newSwapState(amounts, feeGrowthGlobalX128, cache.liquidityStart, slot0Start) | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| comp := SwapComputation{ | ||||||||||||||||||||||||||||
|
|
@@ -731,8 +720,7 @@ func (i *poolV1) tickTransition(step StepComputations, zeroForOne bool, state Sw | |||||||||||||||||||||||||||
| if i.store.HasTickCrossHook() { | ||||||||||||||||||||||||||||
| tickCrossHook := i.store.GetTickCrossHook() | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| currentTime := time.Now().Unix() | ||||||||||||||||||||||||||||
| tickCrossHook(cross, pool.PoolPath(), step.tickNext, zeroForOne, currentTime) | ||||||||||||||||||||||||||||
| tickCrossHook(cross, pool.PoolPath(), step.tickNext, zeroForOne, cache.blockTimestamp) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
729
to
733
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a
🛡️ Proposed fix if i.store.HasTickCrossHook() {
tickCrossHook := i.store.GetTickCrossHook()
-
- tickCrossHook(cross, pool.PoolPath(), step.tickNext, zeroForOne, cache.blockTimestamp)
+ if tickCrossHook != nil {
+ tickCrossHook(cross, pool.PoolPath(), step.tickNext, zeroForOne, cache.blockTimestamp)
+ }
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The function type of
hookcan benil.When actually using the function, it would be best to add a
nilcheck for defensive programming.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed, rolled back all store-related changes, keeping only the block timestamp caching.
See revert commit: 294d158