-
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 3 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) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
175
to
-180
Member
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. The function type of
Author
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. As discussed, rolled back all store-related changes, keeping only the block timestamp caching. |
||||||||||||||||||||||||||||
| if swapStartHook := i.store.GetSwapStartHook(); swapStartHook != nil { | ||||||||||||||||||||||||||||
| swapStartHook(cross, pool.PoolPath(), blockTimestamp) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| defer func() { | ||||||||||||||||||||||||||||
|
|
@@ -186,14 +182,10 @@ func (i *poolV1) Swap( | |||||||||||||||||||||||||||
| slot0End.SetUnlocked(true) | ||||||||||||||||||||||||||||
| 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 swapEndHook := i.store.GetSwapEndHook(); swapEndHook != nil { | ||||||||||||||||||||||||||||
| err := swapEndHook(cross, pool.PoolPath()) | ||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||
| panic(err) | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||
|
|
@@ -204,7 +196,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 +215,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 +310,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 +721,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.
Errors occurring in the store are ignored.
This differs from how other query functions in the store are handled.
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.
Instead of changing the store's error handling, we can keep
GetSwapStartHook/GetSwapEndHookpanicking on error and simplify the call site by trustingHas*as the guard:Since
Has*already confirms the key exists, the nil check afterGet*is unnecessary. This removes the redundant double-check without touching the store's error handling convention.How about reverting the
store.gnochange and updating the call site this way instead?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.
14d95ad