diff --git a/protocol/app/app.go b/protocol/app/app.go index 0ea957281f..5f4ad9bce2 100644 --- a/protocol/app/app.go +++ b/protocol/app/app.go @@ -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(), diff --git a/protocol/x/vault/abci.go b/protocol/x/vault/abci.go new file mode 100644 index 0000000000..7b74f6b859 --- /dev/null +++ b/protocol/x/vault/abci.go @@ -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) +} diff --git a/protocol/x/vault/keeper/keeper.go b/protocol/x/vault/keeper/keeper.go index 319158f723..9fe75f116f 100644 --- a/protocol/x/vault/keeper/keeper.go +++ b/protocol/x/vault/keeper/keeper.go @@ -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), } } diff --git a/protocol/x/vault/keeper/orders.go b/protocol/x/vault/keeper/orders.go new file mode 100644 index 0000000000..515e94c1de --- /dev/null +++ b/protocol/x/vault/keeper/orders.go @@ -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) { +} diff --git a/protocol/x/vault/keeper/orders_test.go b/protocol/x/vault/keeper/orders_test.go new file mode 100644 index 0000000000..9429264902 --- /dev/null +++ b/protocol/x/vault/keeper/orders_test.go @@ -0,0 +1 @@ +package keeper_test diff --git a/protocol/x/vault/module.go b/protocol/x/vault/module.go index bf1c9e1d1b..c77ad88043 100644 --- a/protocol/x/vault/module.go +++ b/protocol/x/vault/module.go @@ -1,8 +1,10 @@ package vault import ( + "context" "encoding/json" "fmt" + "time" "cosmossdk.io/core/appmodule" @@ -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" @@ -24,6 +28,7 @@ var ( _ module.HasGenesisBasics = AppModuleBasic{} _ appmodule.AppModule = AppModule{} + _ appmodule.HasEndBlocker = AppModule{} _ module.HasConsensusVersion = AppModule{} _ module.HasGenesis = AppModule{} _ module.HasServices = AppModule{} @@ -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 +} diff --git a/protocol/x/vault/types/expected_keepers.go b/protocol/x/vault/types/expected_keepers.go index ab1254f4c2..50136cde9c 100644 --- a/protocol/x/vault/types/expected_keepers.go +++ b/protocol/x/vault/types/expected_keepers.go @@ -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 { +} diff --git a/protocol/x/vault/types/vault_id.go b/protocol/x/vault/types/vault_id.go index 131d16f0f9..7db466810c 100644 --- a/protocol/x/vault/types/vault_id.go +++ b/protocol/x/vault/types/vault_id.go @@ -1,5 +1,11 @@ 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 { @@ -7,3 +13,11 @@ func (id *VaultId) ToStateKey() []byte { } return b } + +// ToModuleAccountAddress returns the module account address for the vault +// (generated from string "vault--") +func (id *VaultId) ToModuleAccountAddress() string { + return authtypes.NewModuleAddress( + fmt.Sprintf("vault-%s-%d", id.Type, id.Number), + ).String() +} diff --git a/protocol/x/vault/types/vault_id_test.go b/protocol/x/vault/types/vault_id_test.go index b7e4c77b1f..d93aee6a83 100644 --- a/protocol/x/vault/types/vault_id_test.go +++ b/protocol/x/vault/types/vault_id_test.go @@ -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" ) @@ -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(), + ) +}