Skip to content

Commit

Permalink
Merge pull request #868 from comdex-official/feature/dev
Browse files Browse the repository at this point in the history
v13.2.0 upgrade handler
  • Loading branch information
cgsingh33 authored Dec 8, 2023
2 parents e3bb190 + 9c44db7 commit d387b59
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 69 deletions.
11 changes: 7 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ import (

cwasm "github.com/comdex-official/comdex/app/wasm"

mv12 "github.com/comdex-official/comdex/app/upgrades/mainnet/v12"
mv13 "github.com/comdex-official/comdex/app/upgrades/mainnet/v13"
tv13 "github.com/comdex-official/comdex/app/upgrades/testnet/v13"
)

Expand Down Expand Up @@ -1456,8 +1456,8 @@ func (a *App) ModuleAccountsPermissions() map[string][]string {

func (a *App) registerUpgradeHandlers() {
a.UpgradeKeeper.SetUpgradeHandler(
tv13.UpgradeName,
tv13.CreateUpgradeHandlerV13(a.mm, a.configurator, a.cdc, a.keys[capabilitytypes.ModuleName], a.CapabilityKeeper, a.WasmKeeper, a.ParamsKeeper, a.ConsensusParamsKeeper, *a.IbcKeeper, a.GovKeeper, *a.StakingKeeper, a.MintKeeper, a.SlashingKeeper, a.BandoracleKeeper),
mv13.UpgradeName,
mv13.CreateUpgradeHandlerV13(a.mm, a.configurator, a.cdc, a.ParamsKeeper, a.ConsensusParamsKeeper, *a.IbcKeeper, a.ICQKeeper, a.GovKeeper, a.AssetKeeper, a.LendKeeper, a.NewliqKeeper, a.NewaucKeeper),
)
// When a planned update height is reached, the old binary will panic
// writing on disk the height and name of the update that triggered it
Expand All @@ -1480,12 +1480,15 @@ func (a *App) registerUpgradeHandlers() {
func upgradeHandlers(upgradeInfo upgradetypes.Plan, a *App, storeUpgrades *storetypes.StoreUpgrades) *storetypes.StoreUpgrades {
switch {

case upgradeInfo.Name == mv12.UpgradeName && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
case upgradeInfo.Name == mv13.UpgradeName && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{
icqtypes.StoreKey,
liquidationsV2types.ModuleName,
auctionsV2types.ModuleName,
crisistypes.StoreKey,
consensusparamtypes.StoreKey,
ibcfeetypes.StoreKey,
},
}
case upgradeInfo.Name == tv13.UpgradeName && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
Expand Down
15 changes: 15 additions & 0 deletions app/upgrades/mainnet/v13/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v13

const (
UpgradeName = "v13.2.0"
UpgradeHeight = ""
UpgradeInfo = `'{
"binaries": {
"darwin/arm64":"",
"darwin/x86_64":"",
"linux/arm64":"",
"linux/x86_64":"",
"windows/x86_64":""
}
}'`
)
45 changes: 45 additions & 0 deletions app/upgrades/mainnet/v13/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package v13_test

import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/comdex-official/comdex/app"
v13 "github.com/comdex-official/comdex/app/upgrades/mainnet/v13"
)

type UpgradeTestSuite struct {
app.KeeperTestHelper
}

func (s *UpgradeTestSuite) SetupTest() {
s.Setup()
}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}

// Ensures the test does not error out.
func (s *UpgradeTestSuite) TestUpgrade() {
s.Setup()

preUpgradeChecks(s)

upgradeHeight := int64(5)
s.ConfirmUpgradeSucceeded(v13.UpgradeName, upgradeHeight)

postUpgradeChecks(s)
}

func preUpgradeChecks(s *UpgradeTestSuite) {

}

func postUpgradeChecks(s *UpgradeTestSuite) {

// Ensure the gov params have MinInitialDepositRatio added
gp := s.App.GovKeeper.GetParams(s.Ctx)
s.Require().Equal(gp.MinInitialDepositRatio, "0.200000000000000000")
}
233 changes: 233 additions & 0 deletions app/upgrades/mainnet/v13/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
package v13

import (
"fmt"
assetkeeper "github.com/comdex-official/comdex/x/asset/keeper"
assettypes "github.com/comdex-official/comdex/x/asset/types"
auctionV2keeper "github.com/comdex-official/comdex/x/auctionsV2/keeper"
auctionsV2types "github.com/comdex-official/comdex/x/auctionsV2/types"
lendkeeper "github.com/comdex-official/comdex/x/lend/keeper"
lendtypes "github.com/comdex-official/comdex/x/lend/types"
liquidationV2keeper "github.com/comdex-official/comdex/x/liquidationsV2/keeper"
liquidationV2types "github.com/comdex-official/comdex/x/liquidationsV2/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
consensusparamkeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
icqkeeper "github.com/cosmos/ibc-apps/modules/async-icq/v7/keeper"
icqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v7/types"
exported "github.com/cosmos/ibc-go/v7/modules/core/exported"
ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper"
ibctmmigrations "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint/migrations"
)

func CreateUpgradeHandlerV13(
mm *module.Manager,
configurator module.Configurator,
cdc codec.Codec,
paramsKeeper paramskeeper.Keeper,
consensusParamsKeeper consensusparamkeeper.Keeper,
IBCKeeper ibckeeper.Keeper,
icqkeeper *icqkeeper.Keeper,
GovKeeper govkeeper.Keeper,
assetKeeper assetkeeper.Keeper,
lendKeeper lendkeeper.Keeper,
liquidationV2Keeper liquidationV2keeper.Keeper,
auctionV2Keeper auctionV2keeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
ctx.Logger().Info("Applying main net upgrade - v.13.2.0")
logger := ctx.Logger().With("upgrade", UpgradeName)

// Migrate Tendermint consensus parameters from x/params module to a deprecated x/consensus module.
// The old params module is required to still be imported in your app.go in order to handle this migration.
ctx.Logger().Info("Migrating tendermint consensus params from x/params to x/consensus...")
legacyParamSubspace := paramsKeeper.Subspace(baseapp.Paramspace).WithKeyTable(paramstypes.ConsensusParamsKeyTable())
baseapp.MigrateParams(ctx, legacyParamSubspace, &consensusParamsKeeper)

// ibc v4-to-v5
// https://github.com/cosmos/ibc-go/blob/v7.1.0/docs/migrations/v4-to-v5.md
// -- nothing --

// https://github.com/cosmos/ibc-go/blob/v7.1.0/docs/migrations/v5-to-v6.md

// ibc v6-to-v7
// https://github.com/cosmos/ibc-go/blob/v7.1.0/docs/migrations/v6-to-v7.md#chains
// (optional) prune expired tendermint consensus states to save storage space
ctx.Logger().Info("Pruning expired tendermint consensus states...")
if _, err := ibctmmigrations.PruneExpiredConsensusStates(ctx, cdc, IBCKeeper.ClientKeeper); err != nil {
return nil, err
}

// ibc v7-to-v7.1
// https://github.com/cosmos/ibc-go/blob/v7.1.0/docs/migrations/v7-to-v7_1.md#09-localhost-migration
// explicitly update the IBC 02-client params, adding the localhost client type
params := IBCKeeper.ClientKeeper.GetParams(ctx)
params.AllowedClients = append(params.AllowedClients, exported.Localhost)
IBCKeeper.ClientKeeper.SetParams(ctx, params)
logger.Info(fmt.Sprintf("updated ibc client params %v", params))

// icq params set
icqparams := icqtypes.DefaultParams()
icqparams.AllowQueries = append(icqparams.AllowQueries, "/cosmwasm.wasm.v1.Query/SmartContractState")
icqkeeper.SetParams(ctx, icqparams)

// Run migrations
logger.Info(fmt.Sprintf("pre migrate version map: %v", fromVM))
vm, err := mm.RunMigrations(ctx, configurator, fromVM)
if err != nil {
return nil, err
}
logger.Info(fmt.Sprintf("post migrate version map: %v", vm))

// update gov params to use a 20% initial deposit ratio, allowing us to remote the ante handler
govParams := GovKeeper.GetParams(ctx)
govParams.MinInitialDepositRatio = sdk.NewDec(20).Quo(sdk.NewDec(100)).String()
if err := GovKeeper.SetParams(ctx, govParams); err != nil {
return nil, err
}
logger.Info(fmt.Sprintf("updated gov params to %v", govParams))

UpdateLendParams(ctx, lendKeeper, assetKeeper)
InitializeStates(ctx, liquidationV2Keeper, auctionV2Keeper)

return vm, err
}
}

func UpdateLendParams(
ctx sdk.Context,
lendKeeper lendkeeper.Keeper,
assetKeeper assetkeeper.Keeper,
) {

cSTATOM := assettypes.Asset{
Name: "CSTATOM",
Denom: "ucstatom",
Decimals: sdk.NewInt(1000000),
IsOnChain: true,
IsOraclePriceRequired: false,
IsCdpMintable: true,
}
err := assetKeeper.AddAssetRecords(ctx, cSTATOM)
if err != nil {
fmt.Println(err)
}
assetID := assetKeeper.GetAssetID(ctx)

assetRatesParamsSTAtom := lendtypes.AssetRatesParams{
AssetID: 14,
UOptimal: newDec("0.75"),
Base: newDec("0.002"),
Slope1: newDec("0.07"),
Slope2: newDec("1.25"),
EnableStableBorrow: false,
Ltv: newDec("0.7"),
LiquidationThreshold: newDec("0.75"),
LiquidationPenalty: newDec("0.05"),
LiquidationBonus: newDec("0.05"),
ReserveFactor: newDec("0.2"),
CAssetID: assetID,
IsIsolated: false,
}
lendKeeper.SetAssetRatesParams(ctx, assetRatesParamsSTAtom)

assetRatesParamsCmdx, _ := lendKeeper.GetAssetRatesParams(ctx, 2)
assetRatesParamsCmdx.LiquidationPenalty = newDec("0.075")
assetRatesParamsCmdx.LiquidationBonus = newDec("0.075")
lendKeeper.SetAssetRatesParams(ctx, assetRatesParamsCmdx)

assetRatesParamsCmst, _ := lendKeeper.GetAssetRatesParams(ctx, 3)
assetRatesParamsCmst.LiquidationPenalty = newDec("0.05")
assetRatesParamsCmst.LiquidationBonus = newDec("0.05")
lendKeeper.SetAssetRatesParams(ctx, assetRatesParamsCmst)

cAXLUSDC := assettypes.Asset{
Name: "CAXLUSDC",
Denom: "ucaxlusdc",
Decimals: sdk.NewInt(1000000),
IsOnChain: true,
IsOraclePriceRequired: false,
IsCdpMintable: true,
}
err = assetKeeper.AddAssetRecords(ctx, cAXLUSDC)
if err != nil {
fmt.Println(err)
}
}

func InitializeStates(
ctx sdk.Context,
liquidationKeeper liquidationV2keeper.Keeper,
auctionKeeper auctionV2keeper.Keeper,
) {
dutchAuctionParams := liquidationV2types.DutchAuctionParam{
Premium: newDec("1.15"),
Discount: newDec("0.7"),
DecrementFactor: sdk.NewInt(1),
}
englishAuctionParams := liquidationV2types.EnglishAuctionParam{DecrementFactor: sdk.NewInt(1)}

harborParams := liquidationV2types.LiquidationWhiteListing{
AppId: 2,
Initiator: true,
IsDutchActivated: true,
DutchAuctionParam: &dutchAuctionParams,
IsEnglishActivated: true,
EnglishAuctionParam: &englishAuctionParams,
KeeeperIncentive: sdk.ZeroDec(),
}

commodoParams := liquidationV2types.LiquidationWhiteListing{
AppId: 3,
Initiator: true,
IsDutchActivated: true,
DutchAuctionParam: &dutchAuctionParams,
IsEnglishActivated: false,
EnglishAuctionParam: nil,
KeeeperIncentive: sdk.ZeroDec(),
}

liquidationKeeper.SetLiquidationWhiteListing(ctx, harborParams)
liquidationKeeper.SetLiquidationWhiteListing(ctx, commodoParams)

appReserveFundsTxDataHbr, found := liquidationKeeper.GetAppReserveFundsTxData(ctx, 2)
if !found {
appReserveFundsTxDataHbr.AppId = 2
}
appReserveFundsTxDataHbr.AssetTxData = append(appReserveFundsTxDataHbr.AssetTxData, liquidationV2types.AssetTxData{})
liquidationKeeper.SetAppReserveFundsTxData(ctx, appReserveFundsTxDataHbr)

appReserveFundsTxDataCmdo, found := liquidationKeeper.GetAppReserveFundsTxData(ctx, 3)
if !found {
appReserveFundsTxDataCmdo.AppId = 3
}
appReserveFundsTxDataCmdo.AssetTxData = append(appReserveFundsTxDataCmdo.AssetTxData, liquidationV2types.AssetTxData{})
liquidationKeeper.SetAppReserveFundsTxData(ctx, appReserveFundsTxDataCmdo)

auctionParams := auctionsV2types.AuctionParams{
AuctionDurationSeconds: 18000,
Step: newDec("0.1"),
WithdrawalFee: newDec("0.0005"),
ClosingFee: newDec("0.0005"),
MinUsdValueLeft: 100000,
BidFactor: newDec("0.01"),
LiquidationPenalty: newDec("0.1"),
AuctionBonus: newDec("0.0"),
}
auctionKeeper.SetAuctionParams(ctx, auctionParams)
auctionKeeper.SetParams(ctx, auctionsV2types.Params{})
auctionKeeper.SetAuctionID(ctx, 0)
auctionKeeper.SetUserBidID(ctx, 0)

}

func newDec(i string) sdk.Dec {
dec, _ := sdk.NewDecFromStr(i)
return dec
}
Loading

0 comments on commit d387b59

Please sign in to comment.