Skip to content

Commit

Permalink
Merge pull request #549 from comdex-official/hbrefactor
Browse files Browse the repository at this point in the history
Hbrefactor
  • Loading branch information
dheerajkd30 authored Nov 2, 2022
2 parents 97a2d35 + 6b82f35 commit efc480e
Show file tree
Hide file tree
Showing 107 changed files with 7,638 additions and 2,679 deletions.
Binary file removed .DS_Store
Binary file not shown.
33 changes: 20 additions & 13 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ import (

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

mv5 "github.com/comdex-official/comdex/app/upgrades/mainnet/v5"
tv1_0_0 "github.com/comdex-official/comdex/app/upgrades/testnet/v1_0_0"
tv2_0_0 "github.com/comdex-official/comdex/app/upgrades/testnet/v2_0_0"
tv3_0_0 "github.com/comdex-official/comdex/app/upgrades/testnet/v3_0_0"
tv4_0_0 "github.com/comdex-official/comdex/app/upgrades/testnet/v4_0_0"
tv5_0_0 "github.com/comdex-official/comdex/app/upgrades/testnet/v5_0_0"
)

const (
Expand Down Expand Up @@ -199,7 +199,6 @@ func GetGovProposalHandlers() []govclient.ProposalHandler {
bandoraclemoduleclient.AddFetchPriceHandler,
lendclient.AddLendPairsHandler,
lendclient.AddPoolHandler,
lendclient.UpdateLendPairsHandler,
lendclient.AddAssetToPairHandler,
lendclient.AddAssetRatesParamsHandler,
lendclient.AddAuctionParamsHandler,
Expand Down Expand Up @@ -568,6 +567,8 @@ func New(
&app.AssetKeeper,
&app.MarketKeeper,
&app.EsmKeeper,
&app.LiquidationKeeper,
&app.AuctionKeeper,
)

app.EsmKeeper = esmkeeper.NewKeeper(
Expand Down Expand Up @@ -1194,16 +1195,16 @@ func (a *App) ModuleAccountsPermissions() map[string][]string {
}

func (a *App) registerUpgradeHandlers() {
a.UpgradeKeeper.SetUpgradeHandler(
/*a.UpgradeKeeper.SetUpgradeHandler(
mv5.UpgradeName,
mv5.CreateUpgradeHandler(a.mm, a.configurator, a.WasmKeeper, a.AssetKeeper, a.LiquidityKeeper, a.CollectorKeeper, a.AuctionKeeper, a.LockerKeeper, a.Rewardskeeper, a.LiquidationKeeper),
)

/*a.UpgradeKeeper.SetUpgradeHandler(
tv4_0_0.UpgradeNameV4_4_0,
tv4_0_0.CreateUpgradeHandlerV440(a.mm, a.configurator, a.LendKeeper, a.LiquidationKeeper, a.AuctionKeeper),
)*/

a.UpgradeKeeper.SetUpgradeHandler(
tv5_0_0.UpgradeNameBeta,
tv5_0_0.CreateUpgradeHandlerV5Beta(a.mm, a.configurator, a.LendKeeper, a.LiquidationKeeper, a.VaultKeeper),
)

// 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
// This will read that value, and execute the preparations for the upgrade.
Expand Down Expand Up @@ -1277,11 +1278,17 @@ func upgradeHandlers(upgradeInfo storetypes.UpgradeInfo, a *App, storeUpgrades *
storeUpgrades = &storetypes.StoreUpgrades{}
case upgradeInfo.Name == tv4_0_0.UpgradeNameV4_3_0 && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{}
case upgradeInfo.Name == tv4_0_0.UpgradeNameV4_4_0 && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{}
case upgradeInfo.Name == tv5_0_0.UpgradeNameBeta && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{
Deleted: []string{"bandoracle", "market"},
Added: []string{
bandoraclemoduletypes.ModuleName,
markettypes.ModuleName,
},
}

// prepare store for main net upgrade v5.0.0
case upgradeInfo.Name == mv5.UpgradeName && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
// prepare store for main net upgrade v5.0.0
/*case upgradeInfo.Name == mv5.UpgradeName && !a.UpgradeKeeper.IsSkipHeight(upgradeInfo.Height):
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{
assettypes.ModuleName,
Expand All @@ -1301,7 +1308,7 @@ func upgradeHandlers(upgradeInfo storetypes.UpgradeInfo, a *App, storeUpgrades *
icahosttypes.StoreKey,
authz.ModuleName,
},
}
}*/
}

return storeUpgrades
Expand Down
2 changes: 1 addition & 1 deletion app/upgrades/mainnet/v5/constants.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v5

const (
UpgradeName = "v5.0.0.beta"
UpgradeName = "v5"
UpgradeHeight = ""
UpgradeInfo = `'{
"binaries": {
Expand Down
18 changes: 9 additions & 9 deletions app/upgrades/mainnet/v5/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ func InitializeStates(
}

assets := []assettypes.Asset{
{Name: "ATOM", Denom: "uatom", Decimals: 1000000, IsOnChain: false, IsOraclePriceRequired: true},
{Name: "CMDX", Denom: "ucmdx", Decimals: 1000000, IsOnChain: false, IsOraclePriceRequired: true},
{Name: "CMST", Denom: "ucmst", Decimals: 1000000, IsOnChain: false, IsOraclePriceRequired: false},
{Name: "OSMO", Denom: "uosmo", Decimals: 1000000, IsOnChain: false, IsOraclePriceRequired: true},
{Name: "CATOM", Denom: "ucatom", Decimals: 1000000, IsOnChain: false, IsOraclePriceRequired: false},
{Name: "CCMDX", Denom: "uccmdx", Decimals: 1000000, IsOnChain: false, IsOraclePriceRequired: false},
{Name: "CCMST", Denom: "uccmst", Decimals: 1000000, IsOnChain: false, IsOraclePriceRequired: false},
{Name: "COSMO", Denom: "ucosmo", Decimals: 1000000, IsOnChain: false, IsOraclePriceRequired: false},
{Name: "HARBOR", Denom: "uharbor", Decimals: 1000000, IsOnChain: true, IsOraclePriceRequired: false},
{Name: "ATOM", Denom: "uatom", Decimals: sdk.NewInt(1000000), IsOnChain: false, IsOraclePriceRequired: true},
{Name: "CMDX", Denom: "ucmdx", Decimals: sdk.NewInt(1000000), IsOnChain: false, IsOraclePriceRequired: true},
{Name: "CMST", Denom: "ucmst", Decimals: sdk.NewInt(1000000), IsOnChain: false, IsOraclePriceRequired: false},
{Name: "OSMO", Denom: "uosmo", Decimals: sdk.NewInt(1000000), IsOnChain: false, IsOraclePriceRequired: true},
{Name: "CATOM", Denom: "ucatom", Decimals: sdk.NewInt(1000000), IsOnChain: false, IsOraclePriceRequired: false},
{Name: "CCMDX", Denom: "uccmdx", Decimals: sdk.NewInt(1000000), IsOnChain: false, IsOraclePriceRequired: false},
{Name: "CCMST", Denom: "uccmst", Decimals: sdk.NewInt(1000000), IsOnChain: false, IsOraclePriceRequired: false},
{Name: "COSMO", Denom: "ucosmo", Decimals: sdk.NewInt(1000000), IsOnChain: false, IsOraclePriceRequired: false},
{Name: "HARBOR", Denom: "uharbor", Decimals: sdk.NewInt(1000000), IsOnChain: true, IsOraclePriceRequired: false},
}

for _, asset := range assets {
Expand Down
14 changes: 0 additions & 14 deletions app/upgrades/testnet/v4_0_0/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,3 @@ const (
}
}'`
)

const (
UpgradeNameV4_4_0 = "v4.4.0.beta"
UpgradeHeightV4_4_0 = "" // replace this height
UpgradeInfoV4_4_0 = `'{
"binaries": {
"darwin/arm64":"",
"darwin/x86_64":"",
"linux/arm64":"",
"linux/x86_64":"",
"windows/x86_64":""
}
}'`
)
39 changes: 0 additions & 39 deletions app/upgrades/testnet/v4_0_0/upgrades.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package v4_0_0 //nolint:revive,stylecheck

import (
auctionkeeper "github.com/comdex-official/comdex/x/auction/keeper"
lendkeeper "github.com/comdex-official/comdex/x/lend/keeper"
liquidationkeeper "github.com/comdex-official/comdex/x/liquidation/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
Expand Down Expand Up @@ -125,39 +122,3 @@ func CreateUpgradeHandlerV430(
return newVM, err
}
}

func UpdateDutchLendAuctions(
ctx sdk.Context,
liquidationkeeper liquidationkeeper.Keeper,
auctionkeeper auctionkeeper.Keeper,
) {
lockedVaults := liquidationkeeper.GetLockedVaults(ctx)
for _, v := range lockedVaults {
if v.Kind != nil {
err := auctionkeeper.LendDutchActivator(ctx, v)
if err != nil {
return
}
}
}
}

// CreateUpgradeHandler creates an SDK upgrade handler for v4_4_0
func CreateUpgradeHandlerV440(
mm *module.Manager,
configurator module.Configurator,
lendkeeper lendkeeper.Keeper,
liquidationkeeper liquidationkeeper.Keeper,
auctionkeeper auctionkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
// This change is only for testnet upgrade

UpdateDutchLendAuctions(ctx, liquidationkeeper, auctionkeeper)
newVM, err := mm.RunMigrations(ctx, configurator, fromVM)
if err != nil {
return newVM, err
}
return newVM, err
}
}
15 changes: 15 additions & 0 deletions app/upgrades/testnet/v5_0_0/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package v5_0_0 //nolint:revive,stylecheck

const (
UpgradeNameBeta = "v5.0.0.beta"
UpgradeHeight = "" // replace this height
UpgradeInfo = `'{
"binaries": {
"darwin/arm64":"",
"darwin/x86_64":"",
"linux/arm64":"",
"linux/x86_64":"",
"windows/x86_64":""
}
}'`
)
82 changes: 82 additions & 0 deletions app/upgrades/testnet/v5_0_0/off_load_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package v5_0_0 //nolint:revive,stylecheck

import (
lendkeeper "github.com/comdex-official/comdex/x/lend/keeper"
"github.com/comdex-official/comdex/x/lend/types"
liquidationkeeper "github.com/comdex-official/comdex/x/liquidation/keeper"
vaultkeeper "github.com/comdex-official/comdex/x/vault/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// SetVaultLengthCounter - Set vault length for liquidation check
func SetVaultLengthCounter(
ctx sdk.Context,
vaultkeeper vaultkeeper.Keeper,
) {
var count uint64
appExtendedPairVaultData, found := vaultkeeper.GetAppMappingData(ctx, 2)
if found {
for _, data := range appExtendedPairVaultData {
count += uint64(len(data.VaultIds))
}
}
vaultkeeper.SetLengthOfVault(ctx, count)
}

// FuncMigrateLiquidatedBorrow - Migrate all liquidated borrow to new borrow struct and make is_liquidated field to true
func FuncMigrateLiquidatedBorrow(ctx sdk.Context, k lendkeeper.Keeper, liqK liquidationkeeper.Keeper) error {
liqBorrow := liqK.GetLockedVaultByApp(ctx, 3)
for _, v := range liqBorrow {
if v.AmountIn.GT(sdk.ZeroInt()) && v.AmountOut.GT(sdk.ZeroInt()) {
borrowMetaData := v.GetBorrowMetaData()
pair, _ := k.GetLendPair(ctx, v.ExtendedPairId)
assetIn, _ := k.Asset.GetAsset(ctx, pair.AssetIn)
assetOut, _ := k.Asset.GetAsset(ctx, pair.AssetOut)
amountIn := sdk.NewCoin(assetIn.Denom, v.AmountIn)
amountOut := sdk.NewCoin(assetOut.Denom, v.AmountOut)
var cpoolName string
if pair.AssetOutPoolID == 1 {
cpoolName = "CMDX-ATOM-CMST"
} else {
cpoolName = "OSMO-ATOM-CMST"
}

globalIndex, _ := sdk.NewDecFromStr("0.002")

newBorrow := types.BorrowAsset{
ID: v.OriginalVaultId,
LendingID: borrowMetaData.LendingId,
IsStableBorrow: borrowMetaData.IsStableBorrow,
PairID: v.ExtendedPairId,
AmountIn: amountIn,
AmountOut: amountOut,
BridgedAssetAmount: borrowMetaData.BridgedAssetAmount,
BorrowingTime: ctx.BlockTime(),
StableBorrowRate: borrowMetaData.StableBorrowRate,
InterestAccumulated: sdk.NewDecFromInt(v.InterestAccumulated),
GlobalIndex: globalIndex,
ReserveGlobalIndex: sdk.OneDec(),
LastInteractionTime: ctx.BlockTime(),
CPoolName: cpoolName,
IsLiquidated: false,
}
lend, _ := k.GetLend(ctx, newBorrow.LendingID)
k.UpdateBorrowStats(ctx, pair, newBorrow.IsStableBorrow, v.AmountOut, true)

poolAssetLBMappingData, _ := k.GetAssetStatsByPoolIDAndAssetID(ctx, pair.AssetOutPoolID, pair.AssetOut)
poolAssetLBMappingData.BorrowIds = append(poolAssetLBMappingData.BorrowIds, newBorrow.ID)
k.SetAssetStatsByPoolIDAndAssetID(ctx, poolAssetLBMappingData)

k.SetUserBorrowIDCounter(ctx, newBorrow.ID)
k.SetBorrow(ctx, newBorrow)

mappingData, _ := k.GetUserLendBorrowMapping(ctx, lend.Owner, newBorrow.LendingID)
mappingData.BorrowId = append(mappingData.BorrowId, newBorrow.ID)
k.SetUserLendBorrowMapping(ctx, mappingData)
} else {
// delete faulty lockedVault
liqK.DeleteLockedVault(ctx, 3, v.LockedVaultId)
}
}
return nil
}
31 changes: 31 additions & 0 deletions app/upgrades/testnet/v5_0_0/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package v5_0_0 //nolint:revive,stylecheck

import (
lendkeeper "github.com/comdex-official/comdex/x/lend/keeper"
liquidationkeeper "github.com/comdex-official/comdex/x/liquidation/keeper"
vaultkeeper "github.com/comdex-official/comdex/x/vault/keeper"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

func CreateUpgradeHandlerV5Beta(
mm *module.Manager,
configurator module.Configurator,
lk lendkeeper.Keeper,
liqk liquidationkeeper.Keeper,
vaultkeeper vaultkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, fromVM module.VersionMap) (module.VersionMap, error) {
vm, err := mm.RunMigrations(ctx, configurator, fromVM)
if err != nil {
return nil, err
}
SetVaultLengthCounter(ctx, vaultkeeper)
err = FuncMigrateLiquidatedBorrow(ctx, lk, liqk)
if err != nil {
return nil, err
}
return vm, err
}
}
6 changes: 3 additions & 3 deletions app/wasm/test/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,23 @@ func AddAppAsset(app *app.App, ctx1 sdk.Context) {
msg2 := assetTypes.Asset{
Name: "CMDX",
Denom: "ucmdx",
Decimals: 1000000,
Decimals: sdk.NewInt(1000000),
IsOnChain: true,
}
_ = assetKeeper.AddAssetRecords(*ctx, msg2)

msg3 := assetTypes.Asset{
Name: "CMST",
Denom: "ucmst",
Decimals: 1000000,
Decimals: sdk.NewInt(1000000),
IsOnChain: true,
}
_ = assetKeeper.AddAssetRecords(*ctx, msg3)

msg4 := assetTypes.Asset{
Name: "HARBOR",
Denom: "uharbor",
Decimals: 1000000,
Decimals: sdk.NewInt(1000000),
IsOnChain: true,
}
_ = assetKeeper.AddAssetRecords(*ctx, msg4)
Expand Down
Loading

0 comments on commit efc480e

Please sign in to comment.