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"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/kava-labs/kava/x/hard/keeper"
|
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.
|
2022-01-08 00:39:27 +00:00
|
|
|
func InitGenesis(ctx sdk.Context, k keeper.Keeper, accountKeeper types.AccountKeeper, gs types.GenesisState) {
|
2020-09-21 21:08:43 +00:00
|
|
|
if err := gs.Validate(); err != nil {
|
2022-01-08 00:39:27 +00:00
|
|
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", types.ModuleName, err))
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-01-08 00:39:27 +00:00
|
|
|
DepositModuleAccount := accountKeeper.GetModuleAccount(ctx, types.ModuleAccountName)
|
2020-09-21 21:08:43 +00:00
|
|
|
if DepositModuleAccount == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", DepositModuleAccount))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:18:55 +00:00
|
|
|
// ExportGenesis export genesis state for hard module
|
2022-01-08 00:39:27 +00:00
|
|
|
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) types.GenesisState {
|
2020-09-21 21:08:43 +00:00
|
|
|
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 {
|
2021-02-19 20:49:36 +00:00
|
|
|
k.BeforeDepositModified(ctx, d)
|
2021-02-10 14:56:38 +00:00
|
|
|
syncedDeposit, found := k.GetSyncedDeposit(ctx, d.Depositor)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("syncable deposit not found for %s", d.Depositor))
|
|
|
|
}
|
|
|
|
deposits = append(deposits, syncedDeposit)
|
2021-01-23 05:17:40 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
k.IterateBorrows(ctx, func(b types.Borrow) bool {
|
2021-02-19 20:49:36 +00:00
|
|
|
k.BeforeBorrowModified(ctx, b)
|
2021-02-10 14:56:38 +00:00
|
|
|
syncedBorrow, found := k.GetSyncedBorrow(ctx, b.Borrower)
|
|
|
|
if !found {
|
|
|
|
panic(fmt.Sprintf("syncable borrow not found for %s", b.Borrower))
|
|
|
|
}
|
|
|
|
borrows = append(borrows, syncedBorrow)
|
2021-01-23 05:17:40 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
totalSupplied, found := k.GetSuppliedCoins(ctx)
|
|
|
|
if !found {
|
2022-01-08 00:39:27 +00:00
|
|
|
totalSupplied = types.DefaultTotalSupplied
|
2021-01-23 05:17:40 +00:00
|
|
|
}
|
|
|
|
totalBorrowed, found := k.GetBorrowedCoins(ctx)
|
2020-09-21 21:08:43 +00:00
|
|
|
if !found {
|
2022-01-08 00:39:27 +00:00
|
|
|
totalBorrowed = types.DefaultTotalBorrowed
|
2021-01-23 05:17:40 +00:00
|
|
|
}
|
|
|
|
totalReserves, found := k.GetTotalReserves(ctx)
|
|
|
|
if !found {
|
2022-01-08 00:39:27 +00:00
|
|
|
totalReserves = types.DefaultTotalReserves
|
2021-01-23 05:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, mm := range params.MoneyMarkets {
|
|
|
|
supplyFactor, f := k.GetSupplyInterestFactor(ctx, mm.Denom)
|
|
|
|
if !f {
|
2021-03-11 04:32:35 +00:00
|
|
|
supplyFactor = sdk.OneDec()
|
2021-01-23 05:17:40 +00:00
|
|
|
}
|
|
|
|
borrowFactor, f := k.GetBorrowInterestFactor(ctx, mm.Denom)
|
|
|
|
if !f {
|
2021-03-11 04:32:35 +00:00
|
|
|
borrowFactor = sdk.OneDec()
|
2021-01-23 05:17:40 +00:00
|
|
|
}
|
|
|
|
previousAccrualTime, f := k.GetPreviousAccrualTime(ctx, mm.Denom)
|
|
|
|
if !f {
|
2021-03-11 04:32:35 +00:00
|
|
|
// Goverance adds new params at end of block, but mm's previous accrual time is set in begin blocker.
|
|
|
|
// If a new money market is added and chain is exported before begin blocker runs, then the previous
|
|
|
|
// accrual time will not be found. We can't set it here because our ctx doesn't contain current block
|
|
|
|
// time; if we set it to ctx.BlockTime() then on the next block it could accrue interest from Jan 1st
|
|
|
|
// 0001 to now. To avoid setting up a bad state, we panic.
|
|
|
|
panic(fmt.Sprintf("expected previous accrual time to be set in state for %s", mm.Denom))
|
2021-01-23 05:17:40 +00:00
|
|
|
}
|
|
|
|
gat := types.NewGenesisAccumulationTime(mm.Denom, previousAccrualTime, supplyFactor, borrowFactor)
|
|
|
|
gats = append(gats, gat)
|
|
|
|
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
return types.NewGenesisState(
|
2021-01-23 05:17:40 +00:00
|
|
|
params, gats, deposits, borrows,
|
|
|
|
totalSupplied, totalBorrowed, totalReserves,
|
|
|
|
)
|
2020-09-21 21:08:43 +00:00
|
|
|
}
|