Skip to content
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): CreatePosition and WithdrawPosition messages #3139

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
p0mvn marked this conversation as resolved.
Show resolved Hide resolved

// ensure types.MinTick <= lowerTick < types.MaxTick
// TODO (bez): Add unit tests.
if lowerTick < types.MinTick || lowerTick >= types.MaxTick {
Expand Down Expand Up @@ -44,22 +47,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,21 +17,23 @@ func (s *KeeperTestSuite) TestMint() {
currentTick := sdk.NewInt(85176)
lowerTick := int64(84222)
upperTick := int64(86129)
liquidity, err := sdk.NewDecFromStr("1517.882323")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why move this testing params from test to keeper layer?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API has changed. This is the final API that we are going to have

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)
}
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(sdk.UnwrapSDKContext(goCtx), 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(sdk.UnwrapSDKContext(goCtx), 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"
)
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
Loading