Skip to content

Commit

Permalink
chore(vpool): init vpool module (#476)
Browse files Browse the repository at this point in the history
* Create genesis and params proto

* Create genesis and params scaffolding

* Add codec

* Add dummy handler

* Create vpool app module

* Wire vpool module into app.go

* Rename Pricekeeper --> PricefeedKeeper

* Fix linter
  • Loading branch information
NibiruHeisenberg authored May 23, 2022
1 parent 76ff91d commit 4bac5e6
Show file tree
Hide file tree
Showing 36 changed files with 1,106 additions and 189 deletions.
62 changes: 35 additions & 27 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import (
dextypes "github.com/NibiruChain/nibiru/x/dex/types"
"github.com/NibiruChain/nibiru/x/epochs"
epochskeeper "github.com/NibiruChain/nibiru/x/epochs/keeper"
epochstype "github.com/NibiruChain/nibiru/x/epochs/types"
epochstypes "github.com/NibiruChain/nibiru/x/epochs/types"
"github.com/NibiruChain/nibiru/x/incentivization"
incentivizationkeeper "github.com/NibiruChain/nibiru/x/incentivization/keeper"
incentivizationtypes "github.com/NibiruChain/nibiru/x/incentivization/types"
Expand All @@ -101,11 +101,12 @@ import (
perpkeeper "github.com/NibiruChain/nibiru/x/perp/keeper"
perptypes "github.com/NibiruChain/nibiru/x/perp/types"
"github.com/NibiruChain/nibiru/x/pricefeed"
pricekeeper "github.com/NibiruChain/nibiru/x/pricefeed/keeper"
pricetypes "github.com/NibiruChain/nibiru/x/pricefeed/types"
pricefeedkeeper "github.com/NibiruChain/nibiru/x/pricefeed/keeper"
pricefeedtypes "github.com/NibiruChain/nibiru/x/pricefeed/types"
"github.com/NibiruChain/nibiru/x/stablecoin"
stablecoinkeeper "github.com/NibiruChain/nibiru/x/stablecoin/keeper"
stablecointypes "github.com/NibiruChain/nibiru/x/stablecoin/types"
"github.com/NibiruChain/nibiru/x/vpool"
vpoolkeeper "github.com/NibiruChain/nibiru/x/vpool/keeper"
vpooltypes "github.com/NibiruChain/nibiru/x/vpool/types"

Expand Down Expand Up @@ -169,7 +170,7 @@ var (
perptypes.VaultModuleAccount: {},
perptypes.PerpEFModuleAccount: {},
perptypes.FeePoolModuleAccount: {},
epochstype.ModuleName: {},
epochstypes.ModuleName: {},
lockuptypes.ModuleName: {authtypes.Minter, authtypes.Burner},
stablecointypes.StableEFModuleAccount: {authtypes.Burner},
common.TreasuryPoolModuleAccount: {},
Expand Down Expand Up @@ -214,7 +215,7 @@ type NibiruApp struct {
DexKeeper dexkeeper.Keeper
StablecoinKeeper stablecoinkeeper.Keeper
PerpKeeper perpkeeper.Keeper
PriceKeeper pricekeeper.Keeper
PricefeedKeeper pricefeedkeeper.Keeper
EpochsKeeper epochskeeper.Keeper
LockupKeeper lockupkeeper.Keeper
IncentivizationKeeper incentivizationkeeper.Keeper
Expand Down Expand Up @@ -260,16 +261,16 @@ func NewNibiruApp(
govtypes.StoreKey, paramstypes.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
evidencetypes.StoreKey, capabilitytypes.StoreKey,
authzkeeper.StoreKey,
dextypes.StoreKey, pricetypes.StoreKey, stablecointypes.StoreKey,
epochstype.StoreKey, lockuptypes.StoreKey, perptypes.StoreKey,
dextypes.StoreKey, pricefeedtypes.StoreKey, stablecointypes.StoreKey,
epochstypes.StoreKey, lockuptypes.StoreKey, perptypes.StoreKey,
incentivizationtypes.StoreKey, vpooltypes.StoreKey,
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
// NOTE: The testingkey is just mounted for testing purposes. Actual applications should
// not include this key.
memKeys := sdk.NewMemoryStoreKeys(
capabilitytypes.MemStoreKey, "testingkey",
stablecointypes.MemStoreKey, pricetypes.MemStoreKey,
stablecointypes.MemStoreKey, pricefeedtypes.MemStoreKey,
)

app := &NibiruApp{
Expand Down Expand Up @@ -357,34 +358,34 @@ func NewNibiruApp(
appCodec, keys[dextypes.StoreKey], app.GetSubspace(dextypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.DistrKeeper)

app.PriceKeeper = pricekeeper.NewKeeper(
appCodec, keys[pricetypes.StoreKey], memKeys[pricetypes.MemStoreKey],
app.GetSubspace(pricetypes.ModuleName),
app.PricefeedKeeper = pricefeedkeeper.NewKeeper(
appCodec, keys[pricefeedtypes.StoreKey], memKeys[pricefeedtypes.MemStoreKey],
app.GetSubspace(pricefeedtypes.ModuleName),
)

app.StablecoinKeeper = stablecoinkeeper.NewKeeper(
appCodec, keys[stablecointypes.StoreKey], memKeys[stablecointypes.MemStoreKey],
app.GetSubspace(stablecointypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.PriceKeeper, app.DexKeeper,
app.AccountKeeper, app.BankKeeper, app.PricefeedKeeper, app.DexKeeper,
)

app.VpoolKeeper = vpoolkeeper.NewKeeper(
appCodec,
keys[vpooltypes.StoreKey],
app.PriceKeeper,
app.PricefeedKeeper,
)

app.PerpKeeper = perpkeeper.NewKeeper(
appCodec, keys[perptypes.StoreKey],
app.GetSubspace(perptypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.PriceKeeper, app.VpoolKeeper,
app.AccountKeeper, app.BankKeeper, app.PricefeedKeeper, app.VpoolKeeper,
)

app.EpochsKeeper = epochskeeper.NewKeeper(
appCodec, keys[epochstype.StoreKey],
appCodec, keys[epochstypes.StoreKey],
)
app.EpochsKeeper.SetHooks(
epochstype.NewMultiEpochHooks(app.StablecoinKeeper.Hooks()),
epochstypes.NewMultiEpochHooks(app.StablecoinKeeper.Hooks()),
)

app.LockupKeeper = lockupkeeper.NewLockupKeeper(appCodec,
Expand All @@ -404,16 +405,19 @@ func NewNibiruApp(
dexModule := dex.NewAppModule(
appCodec, app.DexKeeper, app.AccountKeeper, app.BankKeeper)
pricefeedModule := pricefeed.NewAppModule(
appCodec, app.PriceKeeper, app.AccountKeeper, app.BankKeeper)
appCodec, app.PricefeedKeeper, app.AccountKeeper, app.BankKeeper)
epochsModule := epochs.NewAppModule(appCodec, app.EpochsKeeper)
stablecoinModule := stablecoin.NewAppModule(
appCodec, app.StablecoinKeeper, app.AccountKeeper, app.BankKeeper,
app.PriceKeeper,
app.PricefeedKeeper,
)
lockupModule := lockup.NewAppModule(appCodec, app.LockupKeeper, app.AccountKeeper, app.BankKeeper)
perpModule := perp.NewAppModule(
appCodec, app.PerpKeeper, app.AccountKeeper, app.BankKeeper,
app.PriceKeeper,
app.PricefeedKeeper,
)
vpoolModule := vpool.NewAppModule(
appCodec, app.VpoolKeeper, app.PricefeedKeeper,
)

incentivizationModule := incentivization.NewAppModule(appCodec, app.IncentivizationKeeper)
Expand Down Expand Up @@ -445,6 +449,7 @@ func NewNibiruApp(
stablecoinModule,
lockupModule,
epochsModule,
vpoolModule,
perpModule,
incentivizationModule,
)
Expand All @@ -461,9 +466,10 @@ func NewNibiruApp(
authz.ModuleName, feegrant.ModuleName,
paramstypes.ModuleName, vestingtypes.ModuleName,
dextypes.ModuleName,
pricetypes.ModuleName,
epochstype.ModuleName,
pricefeedtypes.ModuleName,
epochstypes.ModuleName,
stablecointypes.ModuleName,
vpooltypes.ModuleName,
perptypes.ModuleName,
lockuptypes.ModuleName,
incentivizationtypes.ModuleName,
Expand All @@ -476,9 +482,10 @@ func NewNibiruApp(
feegrant.ModuleName,
paramstypes.ModuleName, upgradetypes.ModuleName, vestingtypes.ModuleName,
dextypes.ModuleName,
epochstype.ModuleName,
pricetypes.ModuleName,
epochstypes.ModuleName,
pricefeedtypes.ModuleName,
stablecointypes.ModuleName,
vpooltypes.ModuleName,
perptypes.ModuleName,
lockuptypes.ModuleName,
incentivizationtypes.ModuleName,
Expand All @@ -496,9 +503,10 @@ func NewNibiruApp(
feegrant.ModuleName,
paramstypes.ModuleName, upgradetypes.ModuleName, vestingtypes.ModuleName,
dextypes.ModuleName,
pricetypes.ModuleName,
epochstype.ModuleName,
pricefeedtypes.ModuleName,
epochstypes.ModuleName,
stablecointypes.ModuleName,
vpooltypes.ModuleName,
perptypes.ModuleName,
lockuptypes.ModuleName,
incentivizationtypes.ModuleName,
Expand Down Expand Up @@ -737,8 +745,8 @@ func initParamsKeeper(
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypes.ParamKeyTable())
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(dextypes.ModuleName)
paramsKeeper.Subspace(pricetypes.ModuleName)
paramsKeeper.Subspace(epochstype.ModuleName)
paramsKeeper.Subspace(pricefeedtypes.ModuleName)
paramsKeeper.Subspace(epochstypes.ModuleName)
paramsKeeper.Subspace(stablecointypes.ModuleName)
paramsKeeper.Subspace(perptypes.ModuleName)

Expand Down
13 changes: 13 additions & 0 deletions proto/vpool/v1/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

package nibiru.vpool.v1;

import "vpool/v1/params.proto";
import "gogoproto/gogo.proto";

option go_package = "github.com/NibiruChain/nibiru/x/vpool/types";

// GenesisState defines the vpool module's genesis state.
message GenesisState {
Params params = 1 [(gogoproto.nullable) = false];
}
14 changes: 14 additions & 0 deletions proto/vpool/v1/params.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package nibiru.vpool.v1;

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

option go_package = "github.com/NibiruChain/nibiru/x/vpool/types";

// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;
}
20 changes: 10 additions & 10 deletions x/perp/keeper/clearing_house_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ func TestKeeper_getLatestCumulativePremiumFraction(t *testing.T) {
}

type mockedDependencies struct {
mockAccountKeeper *mock.MockAccountKeeper
mockBankKeeper *mock.MockBankKeeper
mockPriceKeeper *mock.MockPriceKeeper
mockVpoolKeeper *mock.MockVpoolKeeper
mockAccountKeeper *mock.MockAccountKeeper
mockBankKeeper *mock.MockBankKeeper
mockPricefeedKeeper *mock.MockPricefeedKeeper
mockVpoolKeeper *mock.MockVpoolKeeper
}

func getKeeper(t *testing.T) (Keeper, mockedDependencies, sdk.Context) {
Expand Down Expand Up @@ -106,7 +106,7 @@ func getKeeper(t *testing.T) (Keeper, mockedDependencies, sdk.Context) {
ctrl := gomock.NewController(t)
mockedAccountKeeper := mock.NewMockAccountKeeper(ctrl)
mockedBankKeeper := mock.NewMockBankKeeper(ctrl)
mockedPriceKeeper := mock.NewMockPriceKeeper(ctrl)
mockedPricefeedKeeper := mock.NewMockPricefeedKeeper(ctrl)
mockedVpoolKeeper := mock.NewMockVpoolKeeper(ctrl)

mockedAccountKeeper.
Expand All @@ -119,17 +119,17 @@ func getKeeper(t *testing.T) (Keeper, mockedDependencies, sdk.Context) {
subSpace,
mockedAccountKeeper,
mockedBankKeeper,
mockedPriceKeeper,
mockedPricefeedKeeper,
mockedVpoolKeeper,
)

ctx := sdk.NewContext(commitMultiStore, tmproto.Header{}, false, nil)

return k, mockedDependencies{
mockAccountKeeper: mockedAccountKeeper,
mockBankKeeper: mockedBankKeeper,
mockPriceKeeper: mockedPriceKeeper,
mockVpoolKeeper: mockedVpoolKeeper,
mockAccountKeeper: mockedAccountKeeper,
mockBankKeeper: mockedBankKeeper,
mockPricefeedKeeper: mockedPricefeedKeeper,
mockVpoolKeeper: mockedVpoolKeeper,
}, ctx
}

Expand Down
18 changes: 9 additions & 9 deletions x/perp/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ type Keeper struct {
storeKey sdk.StoreKey
ParamSubspace paramtypes.Subspace

BankKeeper types.BankKeeper
AccountKeeper types.AccountKeeper
PriceKeeper types.PriceKeeper
VpoolKeeper types.VpoolKeeper
BankKeeper types.BankKeeper
AccountKeeper types.AccountKeeper
PricefeedKeeper types.PricefeedKeeper
VpoolKeeper types.VpoolKeeper
}

// NewKeeper Creates a new x/perp Keeper instance.
Expand All @@ -31,7 +31,7 @@ func NewKeeper(

accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
priceKeeper types.PriceKeeper,
priceKeeper types.PricefeedKeeper,
vpoolKeeper types.VpoolKeeper,
) Keeper {
// Ensure that the module account is set.
Expand All @@ -49,10 +49,10 @@ func NewKeeper(
storeKey: storeKey,
ParamSubspace: paramSubspace,

AccountKeeper: accountKeeper,
BankKeeper: bankKeeper,
PriceKeeper: priceKeeper,
VpoolKeeper: vpoolKeeper,
AccountKeeper: accountKeeper,
BankKeeper: bankKeeper,
PricefeedKeeper: priceKeeper,
VpoolKeeper: vpoolKeeper,
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/perp/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ type AppModule struct {
keeper keeper.Keeper
ak types.AccountKeeper
bk types.BankKeeper
pk types.PriceKeeper
pk types.PricefeedKeeper
}

func NewAppModule(
cdc codec.Codec,
keeper keeper.Keeper,
ak types.AccountKeeper,
bk types.BankKeeper,
pk types.PriceKeeper,
pk types.PricefeedKeeper,
) AppModule {
return AppModule{
AppModuleBasic: NewAppModuleBasic(cdc),
Expand Down
4 changes: 2 additions & 2 deletions x/perp/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package types

//go:generate mockgen -destination=../../testutil/mock/perp_interfaces.go -package=mock github.com/NibiruChain/nibiru/x/perp/types AccountKeeper,BankKeeper,PriceKeeper,VpoolKeeper
//go:generate mockgen -destination=../../testutil/mock/perp_interfaces.go -package=mock github.com/NibiruChain/nibiru/x/perp/types AccountKeeper,BankKeeper,PricefeedKeeper,VpoolKeeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -46,7 +46,7 @@ type BankKeeper interface {
GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
}

type PriceKeeper interface {
type PricefeedKeeper interface {
GetCurrentPrice(ctx sdk.Context, token0 string, token1 string,
) (pftypes.CurrentPrice, error)
GetCurrentPrices(ctx sdk.Context) pftypes.CurrentPrices
Expand Down
6 changes: 3 additions & 3 deletions x/perp/types/expected_keepers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func TestExpectedKeepers(t *testing.T) {
appKeeper interface{}
}{
{
name: "PriceKeeper from x/pricefeed",
expectedKeeper: (*types.PriceKeeper)(nil),
appKeeper: nibiruApp.PriceKeeper,
name: "PricefeedKeeper from x/pricefeed",
expectedKeeper: (*types.PricefeedKeeper)(nil),
appKeeper: nibiruApp.PricefeedKeeper,
},
{
name: "BankKeeper from the cosmos-sdk",
Expand Down
12 changes: 6 additions & 6 deletions x/pricefeed/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ func TestTWAPriceUpdates(t *testing.T) {
ctx = ctx.
WithBlockHeight(ctx.BlockHeight() + 1).
WithBlockTime(ctx.BlockTime().Add(duration))
pricefeed.BeginBlocker(ctx, nibiruApp.PriceKeeper)
pricefeed.BeginBlocker(ctx, nibiruApp.PricefeedKeeper)
}
setPrice := func(price string) {
_, err := nibiruApp.PriceKeeper.SetPrice(
_, err := nibiruApp.PricefeedKeeper.SetPrice(
ctx, oracle, token0, token1,
sdk.MustNewDecFromStr(price), ctx.BlockTime().Add(time.Hour*5000*4))
require.NoError(t, err)
Expand All @@ -49,7 +49,7 @@ func TestTWAPriceUpdates(t *testing.T) {
},
})

nibiruApp.PriceKeeper.SetParams(ctx, markets)
nibiruApp.PricefeedKeeper.SetParams(ctx, markets)

// Sim set price set the price for one hour
setPrice("0.9")
Expand All @@ -70,7 +70,7 @@ func TestTWAPriceUpdates(t *testing.T) {
(0.9 * 1463385600 + (0.9 + 0.8) / 2 * 1481385600) / (1463385600 + 1481385600) = 0.8749844622444971
*/
price, err := nibiruApp.PriceKeeper.GetCurrentTWAPPrice(ctx, token0, token1)
price, err := nibiruApp.PricefeedKeeper.GetCurrentTWAPPrice(ctx, token0, token1)
require.NoError(t, err)
priceFloat, err := price.Price.Float64()
require.NoError(t, err)
Expand All @@ -89,7 +89,7 @@ func TestTWAPriceUpdates(t *testing.T) {
(0.9 * 1463385600 + (0.9 + 0.8) / 2 * 1481385600 + 0.82 * 1499385600) / (1463385600 + 1481385600 + 1499385600) = 0.8563426456960295
*/
price, err = nibiruApp.PriceKeeper.GetCurrentTWAPPrice(ctx, token0, token1)
price, err = nibiruApp.PricefeedKeeper.GetCurrentTWAPPrice(ctx, token0, token1)
require.NoError(t, err)
priceFloat, err = price.Price.Float64()
require.NoError(t, err)
Expand All @@ -108,7 +108,7 @@ func TestTWAPriceUpdates(t *testing.T) {
*/
setPrice("0.83")
runBlock(time.Hour * 5000)
price, err = nibiruApp.PriceKeeper.GetCurrentTWAPPrice(ctx, token0, token1)
price, err = nibiruApp.PricefeedKeeper.GetCurrentTWAPPrice(ctx, token0, token1)

require.NoError(t, err)
priceFloat, err = price.Price.Float64()
Expand Down
Loading

0 comments on commit 4bac5e6

Please sign in to comment.