Skip to content

Commit

Permalink
Remove deprecated message.
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau committed Sep 20, 2024
1 parent d810cd8 commit 3780a04
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 381 deletions.
24 changes: 0 additions & 24 deletions proto/dydxprotocol/vault/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -214,27 +214,3 @@ message MsgRetrieveFromVault {

// MsgRetrieveFromVaultResponse is the Msg/RetrieveFromVault response type.
message MsgRetrieveFromVaultResponse {}

// Deprecated: Only used for v7.x upgrade handler. Not included in message server.
// MsgDepositToVault deposits the specified asset from the subaccount to the
// vault.
message MsgDepositToVault {
option deprecated = true;
// This annotation enforces that the tx signer is the owner specified in
// subaccount_id. Therefore, this enforces that only the owner of the
// subaccount can deposit into the vault using that subaccount.
option (cosmos.msg.v1.signer) = "subaccount_id";

// The vault to deposit into.
VaultId vault_id = 1;

// The subaccount to deposit from.
dydxprotocol.subaccounts.SubaccountId subaccount_id = 2;

// Number of quote quantums to deposit.
bytes quote_quantums = 3 [
(gogoproto.customtype) =
"github.com/dydxprotocol/v4-chain/protocol/dtypes.SerializableInt",
(gogoproto.nullable) = false
];
}
124 changes: 124 additions & 0 deletions protocol/x/vault/keeper/deprecated_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package keeper

import (
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

// This file contains all functions used to fetch deprecated state for the purpose of upgrade
// functions.

// Deprecated state keys
const (
// Deprecated: For use by the v7.x upgrade handler
// QuotingParamsKeyPrefix is the prefix to retrieve all QuotingParams.
// QuotingParams store: vaultId VaultId -> QuotingParams.
QuotingParamsKeyPrefix = "QuotingParams:"

// Deprecated: For use by the v7.x upgrade handler
// TotalSharesKeyPrefix is the prefix to retrieve all TotalShares.
TotalSharesKeyPrefix = "TotalShares:"
)

// v5.x state, used for v6.x upgrade.

// Deprecated: Only used to get vault params as they were in v5.x.
// UnsafeGetParams returns `Params` in state.
// Used for v6.x upgrade handler.
func (k Keeper) UnsafeGetParams(
ctx sdk.Context,
) (
params types.QuotingParams,
) {
store := ctx.KVStore(k.storeKey)
b := store.Get([]byte("Params"))
k.cdc.MustUnmarshal(b, &params)
return params
}

// Deprecated: Only used to delete params as they were in v5.x.
// UnsafeDeleteParams deletes `Params` in state.
// Used for v6.x upgrade handler.
func (k Keeper) UnsafeDeleteParams(
ctx sdk.Context,
) {
store := ctx.KVStore(k.storeKey)
store.Delete([]byte("Params"))
}

// v6.x state, used for v7.x upgrade

// Deprecated: Only used to set quoting params as they were in v6.x.
// UnsafeSetQuotingParams sets quoting parameters for a given vault from state.
// Used for v7.x upgrade handler
func (k Keeper) UnsafeSetQuotingParams(
ctx sdk.Context,
vaultId types.VaultId,
quotingParams types.QuotingParams,
) error {
if err := quotingParams.Validate(); err != nil {
return err
}

b := k.cdc.MustMarshal(&quotingParams)
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(QuotingParamsKeyPrefix))
store.Set(vaultId.ToStateKey(), b)

return nil
}

// Deprecated: Only used to get quoting params as they were in v6.x.
// UnsafeGetQuotingParams returns quoting parameters for a given vault from state.
// Used for v7.x upgrade handler
func (k Keeper) UnsafeGetQuotingParams(
ctx sdk.Context,
vaultId types.VaultId,
) (
quotingParams types.QuotingParams,
exists bool,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(QuotingParamsKeyPrefix))

b := store.Get(vaultId.ToStateKey())
if b == nil {
return quotingParams, false
}

k.cdc.MustUnmarshal(b, &quotingParams)
return quotingParams, true
}

// Deprecated: Only used to set quoting params as they were in v6.x.
// UnsafeDeleteQuotingParams deletes quoting parameters for a given vault from state.
// Used for v7.x upgrade handler
func (k Keeper) UnsafeDeleteQuotingParams(
ctx sdk.Context,
vaultId types.VaultId,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(QuotingParamsKeyPrefix))
b := store.Get(vaultId.ToStateKey())
if b == nil {
return
}
store.Delete(vaultId.ToStateKey())
}

// Deprecated: Only used to fetch vault ids as they were in v6.x
// UnsafeGetAllVaultIds returns all vault ids from state using the deprecated total shares
// state.
func (k Keeper) UnsafeGetAllVaultIds(ctx sdk.Context) []types.VaultId {
vaultIds := []types.VaultId{}
totalSharesStore := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(TotalSharesKeyPrefix))
totalSharesIterator := storetypes.KVStorePrefixIterator(totalSharesStore, []byte{})
defer totalSharesIterator.Close()
for ; totalSharesIterator.Valid(); totalSharesIterator.Next() {
vaultId, err := types.GetVaultIdFromStateKey(totalSharesIterator.Key())
if err != nil {
panic(err)
}
vaultIds = append(vaultIds, *vaultId)
}
return vaultIds
}
37 changes: 37 additions & 0 deletions protocol/x/vault/keeper/grpc_query_vault_params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package keeper

import (
"context"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

func (k Keeper) VaultParams(
goCtx context.Context,
req *types.QueryVaultParamsRequest,
) (*types.QueryVaultParamsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)

vaultId := types.VaultId{
Type: req.Type,
Number: req.Number,
}

// Get vault params.
vaultParams, exists := k.GetVaultParams(ctx, vaultId)
if !exists {
return nil, status.Error(codes.NotFound, "vault not found")
}

return &types.QueryVaultParamsResponse{
VaultId: vaultId,
VaultParams: vaultParams,
}, nil
}
86 changes: 86 additions & 0 deletions protocol/x/vault/keeper/grpc_query_vault_params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package keeper_test

import (
"math/big"
"testing"

testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
"github.com/stretchr/testify/require"
)

func TestVaultParamsQuery(t *testing.T) {
tests := map[string]struct {
/* --- Setup --- */
// Vault ID.
vaultId vaulttypes.VaultId
// Vault params.
vaultParams vaulttypes.VaultParams
// Query request.
req *vaulttypes.QueryVaultParamsRequest

/* --- Expectations --- */
expectedEquity *big.Int
expectedErr string
}{
"Success": {
req: &vaulttypes.QueryVaultParamsRequest{
Type: vaulttypes.VaultType_VAULT_TYPE_CLOB,
Number: 0,
},
vaultId: constants.Vault_Clob0,
vaultParams: constants.VaultParams,
},
"Success: close only vault status": {
req: &vaulttypes.QueryVaultParamsRequest{
Type: vaulttypes.VaultType_VAULT_TYPE_CLOB,
Number: 0,
},
vaultId: constants.Vault_Clob0,
vaultParams: vaulttypes.VaultParams{
Status: vaulttypes.VaultStatus_VAULT_STATUS_CLOSE_ONLY,
},
},
"Error: query non-existent vault": {
req: &vaulttypes.QueryVaultParamsRequest{
Type: vaulttypes.VaultType_VAULT_TYPE_CLOB,
Number: 1, // Non-existent vault.
},
vaultId: constants.Vault_Clob0,
vaultParams: constants.VaultParams,
expectedErr: "vault not found",
},
"Error: nil request": {
req: nil,
vaultId: constants.Vault_Clob0,
vaultParams: constants.VaultParams,
expectedErr: "invalid request",
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.VaultKeeper

// Set vault params.
err := k.SetVaultParams(ctx, tc.vaultId, tc.vaultParams)
require.NoError(t, err)

// Check Vault query response is as expected.
response, err := k.VaultParams(ctx, tc.req)
if tc.expectedErr != "" {
require.ErrorContains(t, err, tc.expectedErr)
} else {
require.NoError(t, err)
expectedResponse := vaulttypes.QueryVaultParamsResponse{
VaultId: tc.vaultId,
VaultParams: tc.vaultParams,
}
require.Equal(t, expectedResponse, *response)
}
})
}
}
Loading

0 comments on commit 3780a04

Please sign in to comment.