2020-12-21 17:18:55 +00:00
|
|
|
package hard
|
2020-09-21 21:08:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/types"
|
2020-09-21 21:08:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitGenesis initializes the store state from a genesis state.
|
|
|
|
func InitGenesis(ctx sdk.Context, k Keeper, supplyKeeper types.SupplyKeeper, gs GenesisState) {
|
|
|
|
if err := gs.Validate(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", ModuleName, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
k.SetParams(ctx, gs.Params)
|
|
|
|
|
2020-12-21 17:28:41 +00:00
|
|
|
for _, mm := range gs.Params.MoneyMarkets {
|
|
|
|
k.SetMoneyMarket(ctx, mm.Denom, mm)
|
|
|
|
}
|
|
|
|
|
2021-01-23 05:17:40 +00:00
|
|
|
for _, gat := range gs.PreviousAccumulationTimes {
|
|
|
|
k.SetPreviousAccrualTime(ctx, gat.CollateralType, gat.PreviousAccumulationTime)
|
|
|
|
k.SetSupplyInterestFactor(ctx, gat.CollateralType, gat.SupplyInterestFactor)
|
|
|
|
k.SetBorrowInterestFactor(ctx, gat.CollateralType, gat.BorrowInterestFactor)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, deposit := range gs.Deposits {
|
|
|
|
k.SetDeposit(ctx, deposit)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, borrow := range gs.Borrows {
|
|
|
|
k.SetBorrow(ctx, borrow)
|
|
|
|
}
|
|
|
|
|
|
|
|
k.SetSuppliedCoins(ctx, gs.TotalSupplied)
|
|
|
|
k.SetBorrowedCoins(ctx, gs.TotalBorrowed)
|
|
|
|
k.SetTotalReserves(ctx, gs.TotalReserves)
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
// check if the module account exists
|
|
|
|
LPModuleAcc := supplyKeeper.GetModuleAccount(ctx, LPAccount)
|
|
|
|
if LPModuleAcc == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", LPAccount))
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the module account exists
|
|
|
|
DelegatorModuleAcc := supplyKeeper.GetModuleAccount(ctx, DelegatorAccount)
|
|
|
|
if DelegatorModuleAcc == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", DelegatorAccount))
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the module account exists
|
|
|
|
DepositModuleAccount := supplyKeeper.GetModuleAccount(ctx, ModuleAccountName)
|
|
|
|
if DepositModuleAccount == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", DepositModuleAccount))
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:38:14 +00:00
|
|
|
// check if the module account exists
|
|
|
|
LiquidatorModuleAcc := supplyKeeper.GetModuleAccount(ctx, LiquidatorAccount)
|
|
|
|
if LiquidatorModuleAcc == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", LiquidatorAccount))
|
|
|
|
}
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
// ExportGenesis export genesis state for hard module
|
2020-09-21 21:08:43 +00:00
|
|
|
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
|
|
|
|
params := k.GetParams(ctx)
|
2021-01-23 05:17:40 +00:00
|
|
|
|
|
|
|
gats := types.GenesisAccumulationTimes{}
|
|
|
|
deposits := types.Deposits{}
|
|
|
|
borrows := types.Borrows{}
|
|
|
|
|
|
|
|
k.IterateDeposits(ctx, func(d types.Deposit) bool {
|
|
|
|
deposits = append(deposits, d)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
k.IterateBorrows(ctx, func(b types.Borrow) bool {
|
|
|
|
borrows = append(borrows, b)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
totalSupplied, found := k.GetSuppliedCoins(ctx)
|
|
|
|
if !found {
|
|
|
|
totalSupplied = DefaultTotalSupplied
|
|
|
|
}
|
|
|
|
totalBorrowed, found := k.GetBorrowedCoins(ctx)
|
2020-09-21 21:08:43 +00:00
|
|
|
if !found {
|
2021-01-23 05:17:40 +00:00
|
|
|
totalBorrowed = DefaultTotalBorrowed
|
|
|
|
}
|
|
|
|
totalReserves, found := k.GetTotalReserves(ctx)
|
|
|
|
if !found {
|
|
|
|
totalReserves = DefaultTotalReserves
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, mm := range params.MoneyMarkets {
|
|
|
|
supplyFactor, f := k.GetSupplyInterestFactor(ctx, mm.Denom)
|
|
|
|
if !f {
|
|
|
|
supplyFactor = sdk.ZeroDec()
|
|
|
|
}
|
|
|
|
borrowFactor, f := k.GetBorrowInterestFactor(ctx, mm.Denom)
|
|
|
|
if !f {
|
|
|
|
borrowFactor = sdk.ZeroDec()
|
|
|
|
}
|
|
|
|
previousAccrualTime, f := k.GetPreviousAccrualTime(ctx, mm.Denom)
|
|
|
|
if !f {
|
|
|
|
previousAccrualTime = ctx.BlockTime()
|
|
|
|
}
|
|
|
|
gat := types.NewGenesisAccumulationTime(mm.Denom, previousAccrualTime, supplyFactor, borrowFactor)
|
|
|
|
gats = append(gats, gat)
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2021-01-23 05:17:40 +00:00
|
|
|
return NewGenesisState(
|
|
|
|
params, gats, deposits, borrows,
|
|
|
|
totalSupplied, totalBorrowed, totalReserves,
|
|
|
|
)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|