-
Notifications
You must be signed in to change notification settings - Fork 607
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
Add state for Mint: tick and Position #3136
Changes from 5 commits
34f850d
36bfd2e
c8ec090
816864f
2c76048
967d5d5
1bdb9e1
016f275
83d5783
d8e39bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,36 +2,53 @@ package concentrated_liquidity | |
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/gogo/protobuf/proto" | ||
|
||
"github.com/osmosis-labs/osmosis/v12/osmoutils" | ||
types "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity/types" | ||
) | ||
|
||
// nolint: unused | ||
func (k Keeper) updatePositionWithLiquidity(ctx sdk.Context, | ||
func (k Keeper) initOrUpdatePosition(ctx sdk.Context, | ||
poolId uint64, | ||
owner sdk.AccAddress, | ||
lowerTick, upperTick int64, | ||
liquidityDelta sdk.Int, | ||
) { | ||
position := k.getPosition(ctx, poolId, owner, lowerTick, upperTick) | ||
) (err error) { | ||
position, err := k.GetPosition(ctx, poolId, owner, lowerTick, upperTick) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
liquidityBefore := position.Liquidity | ||
liquidityAfter := liquidityBefore.Add(liquidityDelta) | ||
var liquidityAfter sdk.Int | ||
if liquidityDelta.IsNegative() { | ||
liquidityAfter = liquidityBefore.Sub(liquidityDelta) | ||
} else { | ||
liquidityAfter = liquidityBefore.Add(liquidityDelta) | ||
} | ||
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. to be clear, this means that no matter what, we are adding liquidity to liquidityAfter, even if we have a negative delta right? 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. So if we have a negative delta, we don't want to subtract it from liquidityBefore, so we do another negative to add the absolute value of liquidityDelta |
||
|
||
position.Liquidity = liquidityAfter | ||
|
||
k.setPosition(ctx, poolId, owner, lowerTick, upperTick, position) | ||
return nil | ||
} | ||
|
||
// nolint: unused | ||
func (k Keeper) getPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, lowerTick, upperTick int64) Position { | ||
func (k Keeper) GetPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, lowerTick, upperTick int64) (position Position, err 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. is there a reason we had to make this public? 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. for testing purposes / I don't think we have security vulnerability exposing getter method |
||
store := ctx.KVStore(k.storeKey) | ||
|
||
var position Position | ||
positionStruct := Position{} | ||
key := types.KeyPosition(poolId, owner, lowerTick, upperTick) | ||
osmoutils.MustGet(store, key, &position) | ||
|
||
return position | ||
bz := store.Get(key) | ||
if bz == nil { | ||
return Position{Liquidity: sdk.ZeroInt()}, nil | ||
} | ||
mattverse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
err = proto.Unmarshal(bz, &positionStruct) | ||
if err != nil { | ||
return positionStruct, err | ||
} | ||
|
||
return positionStruct, nil | ||
} | ||
|
||
// nolint: unused | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
sdk "github.com/cosmos/cosmos-sdk/types" | ||
db "github.com/tendermint/tm-db" | ||
|
||
"github.com/gogo/protobuf/proto" | ||
|
||
"github.com/osmosis-labs/osmosis/v12/osmomath" | ||
"github.com/osmosis-labs/osmosis/v12/osmoutils" | ||
types "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity/types" | ||
|
@@ -26,14 +28,42 @@ func (k Keeper) tickToSqrtPrice(tickIndex sdk.Int) (sdk.Dec, error) { | |
// return sdk.Int{} | ||
// } | ||
|
||
func (k Keeper) UpdateTickWithNewLiquidity(ctx sdk.Context, poolId uint64, tickIndex int64, liquidityDelta sdk.Int) { | ||
tickInfo := k.getTickInfo(ctx, poolId, tickIndex) | ||
func (k Keeper) initOrUpdateTick(ctx sdk.Context, poolId uint64, tickIndex int64, liquidityIn sdk.Int, upper bool) (err error) { | ||
tickInfo, err := k.GetTickInfo(ctx, poolId, tickIndex) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// calculate liquidityGross, which does not care about whether liquidityIn is positive or negative | ||
liquidityBefore := tickInfo.LiquidityGross | ||
var liquidityAfter sdk.Int | ||
if liquidityIn.IsNegative() { | ||
liquidityAfter = liquidityBefore.Sub(liquidityIn) | ||
} else { | ||
liquidityAfter = liquidityBefore.Add(liquidityIn) | ||
} | ||
tickInfo.LiquidityGross = liquidityAfter | ||
|
||
liquidityBefore := tickInfo.Liquidity | ||
liquidityAfter := liquidityBefore.Add(liquidityDelta) | ||
tickInfo.Liquidity = liquidityAfter | ||
// calculate liquidityNet, which we take into account and track depending on whether liquidityIn is positive or negative | ||
if upper { | ||
tickInfo.LiquidityNet = tickInfo.LiquidityNet.Sub(liquidityIn) | ||
} else { | ||
tickInfo.LiquidityNet = tickInfo.LiquidityNet.Add(liquidityIn) | ||
} | ||
|
||
k.setTickInfo(ctx, poolId, tickIndex, tickInfo) | ||
|
||
return nil | ||
} | ||
|
||
// nolint: unused | ||
func (k Keeper) crossTick(ctx sdk.Context, poolId uint64, tickIndex int64) (liquidityDelta sdk.Int, err error) { | ||
tickInfo, err := k.GetTickInfo(ctx, poolId, tickIndex) | ||
if err != nil { | ||
return sdk.Int{}, err | ||
} | ||
|
||
return tickInfo.LiquidityNet, nil | ||
} | ||
|
||
// NextInitializedTick returns the next initialized tick index based on the | ||
|
@@ -87,12 +117,23 @@ func (k Keeper) NextInitializedTick(ctx sdk.Context, poolId uint64, tickIndex in | |
return 0, false | ||
} | ||
|
||
func (k Keeper) getTickInfo(ctx sdk.Context, poolId uint64, tickIndex int64) TickInfo { | ||
// getTickInfo gets tickInfo given poolId and tickIndex. Returns a boolean field that returns true if value is found for given key. | ||
func (k Keeper) GetTickInfo(ctx sdk.Context, poolId uint64, tickIndex int64) (tickInfo TickInfo, err 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. is there a reason we also made this public? 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. for testing purposes / I don't think we have security vulnerability exposing getter method |
||
store := ctx.KVStore(k.storeKey) | ||
tickInfo := TickInfo{} | ||
tickStruct := TickInfo{} | ||
key := types.KeyTick(poolId, tickIndex) | ||
osmoutils.MustGet(store, key, &tickInfo) | ||
return tickInfo | ||
|
||
bz := store.Get(key) | ||
mattverse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if bz == nil { | ||
return TickInfo{LiquidityGross: sdk.ZeroInt(), LiquidityNet: sdk.ZeroInt()}, err | ||
} | ||
|
||
err = proto.Unmarshal(bz, &tickStruct) | ||
if err != nil { | ||
return tickStruct, err | ||
} | ||
|
||
return tickStruct, nil | ||
} | ||
|
||
func (k Keeper) setTickInfo(ctx sdk.Context, poolId uint64, tickIndex int64, tickInfo TickInfo) { | ||
|
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.
can you explain the math behind getting 1517?
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.
It's the liquidity we put in initially as the testing params
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.
Should we add a comment for that?
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.
added