Skip to content

Commit

Permalink
add v6 upgrade handler for x/vault (backport #2040) (#2080)
Browse files Browse the repository at this point in the history
Co-authored-by: Tian <[email protected]>
  • Loading branch information
mergify[bot] and tqin7 authored Aug 13, 2024
1 parent fd1e34c commit af25b92
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions protocol/app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (app *App) setupUpgradeHandlers() {
app.PricesKeeper,
app.MarketMapKeeper,
app.RevShareKeeper,
app.VaultKeeper,
),
)
}
Expand Down
33 changes: 32 additions & 1 deletion protocol/app/upgrades/v6.0.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
revsharetypes "github.com/dydxprotocol/v4-chain/protocol/x/revshare/types"
dydx "github.com/skip-mev/slinky/providers/apis/dydx"
vaultkeeper "github.com/dydxprotocol/v4-chain/protocol/x/vault/keeper"
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
"github.com/skip-mev/slinky/providers/apis/dydx"
dydxtypes "github.com/skip-mev/slinky/providers/apis/dydx/types"
marketmapkeeper "github.com/skip-mev/slinky/x/marketmap/keeper"
marketmaptypes "github.com/skip-mev/slinky/x/marketmap/types"
Expand Down Expand Up @@ -113,13 +115,39 @@ func initRevShareModuleState(
}
}

func initVaultDefaultQuotingParams(
ctx sdk.Context,
vaultKeeper vaultkeeper.Keeper,
) {
// Initialize the default quoting params for the vault module.
oldParams := vaultKeeper.UnsafeGetParams(ctx)
if err := vaultKeeper.SetDefaultQuotingParams(
ctx,
&vaulttypes.QuotingParams{
Layers: oldParams.Layers,
SpreadMinPpm: oldParams.SpreadMinPpm,
SpreadBufferPpm: oldParams.SpreadBufferPpm,
SkewFactorPpm: oldParams.SkewFactorPpm,
OrderSizePctPpm: oldParams.OrderSizePctPpm,
OrderExpirationSeconds: oldParams.OrderExpirationSeconds,
ActivationThresholdQuoteQuantums: oldParams.ActivationThresholdQuoteQuantums,
},
); err != nil {
panic(fmt.Sprintf("failed to set vault default quoting params: %s", err))
}

// Delete deprecated `Params`.
vaultKeeper.UnsafeDeleteParams(ctx)
}

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
clobKeeper clobtypes.ClobKeeper,
pricesKeeper pricestypes.PricesKeeper,
mmKeeper marketmapkeeper.Keeper,
revShareKeeper revsharetypes.RevShareKeeper,
vaultKeeper vaultkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx context.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
sdkCtx := lib.UnwrapSDKContext(ctx, "app/upgrades")
Expand All @@ -136,6 +164,9 @@ func CreateUpgradeHandler(
// Initialize the rev share module state.
initRevShareModuleState(sdkCtx, revShareKeeper, pricesKeeper)

// Initialize x/vault default quoting params.
initVaultDefaultQuotingParams(sdkCtx, vaultKeeper)

sdkCtx.Logger().Info("Successfully removed stateful orders from state")

return mm.RunMigrations(ctx, configurator, vm)
Expand Down
17 changes: 17 additions & 0 deletions protocol/app/upgrades/v6.0.0/upgrade_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
revsharetypes "github.com/dydxprotocol/v4-chain/protocol/x/revshare/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

const (
Expand Down Expand Up @@ -60,6 +61,7 @@ func postUpgradeChecks(node *containertest.Node, t *testing.T) {
postUpgradeMarketMapState(node, t)
postUpgradeStatefulOrderCheck(node, t)
postUpgradeMarketMapperRevShareChecks(node, t)
postUpgradeVaultDefaultQuotingParams(node, t)
}

func placeOrders(node *containertest.Node, t *testing.T) {
Expand Down Expand Up @@ -247,3 +249,18 @@ func postUpgradeMarketMapState(node *containertest.Node, t *testing.T) {

require.Equal(t, v_6_0_0.DefaultMarketMapParams, paramsResp.Params)
}

func postUpgradeVaultDefaultQuotingParams(node *containertest.Node, t *testing.T) {
params := &vaulttypes.QueryParamsResponse{}
resp, err := containertest.Query(
node,
vaulttypes.NewQueryClient,
vaulttypes.QueryClient.Params,
&vaulttypes.QueryParamsRequest{},
)
require.NoError(t, err)
err = proto.UnmarshalText(resp.String(), params)
require.NoError(t, err)

require.Nil(t, params.DefaultQuotingParams.Validate())
}
22 changes: 22 additions & 0 deletions protocol/x/vault/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,25 @@ func (k Keeper) SetVaultQuotingParams(

return nil
}

// 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
}

// 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"))
}

0 comments on commit af25b92

Please sign in to comment.