-
Notifications
You must be signed in to change notification settings - Fork 592
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
[feat: CL hooks]: Add CL hooks into core CL logic and test hook-specific behavior #6859
Changes from all commits
e270013
4b9d72d
0d4140e
a0a4bd6
accfed4
29f79a3
8e5471c
3eca42f
9d3cce8
a2b10d5
0c3c7f0
61a63a6
71bba74
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 |
---|---|---|
|
@@ -2,7 +2,6 @@ package concentrated_liquidity | |
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
@@ -12,16 +11,6 @@ import ( | |
types "github.com/osmosis-labs/osmosis/v20/x/concentrated-liquidity/types" | ||
) | ||
|
||
// Helper function to generate before action prefix | ||
func beforeActionPrefix(action string) string { | ||
return fmt.Sprintf("before%s", action) | ||
} | ||
|
||
// Helper function to generate after action prefix | ||
func afterActionPrefix(action string) string { | ||
return fmt.Sprintf("after%s", action) | ||
} | ||
|
||
// --- Pool Hooks --- | ||
|
||
// BeforeCreatePosition is a hook that is called before a position is created. | ||
|
@@ -32,7 +21,7 @@ func (k Keeper) BeforeCreatePosition(ctx sdk.Context, poolId uint64, owner sdk.A | |
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, beforeActionPrefix(types.CreatePositionPrefix)) | ||
return k.callPoolActionListener(ctx, msgBz, poolId, types.BeforeActionPrefix(types.CreatePositionPrefix)) | ||
} | ||
|
||
// AfterCreatePosition is a hook that is called after a position is created. | ||
|
@@ -43,29 +32,7 @@ func (k Keeper) AfterCreatePosition(ctx sdk.Context, poolId uint64, owner sdk.Ac | |
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, afterActionPrefix(types.CreatePositionPrefix)) | ||
} | ||
|
||
// BeforeAddToPosition is a hook that is called before liquidity is added to a position. | ||
func (k Keeper) BeforeAddToPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, positionId uint64, amount0Added osmomath.Int, amount1Added osmomath.Int, amount0Min osmomath.Int, amount1Min osmomath.Int) error { | ||
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.
Opted to remove and keep hooks on the most primitive actions. |
||
// Build and marshal the message to be passed to the contract | ||
msg := types.BeforeAddToPositionMsg{PoolId: poolId, Owner: owner, PositionId: positionId, Amount0Added: amount0Added, Amount1Added: amount1Added, Amount0Min: amount0Min, Amount1Min: amount1Min} | ||
msgBz, err := json.Marshal(types.BeforeAddToPositionSudoMsg{BeforeAddToPosition: msg}) | ||
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, beforeActionPrefix(types.AddToPositionPrefix)) | ||
} | ||
|
||
// AfterAddToPosition is a hook that is called after liquidity is added to a position. | ||
func (k Keeper) AfterAddToPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, positionId uint64, amount0Added osmomath.Int, amount1Added osmomath.Int, amount0Min osmomath.Int, amount1Min osmomath.Int) error { | ||
// Build and marshal the message to be passed to the contract | ||
msg := types.AfterAddToPositionMsg{PoolId: poolId, Owner: owner, PositionId: positionId, Amount0Added: amount0Added, Amount1Added: amount1Added, Amount0Min: amount0Min, Amount1Min: amount1Min} | ||
msgBz, err := json.Marshal(types.AfterAddToPositionSudoMsg{AfterAddToPosition: msg}) | ||
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, afterActionPrefix(types.AddToPositionPrefix)) | ||
return k.callPoolActionListener(ctx, msgBz, poolId, types.AfterActionPrefix(types.CreatePositionPrefix)) | ||
} | ||
|
||
// BeforeWithdrawPosition is a hook that is called before liquidity is withdrawn from a position. | ||
|
@@ -76,7 +43,7 @@ func (k Keeper) BeforeWithdrawPosition(ctx sdk.Context, poolId uint64, owner sdk | |
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, beforeActionPrefix(types.WithdrawPositionPrefix)) | ||
return k.callPoolActionListener(ctx, msgBz, poolId, types.BeforeActionPrefix(types.WithdrawPositionPrefix)) | ||
} | ||
|
||
// AfterWithdrawPosition is a hook that is called after liquidity is withdrawn from a position. | ||
|
@@ -87,7 +54,7 @@ func (k Keeper) AfterWithdrawPosition(ctx sdk.Context, poolId uint64, owner sdk. | |
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, afterActionPrefix(types.WithdrawPositionPrefix)) | ||
return k.callPoolActionListener(ctx, msgBz, poolId, types.AfterActionPrefix(types.WithdrawPositionPrefix)) | ||
} | ||
|
||
// BeforeSwapExactAmountIn is a hook that is called before a swap is executed (exact amount in). | ||
|
@@ -98,7 +65,7 @@ func (k Keeper) BeforeSwapExactAmountIn(ctx sdk.Context, poolId uint64, sender s | |
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, beforeActionPrefix(types.SwapExactAmountInPrefix)) | ||
return k.callPoolActionListener(ctx, msgBz, poolId, types.BeforeActionPrefix(types.SwapExactAmountInPrefix)) | ||
} | ||
|
||
// AfterSwapExactAmountIn is a hook that is called after a swap is executed (exact amount in). | ||
|
@@ -109,7 +76,7 @@ func (k Keeper) AfterSwapExactAmountIn(ctx sdk.Context, poolId uint64, sender sd | |
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, afterActionPrefix(types.SwapExactAmountInPrefix)) | ||
return k.callPoolActionListener(ctx, msgBz, poolId, types.AfterActionPrefix(types.SwapExactAmountInPrefix)) | ||
} | ||
|
||
// BeforeSwapExactAmountOut is a hook that is called before a swap is executed (exact amount out). | ||
|
@@ -120,7 +87,7 @@ func (k Keeper) BeforeSwapExactAmountOut(ctx sdk.Context, poolId uint64, sender | |
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, beforeActionPrefix(types.SwapExactAmountOutPrefix)) | ||
return k.callPoolActionListener(ctx, msgBz, poolId, types.BeforeActionPrefix(types.SwapExactAmountOutPrefix)) | ||
} | ||
|
||
// AfterSwapExactAmountOut is a hook that is called after a swap is executed (exact amount out). | ||
|
@@ -131,7 +98,7 @@ func (k Keeper) AfterSwapExactAmountOut(ctx sdk.Context, poolId uint64, sender s | |
if err != nil { | ||
return err | ||
} | ||
return k.callPoolActionListener(ctx, msgBz, poolId, afterActionPrefix(types.SwapExactAmountOutPrefix)) | ||
return k.callPoolActionListener(ctx, msgBz, poolId, types.AfterActionPrefix(types.SwapExactAmountOutPrefix)) | ||
} | ||
|
||
// callPoolActionListener processes and dispatches the passed in message to the contract corresponding to the hook | ||
|
@@ -210,6 +177,11 @@ func (k Keeper) getPoolHookContract(ctx sdk.Context, poolId uint64, actionPrefix | |
func (k Keeper) setPoolHookContract(ctx sdk.Context, poolID uint64, actionPrefix string, cosmwasmAddress string) error { | ||
store := k.getPoolHookPrefixStore(ctx, poolID) | ||
|
||
validActionPrefixes := types.GetAllActionPrefixes() | ||
if !osmoutils.Contains(validActionPrefixes, actionPrefix) { | ||
return types.InvalidActionPrefixError{ActionPrefix: actionPrefix, ValidActions: validActionPrefixes} | ||
} | ||
|
||
// If cosmwasm address is nil, treat this as a delete operation for the stored address. | ||
if cosmwasmAddress == "" { | ||
deletePoolHookContract(store, actionPrefix) | ||
|
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.
Note: this was moved below the
hasPositions
check to ensure the before hook was triggered after validation logic but before any state changes (getNextPositionIdAndIncrement
mutates state)