2019-11-25 19:46:02 +00:00
|
|
|
package cdp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// InitGenesis sets initial genesis state for cdp module
|
|
|
|
func InitGenesis(ctx sdk.Context, k Keeper, pk PricefeedKeeper, gs GenesisState) {
|
|
|
|
if err := gs.Validate(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", ModuleName, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate denoms - check that any collaterals in the params are in the pricefeed,
|
|
|
|
// pricefeed MUST call InitGenesis before cdp
|
2019-11-25 19:46:02 +00:00
|
|
|
collateralMap := make(map[string]int)
|
2019-11-28 16:53:59 +00:00
|
|
|
ap := pk.GetParams(ctx)
|
2020-01-12 15:35:34 +00:00
|
|
|
for _, a := range ap.Markets {
|
|
|
|
collateralMap[a.MarketID] = 1
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
for _, col := range gs.Params.CollateralParams {
|
|
|
|
_, found := collateralMap[col.MarketID]
|
2019-11-25 19:46:02 +00:00
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("%s collateral not found in pricefeed", col.Denom))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
k.SetParams(ctx, gs.Params)
|
|
|
|
|
|
|
|
// set the per second fee rate for each collateral type
|
|
|
|
for _, cp := range gs.Params.CollateralParams {
|
|
|
|
for _, dp := range gs.Params.DebtParams {
|
|
|
|
k.SetTotalPrincipal(ctx, cp.Denom, dp.Denom, sdk.ZeroInt())
|
|
|
|
}
|
|
|
|
k.SetFeeRate(ctx, cp.Denom, cp.StabilityFee)
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// 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))
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
k.SetCDP(ctx, cdp)
|
2020-01-12 15:35:34 +00:00
|
|
|
k.IndexCdpByOwner(ctx, cdp)
|
|
|
|
ratio := k.CalculateCollateralToDebtRatio(ctx, cdp.Collateral, cdp.Principal.Add(cdp.AccumulatedFees))
|
|
|
|
k.IndexCdpByCollateralRatio(ctx, cdp.Collateral[0].Denom, cdp.ID, ratio)
|
|
|
|
k.IncrementTotalPrincipal(ctx, cdp.Collateral[0].Denom, cdp.Principal)
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
k.SetNextCdpID(ctx, gs.StartingCdpID)
|
|
|
|
k.SetDebtDenom(ctx, gs.DebtDenom)
|
2020-01-15 14:19:33 +00:00
|
|
|
k.SetGovDenom(ctx, gs.GovDenom)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
for _, d := range gs.Deposits {
|
|
|
|
k.SetDeposit(ctx, d)
|
|
|
|
}
|
|
|
|
// only set the previous block time if it's different than default
|
|
|
|
if !gs.PreviousBlockTime.Equal(DefaultPreviousBlockTime) {
|
|
|
|
k.SetPreviousBlockTime(ctx, gs.PreviousBlockTime)
|
|
|
|
}
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
|
2020-01-12 15:35:34 +00:00
|
|
|
// ExportGenesis export genesis state for cdp module
|
2019-11-25 19:46:02 +00:00
|
|
|
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
|
|
|
|
params := k.GetParams(ctx)
|
2020-01-12 15:35:34 +00:00
|
|
|
cdps := k.GetAllCdps(ctx)
|
|
|
|
cdpID := k.GetNextCdpID(ctx)
|
|
|
|
previousBlockTime, found := k.GetPreviousBlockTime(ctx)
|
|
|
|
if !found {
|
|
|
|
previousBlockTime = DefaultPreviousBlockTime
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
2020-01-12 15:35:34 +00:00
|
|
|
debtDenom := k.GetDebtDenom(ctx)
|
2019-11-25 19:46:02 +00:00
|
|
|
|
|
|
|
return GenesisState{
|
2020-01-12 15:35:34 +00:00
|
|
|
Params: params,
|
|
|
|
StartingCdpID: cdpID,
|
|
|
|
CDPs: cdps,
|
|
|
|
PreviousBlockTime: previousBlockTime,
|
|
|
|
DebtDenom: debtDenom,
|
2019-11-25 19:46:02 +00:00
|
|
|
}
|
|
|
|
}
|