mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-29 17:55:19 +00:00
4a8b5696cb
* initial sketch * add module migrations * add migrations for all accout types * test account migration * add tendermint migration and migrate cmd * remove need for errors pkg dependency * add bech32 decoding fork * add suggested params and cmd to write them * add basic upgrade instructions * fix tests * address some migration todos * tidy contrib folder * finalize params values * align cdp init genesis with other modules * add tendermint and distribution test add custom distribution migration to patch bug * add staking migration test * add slashing, evidence tests, refactor auth tests * add full migration test * remove go-amino dependency from go.mod also tidy up unused indirect dependencies * address remaining TODOs * remove commented out code from legacy types * add spot/liquidation markets ids to kava-3 params * Apply suggestions from code review Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> * address code review suggestions * add validate genesis to migrate test * refactor add params func * remove commented out code from old types * fix add params * add deputy address * add tests using exported kava-2 state * incorporate new cdp params from master * update params from review Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> * add deputy account * add committee permissions for new params Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
119 lines
3.7 KiB
Go
119 lines
3.7 KiB
Go
package cdp
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/kava-labs/kava/x/cdp/types"
|
|
)
|
|
|
|
// InitGenesis sets initial genesis state for cdp module
|
|
func InitGenesis(ctx sdk.Context, k Keeper, pk types.PricefeedKeeper, sk types.SupplyKeeper, gs GenesisState) {
|
|
|
|
if err := gs.Validate(); err != nil {
|
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", ModuleName, err))
|
|
}
|
|
|
|
// check if the module accounts exists
|
|
cdpModuleAcc := sk.GetModuleAccount(ctx, ModuleName)
|
|
if cdpModuleAcc == nil {
|
|
panic(fmt.Sprintf("%s module account has not been set", ModuleName))
|
|
}
|
|
liqModuleAcc := sk.GetModuleAccount(ctx, LiquidatorMacc)
|
|
if liqModuleAcc == nil {
|
|
panic(fmt.Sprintf("%s module account has not been set", LiquidatorMacc))
|
|
}
|
|
savingsRateMacc := sk.GetModuleAccount(ctx, SavingsRateMacc)
|
|
if savingsRateMacc == nil {
|
|
panic(fmt.Sprintf("%s module account has not been set", SavingsRateMacc))
|
|
}
|
|
|
|
// validate denoms - check that any collaterals in the params are in the pricefeed,
|
|
// pricefeed MUST call InitGenesis before cdp
|
|
collateralMap := make(map[string]int)
|
|
ap := pk.GetParams(ctx)
|
|
for _, a := range ap.Markets {
|
|
collateralMap[a.MarketID] = 1
|
|
}
|
|
|
|
for _, col := range gs.Params.CollateralParams {
|
|
_, found := collateralMap[col.SpotMarketID]
|
|
if !found {
|
|
panic(fmt.Sprintf("%s collateral not found in pricefeed", col.Denom))
|
|
}
|
|
// sets the status of the pricefeed in the store
|
|
// if pricefeed not active, debt operations are paused
|
|
_ = k.UpdatePricefeedStatus(ctx, col.SpotMarketID)
|
|
|
|
_, found = collateralMap[col.LiquidationMarketID]
|
|
if !found {
|
|
panic(fmt.Sprintf("%s collateral not found in pricefeed", col.Denom))
|
|
}
|
|
// sets the status of the pricefeed in the store
|
|
// if pricefeed not active, debt operations are paused
|
|
_ = k.UpdatePricefeedStatus(ctx, col.LiquidationMarketID)
|
|
}
|
|
|
|
k.SetParams(ctx, gs.Params)
|
|
|
|
// set the per second fee rate for each collateral type
|
|
for _, cp := range gs.Params.CollateralParams {
|
|
k.SetTotalPrincipal(ctx, cp.Denom, gs.Params.DebtParam.Denom, sdk.ZeroInt())
|
|
}
|
|
|
|
// add cdps
|
|
for _, cdp := range gs.CDPs {
|
|
if cdp.ID == gs.StartingCdpID {
|
|
panic(fmt.Sprintf("starting cdp id is assigned to an existing cdp: %s", cdp))
|
|
}
|
|
err := k.SetCDP(ctx, cdp)
|
|
if err != nil {
|
|
panic(fmt.Sprintf("error setting cdp: %v", err))
|
|
}
|
|
k.IndexCdpByOwner(ctx, cdp)
|
|
ratio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees))
|
|
k.IndexCdpByCollateralRatio(ctx, cdp.Collateral.Denom, cdp.ID, ratio)
|
|
k.IncrementTotalPrincipal(ctx, cdp.Collateral.Denom, cdp.Principal.Add(cdp.AccumulatedFees))
|
|
}
|
|
|
|
k.SetNextCdpID(ctx, gs.StartingCdpID)
|
|
k.SetDebtDenom(ctx, gs.DebtDenom)
|
|
k.SetGovDenom(ctx, gs.GovDenom)
|
|
// only set the previous block time if it's different than default
|
|
if !gs.PreviousDistributionTime.Equal(types.DefaultPreviousDistributionTime) {
|
|
k.SetPreviousSavingsDistribution(ctx, gs.PreviousDistributionTime)
|
|
}
|
|
|
|
for _, d := range gs.Deposits {
|
|
k.SetDeposit(ctx, d)
|
|
}
|
|
}
|
|
|
|
// ExportGenesis export genesis state for cdp module
|
|
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
|
|
params := k.GetParams(ctx)
|
|
|
|
cdps := CDPs{}
|
|
deposits := Deposits{}
|
|
k.IterateAllCdps(ctx, func(cdp CDP) (stop bool) {
|
|
cdps = append(cdps, cdp)
|
|
k.IterateDeposits(ctx, cdp.ID, func(deposit Deposit) (stop bool) {
|
|
deposits = append(deposits, deposit)
|
|
return false
|
|
})
|
|
return false
|
|
})
|
|
|
|
cdpID := k.GetNextCdpID(ctx)
|
|
debtDenom := k.GetDebtDenom(ctx)
|
|
govDenom := k.GetGovDenom(ctx)
|
|
|
|
previousDistributionTime, found := k.GetPreviousSavingsDistribution(ctx)
|
|
if !found {
|
|
previousDistributionTime = DefaultPreviousDistributionTime
|
|
}
|
|
|
|
return NewGenesisState(params, cdps, deposits, cdpID, debtDenom, govDenom, previousDistributionTime)
|
|
}
|