-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d810cd8
commit 3780a04
Showing
5 changed files
with
309 additions
and
381 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ¶ms) | ||
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("ingParams) | ||
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, "ingParams) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.