Skip to content

Commit

Permalink
feat[CL]: implement initialize pool (#3733)
Browse files Browse the repository at this point in the history
* implement initialize pool

* fix test error

* added additional test case
  • Loading branch information
czarcas7ic authored Dec 16, 2022
1 parent 59a7c45 commit 05a9fe4
Show file tree
Hide file tree
Showing 11 changed files with 1,185 additions and 1,114 deletions.
16 changes: 15 additions & 1 deletion app/apptesting/concentrated_liquidity.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package apptesting

import (
clmodel "github.com/osmosis-labs/osmosis/v13/x/concentrated-liquidity/model"
"github.com/osmosis-labs/osmosis/v13/x/concentrated-liquidity/types"
)

Expand All @@ -12,7 +13,20 @@ var (

// PrepareConcentratedPool sets up an eth usdc concentrated liquidity pool with pool ID 1, tick spacing of 1, and no liquidity
func (s *KeeperTestHelper) PrepareConcentratedPool() types.ConcentratedPoolExtension {
pool, err := s.App.ConcentratedLiquidityKeeper.CreateNewConcentratedLiquidityPool(s.Ctx, 1, ETH, USDC, DefaultTickSpacing)
// Mint some assets to the account.
s.FundAcc(s.TestAccs[0], DefaultAcctFunds)

// Create a concentrated pool via the swaprouter
poolID, err := s.App.SwapRouterKeeper.CreatePool(s.Ctx, clmodel.NewMsgCreateConcentratedPool(s.TestAccs[0], ETH, USDC, DefaultTickSpacing))
s.Require().NoError(err)

// Retrieve the poolInterface via the poolID
poolI, err := s.App.ConcentratedLiquidityKeeper.GetPool(s.Ctx, poolID)
s.Require().NoError(err)

// Type cast the PoolInterface to a ConcentratedPoolExtension
pool, ok := poolI.(types.ConcentratedPoolExtension)
s.Require().True(ok)

return pool
}
4 changes: 4 additions & 0 deletions x/concentrated-liquidity/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ func (k Keeper) InitializeInitialPosition(ctx sdk.Context, pool types.Concentrat
func ConvertConcentratedToPoolInterface(concentratedPool types.ConcentratedPoolExtension) (swaproutertypes.PoolI, error) {
return convertConcentratedToPoolInterface(concentratedPool)
}

func ConvertPoolInterfaceToConcentrated(poolI swaproutertypes.PoolI) (types.ConcentratedPoolExtension, error) {
return convertPoolInterfaceToConcentrated(poolI)
}
45 changes: 0 additions & 45 deletions x/concentrated-liquidity/keeper.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package concentrated_liquidity

import (
"errors"

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

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

"github.com/osmosis-labs/osmosis/v13/x/concentrated-liquidity/types"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
)

type Keeper struct {
Expand Down Expand Up @@ -47,48 +44,6 @@ func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}

// TODO: implement minting, spec, tests
func (k Keeper) InitializePool(ctx sdk.Context, poolI swaproutertypes.PoolI, creatorAddress sdk.AccAddress) error {
pool, ok := poolI.(types.ConcentratedPoolExtension)
if !ok {
return errors.New("invalid pool type when setting concentrated pool")
}
//poolId := pool.GetId()

// Add the share token's meta data to the bank keeper.
// poolShareBaseDenom := types.GetPoolShareDenom(poolId)
// poolShareDisplayDenom := fmt.Sprintf("GAMM-%d", poolId)
// k.bankKeeper.SetDenomMetaData(ctx, banktypes.Metadata{
// Description: fmt.Sprintf("The share token of the gamm pool %d", poolId),
// DenomUnits: []*banktypes.DenomUnit{
// {
// Denom: poolShareBaseDenom,
// Exponent: 0,
// Aliases: []string{
// "attopoolshare",
// },
// },
// {
// Denom: poolShareDisplayDenom,
// Exponent: types.OneShareExponent,
// Aliases: nil,
// },
// },
// Base: poolShareBaseDenom,
// Display: poolShareDisplayDenom,
// })

// // Mint the initial pool shares share token to the sender
// err := k.MintPoolShareToAccount(ctx, pool, creatorAddress, pool.GetTotalShares())
// if err != nil {
// return err
// }

// k.RecordTotalLiquidityIncrease(ctx, pool.GetTotalPoolLiquidity(ctx))

return k.setPool(ctx, pool)
}

// Set the swaprouter keeper.
func (k *Keeper) SetSwapRouterKeeper(swaprouterKeeper types.SwaprouterKeeper) {
k.swaprouterKeeper = swaprouterKeeper
Expand Down
2 changes: 2 additions & 0 deletions x/concentrated-liquidity/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/v13/app/apptesting"
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
)

var (
Expand All @@ -26,6 +27,7 @@ var (
DefaultAmt1Expected = sdk.NewInt(5000000000)
DefaultLiquidityAmt = sdk.MustNewDecFromStr("1517818840.967515822610790519")
DefaultTickSpacing = uint64(1)
PoolCreationFee = swaproutertypes.DefaultParams().PoolCreationFee
)

type KeeperTestSuite struct {
Expand Down
9 changes: 8 additions & 1 deletion x/concentrated-liquidity/lp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

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

clmodel "github.com/osmosis-labs/osmosis/v13/x/concentrated-liquidity/model"
types "github.com/osmosis-labs/osmosis/v13/x/concentrated-liquidity/types"
)

Expand Down Expand Up @@ -120,8 +121,14 @@ func (s *KeeperTestSuite) TestCreatePosition() {
mergeConfigs(&baseConfigCopy, &tc)
tc = baseConfigCopy

// Fund account to pay for the pool creation fee.
s.FundAcc(s.TestAccs[0], PoolCreationFee)

// Create a CL pool with custom tickSpacing
pool, err := s.App.ConcentratedLiquidityKeeper.CreateNewConcentratedLiquidityPool(s.Ctx, 1, ETH, USDC, tc.tickSpacing)
poolID, err := s.App.SwapRouterKeeper.CreatePool(s.Ctx, clmodel.NewMsgCreateConcentratedPool(s.TestAccs[0], ETH, USDC, tc.tickSpacing))
s.Require().NoError(err)

pool, err := s.App.ConcentratedLiquidityKeeper.GetPool(s.Ctx, poolID)
s.Require().NoError(err)

// If we want to test a non-first position, we create a first position with a separate account
Expand Down
1 change: 0 additions & 1 deletion x/concentrated-liquidity/model/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ func (p *Pool) SetCurrentSqrtPrice(newSqrtPrice sdk.Dec) {
}

// SetCurrentTick updates the current tick of the pool when the first position is created.
// For safety, we only allow for this method to be called if CurrentTick is zero.
func (p *Pool) SetCurrentTick(newTick sdk.Int) {
p.CurrentTick = newTick
}
Expand Down
48 changes: 21 additions & 27 deletions x/concentrated-liquidity/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,20 @@ import (
swaproutertypes "github.com/osmosis-labs/osmosis/v13/x/swaprouter/types"
)

// CreateNewConcentratedLiquidityPool creates a new concentrated liquidity pool with the given parameters.
// The pool tokens are denom0 and denom1, and are ordered such that denom0 is lexicographically smaller than denom1.
// The pool is created with zero liquidity and the initial sqrt price and current tick set to zero.
// The given token denominations are ordered to ensure that the first token is the numerator of the price, and the second token is the denominator of the price.
// The pool is added to the pool store, and an error is returned if the operation fails.
func (k Keeper) CreateNewConcentratedLiquidityPool(
ctx sdk.Context,
poolId uint64,
denom0, denom1 string,
tickSpacing uint64,
) (types.ConcentratedPoolExtension, error) {
// Order the initial pool denoms so that denom0 is lexicographically smaller than denom1.
denom0, denom1, err := types.OrderInitialPoolDenoms(denom0, denom1)
// InitializePool initializes a concentrated liquidity pool and sets it in state.
func (k Keeper) InitializePool(ctx sdk.Context, poolI swaproutertypes.PoolI, creatorAddress sdk.AccAddress) error {
concentratedPool, err := convertPoolInterfaceToConcentrated(poolI)
if err != nil {
return nil, err
return err
}

// Create a new concentrated liquidity pool with the given parameters.
poolI, err := model.NewConcentratedLiquidityPool(poolId, denom0, denom1, tickSpacing)
if err != nil {
return nil, err
}
tickSpacing := concentratedPool.GetTickSpacing()

if !k.validateTickSpacing(ctx, tickSpacing) {
return nil, err
}

// Add the pool to the pool store.
err = k.setPool(ctx, &poolI)
if err != nil {
return nil, err
return fmt.Errorf("invalid tick spacing. Got %d", tickSpacing)
}

return &poolI, nil
return k.setPool(ctx, concentratedPool)
}

// GetPool returns a pool with a given id.
Expand Down Expand Up @@ -114,6 +94,20 @@ func convertConcentratedToPoolInterface(concentratedPool types.ConcentratedPoolE
return pool, nil
}

// convertPoolInterfaceToConcentrated takes a swaproutertypes.PoolI and attempts to convert it to a
// types.ConcentratedPoolExtension. If the conversion is successful, the converted value is returned. If the conversion fails,
// an error is returned.
func convertPoolInterfaceToConcentrated(poolI swaproutertypes.PoolI) (types.ConcentratedPoolExtension, error) {
// Attempt to convert swaproutertypes.PoolI to a concentratedPool
concentratedPool, ok := poolI.(types.ConcentratedPoolExtension)
if !ok {
// If the conversion fails, return an error
return nil, fmt.Errorf("given pool does not implement ConcentratedPoolExtension, implements %T", poolI)
}
// Return the converted value
return concentratedPool, nil
}

// validateTickSpacing returns true if the given tick spacing is one of the authorized tick spacings set in the
func (k Keeper) validateTickSpacing(ctx sdk.Context, tickSpacing uint64) bool {
params := k.GetParams(ctx)
Expand Down
Loading

0 comments on commit 05a9fe4

Please sign in to comment.