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/spike: CosmWasm Pool Type and Module #4675

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
80 changes: 80 additions & 0 deletions app/apptesting/cosmwasmpool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package apptesting

import (
"encoding/json"
"os"
"strings"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/cosmwasm/msg"
"github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/model"

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

const (
DefaultTransmuterDenomA = "axlusdc"
DefaultTransmuterDenomB = "gravusdc"
DefaultCodeId = 1
)

// PrepareCosmWasmPool sets up a cosmwasm pool with the default parameters.
func (s *KeeperTestHelper) PrepareCosmWasmPool() cosmwasmpooltypes.CosmWasmExtension {
return s.PrepareCustomTransmuterPool(s.TestAccs[0], []string{DefaultTransmuterDenomA, DefaultTransmuterDenomB}, DefaultCodeId)
}

// PrepareCustomConcentratedPool sets up a concentrated liquidity pool with the custom parameters.
func (s *KeeperTestHelper) PrepareCustomTransmuterPool(owner sdk.AccAddress, denoms []string, codeId uint64) cosmwasmpooltypes.CosmWasmExtension {
// Mint some assets to the account.
s.FundAcc(s.TestAccs[0], DefaultAcctFunds)

cosmwasmpoolModuleAddr := s.App.AccountKeeper.GetModuleAddress(cosmwasmpooltypes.ModuleName)
s.Require().NotNil(cosmwasmpoolModuleAddr)

// TODO: make sure permissiosns are updated in the upgrade handler.
s.App.WasmKeeper.SetParams(s.Ctx, wasmtypes.Params{
CodeUploadAccess: wasmtypes.AccessConfig{
Permission: wasmtypes.AccessTypeAnyOfAddresses,
Addresses: []string{cosmwasmpoolModuleAddr.String()},
},
InstantiateDefaultPermission: wasmtypes.AccessTypeAnyOfAddresses,
})

workingDir, err := os.Getwd()
s.Require().NoError(err)

projectRootPath := "/osmosis/"
projectRootIndex := strings.LastIndex(workingDir, projectRootPath) + len(projectRootPath)
workingDir = workingDir[:projectRootIndex]

code, err := os.ReadFile(workingDir + "x/cosmwasmpool/bytecode/transmuter.wasm")
s.Require().NoError(err)

s.Require().NoError(err)
instantiateConfig := wasmtypes.AccessConfig{Permission: wasmtypes.AccessTypeOnlyAddress, Address: cosmwasmpoolModuleAddr.String()}
codeID, _, err := s.App.ContractKeeper.Create(s.Ctx, cosmwasmpoolModuleAddr, code, &instantiateConfig)
s.Require().NoError(err)

instantiateMsg := msg.InstantiateMsg{
PoolAssetDenoms: denoms,
}

instantiateMsgBz, err := json.Marshal(instantiateMsg)
s.Require().NoError(err)

addr, _, err := s.App.ContractKeeper.Instantiate(s.Ctx, codeID, cosmwasmpoolModuleAddr, cosmwasmpoolModuleAddr, instantiateMsgBz, "transmuter contract", nil)
s.Require().NoError(err)

nextPoolId := s.App.PoolManagerKeeper.GetNextPoolId(s.Ctx)
s.App.PoolManagerKeeper.SetNextPoolId(s.Ctx, nextPoolId+1)

pool := model.NewCosmWasmPool(nextPoolId, codeID, []byte{})
pool.SetContractAddress(addr.String())
pool.SetWasmKeeper(s.App.WasmKeeper)

s.App.CosmwasmPoolKeeper.SetPool(s.Ctx, &pool)

return &pool
}
11 changes: 11 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
icq "github.com/cosmos/ibc-apps/modules/async-icq/v4"
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 @@ -146,6 +148,7 @@ type AppKeepers struct {
PoolManagerKeeper *poolmanager.Keeper
ValidatorSetPreferenceKeeper *valsetpref.Keeper
ConcentratedLiquidityKeeper *concentratedliquidity.Keeper
CosmwasmPoolKeeper *cosmwasmpool.Keeper

// IBC modules
// transfer module
Expand Down Expand Up @@ -324,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.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 @@ -451,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 @@ -645,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 @@ -760,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
5 changes: 4 additions & 1 deletion app/upgrades/v15/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
packetforwardtypes "github.com/strangelove-ventures/packet-forward-middleware/v4/router/types"

"github.com/osmosis-labs/osmosis/v15/app/upgrades"
cltypes "github.com/osmosis-labs/osmosis/v15/x/concentrated-liquidity/types"
cosmwasmpooltypes "github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/types"
poolmanagertypes "github.com/osmosis-labs/osmosis/v15/x/poolmanager/types"
protorevtypes "github.com/osmosis-labs/osmosis/v15/x/protorev/types"
valsetpreftypes "github.com/osmosis-labs/osmosis/v15/x/valset-pref/types"
Expand All @@ -25,7 +27,8 @@ var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
CreateUpgradeHandler: CreateUpgradeHandler,
StoreUpgrades: store.StoreUpgrades{
Added: []string{poolmanagertypes.StoreKey, valsetpreftypes.StoreKey, protorevtypes.StoreKey, icqtypes.StoreKey, packetforwardtypes.StoreKey},
// TODO: move cosmwasmpooltypes.StoreKet to v16 upgrade handler.
Added: []string{poolmanagertypes.StoreKey, cltypes.StoreKey, valsetpreftypes.StoreKey, protorevtypes.StoreKey, icqtypes.StoreKey, packetforwardtypes.StoreKey, cosmwasmpooltypes.StoreKey},
Deleted: []string{},
},
}
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{},
},
}
Binary file added x/cosmwasmpool/bytecode/transmuter.wasm
Binary file not shown.
14 changes: 14 additions & 0 deletions x/cosmwasmpool/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cli

import flag "github.com/spf13/pflag"

const (
//TODO: change this, coode-generated
FlagSwapRoutePoolIds = "test-flag"
)

func FlagCodeGenerated() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.String(FlagSwapRoutePoolIds, "", "TODO: change this, coode-generated")
return fs
}
21 changes: 21 additions & 0 deletions x/cosmwasmpool/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cli

import (
"github.com/spf13/cobra"

"github.com/osmosis-labs/osmosis/osmoutils/osmocli"
"github.com/osmosis-labs/osmosis/v15/x/cosmwasmpool/client/queryproto"
"github.com/osmosis-labs/osmosis/v15/x/poolmanager/types"
)

// GetQueryCmd returns the cli query commands for this module.
func GetQueryCmd() *cobra.Command {
cmd := osmocli.QueryIndexCmd(types.ModuleName)

cmd.AddCommand(
osmocli.GetParams[*queryproto.ParamsRequest](
types.ModuleName, queryproto.NewQueryClient),
)

return cmd
}
26 changes: 26 additions & 0 deletions x/cosmwasmpool/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cli

import (
flag "github.com/spf13/pflag"

"github.com/spf13/cobra"

"github.com/osmosis-labs/osmosis/osmoutils/osmocli"
"github.com/osmosis-labs/osmosis/v15/x/poolmanager/types"
)

func NewTxCmd() *cobra.Command {
txCmd := osmocli.TxIndexCmd(types.ModuleName)

osmocli.AddTxCmd(txCmd, NewGeneratedCmd)

return txCmd
}

func NewGeneratedCmd() (*osmocli.TxCliDesc, *types.MsgSwapExactAmountIn) {
return &osmocli.TxCliDesc{
Use: "generated-cmd [token-in] [token-out-min-amount]",
Short: "TODO: code-generated command, change this",
Flags: osmocli.FlagDesc{RequiredFlags: []*flag.FlagSet{FlagCodeGenerated()}},
}, &types.MsgSwapExactAmountIn{}
}
10 changes: 10 additions & 0 deletions x/cosmwasmpool/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cosmwasmpool

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

func (k *Keeper) ConvertToCosmwasmPool(poolI poolmanagertypes.PoolI) (types.CosmWasmExtension, error) {
return k.convertToCosmwasmPool(poolI)
}
64 changes: 54 additions & 10 deletions x/cosmwasmpool/keeper.go
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
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
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, 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, bankKeeper: bankKeeper}
}

// GetParams returns the total set of cosmwasmpool parameters.
Expand All @@ -43,3 +41,49 @@ 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)
}

// InitGenesis initializes the cosmwasmpool module's state from a provided genesis
// state.
func (k Keeper) InitGenesis(ctx sdk.Context, genState *types.GenesisState) {
if err := genState.Validate(); err != nil {
panic(err)
}

k.SetParams(ctx, genState.Params)
}

// ExportGenesis returns the cosmwasmpool module's exported genesis.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return &types.GenesisState{
Params: k.GetParams(ctx),
}
}

// 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
}

// TODO: godoc and test
func (k *Keeper) convertToCosmwasmPool(poolI poolmanagertypes.PoolI) (types.CosmWasmExtension, error) {
cosmwasmPool, ok := poolI.(types.CosmWasmExtension)
if !ok {
return nil, types.InvalidPoolTypeError{
ActualPool: poolI,
}
}

cosmwasmPool.SetWasmKeeper(k.wasmKeeper)

return cosmwasmPool, nil
}
Loading