2024-05-10 16:30:28 +00:00
|
|
|
package precisebank
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2024-05-15 21:07:24 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
2024-05-10 16:30:28 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
|
|
|
"github.com/kava-labs/kava/x/precisebank/keeper"
|
|
|
|
"github.com/kava-labs/kava/x/precisebank/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InitGenesis initializes the store state from a genesis state.
|
|
|
|
func InitGenesis(
|
|
|
|
ctx sdk.Context,
|
|
|
|
keeper keeper.Keeper,
|
|
|
|
ak types.AccountKeeper,
|
2024-05-15 21:07:24 +00:00
|
|
|
bk types.BankKeeper,
|
2024-05-10 16:30:28 +00:00
|
|
|
gs *types.GenesisState,
|
|
|
|
) {
|
2024-05-15 21:07:24 +00:00
|
|
|
// Ensure the genesis state is valid
|
2024-05-10 16:30:28 +00:00
|
|
|
if err := gs.Validate(); err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to validate %s genesis state: %s", types.ModuleName, err))
|
|
|
|
}
|
|
|
|
|
2024-05-15 21:07:24 +00:00
|
|
|
// Initialize module account
|
2024-05-10 16:30:28 +00:00
|
|
|
if moduleAcc := ak.GetModuleAccount(ctx, types.ModuleName); moduleAcc == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
|
|
|
|
}
|
|
|
|
|
2024-05-15 21:07:24 +00:00
|
|
|
// Check module balance matches sum of fractional balances + remainder
|
|
|
|
// This is always a whole integer amount, as previously verified in
|
|
|
|
// GenesisState.Validate()
|
|
|
|
totalAmt := gs.TotalAmountWithRemainder()
|
|
|
|
|
|
|
|
moduleAddr := ak.GetModuleAddress(types.ModuleName)
|
|
|
|
moduleBal := bk.GetBalance(ctx, moduleAddr, types.IntegerCoinDenom)
|
|
|
|
moduleBalExtended := moduleBal.Amount.Mul(types.ConversionFactor())
|
|
|
|
|
|
|
|
// Compare balances in full precise extended amounts
|
|
|
|
if !totalAmt.Equal(moduleBalExtended) {
|
|
|
|
panic(fmt.Sprintf(
|
|
|
|
"module account balance does not match sum of fractional balances and remainder, balance is %s but expected %v%s (%v%s)",
|
|
|
|
moduleBal,
|
|
|
|
totalAmt, types.ExtendedCoinDenom,
|
|
|
|
totalAmt.Quo(types.ConversionFactor()), types.IntegerCoinDenom,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: After keeper methods are implemented
|
|
|
|
// - Set account FractionalBalances
|
2024-05-10 16:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExportGenesis returns a GenesisState for a given context and keeper.
|
|
|
|
func ExportGenesis(ctx sdk.Context, keeper keeper.Keeper) *types.GenesisState {
|
2024-05-15 21:07:24 +00:00
|
|
|
return types.NewGenesisState(types.FractionalBalances{}, sdkmath.ZeroInt())
|
2024-05-10 16:30:28 +00:00
|
|
|
}
|