Skip to content

Commit

Permalink
feat(CL): CreatePosition and WithdrawPosition messages (#3139)
Browse files Browse the repository at this point in the history
* feat(CL): CreatePosition and WithdrawPosition messages

* lint
  • Loading branch information
p0mvn authored Oct 25, 2022
1 parent 52f1897 commit 527f823
Show file tree
Hide file tree
Showing 7 changed files with 1,727 additions and 25 deletions.
78 changes: 78 additions & 0 deletions proto/osmosis/concentrated-liquidity/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
syntax = "proto3";
package osmosis.concentratedliquidity.v1beta1;

import "gogoproto/gogo.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity/types";

service Msg {
rpc CreatePosition(MsgCreatePosition) returns (MsgCreatePositionResponse);
rpc WithdrawPosition(MsgWithdrawPosition)
returns (MsgWithdrawPositionResponse);
}

// ===================== MsgCreatePosition
message MsgCreatePosition {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string sender = 2 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
int64 lower_tick = 3 [ (gogoproto.moretags) = "yaml:\"lower_tick\"" ];
int64 upper_tick = 4 [ (gogoproto.moretags) = "yaml:\"upper_tick\"" ];
cosmos.base.v1beta1.Coin token_desired0 = 5 [
(gogoproto.moretags) = "yaml:\"token_desired0\"",
(gogoproto.nullable) = false
];
cosmos.base.v1beta1.Coin token_desired1 = 6 [
(gogoproto.moretags) = "yaml:\"token_desired1\"",
(gogoproto.nullable) = false
];
string token_min_amount0 = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_min_amount0\"",
(gogoproto.nullable) = false
];
string token_min_amount1 = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"token_min_amount1\"",
(gogoproto.nullable) = false
];
}

message MsgCreatePositionResponse {
string amount0 = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"amount0\"",
(gogoproto.nullable) = false
];
string amount1 = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"amount1\"",
(gogoproto.nullable) = false
];
}

// ===================== MsgWithdrawPosition
message MsgWithdrawPosition {
uint64 pool_id = 1 [ (gogoproto.moretags) = "yaml:\"pool_id\"" ];
string sender = 2 [ (gogoproto.moretags) = "yaml:\"sender\"" ];
int64 lower_tick = 3 [ (gogoproto.moretags) = "yaml:\"lower_tick\"" ];
int64 upper_tick = 4 [ (gogoproto.moretags) = "yaml:\"upper_tick\"" ];
string liquidity_amount = 5 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"liquidity_amount\"",
(gogoproto.nullable) = false
];
}

message MsgWithdrawPositionResponse {
string amount0 = 1 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"amount0\"",
(gogoproto.nullable) = false
];
string amount1 = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.moretags) = "yaml:\"amount1\"",
(gogoproto.nullable) = false
];
}
6 changes: 6 additions & 0 deletions x/concentrated-liquidity/export_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package concentrated_liquidity

import (
sdk "github.com/cosmos/cosmos-sdk/types"

cltypes "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity/types"
)

// OrderInitialPoolDenoms sets the pool denoms of a cl pool
func OrderInitialPoolDenoms(denom0, denom1 string) (string, string, error) {
return cltypes.OrderInitialPoolDenoms(denom0, denom1)
}

func (k Keeper) CreatePosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, amount0Desired, amount1Desired, amount0Min, amount1Min sdk.Int, lowerTick, upperTick int64) (amtDenom0, amtDenom1 sdk.Int, err error) {
return k.createPosition(ctx, poolId, owner, amount0Desired, amount1Desired, amount0Min, amount1Min, lowerTick, upperTick)
}
25 changes: 6 additions & 19 deletions x/concentrated-liquidity/lp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
types "github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity/types"
)

func (k Keeper) Mint(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, liquidityIn sdk.Dec, lowerTick, upperTick int64) (amtDenom0, amtDenom1 sdk.Int, err error) {
func (k Keeper) createPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, amount0Desired, amount1Desired, amount0Min, amount1Min sdk.Int, lowerTick, upperTick int64) (amtDenom0, amtDenom1 sdk.Int, err error) {
// TODO: calculate from amounts given
liquidityIn := sdk.MustNewDecFromStr("1517.882323")

// ensure types.MinTick <= lowerTick < types.MaxTick
// TODO (bez): Add unit tests.
if lowerTick < types.MinTick || lowerTick >= types.MaxTick {
Expand Down Expand Up @@ -58,22 +61,6 @@ func (k Keeper) Mint(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, liqui
return amtDenom0, amtDenom1, nil
}

func (k Keeper) JoinPoolNoSwap(ctx sdk.Context, tokensIn sdk.Coins, swapFee sdk.Dec) (numShares sdk.Int, err error) {
return sdk.Int{}, nil
}

func (k Keeper) CalcJoinPoolShares(ctx sdk.Context, tokensIn sdk.Coins, swapFee sdk.Dec) (numShares sdk.Int, newLiquidity sdk.Coins, err error) {
return sdk.Int{}, sdk.Coins{}, nil
}

func (k Keeper) CalcJoinPoolNoSwapShares(ctx sdk.Context, tokensIn sdk.Coins, swapFee sdk.Dec) (numShares sdk.Int, newLiquidity sdk.Coins, err error) {
return sdk.Int{}, sdk.Coins{}, nil
}

func (k Keeper) ExitPool(ctx sdk.Context, numShares sdk.Int, exitFee sdk.Dec) (exitedCoins sdk.Coins, err error) {
return sdk.Coins{}, nil
}

func (k Keeper) CalcExitPoolCoinsFromShares(ctx sdk.Context, numShares sdk.Int, exitFee sdk.Dec) (exitedCoins sdk.Coins, err error) {
return sdk.Coins{}, nil
func (k Keeper) withdrawPosition(ctx sdk.Context, poolId uint64, owner sdk.AccAddress, lowerTick, upperTick int64, liquidityAmount sdk.Int) (amtDenom0, amtDenom1 sdk.Int, err error) {
panic("not implemented")
}
14 changes: 8 additions & 6 deletions x/concentrated-liquidity/lp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (s *KeeperTestSuite) TestMint() {
func (s *KeeperTestSuite) TestCreatePosition() {
// testing params
// current tick: 85176
// lower tick: 84222
Expand All @@ -17,22 +17,24 @@ func (s *KeeperTestSuite) TestMint() {
currentTick := sdk.NewInt(85176)
lowerTick := int64(84222)
upperTick := int64(86129)
liquidity, err := sdk.NewDecFromStr("1517.882323")
s.Require().NoError(err)

// currentSqrtP, ok := sdk.NewIntFromString("70710678")
currentSqrtP, err := sdk.NewDecFromStr("70.710678")
s.Require().NoError(err)
denom0 := "eth"
denom1 := "usdc"

amount0Desired := sdk.OneInt()
amount1Desired := sdk.NewInt(5000)

s.SetupTest()

s.App.ConcentratedLiquidityKeeper.CreateNewConcentratedLiquidityPool(s.Ctx, poolId, denom0, denom1, currentSqrtP, currentTick)

asset0, asset1, err := s.App.ConcentratedLiquidityKeeper.Mint(s.Ctx, poolId, s.TestAccs[0], liquidity, lowerTick, upperTick)
asset0, asset1, err := s.App.ConcentratedLiquidityKeeper.CreatePosition(s.Ctx, poolId, s.TestAccs[0], amount0Desired, amount1Desired, sdk.OneInt(), sdk.OneInt(), lowerTick, upperTick)
s.Require().NoError(err)
s.Require().Equal(sdk.NewInt(1), asset0)
s.Require().Equal(sdk.NewInt(5000), asset1)
s.Require().Equal(amount0Desired, asset0)
s.Require().Equal(amount1Desired, asset1)

// check position state
// 1517 is from the liquidity originally provided
Expand Down
91 changes: 91 additions & 0 deletions x/concentrated-liquidity/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package concentrated_liquidity

import (
"context"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/v12/x/concentrated-liquidity/types"
)

type msgServer struct {
keeper *Keeper
}

func NewMsgServerImpl(keeper *Keeper) types.MsgServer {
return &msgServer{
keeper: keeper,
}
}

// TODO: spec and tests, including events
func (server msgServer) CreatePosition(goCtx context.Context, msg *types.MsgCreatePosition) (*types.MsgCreatePositionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}

actualAmount0, actualAmount1, err := server.keeper.createPosition(ctx, msg.PoolId, sender, msg.TokenDesired0.Amount, msg.TokenDesired1.Amount, msg.TokenMinAmount0, msg.TokenMinAmount1, msg.LowerTick, msg.UpperTick)
if err != nil {
return nil, err
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
sdk.NewEvent(
types.TypeEvtWithdrawPosition,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(msg.PoolId, 10)),
sdk.NewAttribute(types.AttributeAmount0, actualAmount0.String()),
sdk.NewAttribute(types.AttributeAmount1, actualAmount1.String()),
sdk.NewAttribute(types.AttributeLowerTick, strconv.FormatInt(msg.LowerTick, 10)),
sdk.NewAttribute(types.AttributeUpperTick, strconv.FormatInt(msg.UpperTick, 10)),
),
})

return &types.MsgCreatePositionResponse{Amount0: actualAmount0, Amount1: actualAmount1}, nil
}

// TODO: spec and tests, including events
func (server msgServer) WithdrawPosition(goCtx context.Context, msg *types.MsgWithdrawPosition) (*types.MsgWithdrawPositionResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

sender, err := sdk.AccAddressFromBech32(msg.Sender)
if err != nil {
return nil, err
}

amount0, amount1, err := server.keeper.withdrawPosition(ctx, msg.PoolId, sender, msg.LowerTick, msg.UpperTick, msg.LiquidityAmount)
if err != nil {
return nil, err
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
),
sdk.NewEvent(
types.TypeEvtWithdrawPosition,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
sdk.NewAttribute(types.AttributeKeyPoolId, strconv.FormatUint(msg.PoolId, 10)),
sdk.NewAttribute(types.AttributeLiquidity, msg.LiquidityAmount.String()),
sdk.NewAttribute(types.AttributeAmount0, amount0.String()),
sdk.NewAttribute(types.AttributeAmount1, amount1.String()),
sdk.NewAttribute(types.AttributeLowerTick, strconv.FormatInt(msg.LowerTick, 10)),
sdk.NewAttribute(types.AttributeUpperTick, strconv.FormatInt(msg.UpperTick, 10)),
),
})

return &types.MsgWithdrawPositionResponse{Amount0: amount0, Amount1: amount1}, nil
}
14 changes: 14 additions & 0 deletions x/concentrated-liquidity/types/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package types

const (
TypeEvtCreatePosition = "create_position"
TypeEvtWithdrawPosition = "withdraw_position"

AttributeValueCategory = ModuleName
AttributeKeyPoolId = "pool_id"
AttributeAmount0 = "amount0"
AttributeAmount1 = "amount1"
AttributeLiquidity = "liquidity"
AttributeLowerTick = "lower_tick"
AttributeUpperTick = "upper_tick"
)
Loading

0 comments on commit 527f823

Please sign in to comment.