Skip to content

Commit

Permalink
wip upgrade handler
Browse files Browse the repository at this point in the history
Signed-off-by: Shrenuj Bansal <shrenuj@dydx.exchange>
  • Loading branch information
shrenujb committed Mar 7, 2024
1 parent 1aaa2cc commit a78c68b
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
15 changes: 15 additions & 0 deletions protocol/app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package app
import (
"fmt"

v5_0_0 "github.com/dydxprotocol/v4-chain/protocol/app/upgrades/v5.0.0"

upgradetypes "cosmossdk.io/x/upgrade/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/app/upgrades"
Expand All @@ -14,6 +16,7 @@ var (
// New upgrades should be added to this slice after they are implemented.
Upgrades = []upgrades.Upgrade{
v4_0_0.Upgrade,
v5_0_0.Upgrade,
}
Forks = []upgrades.Fork{}
)
Expand All @@ -32,6 +35,18 @@ func (app *App) setupUpgradeHandlers() {
app.RatelimitKeeper,
),
)

if app.UpgradeKeeper.HasHandler(v5_0_0.UpgradeName) {
panic(fmt.Sprintf("Cannot register duplicate upgrade handler '%s'", v5_0_0.UpgradeName))
}
app.UpgradeKeeper.SetUpgradeHandler(
v5_0_0.UpgradeName,
v5_0_0.CreateUpgradeHandler(
app.ModuleManager,
app.configurator,
app.PerpetualsKeeper,
),
)
}

// setUpgradeStoreLoaders sets custom store loaders to customize the rootMultiStore
Expand Down
17 changes: 17 additions & 0 deletions protocol/app/upgrades/v5.0.0/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package v5_0_0

import (
store "cosmossdk.io/store/types"
"github.com/dydxprotocol/v4-chain/protocol/app/upgrades"
)

const (
UpgradeName = "v5.0.0"
)

var Upgrade = upgrades.Upgrade{
UpgradeName: UpgradeName,
StoreUpgrades: store.StoreUpgrades{
Added: []string{},
},
}
52 changes: 52 additions & 0 deletions protocol/app/upgrades/v5.0.0/upgrade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package v5_0_0

import (
"context"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"

upgradetypes "cosmossdk.io/x/upgrade/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/dydxprotocol/v4-chain/protocol/lib"
)

// Set all existing perpetuals to cross market type
func perpetualsUpgrade(
ctx sdk.Context,
perpetualsKeeper perptypes.PerpetualsKeeper,
) error {

// Set all perpetuals to cross market type
perpetuals, err := perpetualsKeeper.GetAllPerpetuals(ctx)
if err != nil {
panic(fmt.Sprintf("failed to get all perpetuals: %s", err))
}
for _, p := range perpetuals {
_, err = perpetualsKeeper.SetPerpetualMarketType(ctx, p.GetId(),
perptypes.PerpetualMarketType_PERPETUAL_MARKET_TYPE_CROSS)
if err != nil {
panic(fmt.Sprintf("failed to set perpetual market type for perpetual %d: %s", p.GetId(), err))
}
}

return nil
}

func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
perpetualsKeeper perptypes.PerpetualsKeeper,
) upgradetypes.UpgradeHandler {
return func(ctx context.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
sdkCtx := lib.UnwrapSDKContext(ctx, "app/upgrades")
sdkCtx.Logger().Info(fmt.Sprintf("Running %s Upgrade...", UpgradeName))

// Set all perpetuals to cross market type
perpetualsUpgrade(sdkCtx, perpetualsKeeper)

return mm.RunMigrations(ctx, configurator, vm)
}
}
28 changes: 28 additions & 0 deletions protocol/x/perpetuals/keeper/perpetual.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,34 @@ func (k Keeper) ModifyPerpetual(
return perpetual, nil
}

func (k Keeper) SetPerpetualMarketType(
ctx sdk.Context,
perpetualId uint32,
marketType types.PerpetualMarketType,
) (types.Perpetual, error) {

if marketType == types.PerpetualMarketType_PERPETUAL_MARKET_TYPE_UNSPECIFIED {
return types.Perpetual{}, errorsmod.Wrap(
types.ErrInvalidMarketType,
fmt.Sprintf("market type %v", marketType),
)
}

// Get perpetual.
perpetual, err := k.GetPerpetual(ctx, perpetualId)
if err != nil {
return perpetual, err
}

// Modify perpetual.
perpetual.Params.MarketType = marketType

// Store the modified perpetual.
k.setPerpetual(ctx, perpetual)

return perpetual, nil
}

// GetPerpetual returns a perpetual from its id.
func (k Keeper) GetPerpetual(
ctx sdk.Context,
Expand Down
8 changes: 8 additions & 0 deletions protocol/x/perpetuals/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,12 @@ type PerpetualsKeeper interface {
ctx sdk.Context,
params Params,
) error
SetPerpetualMarketType(
ctx sdk.Context,
id uint32,
marketType PerpetualMarketType,
) (Perpetual, error)
GetAllPerpetuals(
ctx sdk.Context,
) []Perpetual
}

0 comments on commit a78c68b

Please sign in to comment.