Skip to content

Commit

Permalink
[TRA-85] scaffold x/vault endblocker (#1197)
Browse files Browse the repository at this point in the history
* scaffold x/vault endblocker
  • Loading branch information
tqin7 authored Mar 19, 2024
1 parent f6afd11 commit d7333d4
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 6 deletions.
4 changes: 4 additions & 0 deletions protocol/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,10 @@ func New(
app.VaultKeeper = *vaultmodulekeeper.NewKeeper(
appCodec,
keys[vaultmoduletypes.StoreKey],
app.ClobKeeper,
app.PerpetualsKeeper,
app.PricesKeeper,
app.SubaccountsKeeper,
[]string{
lib.GovModuleAddress.String(),
delaymsgmoduletypes.ModuleAddress.String(),
Expand Down
13 changes: 13 additions & 0 deletions protocol/x/vault/abci.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package vault

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/keeper"
)

func EndBlocker(
ctx sdk.Context,
keeper *keeper.Keeper,
) {
keeper.RefreshAllVaultOrders(ctx)
}
24 changes: 18 additions & 6 deletions protocol/x/vault/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,33 @@ import (

type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
authorities map[string]struct{}
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
clobKeeper types.ClobKeeper
perpetualsKeeper types.PerpetualsKeeper
pricesKeeper types.PricesKeeper
subaccountsKeeper types.SubaccountsKeeper
authorities map[string]struct{}
}
)

func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
clobKeeper types.ClobKeeper,
perpetualsKeeper types.PerpetualsKeeper,
pricesKeeper types.PricesKeeper,
subaccountsKeeper types.SubaccountsKeeper,
authorities []string,
) *Keeper {
return &Keeper{
cdc: cdc,
storeKey: storeKey,
authorities: lib.UniqueSliceToSet(authorities),
cdc: cdc,
storeKey: storeKey,
clobKeeper: clobKeeper,
perpetualsKeeper: perpetualsKeeper,
pricesKeeper: pricesKeeper,
subaccountsKeeper: subaccountsKeeper,
authorities: lib.UniqueSliceToSet(authorities),
}
}

Expand Down
12 changes: 12 additions & 0 deletions protocol/x/vault/keeper/orders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package keeper

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

// RefreshAllVaultOrders refreshes all orders for all vaults by
// TODO(TRA-134)
// 1. Cancelling all existing orders.
// 2. Placing new orders.
func (k Keeper) RefreshAllVaultOrders(ctx sdk.Context) {
}
1 change: 1 addition & 0 deletions protocol/x/vault/keeper/orders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package keeper_test
15 changes: 15 additions & 0 deletions protocol/x/vault/module.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package vault

import (
"context"
"encoding/json"
"fmt"
"time"

"cosmossdk.io/core/appmodule"

Expand All @@ -12,8 +14,10 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/client/cli"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
Expand All @@ -24,6 +28,7 @@ var (
_ module.HasGenesisBasics = AppModuleBasic{}

_ appmodule.AppModule = AppModule{}
_ appmodule.HasEndBlocker = AppModule{}
_ module.HasConsensusVersion = AppModule{}
_ module.HasGenesis = AppModule{}
_ module.HasServices = AppModule{}
Expand Down Expand Up @@ -141,3 +146,13 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
// consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should
// be set to 1.
func (AppModule) ConsensusVersion() uint64 { return 1 }

// EndBlock executes all ABCI EndBlock logic respective to the vault module.
func (am AppModule) EndBlock(ctx context.Context) error {
defer telemetry.ModuleMeasureSince(am.Name(), time.Now(), telemetry.MetricKeyEndBlocker)
EndBlocker(
lib.UnwrapSDKContext(ctx, types.ModuleName),
&am.keeper,
)
return nil
}
37 changes: 37 additions & 0 deletions protocol/x/vault/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
)

type ClobKeeper interface {
// Clob Pair.
GetAllClobPairs(ctx sdk.Context) (list []clobtypes.ClobPair)

// Order.
GetLongTermOrderPlacement(
ctx sdk.Context,
orderId clobtypes.OrderId,
) (val clobtypes.LongTermOrderPlacement, found bool)
HandleMsgCancelOrder(
ctx sdk.Context,
msg *clobtypes.MsgCancelOrder,
) (err error)
HandleMsgPlaceOrder(
ctx sdk.Context,
msg *clobtypes.MsgPlaceOrder,
) (err error)
}

type PerpetualsKeeper interface {
GetAllPerpetuals(ctx sdk.Context) (list []perptypes.Perpetual)
}

type PricesKeeper interface {
GetAllMarketPrices(ctx sdk.Context) (marketPrices []pricestypes.MarketPrice)
}

type SubaccountsKeeper interface {
}
14 changes: 14 additions & 0 deletions protocol/x/vault/types/vault_id.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package types

import (
fmt "fmt"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

func (id *VaultId) ToStateKey() []byte {
b, err := id.Marshal()
if err != nil {
panic(err)
}
return b
}

// ToModuleAccountAddress returns the module account address for the vault
// (generated from string "vault-<type>-<number>")
func (id *VaultId) ToModuleAccountAddress() string {
return authtypes.NewModuleAddress(
fmt.Sprintf("vault-%s-%d", id.Type, id.Number),
).String()
}
14 changes: 14 additions & 0 deletions protocol/x/vault/types/vault_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types_test
import (
"testing"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
"github.com/stretchr/testify/require"
)
Expand All @@ -14,3 +15,16 @@ func TestToStateKey(t *testing.T) {
b, _ = constants.Vault_Clob_1.Marshal()
require.Equal(t, b, constants.Vault_Clob_1.ToStateKey())
}

func TestToModuleAccountAddress(t *testing.T) {
require.Equal(
t,
authtypes.NewModuleAddress("vault-VAULT_TYPE_CLOB-0").String(),
constants.Vault_Clob_0.ToModuleAccountAddress(),
)
require.Equal(
t,
authtypes.NewModuleAddress("vault-VAULT_TYPE_CLOB-1").String(),
constants.Vault_Clob_1.ToModuleAccountAddress(),
)
}

0 comments on commit d7333d4

Please sign in to comment.