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: cosmwasmpool core logic and tests #5354

Merged
merged 7 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Features

* [#5354](https://github.com/osmosis-labs/osmosis/pull/5354) implement x/cosmwasmpool module.
* [#4659](https://github.com/osmosis-labs/osmosis/pull/4659) implement AllPools query in x/poolmanager.
* [#4783](https://github.com/osmosis-labs/osmosis/pull/4783) Update wasmd to 0.31.0
* [#4629](https://github.com/osmosis-labs/osmosis/pull/4629) add amino proto annotations
Expand Down
36 changes: 27 additions & 9 deletions app/apptesting/pool_manager.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"

"github.com/osmosis-labs/osmosis/v15/x/gamm/pool-models/balancer"
poolmanager "github.com/osmosis-labs/osmosis/v15/x/poolmanager"
poolmanagertypes "github.com/osmosis-labs/osmosis/v15/x/poolmanager/types"
)
Expand Down Expand Up @@ -40,21 +41,38 @@ func (s *KeeperTestHelper) CreatePoolFromType(poolType poolmanagertypes.PoolType
case poolmanagertypes.Concentrated:
s.PrepareConcentratedPool()
return
case poolmanagertypes.CosmWasm:
s.PrepareCosmWasmPool()
return
}
}

// CreatePoolFromTypeWithCoins creates a pool with the given type and initialized with the given coins.
func (s *KeeperTestHelper) CreatePoolFromTypeWithCoins(poolType poolmanagertypes.PoolType, coins sdk.Coins) uint64 {
var poolId uint64
if poolType == poolmanagertypes.Balancer {
poolId = s.PrepareBalancerPoolWithCoins(coins...)
} else if poolType == poolmanagertypes.Concentrated {
return s.CreatePoolFromTypeWithCoinsAndSpreadFactor(poolType, coins, sdk.ZeroDec())
}

// CreatePoolFromTypeWithCoinsAndSpreadFactor creates a pool with given type, initialized with the given coins as initial liquidity and spread factor.
func (s *KeeperTestHelper) CreatePoolFromTypeWithCoinsAndSpreadFactor(poolType poolmanagertypes.PoolType, coins sdk.Coins, spreadFactor sdk.Dec) uint64 {
switch poolType {
case poolmanagertypes.Balancer:
poolId := s.PrepareCustomBalancerPoolFromCoins(coins, balancer.PoolParams{
SwapFee: spreadFactor,
ExitFee: sdk.ZeroDec(),
})
return poolId
case poolmanagertypes.Concentrated:
s.Require().Len(coins, 2)
pool := s.PrepareCustomConcentratedPool(s.TestAccs[0], coins[0].Denom, coins[1].Denom, DefaultTickSpacing, spreadFactor)
s.CreateFullRangePosition(pool, coins)
return pool.GetId()
case poolmanagertypes.CosmWasm:
s.Require().Len(coins, 2)
clPool := s.PrepareConcentratedPoolWithCoins(coins[0].Denom, coins[1].Denom)
s.CreateFullRangePosition(clPool, coins)
poolId = clPool.GetId()
} else {
pool := s.PrepareCustomTransmuterPool(s.TestAccs[0], []string{coins[0].Denom, coins[1].Denom})
s.JoinTransmuterPool(s.TestAccs[0], pool.GetId(), coins)
return pool.GetId()
default:
s.FailNow(fmt.Sprintf("unsupported pool type for this operation (%s)", poolmanagertypes.PoolType_name[int32(poolType)]))
}
return poolId
return 0
}
12 changes: 10 additions & 2 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
icqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v4/types"

"github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool"
cosmwasmpooltypes "github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/types"
downtimedetector "github.com/osmosis-labs/osmosis/v15/x/downtime-detector"
downtimetypes "github.com/osmosis-labs/osmosis/v15/x/downtime-detector/types"
"github.com/osmosis-labs/osmosis/v15/x/gamm"
Expand Down Expand Up @@ -147,8 +148,7 @@ type AppKeepers struct {
PoolManagerKeeper *poolmanager.Keeper
ValidatorSetPreferenceKeeper *valsetpref.Keeper
ConcentratedLiquidityKeeper *concentratedliquidity.Keeper
// TODO: initialize this keeper: https://github.com/osmosis-labs/osmosis/issues/5329
CosmwasmPoolKeeper *cosmwasmpool.Keeper
CosmwasmPoolKeeper *cosmwasmpool.Keeper

// IBC modules
// transfer module
Expand Down Expand Up @@ -327,17 +327,21 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.GAMMKeeper = &gammKeeper
appKeepers.ConcentratedLiquidityKeeper.SetGammKeeper(appKeepers.GAMMKeeper)

appKeepers.CosmwasmPoolKeeper = cosmwasmpool.NewKeeper(appCodec, appKeepers.keys[cosmwasmpooltypes.StoreKey], appKeepers.GetSubspace(cosmwasmpooltypes.ModuleName), appKeepers.AccountKeeper, appKeepers.BankKeeper)

appKeepers.PoolManagerKeeper = poolmanager.NewKeeper(
appKeepers.keys[poolmanagertypes.StoreKey],
appKeepers.GetSubspace(poolmanagertypes.ModuleName),
appKeepers.GAMMKeeper,
appKeepers.ConcentratedLiquidityKeeper,
appKeepers.CosmwasmPoolKeeper,
appKeepers.BankKeeper,
appKeepers.AccountKeeper,
appKeepers.DistrKeeper,
)
appKeepers.GAMMKeeper.SetPoolManager(appKeepers.PoolManagerKeeper)
appKeepers.ConcentratedLiquidityKeeper.SetPoolManagerKeeper(appKeepers.PoolManagerKeeper)
appKeepers.CosmwasmPoolKeeper.SetPoolManagerKeeper(appKeepers.PoolManagerKeeper)

appKeepers.TwapKeeper = twap.NewKeeper(
appKeepers.keys[twaptypes.StoreKey],
Expand Down Expand Up @@ -454,11 +458,13 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
wasmOpts...,
)
appKeepers.WasmKeeper = &wasmKeeper
appKeepers.CosmwasmPoolKeeper.SetWasmKeeper(appKeepers.WasmKeeper)

// Pass the contract keeper to all the structs (generally ICS4Wrappers for ibc middlewares) that need it
appKeepers.ContractKeeper = wasmkeeper.NewDefaultPermissionKeeper(appKeepers.WasmKeeper)
appKeepers.RateLimitingICS4Wrapper.ContractKeeper = appKeepers.ContractKeeper
appKeepers.Ics20WasmHooks.ContractKeeper = appKeepers.ContractKeeper
appKeepers.CosmwasmPoolKeeper.SetContractKeeper(appKeepers.ContractKeeper)

// wire up x/wasm to IBC
ibcRouter.AddRoute(wasm.ModuleName, wasm.NewIBCHandler(appKeepers.WasmKeeper, appKeepers.IBCKeeper.ChannelKeeper, appKeepers.IBCKeeper.ChannelKeeper))
Expand Down Expand Up @@ -648,6 +654,7 @@ func (appKeepers *AppKeepers) initParamsKeeper(appCodec codec.BinaryCodec, legac
paramsKeeper.Subspace(concentratedliquiditytypes.ModuleName)
paramsKeeper.Subspace(icqtypes.ModuleName)
paramsKeeper.Subspace(packetforwardtypes.ModuleName).WithKeyTable(packetforwardtypes.ParamKeyTable())
paramsKeeper.Subspace(cosmwasmpooltypes.ModuleName)

return paramsKeeper
}
Expand Down Expand Up @@ -763,5 +770,6 @@ func KVStoreKeys() []string {
ibchookstypes.StoreKey,
icqtypes.StoreKey,
packetforwardtypes.StoreKey,
cosmwasmpooltypes.StoreKey,
}
}
2 changes: 2 additions & 0 deletions app/keepers/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
_ "github.com/osmosis-labs/osmosis/v15/client/docs/statik"
clclient "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/client"
concentratedliquidity "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/clmodule"
cosmwasmpoolmodule "github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/module"
downtimemodule "github.com/osmosis-labs/osmosis/v15/x/downtime-detector/module"
"github.com/osmosis-labs/osmosis/v15/x/gamm"
gammclient "github.com/osmosis-labs/osmosis/v15/x/gamm/client"
Expand Down Expand Up @@ -111,4 +112,5 @@ var AppModuleBasics = []module.AppModuleBasic{
ibc_hooks.AppModuleBasic{},
ibcratelimitmodule.AppModuleBasic{},
router.AppModuleBasic{},
cosmwasmpoolmodule.AppModuleBasic{},
}
2 changes: 2 additions & 0 deletions app/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import (
"github.com/osmosis-labs/osmosis/v15/simulation/simtypes"
concentratedliquidity "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/clmodule"
concentratedliquiditytypes "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/types"
cosmwasmpooltypes "github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/types"
"github.com/osmosis-labs/osmosis/v15/x/gamm"
gammtypes "github.com/osmosis-labs/osmosis/v15/x/gamm/types"
"github.com/osmosis-labs/osmosis/v15/x/ibc-rate-limit/ibcratelimitmodule"
Expand Down Expand Up @@ -119,6 +120,7 @@ var moduleAccountPermissions = map[string][]string{
tokenfactorytypes.ModuleName: {authtypes.Minter, authtypes.Burner},
valsetpreftypes.ModuleName: {authtypes.Staking},
poolmanagertypes.ModuleName: nil,
cosmwasmpooltypes.ModuleName: nil,
}

// appModules return modules to initialize module manager.
Expand Down
3 changes: 2 additions & 1 deletion app/upgrades/v16/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
store "github.com/cosmos/cosmos-sdk/store/types"

cltypes "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/types"
cosmwasmpooltypes "github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/types"
)

// UpgradeName defines the on-chain upgrade name for the Osmosis v16 upgrade.
Expand All @@ -21,7 +22,7 @@ var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{cltypes.StoreKey},
Added: []string{cltypes.StoreKey, cosmwasmpooltypes.StoreKey},
Deleted: []string{},
},
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,6 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230516205127-c213fddde069 h1:ZgDrTJ2GCH4CJGbV6rudw4O9rPMAuwWoLVZnG6cUr+A=
github.com/osmosis-labs/osmosis/osmomath v0.0.3-dev.0.20230516205127-c213fddde069/go.mod h1:a7lhiXRpn8QJ21OhFpaEnUNErTSIafaYpp02q6uI/Dk=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230516205127-c213fddde069 h1:9C/n+Nx5rre/AHPMlPsQrk1isgydrCNB68aqer86ygE=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230516205127-c213fddde069/go.mod h1:hk/o9/kmTSZmZqwXcSrPuwj/gpRMCqbE/d3vj6teL2A=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230529060317-d6d2dda0fb22 h1:I14d+U4gDSL5dHoQ3G+kGLhZP5Zj3mOxMb/97Xflusc=
github.com/osmosis-labs/osmosis/osmoutils v0.0.0-20230529060317-d6d2dda0fb22/go.mod h1:GIvgXqD8NOtrpu5bJ052tZxWLFj4ekpi1BMwEHIvXVU=
github.com/osmosis-labs/osmosis/x/epochs v0.0.0-20230328024000-175ec88e4304 h1:RIrWLzIiZN5Xd2JOfSOtGZaf6V3qEQYg6EaDTAkMnCo=
Expand Down
48 changes: 38 additions & 10 deletions x/cosmwasmpool/keeper.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
package cosmwasmpool

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

"github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v15/x/poolmanager/types"

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

type Keeper struct {
cdc codec.BinaryCodec
storeKey sdk.StoreKey
paramSpace paramtypes.Subspace

// keepers
// TODO: remove nolint once added.
// nolint: unused
accountKeeper types.AccountKeeper
bankKeeper types.BankKeeper
poolmanagerKeeper types.PoolManagerKeeper
// TODO: remove nolint once added.
// nolint: unused
contractKeeper types.ContractKeeper
// TODO: remove nolint once added.
// nolint: unused
wasmKeeper types.WasmKeeper
contractKeeper types.ContractKeeper
wasmKeeper types.WasmKeeper
}

func NewKeeper(storeKey sdk.StoreKey, paramSpace paramtypes.Subspace) *Keeper {
func NewKeeper(cdc codec.BinaryCodec, storeKey sdk.StoreKey, paramSpace paramtypes.Subspace, accountKeeper types.AccountKeeper, bankKeeper types.BankKeeper) *Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}

return &Keeper{storeKey: storeKey, paramSpace: paramSpace}
return &Keeper{cdc: cdc, storeKey: storeKey, paramSpace: paramSpace, accountKeeper: accountKeeper, bankKeeper: bankKeeper}
}

// GetParams returns the total set of cosmwasmpool parameters.
Expand All @@ -43,3 +42,32 @@ func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}

// Set the poolmanager keeper.
func (k *Keeper) SetPoolManagerKeeper(poolmanagerKeeper types.PoolManagerKeeper) {
k.poolmanagerKeeper = poolmanagerKeeper
}

// Set the contract keeper.
func (k *Keeper) SetContractKeeper(contractKeeper types.ContractKeeper) {
k.contractKeeper = contractKeeper
}

// Set the wasm keeper.
func (k *Keeper) SetWasmKeeper(wasmKeeper types.WasmKeeper) {
k.wasmKeeper = wasmKeeper
}

// convertToCosmwasmPool converts a poolI to a CosmWasmExtension.
func (k *Keeper) convertToCosmwasmPool(poolI poolmanagertypes.PoolI) (types.CosmWasmExtension, error) {
p0mvn marked this conversation as resolved.
Show resolved Hide resolved
cosmwasmPool, ok := poolI.(types.CosmWasmExtension)
if !ok {
return nil, types.InvalidPoolTypeError{
ActualPool: poolI,
}
}

cosmwasmPool.SetWasmKeeper(k.wasmKeeper)

return cosmwasmPool, nil
}
15 changes: 7 additions & 8 deletions x/cosmwasmpool/model/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
Expand Down Expand Up @@ -42,13 +43,11 @@ var (
)

func init() {
// TODO: uncomment the lines below: https://github.com/osmosis-labs/osmosis/issues/5329
RegisterCodec(amino)
sdk.RegisterLegacyAminoCodec(amino)

// RegisterCodec(amino)
// sdk.RegisterLegacyAminoCodec(amino)

// // Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be
// // used to properly serialize MsgGrant and MsgExec instances
// RegisterCodec(authzcodec.Amino)
// amino.Seal()
// Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be
// used to properly serialize MsgGrant and MsgExec instances
RegisterCodec(authzcodec.Amino)
amino.Seal()
}
6 changes: 2 additions & 4 deletions x/cosmwasmpool/model/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func (msg MsgCreateCosmWasmPool) ValidateBasic() error {
}

func (msg MsgCreateCosmWasmPool) GetSignBytes() []byte {
// TODO: uncomment once merging state-breaks: https://github.com/osmosis-labs/osmosis/issues/5329
// return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
return nil
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

func (msg MsgCreateCosmWasmPool) GetSigners() []sdk.AccAddress {
Expand Down Expand Up @@ -82,7 +80,7 @@ func (msg MsgCreateCosmWasmPool) InitialLiquidity() sdk.Coins {

func (msg MsgCreateCosmWasmPool) CreatePool(ctx sdk.Context, poolID uint64) (poolmanagertypes.PoolI, error) {
poolI := NewCosmWasmPool(poolID, msg.CodeId, msg.InstantiateMsg)
return &poolI, nil
return poolI, nil
}

func (msg MsgCreateCosmWasmPool) GetPoolType() poolmanagertypes.PoolType {
Expand Down
4 changes: 2 additions & 2 deletions x/cosmwasmpool/model/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
)

// NewCosmWasmPool creates a new CosmWasm pool with the specified parameters.
func NewCosmWasmPool(poolId uint64, codeId uint64, instantiateMsg []byte) Pool {
func NewCosmWasmPool(poolId uint64, codeId uint64, instantiateMsg []byte) *Pool {
pool := Pool{
CosmWasmPool: CosmWasmPool{
PoolAddress: poolmanagertypes.NewPoolAddress(poolId).String(),
Expand All @@ -36,7 +36,7 @@ func NewCosmWasmPool(poolId uint64, codeId uint64, instantiateMsg []byte) Pool {
WasmKeeper: nil, // N.B.: this is set in InitializePool().
}

return pool
return &pool
}

// poolmanager.PoolI interface implementation
Expand Down
3 changes: 0 additions & 3 deletions x/cosmwasmpool/model/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ const (
)

func TestPoolModuleSuite(t *testing.T) {

t.Skip("Skipping test until https://github.com/osmosis-labs/osmosis/issues/5329 is completed")

suite.Run(t, new(CosmWasmPoolSuite))
}

Expand Down
Loading