mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-11-14 03:55:19 +00:00
c63ecf908a
* Add 'InterestFactor' to CDP type (#734) * update cdp type to include interest factor * fix build * Add cdp accumulator methods (#735) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * Add sync cdp interest method (#737) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * remove old fee functions * add method to synchronize cdp interest * add multi-cdp tests * add test with many blocks * add test for interest getter * address review comments * calculate time difference then convert to seconds * fix: update collateral index when syncing interest * fix: differentiate between case when apy is zero and all fees are being rounded to zero * fix: round time difference properly * update cdp genesis state and migrations (#738) * remame fees to interest * add accumulate interest method * add basic test * add note * address review comments * update tests * remove old fee functions * add method to synchronize cdp interest * add multi-cdp tests * add test with many blocks * add test for interest getter * update cdp genesis state and migrations * address review comments * calculate time difference then convert to seconds * fix: update collateral index when syncing interest * fix: differentiate between case when apy is zero and all fees are being rounded to zero * fix: simplify add/remove/update collateral index * update genesis state to include total principal amounts * update migration * Delete kava-4-cdp-state-block-500000.json * Add cdp liquidations by external keeper (#750) * feat: split liquidations between external keepers and automated begin blocker * address review comments * USDX incentive accumulators (#752) * feat: split liquidations between external keepers and automated begin blocker * wip: refactor usdx minting incentives to use accumulators/hooks * wip: refactor usdx minting claim object * feat: use accumulators/hooks for usdx minting rewards * fix: get tests passing * fix: don't create claim objects unless that cdp type is eligable for rewards * add begin blocker * update client * cleanup comments/tests * update querier * address review comments * fix: check for division by zero * address review comments * run hook before interest is synced * Remove savings rate (#764) * remove savings rate * remove savings rate from debt param * update migrations * address review comments * Add usdx incentives calculation test (#765) * add usdx incentive calculation test * update reward calculation * add allowable error to test criteria * Update x/incentive/keeper/rewards_test.go Co-authored-by: Kevin Davis <karzak@users.noreply.github.com> * fix: remove old fields from test genesis state Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com>
127 lines
3.9 KiB
Go
127 lines
3.9 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))
|
|
}
|
|
|
|
// 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)
|
|
|
|
for _, gat := range gs.PreviousAccumulationTimes {
|
|
k.SetInterestFactor(ctx, gat.CollateralType, gat.InterestFactor)
|
|
if !gat.PreviousAccumulationTime.IsZero() {
|
|
k.SetPreviousAccrualTime(ctx, gat.CollateralType, gat.PreviousAccumulationTime)
|
|
}
|
|
}
|
|
|
|
for _, gtp := range gs.TotalPrincipals {
|
|
k.SetTotalPrincipal(ctx, gtp.CollateralType, types.DefaultStableDenom, gtp.TotalPrincipal)
|
|
}
|
|
// 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.Type, cdp.GetTotalPrincipal())
|
|
k.IndexCdpByCollateralRatio(ctx, cdp.Type, cdp.ID, ratio)
|
|
}
|
|
|
|
k.SetNextCdpID(ctx, gs.StartingCdpID)
|
|
k.SetDebtDenom(ctx, gs.DebtDenom)
|
|
k.SetGovDenom(ctx, gs.GovDenom)
|
|
|
|
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)
|
|
|
|
var previousAccumTimes types.GenesisAccumulationTimes
|
|
var totalPrincipals types.GenesisTotalPrincipals
|
|
|
|
for _, cp := range params.CollateralParams {
|
|
interestFactor, found := k.GetInterestFactor(ctx, cp.Type)
|
|
if !found {
|
|
interestFactor = sdk.OneDec()
|
|
}
|
|
previousAccumTime, _ := k.GetPreviousAccrualTime(ctx, cp.Type)
|
|
previousAccumTimes = append(previousAccumTimes, types.NewGenesisAccumulationTime(cp.Type, previousAccumTime, interestFactor))
|
|
|
|
tp := k.GetTotalPrincipal(ctx, cp.Type, types.DefaultStableDenom)
|
|
genTotalPrincipal := types.NewGenesisTotalPrincipal(cp.Type, tp)
|
|
totalPrincipals = append(totalPrincipals, genTotalPrincipal)
|
|
}
|
|
|
|
return NewGenesisState(params, cdps, deposits, cdpID, debtDenom, govDenom, previousAccumTimes, totalPrincipals)
|
|
}
|